-
Notifications
You must be signed in to change notification settings - Fork 0
/
MonkeyParser.g4
126 lines (94 loc) · 10.4 KB
/
MonkeyParser.g4
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
//-------------------------------------------------------------------------------------------------------------------
// Parser
parser grammar MonkeyParser;
options {
tokenVocab = MonkeyScanner;
}
//-------------------------------------------------------------------------------------------------------------------
// Grammar for Monkey Language
//-------------------------------------------------------------------------------------------------------------------
program : statement* #programAST;
//-------------------------------------------------------------------------------------------------------------------
statement : LET letStatement #statement_LetAST
| RETURN returnStatement #statement_returnAST
| expressionStatement #callExpressionStatementAST;
//-------------------------------------------------------------------------------------------------------------------
letStatement : IDENT ASSIGN expression (PYCOMMA | ) #letStatementAST;
//-------------------------------------------------------------------------------------------------------------------
returnStatement : expression (PYCOMMA | ) #returnStatementAST;
//-------------------------------------------------------------------------------------------------------------------
expressionStatement : expression (PYCOMMA | ) #expressionStatementAST;
//-------------------------------------------------------------------------------------------------------------------
expression : additionExpression comparison #expressionAST;
//-------------------------------------------------------------------------------------------------------------------
comparison : ((LT|GT|LE|GE|EQUAL) additionExpression)* #comparisonAST;
//-------------------------------------------------------------------------------------------------------------------
additionExpression : multiplicationExpression additionFactor #additionExpressionAST;
//-------------------------------------------------------------------------------------------------------------------
additionFactor : ((ADD|SUB) multiplicationExpression)* #additionFactorAST;
//-------------------------------------------------------------------------------------------------------------------
multiplicationExpression : elementExpression multiplicationFactor #multiplicationExpressionAST;
//-------------------------------------------------------------------------------------------------------------------
multiplicationFactor : ((MUL|DIV) elementExpression)* #multiplicationFactorAST;
//-------------------------------------------------------------------------------------------------------------------
elementExpression : primitiveExpression (elementAccess | callExpression | ) #elementExpressionAST;
//-------------------------------------------------------------------------------------------------------------------
//[...]
elementAccess : L_BRACK expression R_BRACK #elementAccessAST;
//-------------------------------------------------------------------------------------------------------------------
//(...)
callExpression : L_PAREN expressionList R_PAREN #callExpressionAST;
//-------------------------------------------------------------------------------------------------------------------
primitiveExpression : NUMBER #primitiveExpression_numberAST
| STRING #primitiveExpression_stringAST
| IDENT #primitiveExpression_identAST
| TRUE #primitiveExpression_trueAST
| FALSE #primitiveExpression_falseAST
| L_PAREN expression R_PAREN #primitiveExpression_expressionAST
| arrayLiteral #primitiveExpression_literalAST
| arrayFunctions L_PAREN expressionList R_PAREN #primitiveExpression_ArrayFunctionsAST
| functionLiteral #primitiveExpression_FunctionLiteral_AST
| hashLiteral #primitiveExpression_HashLiteral_AST
| printExpression #primitiveExpression_PrintExpressionAST
| ifExpression #primitiveExpression_IfExpression;
//-------------------------------------------------------------------------------------------------------------------
arrayFunctions : LEN #arrayFunctions_lenAST
| FIRST #arrayFunctions_firstAST
| LAST #arrayFunctions_lastAST
| REST #arrayFunctions_restAST
| PUSH #arrayFunctions_pushAST;
//-------------------------------------------------------------------------------------------------------------------
//[...]
arrayLiteral : L_BRACK expressionList R_BRACK #arrayLiteralAST;
//-------------------------------------------------------------------------------------------------------------------
//fn (...){...}
functionLiteral : FN L_PAREN functionParameters R_PAREN blockStatement #functionLiteralAST;
//-------------------------------------------------------------------------------------------------------------------
//
functionParameters : IDENT (COMMA IDENT)* /*moreIdentifiers */ #functionParametersAST;
//-------------------------------------------------------------------------------------------------------------------
//moreIdentifiers : (COMMA IDENT)* #moreIdentifiersAST;
//-------------------------------------------------------------------------------------------------------------------
//{...}
hashLiteral : L_BRACE hashContent (COMMA hashContent)* /*moreHashContent*/ R_BRACE #hashLiteralAST;
//-------------------------------------------------------------------------------------------------------------------
//
hashContent : expression COLON expression #hashContentAST;
//-------------------------------------------------------------------------------------------------------------------
//moreHashContent : (COMMA hashContent)* #moreHashContentAST;
//-------------------------------------------------------------------------------------------------------------------
expressionList : expression (COMMA expression)* /*moreExpressions */ #expressionList_expressionAST
| #expressionList_nullAST
;
//-------------------------------------------------------------------------------------------------------------------
//moreExpressions : (COMMA expression)* #moreExpressionsAST;
//-------------------------------------------------------------------------------------------------------------------
//puts ( ... )
printExpression : PUTS L_PAREN expression R_PAREN #printExpressionAST;
//-------------------------------------------------------------------------------------------------------------------
//if
ifExpression : IF expression blockStatement (ELSE blockStatement| ) #ifExpressionAST;
//-------------------------------------------------------------------------------------------------------------------
//{ ... }
blockStatement : L_BRACE statement* R_BRACE #blockStatementAST;
//-------------------------------------------------------------------------------------------------------------------