grammar SimpleAst;
/*
options {
tokenVocab=SimpleAstLexer;
}
*/
compilationUnit
: arrClassDeclaration
;
/**************************************************************************
Class Declaration
**************************************************************************/
arrClassDeclaration
: '[' ']'
| '[' objClassDeclaration (',' objClassDeclaration)* ','? ']'
| objClassDeclaration
;
objClassDeclaration
: '{' pairClassDeclaration '}'
;
pairClassDeclaration
: AST_LEXER_CLASS_DECLARATION ':' '{' '}'
| AST_LEXER_CLASS_DECLARATION ':' '{' valueClassDeclaration '}'
;
valueClassDeclaration
: recordClassDeclaration (',' recordClassDeclaration)*
;
recordClassDeclaration
: pairComm
| pairClassName
| pairClassBody
;
/**************************************************************************
Class Body
**************************************************************************/
arrClassBody
: '[' objClassBody (',' objClassBody)* ','? ']'
;
objClassBody
: '{' pairClassBody '}'
;
pairClassBody
: AST_LEXER_CLASS_BODY ':' '{' '}'
| AST_LEXER_CLASS_BODY ':' '{' valueClassBody '}'
;
valueClassBody
: reocrdClassBody (',' reocrdClassBody)*
;
reocrdClassBody
: arrClassMember
;
/**************************************************************************
Class Memeber
**************************************************************************/
arrClassMember
: '[' ']'
| '[' objClassMember (',' objClassMember)* ','? ']'
;
objClassMember
: '{' baseClassMember '}'
;
baseClassMember
: pairFieldDeclaration
;
/**************************************************************************
Field Declaration
**************************************************************************/
arrFieldDeclaration
: '[' objFieldDeclaration (',' objFieldDeclaration)* ','? ']'
;
objFieldDeclaration
: '{' pairFieldDeclaration '}'
;
pairFieldDeclaration
: AST_LEXER_FIELD_DECLARATION ':' '{' valueFieldDeclaration '}'
;
valueFieldDeclaration
: reocrdFieldDeclaration (',' reocrdFieldDeclaration)*
;
reocrdFieldDeclaration
: pairComm
| pairFieldType
| pairFieldName
| pairFieldInitializer
;
/**************************************************************************
Field Type
**************************************************************************/
arrFieldType
: '[' objFieldType (',' objFieldType)* ','? ']'
;
objFieldType
: '{' pairFieldType '}'
;
pairFieldType
: AST_LEXER_FIELD_TYPE ':' '{' valueFieldType '}'
;
valueFieldType
: reocrdFieldType (',' reocrdFieldType)*
;
reocrdFieldType
: pairPrimitiveType
| pairClassType
;
/**************************************************************************
Primitive Type
**************************************************************************/
arrClassType
: '[' objClassType (',' objClassType)* ','? ']'
;
objClassType
: '{' pairClassType '}'
;
pairClassType
: pairStringType
| pairCustomType
;
/**************************************************************************
Custom Type
**************************************************************************/
arrCustomType
: '[' objCustomType (',' objCustomType)* ','? ']'
;
objCustomType
: '{' pairCustomType '}'
;
pairCustomType
: AST_LEXER_CUSTOM_TYPE ':' '{' valueCustomType '}'
;
valueCustomType
: reocrdCustomType (',' reocrdCustomType)*
;
reocrdCustomType
: pairIdentifier
| pairCustomType
;
/**************************************************************************
String Type
**************************************************************************/
arrStringType
: '[' objStringType (',' objStringType)* ','? ']'
;
objStringType
: '{' pairStringType '}'
;
pairStringType
: AST_LEXER_STRING_TYPE ':' '{' valueStringType '}'
;
valueStringType
: reocrdStringType (',' reocrdStringType)*
;
reocrdStringType
: pairIdentifier
;
/**************************************************************************
Primitive Type
**************************************************************************/
arrPrimitiveType
: '[' objPrimitiveType (',' objPrimitiveType)* ','? ']'
;
objPrimitiveType
: '{' pairPrimitiveType '}'
;
pairPrimitiveType
: pairPrimitiveIntType
| pairPrimitiveLongType
| arrPrimitiveDoubleType
| arrPrimitiveDecimalType
;
/**************************************************************************
Primitive Decimal Type
**************************************************************************/
arrPrimitiveDecimalType
: '[' objPrimitiveDecimalType (',' objPrimitiveDecimalType)* ','? ']'
;
objPrimitiveDecimalType
: '{' pairPrimitiveDecimalType '}'
;
pairPrimitiveDecimalType
: AST_LEXER_PRIMITIVE_DECIMAL_TYPE ':' '{' valuePrimitiveDecimalType '}'
;
valuePrimitiveDecimalType
: reocrdPrimitiveDecimalType (',' reocrdPrimitiveDecimalType)*
;
reocrdPrimitiveDecimalType
: pairIdentifier
;
/**************************************************************************
Primitive Double Type
**************************************************************************/
arrPrimitiveDoubleType
: '[' objPrimitiveDoubleType (',' objPrimitiveDoubleType)* ','? ']'
;
objPrimitiveDoubleType
: '{' pairPrimitiveDoubleType '}'
;
pairPrimitiveDoubleType
: AST_LEXER_PRIMITIVE_DOUBLE_TYPE ':' '{' valuePrimitiveDoubleType '}'
;
valuePrimitiveDoubleType
: reocrdPrimitiveDoubleType (',' reocrdPrimitiveDoubleType)*
;
reocrdPrimitiveDoubleType
: pairIdentifier
;
/**************************************************************************
Primitive Int Type
**************************************************************************/
arrPrimitiveIntType
: '[' objPrimitiveIntType (',' objPrimitiveIntType)* ','? ']'
;
objPrimitiveIntType
: '{' pairPrimitiveIntType '}'
;
pairPrimitiveIntType
: AST_LEXER_PRIMITIVE_INT_TYPE ':' '{' valuePrimitiveIntType '}'
;
valuePrimitiveIntType
: reocrdPrimitiveIntType (',' reocrdPrimitiveIntType)*
;
reocrdPrimitiveIntType
: pairIdentifier
;
/**************************************************************************
Primitive Long Type
**************************************************************************/
arrPrimitiveLongType
: '[' objPrimitiveLongType (',' objPrimitiveLongType)* ','? ']'
;
objPrimitiveLongType
: '{' pairPrimitiveLongType '}'
;
pairPrimitiveLongType
: AST_LEXER_PRIMITIVE_LONG_TYPE ':' '{' valuePrimitiveLongType '}'
;
valuePrimitiveLongType
: reocrdPrimitiveLongType (',' reocrdPrimitiveLongType)*
;
reocrdPrimitiveLongType
: pairIdentifier
;
/**************************************************************************
Field Name
**************************************************************************/
arrFieldName
: '[' objFieldName (',' objFieldName)* ','? ']'
;
objFieldName
: '{' pairFieldName '}'
;
pairFieldName
: AST_LEXER_CLASS_NAME ':' '{' valueFieldName '}'
;
valueFieldName
: reocrdFieldName (',' reocrdFieldName)*
;
reocrdFieldName
: pairIdentifier
;
/**************************************************************************
Field Initializer
**************************************************************************/
arrFieldInitializer
: '[' objFieldInitializer (',' objFieldInitializer)* ','? ']'
;
objFieldInitializer
: '{' pairFieldInitializer '}'
;
pairFieldInitializer
: AST_LEXER_FIELD_INITIALIZER ':' '{' valueFieldInitializer '}'
;
valueFieldInitializer
: reocrdFieldInitializer (',' reocrdFieldInitializer)*
;
reocrdFieldInitializer
: pairIdentifier /* TODO */
;
/**************************************************************************
Class Name
**************************************************************************/
arrClassName
: '[' objClassName (',' objClassName)* ','? ']'
;
objClassName
: '{' pairClassName '}'
;
pairClassName
: AST_LEXER_CLASS_NAME ':' '{' valueClassName '}'
;
valueClassName
: reocrdClassName (',' reocrdClassName)*
;
reocrdClassName
: pairIdentifier
;
/**************************************************************************
COMMON
**************************************************************************/
arrIdentifier
: '[' objIdentifier (',' objIdentifier)* ','? ']'
;
objIdentifier
: '{' pairIdentifier '}'
;
pairIdentifier
: AST_LEXER_IDENTIFIER ':' '{' valueIdentifier '}'
;
valueIdentifier
: reocrdIdentifier (',' reocrdIdentifier)*
;
reocrdIdentifier
: AST_LEXER_LOGIC ':' STRING
| AST_LEXER_PHYSICAL ':' STRING
;
arrComm
: '[' objComm (',' objComm)* ','? ']'
;
objComm
: '{' pairIdentifier '}'
;
pairComm
: AST_LEXER_COMM ':' '{' '}'
;
valueComm
: recordComm (',' recordComm)*
;
recordComm
: pairUid
| pairCreateDate
| pairModifyDate
;
pairUid
: AST_LEXER_UID ':' STRING
;
pairCreateDate
: AST_LEXER_CREATE_DATE ':' STRING
;
pairModifyDate
: AST_LEXER_MODIFY_DATE ':' STRING
;
/**************************************************************************
Simple Ast LEXER
**************************************************************************/
AST_LEXER_CUSTOM_TYPE
: '"' 'AstCustomType' '"'
;
AST_LEXER_STRING_TYPE
: '"' 'AstStringType' '"'
;
AST_LEXER_PRIMITIVE_DECIMAL_TYPE
: '"' 'AstPrimitiveDecimalType' '"'
;
AST_LEXER_PRIMITIVE_DOUBLE_TYPE
: '"' 'AstPrimitiveDoubleType' '"'
;
AST_LEXER_PRIMITIVE_LONG_TYPE
: '"' 'AstPrimitiveLongType' '"'
;
AST_LEXER_PRIMITIVE_INT_TYPE
: '"' 'AstPrimitiveIntType' '"'
;
AST_LEXER_FIELD_INITIALIZER
: '"' 'AstFieldInitializer' '"'
;
AST_LEXER_FIELD_NAME
: '"' 'AstFieldName' '"'
;
AST_LEXER_FIELD_TYPE
: '"' 'AstFieldType' '"'
;
AST_LEXER_FIELD_DECLARATION
: '"' 'AstFieldDeclaration' '"'
;
AST_LEXER_CLASS_BODY
: '"' 'AstClassBody' '"'
;
AST_LEXER_LOGIC
: '"' 'AstLogic' '"'
;
AST_LEXER_PHYSICAL
: '"' 'AstPhysical' '"'
;
AST_LEXER_IDENTIFIER
: '"' 'AstIdentifier' '"'
;
AST_LEXER_CLASS_NAME
: '"' 'AstClassName' '"'
;
AST_LEXER_CLASS_DECLARATION
: '"' 'AstClassDeclaration' '"'
;
AST_LEXER_COMM
: '"' 'AstComm' '"'
;
AST_LEXER_UID
: '"' 'AstUid' '"'
;
AST_LEXER_CREATE_DATE
: '"' 'AstCreateDate' '"'
;
AST_LEXER_MODIFY_DATE
: '"' 'AstModifyDate' '"'
;
/**************************************************************************
JSON COMMON LEXER
**************************************************************************/
SINGLE_LINE_COMMENT
: '//' .*? (NEWLINE | EOF) -> skip
;
MULTI_LINE_COMMENT
: '/*' .*? '*/' -> skip
;
LITERAL
: 'true'
| 'false'
| 'null'
;
STRING
: '"' DOUBLE_QUOTE_CHAR* '"'
| '\'' SINGLE_QUOTE_CHAR* '\''
;
fragment DOUBLE_QUOTE_CHAR
: ~["\\\r\n]
| ESCAPE_SEQUENCE
;
fragment SINGLE_QUOTE_CHAR
: ~['\\\r\n]
| ESCAPE_SEQUENCE
;
fragment ESCAPE_SEQUENCE
: '\\'
( NEWLINE
| UNICODE_SEQUENCE // \u1234
| ['"\\/bfnrtv] // single escape char
| ~['"\\bfnrtv0-9xu\r\n] // non escape char
| '0' // \0
| 'x' HEX HEX // \x3a
)
;
IDENTIFIER
: IDENTIFIER_START IDENTIFIER_PART*
;
fragment IDENTIFIER_START
: [\p{L}]
| '$'
| '_'
| '\\' UNICODE_SEQUENCE
;
fragment IDENTIFIER_PART
: IDENTIFIER_START
| [\p{M}]
| [\p{N}]
| [\p{Pc}]
| '\u200C'
| '\u200D'
;
fragment UNICODE_SEQUENCE
: 'u' HEX HEX HEX HEX
;
NUMBER
: SYMBOL
( INT ('.' [0-9]*)? EXP? // +1.e2, 1234, 1234.5
| '.' [0-9]+ EXP? // -.2e3
| '0' [xX] HEX+ // 0x12345678
| 'Infinity'
| 'NaN'
)
;
fragment SYMBOL
: ('+' | '-')?
;
fragment HEX
: [0-9a-fA-F]
;
fragment INT
: '0' | [1-9] [0-9]*
;
fragment EXP
: [Ee] SYMBOL INT
;
fragment NEWLINE
: '\r\n'
| [\r\n\u2028\u2029]
;
WS
: [ \t\n\r] + -> skip
;
Simple Ast
最新推荐文章于 2021-07-02 15:33:47 发布