-
Notifications
You must be signed in to change notification settings - Fork 1
/
JavaScript_grammar.lark
150 lines (115 loc) · 5.27 KB
/
JavaScript_grammar.lark
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
140
141
142
143
144
145
146
147
148
149
150
# Grammar is composed of definitions (named literals and named terminals) and some directives (at the end)
?start: (statement|expression)+ // a program is composed of one or more statement
?statement: print_statement ";"?
| variable_statement ";"?
| variable_assignment ";"?
| logical_statement ";"?
| if_statement
| while_statement
| return_statement ";"?
| function_declaration
| function_call ";"?
| ternary_condition_statement
// Logical productions
?logical_statement : "(" logical_statement ")"
| logical_and
| logical_or
| logical_not
| relational # <, >, <=, >=, ==, !=, ===, !==
?relational: expression "<" expression -> less_than
| expression ">" expression -> greater_than
| expression "<=" expression -> less_than_or_equal
| expression ">=" expression -> greater_than_or_equal
| equality # equality with == in JS
| strict_equality # equality with === in JS
| "(" relational ")"
?logical_or: logical_statement "||" logical_statement
| "(" logical_or ")"
?logical_and: logical_statement "&&" logical_statement
| "(" logical_and ")"
logical_not: "!" logical_statement
| "!" "(" logical_not ")"
?equality: expression "==" expression
| expression "!=" expression -> inequality
?strict_equality: expression "===" expression
| expression "!==" expression -> strict_inequality
// Arithmetic productions
additive: expression "+" expression -> add
| expression "-" expression -> sub
multiplicative: term "*" term -> mul
| term "/" term -> div
negative: "-" expression
| "-" "(" negative ")"
?block: "{" statement* "}"
// input and output statements
input_statement: "prompt" "(" expression? ")" ";"? # prompt can take whatever input you give to it
| "prompt" "(" variable_assignment ")" ";"?
| "prompt" "(" logical_statement ")" ";"?
| "prompt" "(" function_call ")" ";"?
| "prompt" "(" ternary_condition_statement ")" ";"?
print_statement: "console" "." "log" "(" expression? ")" ";"?
| "console" "." "log" "(" variable_assignment ")" ";"?
| "console" "." "log" "(" logical_statement ")" ";"?
| "console" "." "log" "(" function_call ")" ";"?
| "console" "." "log" "(" ternary_condition_statement ")" ";"?
// the ! is required to not filter keywords like let/const/var
!variable_statement: ("let"|"var") IDENTIFIER ("=" (expression|function_call|logical_statement|ternary_condition_statement))?
|("let"|"var")? IDENTIFIER ("[" expression "]")? ("=" (expression|function_call|logical_statement|ternary_condition_statement)) // we can define but not assign a variable or vice versa
|"const" IDENTIFIER "=" (expression|function_call|logical_statement|ternary_condition_statement)
!variable_assignment: IDENTIFIER "+=" expression
| IDENTIFIER "-=" expression
| IDENTIFIER "*=" expression
| IDENTIFIER "/=" expression
| IDENTIFIER "++" // post-increment
| IDENTIFIER "--" // post-decrement
| "++" IDENTIFIER // pre-increment
| "--" IDENTIFIER // pre-decrement
?expression: "(" expression ")"
| additive
| term
| negative
term : multiplicative
| factor
| "(" additive ")"
| "(" term ")"
factor : BOOL
| STRING
| template_literal // because it is just a more complex string
| INT
| FLOAT
| IDENTIFIER
| IDENTIFIER "[" expression "]" -> array_access
| IDENTIFIER "." "length" -> array_length
| input_statement
| array
array: "[" [expression ("," expression)*] "]"
?template_literal: "`" (template_string_part | "${" (expression | logical_statement | ternary_condition_statement | function_call) "}")* "`"
?template_string_part: /[^$`]+/
!function_declaration : "function" IDENTIFIER "(" parameter_list? ")" block
?parameter_list: IDENTIFIER ("," IDENTIFIER)*
!function_call: IDENTIFIER "(" argument_list? ")"
?argument_list: expression ("," expression)*
!return_statement: "return" expression? ";"?
// branching production
?if_statement : "if" "(" (logical_statement | expression) ")" block ("else" (if_statement|block))? // this way we include all the possible cases, statement can be a block or another if statement
// iteration production
?while_statement : "while" "(" (logical_statement | expression) ")" block
// ternary condition statement
?ternary_condition_statement: (logical_statement | expression) "?" statement ":" statement
BOOL.1: "true" | "false"
STRING: "'" _STRING_INNER* "'"
| "\"" _STRING_INNER* "\""
// comment management (ignore them)
MULTILINE_COMMENT: "/*" (/./ | "\n")* "*/"
INLINE_COMMENT: "//" /[^\n]*/
NEWLINE: "\n"
%import common.ESCAPED_STRING
%import common._STRING_INNER
%import common.CNAME -> IDENTIFIER
%import common.INT
%import common.FLOAT
%import common.WS
%ignore WS
%ignore INLINE_COMMENT
%ignore NEWLINE
%ignore MULTILINE_COMMENT