aboutsummaryrefslogtreecommitdiff
path: root/src/parser.y
blob: d053cceaff5b5580093901e7e30e490cad588726 (plain) (blame)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
%{

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

#include <posthaste/parser.h>
#include <posthaste/date.h>
#include <posthaste/ast.h>

%}

%locations

%define parse.trace
%define parse.error verbose
%define api.pure full
%define lr.type ielr

%lex-param {void *scanner} {struct parser *parser}
%parse-param {void *scanner} {struct parser* parser}

%union {
	struct ast *node;
	ph_date_t num;
	int64_t snum;
	char *str;
};

%token <str> STRING
%token <num> DATE_LITERAL
%token <snum> INT_LITERAL
%token <str> IDENT;
%token <str> FUNC_IDENT;
%token <str> PROC_IDENT;

%token LPAREN "("
%token RPAREN ")"
%token LSQUARE "["
%token RSQUARE "]"
%token LCURLY "{"
%token RCURLY "}"

%token APOSTROPHE "'"
%token AMPERSAND "&"
%token COMMA ","
%token DOT "."
%token EQ "="
%token LT "<"
%token PLUS "+"
%token MINUS "-"
%token MULT "*"
%token DIV "/"

%token VAR "var"
%token IS "is"
%token UNLESS "unless"
%token OTHERWISE "otherwise"
%token UNTIL "until"
%token DO "do"
%token DONE "done"
%token PROCEDURE "procedure"
%token FUNCTION "function"
%token RETURN "return"
%token PRINT "print"
%token END "end"

%nterm <node> program
%nterm <node> definition
%nterm <node> definitions
%nterm <node> opt_definitions
%nterm <node> variable_definition
%nterm <node> variable_definitions
%nterm <node> opt_variable_definitions
%nterm <node> procedure_definition
%nterm <node> function_definition
%nterm <node> formal_arg
%nterm <node> formals
%nterm <node> lvalue
%nterm <node> rvalue
%nterm <node> print_item
%nterm <node> print_items
%nterm <node> opt_formals
%nterm <node> statement
%nterm <node> statement_list
%nterm <node> opt_arguments
%nterm <node> arguments
%nterm <node> print_statement
%nterm <node> unless_statement
%nterm <node> unless_expression
%nterm <node> until_statement
%nterm <node> assignment
%nterm <node> procedure_call
%nterm <node> function_call
%nterm <node> simple_expr
%nterm <node> atom
%nterm <node> term
%nterm <node> factor
%nterm <node> expression
%nterm <node> ident

/* special case */
%nterm <str> return
%nterm <str> opt_return

%{

/** Modifies the signature of yylex to fit our parser better. */
#define YY_DECL int yylex(YYSTYPE *yylval, YYLTYPE *yylloc, \
	                  void *yyscanner, struct parser *parser)

/**
 * Declare yylex.
 *
 * @param yylval Bison current value.
 * @param yylloc Bison location info.
 * @param yyscanner Flex scanner.
 * @param parser Current parser state.
 * @return \c 0 when succesful, \c 1 otherwise.
 * More info on yylex() can be found in the flex manual.
 */
YY_DECL;

/**
 * Convert bison location info to our own source location info.
 *
 * @param yylloc Bison location info.
 * @return Internal location info.
 */
static struct src_loc src_loc(YYLTYPE yylloc);

/**
 * Print parsing error.
 * Automatically called by bison.
 *
 * @param yylloc Location of error.
 * @param lexer Lexer.
 * @param parser Parser state.
 * @param msg Message to print.
 */
static void yyerror(YYLTYPE *yylloc, void *lexer,
		struct parser *parser, const char *msg);

%}

%start program;
%%

statement_list
	: statement "," statement_list { $$ = $1; $$->n = $3; }
	| statement

definitions
	: definition definitions { $$ = $1; $$->n = $2; }
	| definition

definition
	: function_definition
	| procedure_definition
	| variable_definition

variable_definition
	: "var" IDENT "=" expression {
		$$ = gen_var_def($[IDENT], $[expression], src_loc(@$));
	}

variable_definitions
	: variable_definition variable_definitions { $$ = $1; $$->n = $2; }
	| variable_definition

opt_variable_definitions
	: variable_definitions
	| { $$ = NULL; }

return
	: "return" IDENT { $$ = $2; }

opt_return
	: return
	| { $$ = NULL; }

function_definition
	: "function" FUNC_IDENT "{" opt_formals "}"
	return opt_variable_definitions "is" rvalue "end" "function" {
		struct ast *ret = gen_return($[rvalue], $[rvalue]->loc);
		$$ = gen_func_def($[FUNC_IDENT],
				  $[return],
				  $[opt_formals],
				  $[opt_variable_definitions],
				  ret,
				  src_loc(@$));
	}

procedure_definition
	: "procedure" PROC_IDENT "{" opt_formals "}"
	opt_return opt_variable_definitions "is" statement_list
	"end" "procedure" {
		$$ = gen_proc_def($[PROC_IDENT],
				$[opt_return],
				$[opt_formals],
				$[opt_variable_definitions],
				$[statement_list],
				src_loc(@$));
	}

