-
Notifications
You must be signed in to change notification settings - Fork 7
/
parser.yy
201 lines (170 loc) · 11.7 KB
/
parser.yy
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// SPDX-License-Identifier: Apache-2.0
// Copyright © 2021 Intel Corporation
%skeleton "lalr1.cc"
%require "3.2"
%debug
%defines
%define api.namespace { Frontend }
%define api.parser.class { Parser }
%define api.location.file "locations.hpp"
%code requires {
#include <memory>
#include "node.hpp"
namespace Frontend {
class Scanner;
}
}
%parse-param { Scanner & scanner }
%parse-param { std::unique_ptr<AST::CodeBlock> & block }
%locations
%initial-action {
@$.begin.filename = @$.end.filename = &scanner.filename;
}
%code {
#include <iostream>
#include <fstream>
#include <memory>
#include "node.hpp"
#include "scanner.hpp"
#undef yylex
#define yylex scanner.yylex
}
%define api.value.type variant
%define parse.assert
%token <std::string> IDENTIFIER TSTRING STRING FSTRING RELATIONAL
%token <int64_t> NUMBER
%token <bool> BOOL
%token <AST::AssignOp> ASSIGN
%token LBRACKET "["
%token RBRACKET "]"
%token LCURLY "{"
%token RCURLY "}"
%token LPAREN "("
%token RPAREN ")"
%token ADD "+"
%token SUB "-"
%token MUL "*"
%token DIV "/"
%token MOD "%"
%token COMMA ","
%token COLON ":"
%token DOT "."
%token QMARK "?"
%token IF ELIF ELSE ENDIF FOREACH ENDFOREACH
%token UMINUS
%token NOT
%token BREAK CONTINUE
%token NEWLINE "\n"
%token END 0
%nterm <AST::ExpressionV> literal expression
%nterm <AST::StatementV> statement
%nterm <std::unique_ptr<AST::IfStatement>> if_statement
%nterm <std::unique_ptr<AST::ForeachStatement>> foreach_statement
%nterm <AST::KeywordPair> keyword_item
%nterm <AST::KeywordList> keyword_arguments
%nterm <std::unique_ptr<AST::Arguments>> arguments
%nterm <AST::ExpressionList> positional_arguments
%nterm <std::unique_ptr<AST::CodeBlock>> program statements
%nterm <AST::ElseBlock> else_clause
%nterm <std::vector<AST::ElifBlock>> elif_clause in_elif_clause
%nterm <AST::IfBlock> if_clause
%right NOT
%left "-" "+"
%left "*" "/" "%"
%left "."
%left "(" ")" "[" "]"
%left RELATIONAL // XXX: is the priority of this off?
%left "\n"
%nonassoc "?" ":"
%nonassoc IF ELIF ELSE ENDIF
%right UMINUS // Negation
%%
program : %empty { block = std::make_unique<AST::CodeBlock>(); }
| statements { block = std::move($1); }
| statements "\n" { block = std::move($1); }
;
statements : statement { $$ = std::make_unique<AST::CodeBlock>(std::move($1)); }
| statements "\n" statement { $1->statements.push_back(std::move($3)); $$ = std::move($1); }
;
statement : expression { $$ = AST::StatementV(std::make_unique<AST::Statement>(std::move($1))); }
| expression ASSIGN expression { $$ = AST::StatementV(std::make_unique<AST::Assignment>(std::move($1), $2, std::move($3))); }
| if_statement { $$ = AST::StatementV(std::move($1)); }
| foreach_statement { $$ = AST::StatementV(std::move($1)); }
| BREAK { $$ = AST::StatementV(std::make_unique<AST::Break>()); }
| CONTINUE { $$ = AST::StatementV(std::make_unique<AST::Continue>()); }
;
foreach_statement : FOREACH IDENTIFIER ":" expression "\n" statements "\n" ENDFOREACH {
$$ = std::make_unique<AST::ForeachStatement>(
AST::Identifier{$2, @2},
std::move($4), std::move($6));
}
| FOREACH IDENTIFIER "," IDENTIFIER ":" expression "\n" statements "\n" ENDFOREACH {
$$ = std::make_unique<AST::ForeachStatement>(
AST::Identifier{$2, @4},
AST::Identifier{$4, @4},
std::move($6), std::move($8));
}
;
if_statement : if_clause ENDIF { $$ = std::make_unique<AST::IfStatement>(std::move($1)); }
| if_clause else_clause ENDIF { $$ = std::make_unique<AST::IfStatement>(std::move($1), std::move($2)); }
| if_clause elif_clause ENDIF { $$ = std::make_unique<AST::IfStatement>(std::move($1), std::move($2)); }
| if_clause elif_clause else_clause ENDIF { $$ = std::make_unique<AST::IfStatement>(std::move($1), std::move($2), std::move($3)); }
;
if_clause : IF expression "\n" statements "\n" { $$ = AST::IfBlock(std::move($2), std::move($4)); }
;
in_elif_clause : ELIF expression "\n" statements "\n" { $$ = std::vector<AST::ElifBlock>{}; $$.emplace_back(AST::ElifBlock(std::move($2), std::move($4))); }
;
elif_clause : in_elif_clause { $$ = std::move($1); }
| elif_clause in_elif_clause { $$ = std::move($1); std::move($2.begin(), $2.end(), std::back_inserter($$)); }
;
else_clause : ELSE "\n" statements "\n" { $$ = AST::ElseBlock(std::move($3)); }
;
expression : expression "+" expression { $$ = AST::ExpressionV(std::make_unique<AST::AdditiveExpression>(std::move($1), AST::AddOp::ADD, std::move($3), @$)); }
| expression "-" expression { $$ = AST::ExpressionV(std::make_unique<AST::AdditiveExpression>(std::move($1), AST::AddOp::SUB, std::move($3), @$)); }
| expression "*" expression { $$ = AST::ExpressionV(std::make_unique<AST::MultiplicativeExpression>(std::move($1), AST::MulOp::MUL, std::move($3), @$)); }
| expression "/" expression { $$ = AST::ExpressionV(std::make_unique<AST::MultiplicativeExpression>(std::move($1), AST::MulOp::DIV, std::move($3), @$)); }
| expression "%" expression { $$ = AST::ExpressionV(std::make_unique<AST::MultiplicativeExpression>(std::move($1), AST::MulOp::MOD, std::move($3), @$)); }
| expression "[" expression "]" { $$ = AST::ExpressionV(std::make_unique<AST::Subscript>(std::move($1), std::move($3), @$)); }
| expression RELATIONAL expression { $$ = AST::ExpressionV(std::make_unique<AST::Relational>(std::move($1), $2, std::move($3), @$)); } // XXX: this might now actually be safe, since x < y < z isnt valid.
| "(" expression ")" { $$ = std::move($2); } // XXX: Do we need a subexpression type?
| "-" expression %prec UMINUS { $$ = AST::ExpressionV(std::make_unique<AST::UnaryExpression>(AST::UnaryOp::NEG, std::move($2), @$)); }
| NOT expression { $$ = AST::ExpressionV(std::make_unique<AST::UnaryExpression>(AST::UnaryOp::NOT, std::move($2), @$)); }
| expression "." expression { $$ = AST::ExpressionV(std::make_unique<AST::GetAttribute>(std::move($1), std::move($3), @$)); }
| expression "(" arguments ")" { $$ = AST::ExpressionV(std::make_unique<AST::FunctionCall>(std::move($1), std::move($3), @$)); }
| expression "?" expression ":" expression { $$ = AST::ExpressionV(std::make_unique<AST::Ternary>(std::move($1), std::move($3), std::move($5), @$)); }
| "[" positional_arguments "]" { $$ = AST::ExpressionV(std::make_unique<AST::Array>(std::move($2), @$)); }
| "[" positional_arguments "," "]" { $$ = AST::ExpressionV(std::make_unique<AST::Array>(std::move($2), @$)); }
| "[" "]" { $$ = AST::ExpressionV(std::make_unique<AST::Array>(@$)); }
| "{" keyword_arguments "}" { $$ = AST::ExpressionV(std::make_unique<AST::Dict>(std::move($2), @$)); }
| "{" keyword_arguments "," "}" { $$ = AST::ExpressionV(std::make_unique<AST::Dict>(std::move($2), @$)); }
| "{" "}" { $$ = AST::ExpressionV(std::make_unique<AST::Dict>(@$)); }
| literal { $$ = std::move($1); }
;
arguments : %empty { $$ = std::make_unique<AST::Arguments>(@$); }
| positional_arguments { $$ = std::make_unique<AST::Arguments>(std::move($1), @$); }
| positional_arguments "," { $$ = std::make_unique<AST::Arguments>(std::move($1), @$); }
| keyword_arguments { $$ = std::make_unique<AST::Arguments>(std::move($1), @$); }
| keyword_arguments "," { $$ = std::make_unique<AST::Arguments>(std::move($1), @$); }
| positional_arguments "," keyword_arguments { $$ = std::make_unique<AST::Arguments>(std::move($1), std::move($3), @$); }
| positional_arguments "," keyword_arguments "," { $$ = std::make_unique<AST::Arguments>(std::move($1), std::move($3), @$); }
;
positional_arguments : expression { $$ = AST::ExpressionList(); $$.emplace_back(std::move($1)); }
| positional_arguments "," expression { $1.emplace_back(std::move($3)); $$ = std::move($1); }
;
keyword_arguments : keyword_item { $$ = AST::KeywordList(); $$.emplace_back(std::move($1)); }
| keyword_arguments "," keyword_item { $1.emplace_back(std::move($3)); $$ = std::move($1); }
;
keyword_item : expression ":" expression { $$ = AST::KeywordPair(std::move($1), std::move($3)); }
;
literal : NUMBER { $$ = AST::ExpressionV(std::make_unique<AST::Number>($1, @$)); }
| STRING { $$ = AST::ExpressionV(std::make_unique<AST::String>($1.substr(1, $1.size() - 2), false, false, @$)); }
| FSTRING { $$ = AST::ExpressionV(std::make_unique<AST::String>($1.substr(1, $1.size() - 2), false, true, @$)); }
| TSTRING { $$ = AST::ExpressionV(std::make_unique<AST::String>($1.substr(3, $1.size() - 6), true, false, @$)); }
| BOOL { $$ = AST::ExpressionV(std::make_unique<AST::Boolean>($1, @$)); }
| IDENTIFIER { $$ = AST::ExpressionV(std::make_unique<AST::Identifier>($1, @$)); }
;
%%
void Frontend::Parser::error(const location_type & loc, const std::string & err_message)
{
std::cerr << "Error: " << err_message << " at " << loc << std::endl;
}