Files
kotlin-fork/grammar/src/expressions.grm
T
Andrey Breslav 41fd43b5e5 GreatSyntacticShift: Syntax for function types, function literals, when and tuples
Bug:
fun loop(var times : Int) {
   while(times > 0) {
        val u : (value : Int) -> Unit = { // This arrow is confusing the lookahead
            System.out?.println(it)
        }
        u(times--)
   }
}
2011-12-20 22:55:25 +04:00

289 lines
4.7 KiB
Plaintext

/**
h2. Expressions
bq. See [Expressions]
h3. Precedence
||*Precedence*||Title||Symbols||
|Highest|Postfix|{{\+\+}}, {{\-\-}}, {{.}}, {{?.}}, {{?}}|
| |Prefix|{{-}}, {{\+}}, {{\+\+}}, {{\-\-}}, {{!}}, [{{@label}}|#LabelName], {{@}}, {{@@}} |
| |Type RHS|{{:}}, {{as}}, {{as?}} |
| |Multiplicative|{{*}}, {{/}}, {{%}} |
| |Additive|{{\+}}, {{-}} |
| |Range|{{..}} |
| |Infix function|[{{SimpleName}}|#SimpleName] |
| |Elvis|{{?:}} |
| |Named checks|{{in}}, {{\!in}}, {{is}}, {{\!is}} |
| |Comparison|{{<}}, {{>}}, {{<=}}, {{>=}} |
| |Equality|{{==}}, {{\!==}}|
| |Conjunction|{{&&}}|
| |Disjunction|{{\|\|}}|
|Lowest|Assignment|{{=}}, {{+=}}, {{-=}}, {{*=}}, {{/=}}, {{%=}}|
h3. Rules
*/
/*
Decreasing precedence:
memberAccessOperation
postfixUnaryOperation
prefixUnaryOperation
multiplicativeOperation
additiveOperation
".."
SimpleName
"?:"
namedInfixOrTypeOperation
comparisonOperation
equalityOperation
"&&"
"||"
assignmentOperator
*/
expression
: disjunction (assignmentOperator disjunction)*
;
disjunction
: conjunction ("||" conjunction)*
;
conjunction
: equalityComparison ("&&" equalityComparison)*
;
equalityComparison
: comparison (equalityOperation comparison)*
;
comparison
: namedInfix (comparisonOperation namedInfix)*
;
namedInfix
: elvisExpression (inOperation elvisExpression)*
: elvisExpression (isOperation isRHS)?
;
elvisExpression
: infixFunctionCall ("?:" infixFunctionCall)*
;
infixFunctionCall
: rangeExpression (SimpleName rangeExpression)*
;
rangeExpression
: additiveExpression (".." additiveExpression)*
;
additiveExpression
: multiplicativeExpression (additiveOperation multiplicativeExpression)*
;
multiplicativeExpression
: typeRHS (multiplicativeOperation typeRHS)*
;
typeRHS
: prefixUnaryExpression (typeOperation prefixUnaryExpression)*
;
prefixUnaryExpression
: prefixUnaryOperation* postfixUnaryExpression
;
postfixUnaryExpression
: atomicExpression postfixUnaryOperation*
;
// !!! When you add here, remember to update the FIRST set in the parser
atomicExpression
: "(" expression ")"
: literalConstant
: functionLiteral
: tupleLiteral
: "this" label?
: "super" ("<" type ">")? label?
: if
: when
: try
: objectLiteral
: jump
: loop
: SimpleName
: FieldName
: "namespace" // for the root namespace
;
label
: "@"
: "@@"
: LabelName
;
literalConstant
: "true" | "false"
: stringTemplate
: NoEscapeString
: IntegerLiteral
: HexadecimalLiteral
: CharacterLiteral
: FloatLiteral
: "null"
;
stringTemplate
: "\"" stringTemplateElement* "\""
;
stringTemplateElement
: RegularStringPart
: ShortTemplateEntrySTART (SimpleName | "this")
: EscapeSequence
: longTemplate
;
longTemplate
: "${" expression "}"
;
isRHS
: pattern
;
declaration
: function
: property
: class
: typedef
: object
;
statement
: declaration
: expression
;
multiplicativeOperation
: "*" : "/" : "%"
;
additiveOperation
: "+" : "-"
;
inOperation
: "in" : "!in"
;
typeOperation
: "as" : "as?" : ":"
;
isOperation
: "is" : "!is"
;
comparisonOperation
: "<" : ">" : ">=" : "<="
;
equalityOperation
: "!=" : "=="
;
assignmentOperator
: "="
: "+=" : "-=" : "*=" : "/=" : "%="
;
prefixUnaryOperation
: "-" : "+"
: "++" : "--"
: "!" // No ~
: annotations // mandatory
: label
;
postfixUnaryOperation
: "++" : "--"
: callSuffix
: arrayAccess
: memberAccessOperation postfixUnaryExpression // TODO: Review
;
callSuffix
: typeArguments? valueArguments (label? functionLiteral)
: typeArguments (label? functionLiteral)
;
memberAccessOperation
: "." : "?." : "?"
;
typeArguments
: "<" type{","} ">"
;
valueArguments
: "(" (SimpleName "=")? ("out" | "ref")? expression{","} ")"
;
jump
: "throw" expression
: "return" label? expression?
: "continue" label?
: "break" label?
// yield ?
;
tupleLiteral
: "#" "(" (((SimpleName "=")? expression){","})? ")"
;
// one can use "it" as a parameter name
functionLiteral
: "{" statements "}"
: "{" (modifiers SimpleName){","} "->" statements "}"
: "{" (type ".")? "(" (modifiers SimpleName (":" type)?){","} ")" (":" type)? "->" statements "}"
;
statements
: SEMI* statement{SEMI+} SEMI*
;
constructorInvocation
: userType callSuffix
;
arrayAccess
: "[" expression{","} "]"
;
objectLiteral
: "object" ":" delegationSpecifier{","}? classBody // Cannot make class body optional: foo(object : F, A)
;
/* Factory methods:
objectLiteral
: "object" delegationSpecifier{","} ("{" objectLiteralMember{","} "}")?
;
objectLiteralMember
: memberDeclaration
: factoryMethod
;
factoryMethod
: accessModifier? SimpleName typeParameters? functionParameters functionBody
;
*/