mirror of
https://gitee.com/BDWare/genparser
synced 2025-04-27 06:22:17 +00:00
451 lines
12 KiB
ANTLR
451 lines
12 KiB
ANTLR
/*
|
|
* The MIT License (MIT)
|
|
*
|
|
* Copyright (c) 2014 by Bart Kiers (original author) and Alexandre Vitorelli (contributor -> ported to CSharp)
|
|
* Copyright (c) 2017 by Ivan Kochurkin (Positive Technologies):
|
|
added ECMAScript 6 support, cleared and transformed to the universal grammar.
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person
|
|
* obtaining a copy of this software and associated documentation
|
|
* files (the "Software"), to deal in the Software without
|
|
* restriction, including without limitation the rights to use,
|
|
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the
|
|
* Software is furnished to do so, subject to the following
|
|
* conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be
|
|
* included in all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
parser grammar YJSParser;
|
|
|
|
options {
|
|
tokenVocab=JavaScriptLexer;
|
|
superClass=JavaScriptBaseParser;
|
|
}
|
|
|
|
program
|
|
: importStmts? contractDeclar
|
|
;
|
|
|
|
contractDeclar
|
|
: annotations? (Contract|Module|Oracle|DoipModule) Identifier '{' clzOrFunctionDeclaration+ '}'
|
|
;
|
|
|
|
annotations
|
|
: annotation+
|
|
;
|
|
|
|
annotation
|
|
: AtToken Identifier '(' annotationArgs? ')'
|
|
;
|
|
|
|
annotationArgs
|
|
: annotationLiteral (',' annotationLiteral)*
|
|
;
|
|
annotationLiteral
|
|
:numericLiteral
|
|
|StringLiteral
|
|
|objectLiteral
|
|
;
|
|
|
|
clzOrFunctionDeclaration
|
|
:classDeclaration
|
|
|functionDeclaration
|
|
|interfaceDeclaration
|
|
|eventDeclaration
|
|
;
|
|
|
|
interfaceDeclaration
|
|
: annotations? Interface Identifier '(' formalParameterList? ')' eos
|
|
;
|
|
|
|
eventDeclaration
|
|
:Event eventGlobalOrLocal? Identifier SemiColon
|
|
|Event eventGlobalOrLocal? Identifier '(' eventSemantics? ')' SemiColon
|
|
;
|
|
eventGlobalOrLocal
|
|
:Global
|
|
|Local
|
|
;
|
|
eventSemantics
|
|
:AtLeastOnce
|
|
|AtMostOnce
|
|
|OnlyOnce
|
|
;
|
|
sourceElement
|
|
: statement
|
|
;
|
|
|
|
importStmts
|
|
: importStmt+
|
|
;
|
|
|
|
importStmt
|
|
: Import StringLiteral SemiColon
|
|
;
|
|
|
|
exportStmt
|
|
: Export Identifier versionName SemiColon
|
|
;
|
|
|
|
versionName: DecimalLiteral ('.' DecimalLiteral)* ;
|
|
|
|
statement
|
|
: block
|
|
| variableStatement
|
|
| emptyStatement
|
|
| expressionStatement
|
|
| ifStatement
|
|
| iterationStatement
|
|
| continueStatement
|
|
| breakStatement
|
|
| returnStatement
|
|
| switchStatement
|
|
;
|
|
|
|
block
|
|
: '{' statementList? '}'
|
|
;
|
|
|
|
statementList
|
|
: statement+
|
|
;
|
|
|
|
variableStatement
|
|
: varModifier variableDeclarationList eos
|
|
;
|
|
|
|
variableDeclarationList
|
|
: variableDeclaration (',' variableDeclaration)*
|
|
;
|
|
|
|
variableDeclaration
|
|
: (Identifier) ('=' singleExpression)? // ECMAScript 6: Array & Object Matching
|
|
;
|
|
|
|
emptyStatement
|
|
: SemiColon
|
|
;
|
|
|
|
expressionStatement
|
|
: {notOpenBraceAndNotFunction()}? expressionSequence eos
|
|
;
|
|
|
|
ifStatement
|
|
: If '(' expressionSequence ')' statement (Else statement)?
|
|
;
|
|
|
|
|
|
iterationStatement
|
|
: Do statement While '(' expressionSequence ')' eos # DoStatement
|
|
| While '(' expressionSequence ')' statement # WhileStatement
|
|
| For '(' expressionSequence? ';' expressionSequence? ';' expressionSequence? ')' statement # ForStatement
|
|
| For '(' varModifier variableDeclarationList ';' expressionSequence? ';' expressionSequence? ')'
|
|
statement # ForVarStatement
|
|
| For '(' singleExpression (In | Identifier{p("of")}?) expressionSequence ')' statement # ForInStatement
|
|
| For '(' varModifier variableDeclaration (In | Identifier{p("of")}?) expressionSequence ')' statement # ForVarInStatement
|
|
;
|
|
|
|
varModifier // let, const - ECMAScript 6
|
|
: Var
|
|
;
|
|
|
|
continueStatement
|
|
: Continue ({notLineTerminator()}? Identifier)? eos
|
|
;
|
|
|
|
breakStatement
|
|
: Break ({notLineTerminator()}? Identifier)? eos
|
|
;
|
|
|
|
returnStatement
|
|
: Return ({notLineTerminator()}? expressionSequence)? eos
|
|
;
|
|
|
|
withStatement
|
|
: With '(' expressionSequence ')' statement
|
|
;
|
|
|
|
switchStatement
|
|
: Switch '(' expressionSequence ')' caseBlock
|
|
;
|
|
|
|
caseBlock
|
|
: '{' caseClauses? (defaultClause caseClauses?)? '}'
|
|
;
|
|
|
|
caseClauses
|
|
: caseClause+
|
|
;
|
|
|
|
caseClause
|
|
: Case expressionSequence ':' statementList?
|
|
;
|
|
|
|
defaultClause
|
|
: Default ':' statementList?
|
|
;
|
|
|
|
|
|
|
|
throwStatement
|
|
: Throw {notLineTerminator()}? expressionSequence eos
|
|
;
|
|
|
|
tryStatement
|
|
: Try block (catchProduction finallyProduction? | finallyProduction)
|
|
;
|
|
|
|
catchProduction
|
|
: Catch '(' Identifier ')' block
|
|
;
|
|
|
|
finallyProduction
|
|
: Finally block
|
|
;
|
|
|
|
debuggerStatement
|
|
: Debugger eos
|
|
;
|
|
|
|
functionDeclaration
|
|
: annotations? Export? Function Identifier '(' formalParameterList? ')' View? '{' functionBody '}'
|
|
;
|
|
|
|
classDeclaration
|
|
: Class Identifier classTail
|
|
;
|
|
|
|
classTail
|
|
: (Extends singleExpression)? '{' classElement* '}'
|
|
;
|
|
|
|
classElement
|
|
: Static? methodDefinition
|
|
;
|
|
|
|
methodDefinition
|
|
: propertyName '(' formalParameterList? ')' '{' functionBody '}'
|
|
;
|
|
|
|
formalParameterList
|
|
: formalParameterArg (',' formalParameterArg)* (',' lastFormalParameterArg)?
|
|
| lastFormalParameterArg
|
|
| arrayLiteral // ECMAScript 6: Parameter Context Matching
|
|
| objectLiteral // ECMAScript 6: Parameter Context Matching
|
|
;
|
|
|
|
formalParameterArg
|
|
: Identifier ('=' singleExpression)? // ECMAScript 6: Initialization
|
|
;
|
|
|
|
lastFormalParameterArg // ECMAScript 6: Rest Parameter
|
|
: Ellipsis Identifier
|
|
;
|
|
|
|
functionBody
|
|
: sourceElements?
|
|
;
|
|
|
|
sourceElements
|
|
: sourceElement+
|
|
;
|
|
|
|
arrayLiteral
|
|
: '[' ','* elementList? ','* ']'
|
|
;
|
|
|
|
elementList
|
|
: singleExpression (','+ singleExpression)* (','+ lastElement)?
|
|
| lastElement
|
|
;
|
|
|
|
lastElement // ECMAScript 6: Spread Operator
|
|
: Ellipsis Identifier
|
|
;
|
|
|
|
objectLiteral
|
|
: '{' (propertyAssignment (',' propertyAssignment)*)? ','? '}'
|
|
;
|
|
|
|
propertyAssignment
|
|
: propertyName (':' |'=') singleExpression # PropertyExpressionAssignment
|
|
| '[' singleExpression ']' ':' singleExpression # ComputedPropertyExpressionAssignment
|
|
| Identifier # PropertyShorthand
|
|
;
|
|
|
|
propertyName
|
|
: identifierName
|
|
| StringLiteral
|
|
| numericLiteral
|
|
;
|
|
|
|
arguments
|
|
: '('(
|
|
singleExpression (',' singleExpression)* (',' lastArgument)? |
|
|
lastArgument
|
|
)?')'
|
|
;
|
|
|
|
lastArgument // ECMAScript 6: Spread Operator
|
|
: Ellipsis Identifier
|
|
;
|
|
|
|
expressionSequence
|
|
: singleExpression (',' singleExpression)*
|
|
;
|
|
|
|
singleExpression
|
|
: singleExpression '[' expressionSequence ']' # MemberIndexExpression
|
|
| singleExpression '.' identifierName # MemberDotExpression
|
|
| singleExpression arguments # ArgumentsExpression
|
|
| New singleExpression arguments? # NewExpression
|
|
| singleExpression {notLineTerminator()}? '++' # PostIncrementExpression
|
|
| singleExpression {notLineTerminator()}? '--' # PostDecreaseExpression
|
|
| Typeof singleExpression # TypeofExpression
|
|
| '++' singleExpression # PreIncrementExpression
|
|
| '--' singleExpression # PreDecreaseExpression
|
|
| '+' singleExpression # UnaryPlusExpression
|
|
| '-' singleExpression # UnaryMinusExpression
|
|
| '~' singleExpression # BitNotExpression
|
|
| '!' singleExpression # NotExpression
|
|
| singleExpression ('*' | '/' | '%') singleExpression # MultiplicativeExpression
|
|
| singleExpression ('+' | '-') singleExpression # AdditiveExpression
|
|
| singleExpression ('<<' | '>>' | '>>>') singleExpression # BitShiftExpression
|
|
| singleExpression ('<' | '>' | '<=' | '>=') singleExpression # RelationalExpression
|
|
| singleExpression Instanceof singleExpression # InstanceofExpression
|
|
| singleExpression In singleExpression # InExpression
|
|
| singleExpression ('==' | '!=' | '===' | '!==') singleExpression # EqualityExpression
|
|
| singleExpression '&' singleExpression # BitAndExpression
|
|
| singleExpression '^' singleExpression # BitXOrExpression
|
|
| singleExpression '|' singleExpression # BitOrExpression
|
|
| singleExpression '&&' singleExpression # LogicalAndExpression
|
|
| singleExpression '||' singleExpression # LogicalOrExpression
|
|
| singleExpression '?' singleExpression ':' singleExpression # TernaryExpression
|
|
| singleExpression '=' singleExpression # AssignmentExpression
|
|
| singleExpression assignmentOperator singleExpression # AssignmentOperatorExpression
|
|
| singleExpression TemplateStringLiteral # TemplateStringExpression // ECMAScript 6
|
|
| This # ThisExpression
|
|
| Identifier # IdentifierExpression
|
|
| Super # SuperExpression
|
|
| literal # LiteralExpression
|
|
| arrayLiteral # ArrayLiteralExpression
|
|
| objectLiteral # ObjectLiteralExpression
|
|
| '(' expressionSequence ')' # ParenthesizedExpression
|
|
| arrowFunctionParameters '=>' arrowFunctionBody # ArrowFunctionExpression // ECMAScript 6
|
|
;
|
|
|
|
arrowFunctionParameters
|
|
: Identifier
|
|
| '(' formalParameterList? ')'
|
|
;
|
|
|
|
arrowFunctionBody
|
|
: singleExpression
|
|
| '{' functionBody '}'
|
|
;
|
|
|
|
assignmentOperator
|
|
: '*='
|
|
| '/='
|
|
| '%='
|
|
| '+='
|
|
| '-='
|
|
| '<<='
|
|
| '>>='
|
|
| '>>>='
|
|
| '&='
|
|
| '^='
|
|
| '|='
|
|
;
|
|
|
|
literal
|
|
: NullLiteral
|
|
| BooleanLiteral
|
|
| StringLiteral
|
|
| TemplateStringLiteral
|
|
| RegularExpressionLiteral
|
|
| numericLiteral
|
|
;
|
|
|
|
numericLiteral
|
|
: DecimalLiteral
|
|
| HexIntegerLiteral
|
|
| OctalIntegerLiteral
|
|
| OctalIntegerLiteral2
|
|
| BinaryIntegerLiteral
|
|
;
|
|
|
|
identifierName
|
|
: Identifier
|
|
| reservedWord
|
|
;
|
|
|
|
reservedWord
|
|
: keyword
|
|
| NullLiteral
|
|
| BooleanLiteral
|
|
;
|
|
|
|
keyword
|
|
: Break
|
|
| Do
|
|
| Instanceof
|
|
| Typeof
|
|
| Case
|
|
| Else
|
|
| New
|
|
| Var
|
|
| Catch
|
|
| Finally
|
|
| Return
|
|
| Void
|
|
| Continue
|
|
| For
|
|
| Switch
|
|
| While
|
|
| Debugger
|
|
| Function
|
|
| This
|
|
| With
|
|
| Default
|
|
| If
|
|
| Throw
|
|
| Delete
|
|
| In
|
|
| Try
|
|
|
|
| Class
|
|
| Enum
|
|
| Extends
|
|
| Super
|
|
| Const
|
|
| Export
|
|
| Import
|
|
| Implements
|
|
| Let
|
|
| Private
|
|
| Public
|
|
| Interface
|
|
| Package
|
|
| Protected
|
|
| Static
|
|
| Yield
|
|
;
|
|
|
|
eos
|
|
: SemiColon
|
|
| EOF
|
|
| {lineTerminatorAhead()}?
|
|
| {closeBrace()}?
|
|
; |