Update: ExprParser
This commit is contained in:
189
ExprParser.g4
189
ExprParser.g4
@@ -3,7 +3,7 @@ parser grammar ExprParser;
|
||||
options {
|
||||
tokenVocab=ExprLexer;
|
||||
}
|
||||
|
||||
prog: compilationUnit EOF;
|
||||
identifier: IDENTIFIER;
|
||||
|
||||
qualifiedIdentifier: identifier (Dot identifier)*;
|
||||
@@ -14,10 +14,42 @@ literal: IntegerLiteral
|
||||
| StringLiteral
|
||||
| BooleanLiteral
|
||||
| NullLiteral;
|
||||
//Section 15.26: Assignment Operators
|
||||
expression: unaryExpr assignmentOperator expression | conditionalExpr ;
|
||||
|
||||
//Highest Prescedence _Expr = expression
|
||||
//CI cha 6
|
||||
conditionalExpr: logicalOrExpr Question conditionalExpr Colon conditionalExpr | logicalOrExpr;
|
||||
|
||||
logicalOrExpr: logicalAndExpr (ConditionalOR logicalAndExpr)*;
|
||||
|
||||
logicalAndExpr: equalityExpr (ConditionalAND equalityExpr)*;
|
||||
|
||||
equalityExpr: relationalExpr ((EqualTo | NotEqualTo) relationalExpr)*;
|
||||
|
||||
relationalExpr: additiveExpr((LessThan | GreaterThan | LessThanEqualTo | GreaterThanEqualTo) additiveExpr | InstanceOf type)*;
|
||||
|
||||
additiveExpr: multiplicativeExpr((Addition | Subtraction) multiplicativeExpr)*;
|
||||
multiplicativeExpr: unaryExpr((Multiplication | Division | Remainder) unaryExpr)*;
|
||||
|
||||
unaryExpr:postfixExpr |(Addition | Subtraction | LogicalComplement | BitwiseComplement | Increment | Decrement) unaryExpr | parExpression type unaryExpr;
|
||||
|
||||
postfixExpr: primaryExpr (Increment| Decrement| Dot IDENTIFIER | SquareBracketLeft expression SquareBracketRight | arguments)*;
|
||||
|
||||
primaryExpr: parExpression
|
||||
| IntegerLiteral
|
||||
| FloatingPointLiteral
|
||||
| StringLiteral
|
||||
| CharacterLiteral
|
||||
| BooleanLiteral
|
||||
| NullLiteral
|
||||
| IDENTIFIER
|
||||
| This
|
||||
| Super (Dot IDENTIFIER)?
|
||||
| New type arguments
|
||||
| primitiveType (SquareBracketLeft SquareBracketRight)* Dot Class
|
||||
| type Dot Class;
|
||||
|
||||
expression
|
||||
: expression1 (assignmentOperator expression1)?
|
||||
;
|
||||
|
||||
assignmentOperator: Assignment
|
||||
| AddAssign
|
||||
@@ -32,88 +64,21 @@ assignmentOperator: Assignment
|
||||
| SignedRightShiftAssign
|
||||
| UnsignedRightShiftAssign;
|
||||
|
||||
type: identifier (Dot identifier)* bracketsOpt
|
||||
| basicType bracketsOpt;
|
||||
type: identifier (Dot identifier)* bracketsOpt | primitiveType;
|
||||
|
||||
statementExpression: expression;
|
||||
|
||||
constantExpression: expression;
|
||||
|
||||
expression1: expression2 (Question expression Colon expression1)?;
|
||||
|
||||
expression2: expression3 (expression2Rest)?;
|
||||
|
||||
expression2Rest: (infixop expression3)+
|
||||
| InstanceOf type;
|
||||
|
||||
infixop: ConditionalOR
|
||||
| ConditionalAND
|
||||
| BitwiseOR
|
||||
| BitwiseXOR
|
||||
| BitwiseAND
|
||||
| EqualTo
|
||||
| NotEqualTo
|
||||
| LessThan
|
||||
| GreaterThan
|
||||
| LessThanEqualTo
|
||||
| GreaterThanEqualTo
|
||||
| LeftShift
|
||||
| SignedRightShift
|
||||
| UnsignedRightShift
|
||||
| Addition
|
||||
| Subtraction
|
||||
| Multiplication
|
||||
| Division
|
||||
| Remainder;
|
||||
|
||||
expression3: prefixOp expression3
|
||||
| ParenthesesLeft type ParenthesesRight expression3
|
||||
| primary postfixExpression*;
|
||||
|
||||
postfixExpression: selector | postfixOp;
|
||||
|
||||
primary: ParenthesesLeft expression ParenthesesRight
|
||||
| This (arguments)?
|
||||
| Super superSuffix
|
||||
| literal
|
||||
| New creator
|
||||
| identifier (Dot identifier)* (identifierSuffix)?
|
||||
| basicType bracketsOpt Dot Class
|
||||
| Void Dot Class;
|
||||
|
||||
identifierSuffix: SquareBracketLeft SquareBracketRight bracketsOpt Dot Class //Case []...'.'class
|
||||
|SquareBracketLeft expression SquareBracketRight //arr[5]
|
||||
identifierSuffix: SquareBracketLeft SquareBracketRight bracketsOpt Dot Class
|
||||
|SquareBracketLeft expression SquareBracketRight
|
||||
|arguments
|
||||
|Dot (Class | This | Super arguments | New innerCreator);
|
||||
|
||||
prefixOp: Increment
|
||||
| Decrement
|
||||
| LogicalComplement
|
||||
| BitwiseComplement
|
||||
| Addition
|
||||
| Subtraction;
|
||||
|Dot (Class | This | Super arguments New innerCreator);
|
||||
|
||||
postfixOp: Increment | Decrement;
|
||||
|
||||
selector: Dot identifier (arguments)?
|
||||
| Dot This
|
||||
| Dot Super superSuffix
|
||||
| Dot New innerCreator
|
||||
| SquareBracketLeft expression SquareBracketRight;
|
||||
|
||||
superSuffix: arguments
|
||||
| Dot identifier (arguments)?;
|
||||
|
||||
//primitives
|
||||
basicType: Byte
|
||||
| Short
|
||||
| Char
|
||||
| Int
|
||||
| Long
|
||||
| Float
|
||||
| Double
|
||||
| Boolean;
|
||||
|
||||
primitiveType: Byte | Short| Char | Int| Long| Float| Double| Boolean;
|
||||
//Method arguments
|
||||
argumentsOpt: (arguments)?;
|
||||
|
||||
arguments: ParenthesesLeft (expression (Comma expression)*)? ParenthesesRight;
|
||||
@@ -125,8 +90,7 @@ creator: qualifiedIdentifier ( arrayCreatorRest | classCreatorRest);
|
||||
innerCreator: identifier classCreatorRest;
|
||||
|
||||
arrayCreatorRest: SquareBracketLeft SquareBracketRight bracketsOpt arrayInitializer
|
||||
| SquareBracketLeft expression SquareBracketRight (SquareBracketLeft expression SquareBracketRight)* bracketsOpt;
|
||||
|
||||
| SquareBracketLeft expression SquareBracketRight (SquareBracketLeft expression SquareBracketRight)*;
|
||||
classCreatorRest: arguments (classBody)?;
|
||||
|
||||
arrayInitializer: CurlyBracketLeft (variableInitializer(Comma variableInitializer)*(Comma)?)? CurlyBracketRight;
|
||||
@@ -142,44 +106,45 @@ blockStatements: (blockStatement)*;
|
||||
|
||||
blockStatement: localVariableDeclarationStatement
|
||||
|classOrInterfaceDeclaration
|
||||
|labeledStatement
|
||||
|statement;
|
||||
|(identifier Colon)? statement;
|
||||
|
||||
labeledStatement: identifier Colon statement;
|
||||
localVariableDeclarationStatement: (Final)? type variableDeclarators;
|
||||
|
||||
localVariableDeclarationStatement: (Final)? type variableDeclarators Semicolon;
|
||||
statement:completeIf | If parExpression statementIncompleteIf;
|
||||
|
||||
statement: block
|
||||
| If parExpression statement (Else statement)?
|
||||
| For ParenthesesLeft forInit? Semicolon (expression)? Semicolon forUpdate? ParenthesesRight statement
|
||||
| While parExpression statement
|
||||
| Do statement While parExpression Semicolon
|
||||
| Try block catches
|
||||
| Try block Finally block
|
||||
| Try block catches Finally block
|
||||
| switchStatement
|
||||
| Synchronized parExpression block
|
||||
| Return (expression)? Semicolon
|
||||
| Throw expression Semicolon
|
||||
| Break (identifier)? Semicolon
|
||||
| Continue (identifier)? Semicolon
|
||||
| statementExpression Semicolon
|
||||
| Semicolon;
|
||||
statementIncompleteIf: block
|
||||
| If parExpression completeIf Else statementIncompleteIf
|
||||
| For ParenthesesLeft forInit? Semicolon (expression)? Semicolon forUpdate? ParenthesesRight statement
|
||||
| While parExpression statement
|
||||
| Do statement While parExpression Semicolon
|
||||
| Try block catches
|
||||
| Try block Finally block
|
||||
| Try block catches Finally block
|
||||
| switchStatement
|
||||
| Synchronized parExpression block
|
||||
| Return (expression)? Semicolon
|
||||
| Throw expression Semicolon
|
||||
| Break (identifier)? Semicolon
|
||||
| Continue (identifier)? Semicolon
|
||||
| statementExpression Semicolon
|
||||
| Semicolon;
|
||||
//solve ambiguity over If()else. aka (Dangling else).
|
||||
//this will do the longest check by looking for an else section, fincding the closest else!
|
||||
completeIf: If parExpression completeIf Else completeIf | statementIncompleteIf;
|
||||
|
||||
catches: catchClause+;
|
||||
catches: catchClause (catchClause)*;
|
||||
|
||||
catchClause: Catch ParenthesesLeft formalParameter ParenthesesRight block;
|
||||
|
||||
switchStatement: Switch parExpression CurlyBracketLeft CurlyBracketRight
|
||||
| Switch parExpression CurlyBracketLeft switchBlockStatementGroups CurlyBracketRight;
|
||||
| Switch parExpression CurlyBracketLeft switchBlockStatementGroups? CurlyBracketRight;
|
||||
|
||||
switchBlockStatementGroups: switchBlockStatementGroup+;
|
||||
switchBlockStatementGroups: (switchBlockStatementGroup)*;
|
||||
|
||||
switchBlockStatementGroup: switchLabel blockStatements;
|
||||
|
||||
switchLabel: Case constantExpression Colon
|
||||
| Default Colon;
|
||||
|
||||
moreStatementExpressions: (Comma statementExpression)*;
|
||||
|
||||
forInit: statementExpression moreStatementExpressions
|
||||
@@ -207,7 +172,7 @@ variableDeclaratorsRest: variableDeclaratorRest (Comma variableDeclarator)*;
|
||||
|
||||
constantDeclaratorsRest: constantDeclaratorRest (Comma constantDeclarator)*;
|
||||
|
||||
variableDeclarator: identifier variableDeclaratorRest;
|
||||
variableDeclarator: identifier variableDeclaratorsRest;
|
||||
|
||||
constantDeclarator: identifier constantDeclaratorRest;
|
||||
|
||||
@@ -217,12 +182,11 @@ constantDeclaratorRest: bracketsOpt Assignment variableInitializer;
|
||||
|
||||
variableDeclaratorId: identifier bracketsOpt;
|
||||
|
||||
compilationUnit: (Package qualifiedIdentifier Semicolon)? (importDeclaration)* (typeDeclaration)*;
|
||||
compilationUnit: (Package qualifiedIdentifier Semicolon)? (importDeclaration)*(typeDeclaration)*;
|
||||
|
||||
importDeclaration: Import identifier (Dot identifier)* (Dot Multiplication)? Semicolon;
|
||||
|
||||
typeDeclaration: classOrInterfaceDeclaration
|
||||
| Semicolon;
|
||||
typeDeclaration: classOrInterfaceDeclaration | Semicolon;
|
||||
|
||||
classOrInterfaceDeclaration: modifiersOpt (classDeclaration | interfaceDeclaration);
|
||||
|
||||
@@ -239,7 +203,6 @@ interfaceBody: CurlyBracketLeft (interfaceBodyDeclaration)* CurlyBracketRight;
|
||||
classBodyDeclaration: Semicolon
|
||||
| (Static)? block
|
||||
| modifiersOpt memberDecl;
|
||||
|
||||
memberDecl: methodOrFieldDecl
|
||||
| Void identifier methodDeclaratorRest
|
||||
| identifier constructorDeclaratorRest
|
||||
@@ -247,7 +210,7 @@ memberDecl: methodOrFieldDecl
|
||||
|
||||
methodOrFieldDecl: type identifier methodOrFieldRest;
|
||||
|
||||
methodOrFieldRest: variableDeclaratorRest Semicolon
|
||||
methodOrFieldRest: variableDeclaratorRest
|
||||
|methodDeclaratorRest;
|
||||
|
||||
interfaceBodyDeclaration: Semicolon
|
||||
@@ -268,7 +231,7 @@ voidMethodDeclaratorRest: formalParameters (Throws qualifiedIdentifierList)? (me
|
||||
|
||||
interfaceMethodDeclaratorRest: formalParameters bracketsOpt (Throws qualifiedIdentifierList)? Semicolon;
|
||||
|
||||
voidInterfaceMethodDeclaratorRest: formalParameters (Throws qualifiedIdentifierList)? Semicolon;
|
||||
voidInterfaceMethodDeclaratorRest: formalParameters (Throws qualifiedIdentifierList)?;
|
||||
|
||||
constructorDeclaratorRest: formalParameters (Throws qualifiedIdentifierList)? methodBody;
|
||||
|
||||
@@ -278,4 +241,8 @@ formalParameters: ParenthesesLeft (formalParameter (Comma formalParameter)*)? Pa
|
||||
|
||||
formalParameter: (Final)? type variableDeclaratorId;
|
||||
|
||||
methodBody: block;
|
||||
methodBody: block;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user