-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.y
81 lines (63 loc) · 2.39 KB
/
parser.y
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
%parse-param {struct ast_script ** result} {char ** error}
%{
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "ast.h"
int yylex(void);
void yyerror(struct ast_script ** result, char ** error, const char * str);
%}
%code requires {
#include "ast.h"
}
%union {
struct ast_script * script;
struct ast_transformation transformation;
struct ast_transformation_args * transformation_args;
struct ast_literal literal;
char * token;
}
%token T_IDENTIFIER T_INTEGER T_FLOATING T_STRING
%type<script> script
%type<transformation> transformation
%type<transformation_args> transformation_args transformation_args_req
%type<literal> literal
%type<token> T_IDENTIFIER T_INTEGER T_FLOATING T_STRING
%%
file
: script YYEOF { *result = ast_script_reverse($1); }
;
script
: /* empty */ { $$ = NULL; }
| script transformation ';' { $$ = ast_script_new($2, $1); }
;
transformation
: T_IDENTIFIER '(' transformation_args ')' {
$$ = ast_transformation_create(NULL, $1, ast_transformation_args_reverse($3),
ast_position_create(@$.first_line, @$.first_column));
}
| T_IDENTIFIER '.' T_IDENTIFIER '(' transformation_args ')' {
$$ = ast_transformation_create($1, $3, ast_transformation_args_reverse($5),
ast_position_create(@$.first_line, @$.first_column));
}
;
transformation_args
: /* empty */ { $$ = NULL; }
| transformation_args_req { $$ = $1; }
;
transformation_args_req
: literal { $$ = ast_transformation_args_new($1, NULL); }
| transformation_args_req ',' literal { $$ = ast_transformation_args_new($3, $1); }
;
literal
: T_INTEGER { $$ = ast_literal_create(L_INTEGER, $1, ast_position_create(@$.first_line, @$.first_column)); }
| T_FLOATING { $$ = ast_literal_create(L_FLOATING, $1, ast_position_create(@$.first_line, @$.first_column)); }
| T_STRING { $$ = ast_literal_create(L_STRING, $1, ast_position_create(@$.first_line, @$.first_column)); }
| T_IDENTIFIER { $$ = ast_literal_create(L_IDENTIFIER, $1, ast_position_create(@$.first_line, @$.first_column)); }
;
%%
void yyerror(struct ast_script ** result, char ** error, const char * str) {
free(*error);
*error = malloc(strlen(str) + 56);
sprintf(*error, "at %d:%d: %s", yylloc.first_line, yylloc.first_column, str);
}