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--)
   }
}
This commit is contained in:
Andrey Breslav
2011-12-20 22:55:25 +04:00
parent c9959a10f9
commit 41fd43b5e5
124 changed files with 1602 additions and 758 deletions
+7 -15
View File
@@ -6,7 +6,7 @@ bq. See [Expressions]
h3. Precedence
||*Precedence*||Title||Symbols||
|Highest|Postfix|{{\+\+}}, {{\-\-}}, {{#}}, {{.}}, {{?.}}, {{?}}|
|Highest|Postfix|{{\+\+}}, {{\-\-}}, {{.}}, {{?.}}, {{?}}|
| |Prefix|{{-}}, {{\+}}, {{\+\+}}, {{\-\-}}, {{!}}, [{{@label}}|#LabelName], {{@}}, {{@@}} |
| |Type RHS|{{:}}, {{as}}, {{as?}} |
| |Multiplicative|{{*}}, {{/}}, {{%}} |
@@ -19,7 +19,6 @@ h3. Precedence
| |Equality|{{==}}, {{\!==}}|
| |Conjunction|{{&&}}|
| |Disjunction|{{\|\|}}|
| |Arrow|{{\->}}|
|Lowest|Assignment|{{=}}, {{+=}}, {{-=}}, {{*=}}, {{/=}}, {{%=}}|
h3. Rules
@@ -41,17 +40,12 @@ Decreasing precedence:
equalityOperation
"&&"
"||"
"->"
assignmentOperator
*/
expression
: arrowTupleExpression (assignmentOperator arrowTupleExpression)?
;
arrowTupleExpression
: disjunction ("->" disjunction)*
: disjunction (assignmentOperator disjunction)*
;
disjunction
@@ -109,7 +103,7 @@ postfixUnaryExpression
// !!! When you add here, remember to update the FIRST set in the parser
atomicExpression
: "(" expression ")" // see tupleLiteral
: "(" expression ")"
: literalConstant
: functionLiteral
: tupleLiteral
@@ -230,7 +224,7 @@ callSuffix
;
memberAccessOperation
: "." : "?." : "#" : "?"
: "." : "?." : "?"
;
typeArguments
@@ -249,17 +243,15 @@ jump
// yield ?
;
// Ambiguity when after a SimpleName (infix call). In this case (e) is treated as an expression in parentheses
// to put a tuple, write write ((e))
tupleLiteral
: "(" (((SimpleName "=")? expression){","})? ")"
: "#" "(" (((SimpleName "=")? expression){","})? ")"
;
// one can use "it" as a parameter name
functionLiteral
: "{" statements "}"
: "{" (modifiers SimpleName){","} "=>" statements "}"
: "{" (type ".")? "(" (modifiers SimpleName (":" type)?){","} ")" (":" type)? "=>" statements "}"
: "{" (modifiers SimpleName){","} "->" statements "}"
: "{" (type ".")? "(" (modifiers SimpleName (":" type)?){","} ")" (":" type)? "->" statements "}"
;
statements
+9 -8
View File
@@ -7,11 +7,11 @@ bq. See [Types]
/*
Foo<Bar<X>, T, Object> // user type
{(A, Object) : Foo} // function type
{ () : Foo} // function with no arguments
(A, B, C) // tuple type
(a : A, b : B) // tuple type with named entries
() // 0-ary tuple type (Unit)
(A, Object) -> Foo // function type
() -> Foo // function with no arguments
#(A, B, C) // tuple type
#(a : A, b : B) // tuple type with named entries
#() // 0-ary tuple type (Unit)
*/
@@ -20,6 +20,7 @@ type
// IF YOU CHANGE THIS, please, update TYPE_FIRST in JetParsing
typeDescriptor
: "(" typeDescriptor ")"
: selfType
: functionType
: userType
@@ -47,12 +48,12 @@ optionalProjection
;
functionType
: "fun" (type ".")? "(" (parameter | modifiers /*lazy out ref*/ type){","} ")" (":" type)? // Unit by default
: (type ".")? "(" (parameter | modifiers /*lazy out ref*/ type){","} ")" "->" type?
;
tupleType
: "(" type{","}? ")"
: "(" parameter{","} ")" // tuple with named entries, the names do not affect assignment compatibility
: "#" "(" type{","}? ")"
: "#" "(" parameter{","} ")" // tuple with named entries, the names do not affect assignment compatibility
;
////////////////////////////////////////
+1 -1
View File
@@ -36,7 +36,7 @@ pattern
decomposerPattern
: type
// TODO : typeArguments will be consumed by the expression
: elvisExpression typeArguments? "@" tuplePattern
: elvisExpression typeArguments? tuplePattern
;
constantPattern