Skip to content

Commit e9496e2

Browse files
committed
add antlr readable syntax
1 parent 86d650b commit e9496e2

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

documentation/wenyan.g4

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
grammar wenyan;
2+
program : statement* ;
3+
statement : declare_statement
4+
| define_statement
5+
| print_statement
6+
| for_statement
7+
| function_statement
8+
| if_statement
9+
| return_statement
10+
| math_statement
11+
| assign_statement
12+
| import_statement
13+
| object_statement
14+
| reference_statement
15+
| array_statement
16+
| flush_statement
17+
| BREAK
18+
| comment;
19+
20+
reference_statement : '' data ('' (STRING_LITERAL|INT_NUM|'其餘'|IDENTIFIER|''))? name_single_statement? ;
21+
22+
23+
array_statement : array_cat_statement|array_push_statement ;
24+
array_cat_statement : '' (IDENTIFIER|'') (PREPOSITION_RIGHT IDENTIFIER)+ name_single_statement?;
25+
array_push_statement : '' (IDENTIFIER|'') (PREPOSITION_RIGHT data)+ name_single_statement?;
26+
27+
28+
function_statement : function_define_statement|(function_call_statement (name_single_statement)?) ;
29+
function_call_statement : function_pre_call|function_post_call ;
30+
function_pre_call : ('' IDENTIFIER (preposition data)*)|('施其' (preposition data)*) ;
31+
function_post_call : ('' INT_NUM '以施' IDENTIFIER)+ ;
32+
function_define_statement : '吾有' INT_NUM '' name_single_statement ('欲行是術' '必先得' (INT_NUM TYPE ('' IDENTIFIER)+)+)? ('是術曰'|'乃行是術曰') statement* '是謂' IDENTIFIER '之術也' ;
33+
34+
35+
if_statement : IF if_expression '' statement+ (ELSE statement+)? FOR_IF_END ;
36+
if_expression : unary_if_expression|binary_if_expression ;
37+
unary_if_expression : data|(IDENTIFIER ''(''|STRING_LITERAL|IDENTIFIER))|'' ;
38+
binary_if_expression : unary_if_expression IF_LOGIC_OP unary_if_expression ;
39+
40+
41+
declare_statement : ('吾有'|'今有') INT_NUM TYPE ('' data)*;
42+
define_statement : (declare_statement name_multi_statement)|init_define_statement ;
43+
44+
45+
name_multi_statement : '名之' ('' IDENTIFIER)+ ;
46+
name_single_statement : '名之' ('' IDENTIFIER) ;
47+
init_define_statement : '' TYPE data (name_single_statement)? ;
48+
49+
50+
for_statement : for_arr_statement
51+
| for_enum_statement
52+
| for_while_statement ;
53+
for_arr_statement : FOR_START_ARR IDENTIFIER FOR_MID_ARR IDENTIFIER statement* FOR_IF_END ;
54+
for_enum_statement : FOR_START_ENUM (INT_NUM|IDENTIFIER) FOR_MID_ENUM statement* FOR_IF_END ;
55+
for_while_statement : FOR_START_WHILE statement* FOR_IF_END ;
56+
57+
58+
math_statement : (arith_math_statement|boolean_algebra_statement|mod_math_statement) (name_multi_statement)? ;
59+
arith_math_statement : arith_binary_math|arith_unary_math ;
60+
arith_binary_math : ARITH_BINARY_OP (data|'') preposition (data|'') ;
61+
arith_unary_math : UNARY_OP (IDENTIFIER|'') ;
62+
mod_math_statement : '' (INT_NUM|FLOAT_NUM|IDENTIFIER|'') preposition (INT_NUM|FLOAT_NUM|IDENTIFIER) POST_MOD_MATH_OP? ;
63+
boolean_algebra_statement : '' IDENTIFIER IDENTIFIER LOGIC_BINARY_OP ;
64+
65+
66+
assign_statement : '昔之' IDENTIFIER ('' (INT_NUM|STRING_LITERAL|IDENTIFIER))? '' (('' ((data ('' INT_NUM)?)|'') '是矣')|'今不復存矣') ;
67+
68+
69+
return_statement : '乃得' (data|'')|'乃歸空無'|'乃得矣' ;
70+
71+
72+
import_statement : '吾嘗觀' STRING_LITERAL '之書' ('方悟' IDENTIFIER+ '之義')? ;
73+
74+
75+
object_statement : '吾有' INT_NUM '' name_multi_statement (object_define_statement)? ;
76+
object_define_statement : '其物如是' ('物之' STRING_LITERAL '' TYPE '' data)+ '是謂' IDENTIFIER '之物也' ;
77+
78+
79+
data : STRING_LITERAL|BOOL_VALUE|IDENTIFIER|INT_NUM|FLOAT_NUM ;
80+
81+
STRING_LITERAL : '「「' ( ~('') )* '」」' ;
82+
IDENTIFIER : '' ( ~('') )+ '' ;
83+
84+
ARITH_BINARY_OP : ''|''|'' ;
85+
LOGIC_BINARY_OP : '中有陽乎'|'中無陰乎' ;
86+
POST_MOD_MATH_OP : '所餘幾何' ;
87+
UNARY_OP : '' ;
88+
89+
preposition : PREPOSITION_LEFT|PREPOSITION_RIGHT ;
90+
PREPOSITION_LEFT : '' ;
91+
PREPOSITION_RIGHT : '' ;
92+
93+
IF : '' ;
94+
ELSE : '若非' ;
95+
IF_LOGIC_OP : '等於'|'不等於'|'不大於'|'不小於'|'大於'|'小於' ;
96+
97+
FOR_START_ARR : '' ;
98+
FOR_START_ENUM : '為是' ;
99+
FOR_START_WHILE : '恆為是' ;
100+
FOR_MID_ARR : '中之' ;
101+
FOR_MID_ENUM : '' ;
102+
FOR_IF_END : '云云'|'' ;
103+
104+
FLOAT_NUM : INT_NUM '' (INT_NUM FLOAT_NUM_KEYWORDS)+ ;
105+
FLOAT_NUM_KEYWORDS : ''|''|''|''|''|''|''|''|''|'' ;
106+
INT_NUM : INT_NUM_KEYWORDS+ ;
107+
108+
INT_NUM_KEYWORDS : ''|''|''|''|''|''|''|''|''|''|''|''|''|''|''|''|''|''|''|''|''|''|''|''|'' ;
109+
TYPE : ''|''|''|'' ;
110+
BOOL_VALUE : ''|'' ;
111+
print_statement : '書之' ;
112+
113+
WS : ([ \t\r\n]|''|'')+ -> skip ;
114+
comment : ('注曰'|'疏曰'|'批曰') STRING_LITERAL ;
115+
flush_statement : '' ;
116+
117+
BREAK : '乃止' ;

0 commit comments

Comments
 (0)