-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPreprocess.y
154 lines (128 loc) · 4.12 KB
/
Preprocess.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
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
%{
#include <stdio.h>
#include "PreprocessState.h"
%}
%union
{
char* str;
Token* token;
std::vector< Ref<Token> >* tokens;
std::vector<std::string>* params;
}
%{
using namespace std;
extern int Preprocess_lex(void* yyscanner);
extern void Preprocess_set_extra(void* user_defined, void* yyscanner);
extern int Preprocess_get_lineno(void* yyscanner);
static int Preprocess_lex_wrapper(void* yyscanner)
{
return Preprocess_lex(yyscanner);
}
#define Preprocess_lex Preprocess_lex_final
static int Preprocess_lex_final(YYSTYPE* lval, PreprocessState* state)
{
state->SetLValue(lval);
Preprocess_set_extra(state, state->GetScanner());
return Preprocess_lex_wrapper(state->GetScanner());
}
void Preprocess_error(PreprocessState* state, const char* msg)
{
state->Error();
fprintf(stderr, "%s:%d: error: %s\n", state->GetFileName().c_str(), Preprocess_get_lineno(state->GetScanner()), msg);
}
%}
%pure-parser
%parse-param {PreprocessState* state}
%lex-param {PreprocessState* state}
%token END 0
%token _ERROR
%token LPAREN RPAREN COMMA
%token NEWLINE PASTE
%token UNDEF_TOK IFDEF_TOK IFNDEF_TOK ELSE_TOK ENDIF_TOK
%token <str> ID TOKEN MACRO DEFINE_PARAMS DEFINE_NO_PARAMS
%destructor { free($$); } ID TOKEN MACRO DEFINE_PARAMS DEFINE_NO_PARAMS id_or_macro
%destructor { delete $$; } token_list
%destructor { delete $$; } param_list
%destructor { $$->Release(); } token
%type <tokens> token_list
%type <token> token
%type <params> param_list
%type <str> id_or_macro
%%
input: toplevel_stmt_list END { state->Finalize(); }
|
;
toplevel_stmt_list: toplevel_stmt_list toplevel_stmt
| toplevel_stmt
;
toplevel_stmt: output_token_list NEWLINE
| NEWLINE
| IFDEF_TOK id_or_macro NEWLINE { state->BeginIf(state->IsDefined($2)); free($2); }
| IFNDEF_TOK id_or_macro NEWLINE { state->BeginIf(!state->IsDefined($2)); free($2); }
| ELSE_TOK NEWLINE { state->Else(); }
| ENDIF_TOK NEWLINE { state->EndIf(); }
| DEFINE_NO_PARAMS NEWLINE
{
state->Define($1, std::vector<std::string>(), std::vector< Ref<Token> >(), false);
free($1);
}
| DEFINE_NO_PARAMS token_list NEWLINE
{
state->Define($1, std::vector<std::string>(), *$2, false);
free($1);
delete $2;
}
| DEFINE_PARAMS RPAREN token_list NEWLINE
{
state->Define($1, std::vector<std::string>(), *$3, true);
free($1);
delete $3;
}
| DEFINE_PARAMS RPAREN NEWLINE
{
state->Define($1, std::vector<std::string>(), std::vector< Ref<Token> >(), true);
free($1);
}
| DEFINE_PARAMS param_list RPAREN token_list NEWLINE
{
state->Define($1, *$2, *$4, true);
free($1);
delete $2;
delete $4;
}
| DEFINE_PARAMS param_list RPAREN NEWLINE
{
state->Define($1, *$2, std::vector< Ref<Token> >(), true);
free($1);
delete $2;
}
| UNDEF_TOK id_or_macro NEWLINE { state->Undefine($2); free($2); }
;
id_or_macro: ID { $$ = $1; }
| MACRO { $$ = $1; }
;
token_list: token_list token { $$ = $1; $$->push_back($2); $2->Release(); }
| token { $$ = new std::vector< Ref<Token> >(); $$->push_back($1); $1->Release(); }
;
token: TOKEN { $$ = new Token(TOKEN_BASIC, $1); $$->AddRef(); free($1); }
| ID { $$ = new Token(TOKEN_ID, $1); $$->AddRef(); free($1); }
| MACRO { $$ = new Token(TOKEN_ID, $1); $$->AddRef(); free($1); }
| LPAREN { $$ = new Token(TOKEN_LPAREN); $$->AddRef(); }
| RPAREN { $$ = new Token(TOKEN_RPAREN); $$->AddRef(); }
| COMMA { $$ = new Token(TOKEN_COMMA); $$->AddRef(); }
| PASTE { $$ = new Token(TOKEN_PASTE); $$->AddRef(); }
;
param_list: param_list COMMA ID { $$ = $1; $$->push_back($3); free($3); }
| ID { $$ = new std::vector<std::string>(); $$->push_back($1); free($1); }
;
output_token_list: output_token_list output_token
| output_token
;
output_token: TOKEN { Ref<Token> token = new Token(TOKEN_BASIC, $1); state->Append(token); free($1); }
| ID { Ref<Token> token = new Token(TOKEN_ID, $1); state->Append(token); free($1); }
| LPAREN { Ref<Token> token = new Token(TOKEN_LPAREN); state->Append(token); }
| RPAREN { Ref<Token> token = new Token(TOKEN_RPAREN); state->Append(token); }
| COMMA { Ref<Token> token = new Token(TOKEN_COMMA); state->Append(token); }
| MACRO { state->BeginMacroExpansion($1); free($1); }
;
%%