Grammar reviewed

This commit is contained in:
Andrey Breslav
2010-12-07 18:59:38 +03:00
parent 03ef48508f
commit 0f87280fb5
10 changed files with 114 additions and 79 deletions
+22 -8
View File
@@ -16,19 +16,28 @@ class
"wraps"? "wraps"?
("(" primaryConstructorParameter{","} ")")? ("(" primaryConstructorParameter{","} ")")?
(":" delegationSpecifier{","})? (":" delegationSpecifier{","})?
("where" typeConstraint{","})? // Syntax is questionable ("where" typeConstraint{","})? // TODO: Syntax is questionable
classBody? classBody?
; ;
classBody
: ("{" memberDeclaration{","} "}"
;
classModifier classModifier
: accessModifier : accessModifier
: "abstract" : "abstract"
: "interface" // TODO: better name : "virtual"
: "sealed" : "enum"
: "extendable" // TODO: open?
// sealed by default
;
accessModifier
: "private"
: "protected"
: "public"
: "internal"
;
classBody
: ("{" memberDeclaration{","} "}")?
; ;
delegationSpecifier delegationSpecifier
@@ -44,8 +53,13 @@ typeParameter
: varianceAnnotation? SimpleName (":" userType)? : varianceAnnotation? SimpleName (":" userType)?
; ;
varianceAnnotation
: "in"
: "out"
;
typeConstraint typeConstraint
: userType ":" (userType | "this") : userType ":" (userType | "this") // "this" for self-type
// TODO: other constraints, maybe // TODO: other constraints, maybe
; ;
+37 -19
View File
@@ -39,10 +39,6 @@ memberDeclaration
: class : class
; ;
decomposer // TODO: consider other names
: "decomposer" SimpleName "(" SimpleName{","} ")" // Public properties only
;
classObject classObject
: "class" objectLiteral : "class" objectLiteral
; ;
@@ -51,38 +47,57 @@ constructor
: accessModifier? "this" functionParameters (":" initializers) block : accessModifier? "this" functionParameters (":" initializers) block
; ;
functionParameters
: "(" functionParameter{","}? ")" // default values
;
functionParameter
: parameterKind parameter ("=" expression)?
;
parameterKind
: "lazy"
: "out"
: "ref"
;
initializers initializers
: "this" functionArguments : "this" valueArguments
: constructorInvocation // type parameters may (must?) be omitted : constructorInvocation // type parameters may (must?) be omitted
; ;
super block
: "super" "<" name ">" : "{" expression "}"
;
decomposer // TODO: consider other names
: "decomposer" SimpleName "(" SimpleName{","} ")" // Public properties only
; ;
method method
: methodModifier* "fun" functionRest : memberModifier* "fun" functionRest
;
memberModifier
: accessModifier
: "override"
: "virtual"
: "abstract"
: // TODO: inline
; ;
functionRest functionRest
: SimpleName typeParameters? functionParameters (":" type)? functionBody? : SimpleName typeParameters? functionParameters (":" type)? functionBody?
; ;
functionParameters
: "(" ((parameter ("=" expression)?){","})? ")" // default values
;
block
: "{" expression* "}"
;
functionBody functionBody
: block : block
: "=" expression : "=" expression
; ;
property property
: propertyModifier* ("val" | "var") : memberModifier* "lazy"? ("val" | "var") propertyRest
;
propertyRest propertyRest
: SimpleName (":" type)? ("=" expression)? : SimpleName (":" type)? ("=" expression)?
@@ -90,10 +105,13 @@ propertyRest
; ;
getter getter
: methodModifier* "get" "(" ")" functionBody : memberModifier* "get" "(" ")" functionBody
; ;
setter setter
: methodModifier* "set" "(" parameter ")" functionBody : memberModifier* "set" "(" parameter ")" functionBody
; ;
parameter
: SimpleName ":" type
;
+6
View File
@@ -2,6 +2,12 @@ if
: "if" "(" expression ")" expression ("else" expression)? : "if" "(" expression ")" expression ("else" expression)?
; ;
loop
: for
: while
: doWhile
;
for for
: "for" "(" valOrVar? (SimpleName | parameter) "in" expression ")" expression : "for" "(" valOrVar? (SimpleName | parameter) "in" expression ")" expression
; ;
+35 -26
View File
@@ -7,15 +7,32 @@ expression
: mapLiteral : mapLiteral
: range : range
: "null" : "null"
: "this" : "this" ("<" type ">")?
: memberAccessExpression : memberAccessExpression
: expressionWithPrecedences : expressionWithPrecedences
: match : match
: if : if
: "typeof" : "typeof" // TODO: another keyword
: "new" constructorInvocation // TODO: Do we need "new"?, see factory methods : "new" constructorInvocation // TODO: Do we need "new"?, see factory methods
: objectLiteral : objectLiteral
: declaration
: jump : 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 expressionWithPrecedences // See the precedence table, everything associates to the left
@@ -25,6 +42,11 @@ expressionWithPrecedences // See the precedence table, everything associates to
: unOpExpression : unOpExpression
: castExpression : castExpression
: typingExpression : typingExpression
: memberAccessExpression
;
memberAccessExpression
: (expression ".")? memberAccess
; ;
typingExpression typingExpression
@@ -32,18 +54,15 @@ typingExpression
; ;
typingOperation typingOperation
: "as" : "is" : "isnot" : "as"
: "is"
: "isnot"
; ;
binOpExpression binOpExpression
: expression binaryOperation expression // see priorities : expression binaryOperation expression // see priorities
; ;
unOpExpression
: expression postfixUnaryOperation
: prefixUnaryOperation expression
;
binaryOperation // Decreasing precedence binaryOperation // Decreasing precedence
// . // .
// unary // unary
@@ -68,6 +87,11 @@ assignment
: memberAccessExpression assignmentOperator expression // Assignment to functions prohibited by a semantic check : memberAccessExpression assignmentOperator expression // Assignment to functions prohibited by a semantic check
; ;
unOpExpression
: expression postfixUnaryOperation
: prefixUnaryOperation expression
;
prefixUnaryOperation prefixUnaryOperation
: "-" : "+" : "-" : "+"
: "++" : "--" : "++" : "--"
@@ -120,11 +144,11 @@ constructorInvocation
; ;
listLiteral listLiteral
: "[" expression{","} "]" : "[" expression{","}? "]"
; ;
mapLiteral mapLiteral
: "[" mapEntryLiteral ("," mapEntryLiteral)* "]" : "[" mapEntryLiteral{","} "]"
: "[" ":" "]" : "[" ":" "]"
; ;
@@ -132,25 +156,10 @@ mapEntryLiteral
: expression ":" expression : expression ":" expression
; ;
literalConstant
: booleanLiteral
: IntegerLiteral
: HexadecimalLiteral
: CharacterLiteral
: SingleLineStringLiteral
: SingleLineStringTemplate
: MultiLineStringLiteral
: MultiLineStringTemplate
;
range range
: "[" expression ".." expression "]" : "[" expression ".." expression "]"
; ;
memberAccessExpression
: (expression ".")? memberAccess
;
memberAccess memberAccess
: functionCall : functionCall
: fieldOrPropertyAccess : fieldOrPropertyAccess
@@ -166,7 +175,7 @@ arrayAccess
; ;
objectLiteral objectLiteral
: "object" delegationSpecifier{","} classBody? : "object" delegationSpecifier{","}? classBody?
; ;
/* Factory methods: /* Factory methods:
+2 -2
View File
@@ -6,7 +6,7 @@ extension
; ;
completeExtension completeExtension
: accessModifier? "extension" SimpleName? typeParameters? "for" type classBody? : accessModifier? "extension" SimpleName? typeParameters? "for" type classBody? // properties cannot be lazy, cannot have backing fields
; ;
extensionMethod extensionMethod
@@ -14,7 +14,7 @@ extensionMethod
; ;
extensionProperty extensionProperty
: accessModifier? propertyModifier? ("val" | "var") type "." propertyRest : accessModifier? propertyModifier? ("val" | "var") type "." propertyRest // Extension property cannot be lazy
; ;
extensionDecomposer extensionDecomposer
+3 -1
View File
@@ -5,7 +5,9 @@ IntegerLiteral : Digit+ "L"?
[helper] HexDigit : Digit | ["A"-"F", "a"-"f"]; [helper] HexDigit : Digit | ["A"-"F", "a"-"f"];
HexadecimalLiteral : "0x" HexDigit+; 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 */; NoEscapeString : /* @-prefixed string, maybe allow putting a bar for indentation correction */;
/* /*
+4
View File
@@ -14,6 +14,10 @@ pattern
: decomposerPattern // labeled components are allowed : decomposerPattern // labeled components are allowed
; ;
constantPattern
: literalConstant
;
tuplePattern tuplePattern
: "(" ((SimpleName "=")? pattern{","})? ")" : "(" ((SimpleName "=")? pattern{","})? ")"
; ;
-15
View File
@@ -1,15 +0,0 @@
accessModifier
: "private"
: "protected"
: "public"
: "internal"
;
parameter
: SimpleName ":" type
// TODO: lazy, out, ref ...
;
functionArguments
: "(" (expression{","})* ")"
;
+4 -1
View File
@@ -1,7 +1,10 @@
jetlFile [start] jetlFile
: preamble toplevelObject* : preamble toplevelObject*
; ;
[start] script
: expression
preamble preamble
: namespaceHeader? import* : namespaceHeader? import*
; ;
+1 -7
View File
@@ -16,13 +16,7 @@ type
; ;
userType userType
: name ("<" (projectionSpecifier type){","} ">")? : SimpleName{"."} ("<" (varianceSpecifier? type){","} ">")?
;
projectionSpecifier
: "in"
: "out"
: empty
; ;
functionType functionType