diff --git a/grammar/src/class.grm b/grammar/src/class.grm index c7d533f6925..abe86e7dd97 100644 --- a/grammar/src/class.grm +++ b/grammar/src/class.grm @@ -16,19 +16,28 @@ class "wraps"? ("(" primaryConstructorParameter{","} ")")? (":" delegationSpecifier{","})? - ("where" typeConstraint{","})? // Syntax is questionable + ("where" typeConstraint{","})? // TODO: Syntax is questionable classBody? ; -classBody - : ("{" memberDeclaration{","} "}" - ; - classModifier : accessModifier : "abstract" - : "interface" // TODO: better name - : "sealed" + : "virtual" + : "enum" + : "extendable" // TODO: open? + // sealed by default + ; + +accessModifier + : "private" + : "protected" + : "public" + : "internal" + ; + +classBody + : ("{" memberDeclaration{","} "}")? ; delegationSpecifier @@ -44,8 +53,13 @@ typeParameter : varianceAnnotation? SimpleName (":" userType)? ; +varianceAnnotation + : "in" + : "out" + ; + typeConstraint - : userType ":" (userType | "this") + : userType ":" (userType | "this") // "this" for self-type // TODO: other constraints, maybe ; diff --git a/grammar/src/class_members.grm b/grammar/src/class_members.grm index 2fd7ae9de5d..3ee92cc10a7 100644 --- a/grammar/src/class_members.grm +++ b/grammar/src/class_members.grm @@ -39,10 +39,6 @@ memberDeclaration : class ; -decomposer // TODO: consider other names - : "decomposer" SimpleName "(" SimpleName{","} ")" // Public properties only - ; - classObject : "class" objectLiteral ; @@ -51,38 +47,57 @@ constructor : accessModifier? "this" functionParameters (":" initializers) block ; +functionParameters + : "(" functionParameter{","}? ")" // default values + ; + +functionParameter + : parameterKind parameter ("=" expression)? + ; + +parameterKind + : "lazy" + : "out" + : "ref" + ; + initializers - : "this" functionArguments + : "this" valueArguments : constructorInvocation // type parameters may (must?) be omitted ; -super - : "super" "<" name ">" +block + : "{" expression "}" + ; + +decomposer // TODO: consider other names + : "decomposer" SimpleName "(" SimpleName{","} ")" // Public properties only ; method - : methodModifier* "fun" functionRest + : memberModifier* "fun" functionRest + ; + +memberModifier + : accessModifier + : "override" + : "virtual" + : "abstract" + : // TODO: inline ; functionRest : SimpleName typeParameters? functionParameters (":" type)? functionBody? ; -functionParameters - : "(" ((parameter ("=" expression)?){","})? ")" // default values - ; - -block - : "{" expression* "}" - ; - functionBody : block : "=" expression ; property - : propertyModifier* ("val" | "var") + : memberModifier* "lazy"? ("val" | "var") propertyRest + ; propertyRest : SimpleName (":" type)? ("=" expression)? @@ -90,10 +105,13 @@ propertyRest ; getter - : methodModifier* "get" "(" ")" functionBody + : memberModifier* "get" "(" ")" functionBody ; setter - : methodModifier* "set" "(" parameter ")" functionBody + : memberModifier* "set" "(" parameter ")" functionBody ; +parameter + : SimpleName ":" type + ; \ No newline at end of file diff --git a/grammar/src/control.grm b/grammar/src/control.grm index 84e5b1329d6..0a80375a059 100644 --- a/grammar/src/control.grm +++ b/grammar/src/control.grm @@ -2,6 +2,12 @@ if : "if" "(" expression ")" expression ("else" expression)? ; +loop + : for + : while + : doWhile + ; + for : "for" "(" valOrVar? (SimpleName | parameter) "in" expression ")" expression ; diff --git a/grammar/src/expressions.grm b/grammar/src/expressions.grm index 6928ea7ef97..0fe5ec86337 100644 --- a/grammar/src/expressions.grm +++ b/grammar/src/expressions.grm @@ -7,15 +7,32 @@ expression : mapLiteral : range : "null" - : "this" + : "this" ("<" type ">")? : memberAccessExpression : expressionWithPrecedences : match : if - : "typeof" + : "typeof" // TODO: another keyword : "new" constructorInvocation // TODO: Do we need "new"?, see factory methods : objectLiteral + : declaration : jump + : loop + // block is syntactically equivalent to a functionLiteral with no parameters + ; + +literalConstant + : "true" | "false" + : StringWithTemplates + : NoEscapeString + : IntegerLiteral + : HexadecimalLiteral + : CharacterLiteral + ; + +declaration + : function + : property ; expressionWithPrecedences // See the precedence table, everything associates to the left @@ -25,6 +42,11 @@ expressionWithPrecedences // See the precedence table, everything associates to : unOpExpression : castExpression : typingExpression + : memberAccessExpression + ; + +memberAccessExpression + : (expression ".")? memberAccess ; typingExpression @@ -32,18 +54,15 @@ typingExpression ; typingOperation - : "as" : "is" : "isnot" + : "as" + : "is" + : "isnot" ; binOpExpression : expression binaryOperation expression // see priorities ; -unOpExpression - : expression postfixUnaryOperation - : prefixUnaryOperation expression - ; - binaryOperation // Decreasing precedence // . // unary @@ -68,6 +87,11 @@ assignment : memberAccessExpression assignmentOperator expression // Assignment to functions prohibited by a semantic check ; +unOpExpression + : expression postfixUnaryOperation + : prefixUnaryOperation expression + ; + prefixUnaryOperation : "-" : "+" : "++" : "--" @@ -120,11 +144,11 @@ constructorInvocation ; listLiteral - : "[" expression{","} "]" + : "[" expression{","}? "]" ; mapLiteral - : "[" mapEntryLiteral ("," mapEntryLiteral)* "]" + : "[" mapEntryLiteral{","} "]" : "[" ":" "]" ; @@ -132,25 +156,10 @@ mapEntryLiteral : expression ":" expression ; -literalConstant - : booleanLiteral - : IntegerLiteral - : HexadecimalLiteral - : CharacterLiteral - : SingleLineStringLiteral - : SingleLineStringTemplate - : MultiLineStringLiteral - : MultiLineStringTemplate - ; - range : "[" expression ".." expression "]" ; -memberAccessExpression - : (expression ".")? memberAccess - ; - memberAccess : functionCall : fieldOrPropertyAccess @@ -166,7 +175,7 @@ arrayAccess ; objectLiteral - : "object" delegationSpecifier{","} classBody? + : "object" delegationSpecifier{","}? classBody? ; /* Factory methods: diff --git a/grammar/src/extension.grm b/grammar/src/extension.grm index d79baa79817..b919f270664 100644 --- a/grammar/src/extension.grm +++ b/grammar/src/extension.grm @@ -6,7 +6,7 @@ extension ; completeExtension - : accessModifier? "extension" SimpleName? typeParameters? "for" type classBody? + : accessModifier? "extension" SimpleName? typeParameters? "for" type classBody? // properties cannot be lazy, cannot have backing fields ; extensionMethod @@ -14,7 +14,7 @@ extensionMethod ; extensionProperty - : accessModifier? propertyModifier? ("val" | "var") type "." propertyRest + : accessModifier? propertyModifier? ("val" | "var") type "." propertyRest // Extension property cannot be lazy ; extensionDecomposer diff --git a/grammar/src/lexical.grm b/grammar/src/lexical.grm index 2368e558c62..9b98f837acd 100644 --- a/grammar/src/lexical.grm +++ b/grammar/src/lexical.grm @@ -5,7 +5,9 @@ IntegerLiteral : Digit+ "L"? [helper] HexDigit : Digit | ["A"-"F", "a"-"f"]; HexadecimalLiteral : "0x" HexDigit+; -StringWithPatterns : /*single-quoted string, $ can be escaped*/; +CharacterLiteral : /*character as in Java*/ + +StringWithTemplates : /*single-quoted string, $ can be escaped*/; NoEscapeString : /* @-prefixed string, maybe allow putting a bar for indentation correction */; /* diff --git a/grammar/src/match.grm b/grammar/src/match.grm index 2b88ee7fc4c..99c1e8ca479 100644 --- a/grammar/src/match.grm +++ b/grammar/src/match.grm @@ -14,6 +14,10 @@ pattern : decomposerPattern // labeled components are allowed ; +constantPattern + : literalConstant + ; + tuplePattern : "(" ((SimpleName "=")? pattern{","})? ")" ; diff --git a/grammar/src/other.grm b/grammar/src/other.grm index bc4806ee703..e69de29bb2d 100644 --- a/grammar/src/other.grm +++ b/grammar/src/other.grm @@ -1,15 +0,0 @@ -accessModifier - : "private" - : "protected" - : "public" - : "internal" - ; - -parameter - : SimpleName ":" type - // TODO: lazy, out, ref ... - ; - -functionArguments - : "(" (expression{","})* ")" - ; diff --git a/grammar/src/toplevel.grm b/grammar/src/toplevel.grm index 9e0abe9928e..9cec344a502 100644 --- a/grammar/src/toplevel.grm +++ b/grammar/src/toplevel.grm @@ -1,7 +1,10 @@ -jetlFile +[start] jetlFile : preamble toplevelObject* ; +[start] script + : expression + preamble : namespaceHeader? import* ; diff --git a/grammar/src/types.grm b/grammar/src/types.grm index 017f0d2beb9..bd13fa46ff2 100644 --- a/grammar/src/types.grm +++ b/grammar/src/types.grm @@ -16,13 +16,7 @@ type ; userType - : name ("<" (projectionSpecifier type){","} ">")? - ; - -projectionSpecifier - : "in" - : "out" - : empty + : SimpleName{"."} ("<" (varianceSpecifier? type){","} ">")? ; functionType