forked from TarsCloud/TarsJava
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTafParser.g
139 lines (102 loc) · 3.5 KB
/
TafParser.g
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
parser grammar TarsParser;
options {
tokenVocab=TarsLexer;
output = AST;
}
tokens
{COLON;COMMA;COMMENT;DOT;
EQ;ESC_SEQ;GT;HEX_DIGIT;LBRACE;LBRACKET;LPAREN;LT;
OCTAL_ESC; QUOTE; RBRACE; RBRACKET; RPAREN; SEMI; TARS_BOOL;
TARS_BYTE; TARS_CONST; TARS_DOUBLE; TARS_ENUM; TARS_FALSE;TARS_FLOAT;
TARS_FLOATING_POINT_LITERAL; TARS_IDENTIFIER;TARS_INCLUDE;TARS_INT;
TARS_INTEGER_LITERAL;TARS_INTERFACE;TARS_KEY;TARS_LONG;TARS_MAP;
TARS_NAMESPACE;TARS_OPTIONAL;TARS_OUT;TARS_REQUIRE;TARS_ROUTE_KEY;
TARS_SHORT;TARS_STRING;TARS_STRING_LITERAL;TARS_STRUCT;TARS_TRUE;
TARS_UNSIGNED;TARS_VECTOR;TARS_VOID;UNICODE_ESC;WS;TARS_OPERATION;
TARS_PARAM;TARS_REF;TARS_ROOT;TARS_STRUCT_MEMBER;}
@header {package com.qq.tars.maven.parse;}
start
: include_def* namespace_def +
-> ^( TARS_ROOT (include_def )* ( namespace_def )+ ) ;
include_def
: TARS_INCLUDE TARS_STRING_LITERAL
-> ^( TARS_INCLUDE[$TARS_STRING_LITERAL.text] ) ;
namespace_def
: TARS_NAMESPACE TARS_IDENTIFIER LBRACE (definition SEMI)+ RBRACE
-> ^( TARS_NAMESPACE[$TARS_IDENTIFIER.text] ( definition )+ ) ;
definition
: const_def |
enum_def |
struct_def |
key_def |
interface_def
;
const_def
: TARS_CONST type_primitive TARS_IDENTIFIER EQ v= const_initializer
-> ^( TARS_CONST[$TARS_IDENTIFIER.text, $v.text] type_primitive ) ;
enum_def
: TARS_ENUM n=TARS_IDENTIFIER LBRACE m+=TARS_IDENTIFIER (COMMA m+=TARS_IDENTIFIER)* COMMA? RBRACE
-> ^( TARS_ENUM[$n.text] ( $m)+ ) |
TARS_ENUM n=TARS_IDENTIFIER LBRACE m+=TARS_IDENTIFIER EQ v+=TARS_INTEGER_LITERAL (COMMA m+=TARS_IDENTIFIER EQ v+=TARS_INTEGER_LITERAL)* COMMA? RBRACE
-> ^( TARS_ENUM[$n.text] ( $m)+ ( $v)+ );
struct_def
: TARS_STRUCT TARS_IDENTIFIER LBRACE (struct_member SEMI)+ RBRACE
-> ^( TARS_STRUCT[$TARS_IDENTIFIER.text] ( struct_member )+ ) ;
struct_member
: TARS_INTEGER_LITERAL (r= TARS_REQUIRE |r=TARS_OPTIONAL) type TARS_IDENTIFIER (EQ v= const_initializer )?
-> ^( TARS_STRUCT_MEMBER[$TARS_INTEGER_LITERAL.text, $r, $TARS_IDENTIFIER.text, $v.result] type ) ;
key_def
: TARS_KEY LBRACKET n=TARS_IDENTIFIER (COMMA k+= TARS_IDENTIFIER )+ RBRACKET
-> ^( TARS_KEY[$n.text] ( $k)+ ) ;
interface_def
: TARS_INTERFACE TARS_IDENTIFIER LBRACE (operation SEMI)+ RBRACE
-> ^( TARS_INTERFACE[$TARS_IDENTIFIER.text] ( operation )+ ) ;
operation
: type TARS_IDENTIFIER LPAREN (param (COMMA param )* )? RPAREN
-> ^( TARS_OPERATION[$TARS_IDENTIFIER.text] type ( param )* );
param
: TARS_ROUTE_KEY? TARS_OUT? type TARS_IDENTIFIER
-> ^( TARS_PARAM[$TARS_IDENTIFIER.text, $TARS_OUT, $TARS_ROUTE_KEY] type ) ;
const_initializer returns [String result]
: TARS_INTEGER_LITERAL |
TARS_FLOATING_POINT_LITERAL |
TARS_STRING_LITERAL |
TARS_FALSE |
TARS_TRUE;
type
: type_primitive |
type_vector |
type_map |
type_custom;
type_primitive
: TARS_VOID
-> ^( TARS_VOID ) |
TARS_BOOL
-> ^( TARS_BOOL ) |
TARS_BYTE
-> ^( TARS_BYTE ) |
TARS_SHORT
-> ^( TARS_SHORT ) |
TARS_INT
-> ^( TARS_INT ) |
TARS_LONG
-> ^( TARS_LONG ) |
TARS_FLOAT
-> ^( TARS_FLOAT ) |
TARS_DOUBLE
-> ^( TARS_DOUBLE ) |
TARS_STRING
-> ^( TARS_STRING ) |
TARS_UNSIGNED TARS_INT
-> ^( TARS_LONG );
type_vector
: TARS_VECTOR LT type GT
-> ^( TARS_VECTOR type ) ;
type_map
: TARS_MAP LT type COMMA type GT
-> ^( TARS_MAP type type ) ;
type_custom
: TARS_IDENTIFIER
-> ^( TARS_MAP[$TARS_IDENTIFIER.text] ) |
ns= TARS_IDENTIFIER COLON COLON id= TARS_IDENTIFIER
-> ^( TARS_REF[$ns.text,$id.text] ) ;