formals
	: formal_arg "," formals { $$ = $1; $$->n = $3; }
	| formal_arg

opt_formals
	: formals
	| { $$ = NULL; }

formal_arg
	: IDENT "[" IDENT "]" {
		$$ = gen_formal_def($1, $3, src_loc(@$));
	}

procedure_call
	: PROC_IDENT "(" opt_arguments ")" {
		$$ = gen_proc_call($[PROC_IDENT], $[opt_arguments], src_loc(@$));
	}

arguments
	: expression "," arguments { $$ = $1; $$->n = $3; }
	| expression

opt_arguments
	: arguments
	| { $$ = NULL; }

assignment
	: lvalue "=" rvalue {
		$$ = gen_assign($[lvalue], $[rvalue], src_loc(@$));
	}

ident
	: IDENT {
		$$ = gen_id($[IDENT], src_loc(@$));
	}

lvalue
	: ident
	| ident "." IDENT { $$ = gen_dot($1, $3, src_loc(@$)); }

rvalue
	: expression
	| unless_expression

print_statement
	: "print" print_items { $$ = gen_print($[print_items], src_loc(@$)); }

print_items
	: print_item "&" print_items { $$ = $1; $$->n = $3; }
	| print_item

print_item
	: STRING { $$ = gen_const_string($[STRING], src_loc(@$)); }
	| expression

statement
	: procedure_call
	| assignment
	| print_statement
	| until_statement
	| unless_statement
	| "return" expression { $$ = gen_return($[expression], src_loc(@$)); }

until_statement
	: "do" statement_list "until" expression {
		$$ = gen_until($[statement_list], $[expression], src_loc(@$));
	}

unless_statement
	: "do" statement_list "unless" expression "done" {
		$$ = gen_unless($[statement_list], $[expression], NULL, src_loc(@$));
	}
	| "do" statement_list "unless" expression "otherwise" statement_list "done" {
		$$ = gen_unless($2, $[expression], $6, src_loc(@$));
	}

expression
	: simple_expr
	| expression "=" simple_expr {
		$$ = gen_eq($1, $[simple_expr], src_loc(@$));
	}
	| expression "<" simple_expr {
		$$ = gen_lt($1, $[simple_expr], src_loc(@$));
	}

simple_expr
	: term
	| simple_expr "+" term {
		$$ = gen_add($1, $[term], src_loc(@$));
	}
	| simple_expr "-" term {
		$$ = gen_sub($1, $[term], src_loc(@$));
	}

term
	: factor
	| term "*" factor {
		$$ = gen_mul($1, $[factor], src_loc(@$));
	}
	| term "/" factor {
		$$ = gen_div($1, $[factor], src_loc(@$));
	}

factor
	: "+" atom {
		$$ = gen_pos($[atom], src_loc(@$));
	}
	| "-" atom {
		$$ = gen_neg($[atom], src_loc(@$));
	}
	| atom

atom
	: ident
	| ident "'" IDENT {
		$$ = gen_attr($[ident], $[IDENT], src_loc(@$));
	}
	| INT_LITERAL {
		$$ = gen_const_int($[INT_LITERAL], src_loc(@$));
	}
	| DATE_LITERAL {
		$$ = gen_const_date($[DATE_LITERAL], src_loc(@$));
	}
	| function_call
	| procedure_call
	| "(" expression ")" { $$ = $2; }

function_call
	: FUNC_IDENT "(" opt_arguments ")" {
		$$ = gen_func_call($[FUNC_IDENT], $[opt_arguments], src_loc(@$));
	}

unless_expression
	: "do" expression "unless" expression "otherwise" expression "done" {
		$$ = gen_unless_expr($2, $4, $6, src_loc(@$));
	}

opt_definitions
	: definitions
	| { $$ = NULL; }

program
	: opt_definitions statement_list {
		if ($1) {
			ast_last($1)->n = $2;
			$$ = $1;
		}
		else {
			$$ = $2;
		}

		parser->tree = $$;
	}
	| error {
		/* make sure to catch parse errors */
		parser->failed = true;
	}

%%

#include "gen_lexer.inc"

static struct src_loc src_loc(YYLTYPE yylloc)
{
	struct src_loc loc;
	loc.first_line = yylloc.first_line;
	loc.last_line = yylloc.last_line;
	loc.first_col = yylloc.first_column;
	loc.last_col = yylloc.last_column;
	return loc;
}

static void yyerror(YYLTYPE *yylloc, void *lexer,
		struct parser *parser, const char *msg)
{
	(void)lexer;

	struct src_issue issue;
	issue.loc = src_loc(*yylloc);
	issue.fname = parser->fname;
	issue.buf = parser->buf;

	src_issue(issue, msg);
}

struct parser *create_parser()
{
	return calloc(1, sizeof(struct parser));
}

void destroy_parser(struct parser *p)
{
	yylex_destroy(p->lexer);
	free(p);
}

void parse(struct parser *p, const char *fname, const char *buf)
{
	p->fname = fname;
	p->buf = buf;

	p->comment_nesting = 0;

	p->failed = false;

	yylex_init(&p->lexer);

	yy_scan_string(buf, p->lexer);
	yyparse(p->lexer, p);
}