-
Notifications
You must be signed in to change notification settings - Fork 0
/
barf_targetspec_parser.trison
330 lines (286 loc) · 12.7 KB
/
barf_targetspec_parser.trison
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
// 2006.10.22 - Copyright Victor Dods - Licensed under Apache 2.0
%targets cpp
%target.cpp.header_filename "barf_targetspec_parser.hpp"
%target.cpp.implementation_filename "barf_targetspec_parser.cpp"
%target.cpp.top_of_header_file %{
#if !defined(BARF_TARGETSPEC_PARSER_HPP_)
#define BARF_TARGETSPEC_PARSER_HPP_
#include "barf.hpp"
#include "barf_commonlang_scanner.hpp"
namespace Barf {
namespace Ast {
class Base;
} // end of namespace Ast
namespace Targetspec {
struct AddCodespecList;
struct AddDirectiveMap;
%}
%target.cpp.class_name Parser
%target.cpp.bottom_of_class {
inline FiLoc const &GetFiLoc () const { return m_scanner.GetFiLoc(); }
bool ScannerDebugSpewIsEnabled () const { return m_scanner.DebugSpewIsEnabled(); }
std::ostream *ScannerDebugSpewStream () { return m_scanner.DebugSpewStream(); }
void SetScannerDebugSpewStream (std::ostream *debug_spew_stream) { m_scanner.SetDebugSpewStream(debug_spew_stream); }
bool OpenTargetspec (string const &input_filename, string const &target_id);
private:
string m_target_id;
CommonLang::Scanner m_scanner;
AddCodespecList *m_add_codespec_list;
AddDirectiveMap *m_add_directive_map;
}
%target.cpp.bottom_of_header_file %{
} // end of namespace Targetspec
} // end of namespace Barf
#endif // !defined(BARF_TARGETSPEC_PARSER_HPP_)
%}
%target.cpp.top_of_implementation_file %{
#include "barf_ast.hpp"
#include "barf_targetspec_ast.hpp"
namespace Barf {
namespace Targetspec {
%}
%target.cpp.top_of_parse_method_actions %{
m_add_codespec_list = new AddCodespecList();
m_add_directive_map = new AddDirectiveMap();
EmitExecutionMessage("starting targetspec parser");
%}
%target.cpp.bottom_of_parse_method_actions %{
if (parse_return_code == PRC_SUCCESS)
EmitExecutionMessage("targetspec parse was successful");
%}
%target.cpp.bottom_of_implementation_file %{
bool Parser::OpenTargetspec (string const &input_filename, string const &target_id)
{
assert(!target_id.empty());
ResetForNewInput();
m_target_id.empty();
EmitExecutionMessage("opening file \"" + input_filename + "\" for input");
bool scanner_open_file_succeeded = m_scanner.OpenFile(input_filename);
if (scanner_open_file_succeeded)
{
EmitExecutionMessage("opened file \"" + input_filename + "\" successfully");
m_target_id = target_id;
}
return scanner_open_file_succeeded;
}
} // end of namespace Targetspec
} // end of namespace Barf
%}
%target.cpp.token_data_type "Ast::Base *"
%target.cpp.token_data_default "NULL"
%target.cpp.custom_token_data_type_cast "Dsc"
%target.cpp.throw_away_token_actions {
delete token_data;
}
%target.cpp.scan_actions {
Ast::Base *lookahead_token_data = NULL;
CommonLang::Scanner::Token::Type scanner_token_type = m_scanner.Scan(lookahead_token_data);
assert(scanner_token_type >= 0);
if (scanner_token_type < 0x100)
return Token(scanner_token_type);
switch (scanner_token_type)
{
case CommonLang::Scanner::Token::BAD_END_OF_FILE: return Token(Terminal::END_, lookahead_token_data);
case CommonLang::Scanner::Token::BAD_TOKEN: return Token(Terminal::BAD_TOKEN, lookahead_token_data);
case CommonLang::Scanner::Token::DIRECTIVE_ADD_CODESPEC: return Token(Terminal::DIRECTIVE_ADD_CODESPEC, lookahead_token_data);
case CommonLang::Scanner::Token::DIRECTIVE_ADD_OPTIONAL_DIRECTIVE: return Token(Terminal::DIRECTIVE_ADD_OPTIONAL_DIRECTIVE, lookahead_token_data);
case CommonLang::Scanner::Token::DIRECTIVE_ADD_REQUIRED_DIRECTIVE: return Token(Terminal::DIRECTIVE_ADD_REQUIRED_DIRECTIVE, lookahead_token_data);
case CommonLang::Scanner::Token::DIRECTIVE_DEFAULT: return Token(Terminal::DIRECTIVE_DEFAULT, lookahead_token_data);
case CommonLang::Scanner::Token::DIRECTIVE_DUMB_CODE_BLOCK: return Token(Terminal::DIRECTIVE_DUMB_CODE_BLOCK, lookahead_token_data);
case CommonLang::Scanner::Token::DIRECTIVE_ID: return Token(Terminal::DIRECTIVE_ID, lookahead_token_data);
case CommonLang::Scanner::Token::DIRECTIVE_STRICT_CODE_BLOCK: return Token(Terminal::DIRECTIVE_STRICT_CODE_BLOCK, lookahead_token_data);
case CommonLang::Scanner::Token::DIRECTIVE_STRING: return Token(Terminal::DIRECTIVE_STRING, lookahead_token_data);
case CommonLang::Scanner::Token::DIRECTIVE_TARGET: return Token(Terminal::DIRECTIVE_TARGET, lookahead_token_data);
case CommonLang::Scanner::Token::DUMB_CODE_BLOCK: return Token(Terminal::DUMB_CODE_BLOCK, lookahead_token_data);
case CommonLang::Scanner::Token::END_OF_FILE: return Token(Terminal::END_, lookahead_token_data);
case CommonLang::Scanner::Token::ID: return Token(Terminal::ID, lookahead_token_data);
case CommonLang::Scanner::Token::NEWLINE: return Token(Terminal::NEWLINE, lookahead_token_data);
case CommonLang::Scanner::Token::STRICT_CODE_BLOCK: return Token(Terminal::STRICT_CODE_BLOCK, lookahead_token_data);
case CommonLang::Scanner::Token::STRING_LITERAL: return Token(Terminal::STRING_LITERAL, lookahead_token_data);
case CommonLang::Scanner::Token::CHAR_LITERAL:
case CommonLang::Scanner::Token::DIRECTIVE_DEFAULT_PARSE_NONTERMINAL:
case CommonLang::Scanner::Token::DIRECTIVE_ERROR:
case CommonLang::Scanner::Token::DIRECTIVE_LOOKAHEAD:
case CommonLang::Scanner::Token::DIRECTIVE_MACRO:
case CommonLang::Scanner::Token::DIRECTIVE_NONTERMINAL:
case CommonLang::Scanner::Token::DIRECTIVE_PREC:
case CommonLang::Scanner::Token::DIRECTIVE_START_IN_STATE_MACHINE:
case CommonLang::Scanner::Token::DIRECTIVE_STATE_MACHINE:
case CommonLang::Scanner::Token::DIRECTIVE_TARGETS:
case CommonLang::Scanner::Token::DIRECTIVE_TERMINAL:
case CommonLang::Scanner::Token::DIRECTIVE_TYPE:
case CommonLang::Scanner::Token::END_PREAMBLE:
case CommonLang::Scanner::Token::REGEX:
assert(lookahead_token_data != NULL);
EmitError(FORMAT("unrecognized token " << scanner_token_type), lookahead_token_data->GetFiLoc());
delete lookahead_token_data;
lookahead_token_data = NULL;
return Token(Terminal::BAD_TOKEN);
default:
assert(false && "this should never happen");
return Token(Terminal::BAD_TOKEN);
}
}
%target.cpp.generate_debug_spew_code
%target.cpp.debug_spew_prefix {"TargetSpec::Parser" << (GetFiLoc().IsValid() ? " ("+GetFiLoc().AsString()+")" : g_empty_string) << ":"}
%terminal BAD_TOKEN
%terminal DIRECTIVE_ADD_CODESPEC %type.cpp "Ast::ThrowAway *"
%terminal DIRECTIVE_ADD_OPTIONAL_DIRECTIVE %type.cpp "Ast::ThrowAway *"
%terminal DIRECTIVE_ADD_REQUIRED_DIRECTIVE %type.cpp "Ast::ThrowAway *"
%terminal DIRECTIVE_DEFAULT %type.cpp "Ast::ThrowAway *"
%terminal DIRECTIVE_DUMB_CODE_BLOCK
%terminal DIRECTIVE_ID
%terminal DIRECTIVE_STRICT_CODE_BLOCK
%terminal DIRECTIVE_STRING
%terminal DIRECTIVE_TARGET %type.cpp "Ast::ThrowAway *"
%terminal DUMB_CODE_BLOCK %type.cpp "Ast::DumbCodeBlock *"
%terminal ID %type.cpp "Ast::Id *"
%terminal NEWLINE
%terminal STRICT_CODE_BLOCK %type.cpp "Ast::StrictCodeBlock *"
%terminal STRING_LITERAL %type.cpp "Ast::String *"
%prec.left %default
%default_parse_nonterminal root
%%
// ///////////////////////////////////////////////////////////////////////////
// root rule
// ///////////////////////////////////////////////////////////////////////////
%nonterminal root %type.cpp "Specification *"
:
directives %end
%target.cpp {
assert(m_add_codespec_list != NULL);
assert(m_add_directive_map != NULL);
return new Specification(
new Ast::Id(m_target_id, FiLoc::ms_invalid),
m_add_codespec_list,
m_add_directive_map);
}
;
// ///////////////////////////////////////////////////////////////////////////
// directives (add_codespec and add_*_directive)
// ///////////////////////////////////////////////////////////////////////////
%nonterminal directives
:
directives directive %target.cpp { return NULL; }
|
%empty %target.cpp { return NULL; }
;
%nonterminal directive
:
add_codespec at_least_one_newline %target.cpp { return NULL; }
|
add_directive at_least_one_newline %target.cpp { return NULL; }
|
at_least_one_newline %target.cpp { return NULL; }
;
%nonterminal add_codespec
:
DIRECTIVE_ADD_CODESPEC:throwaway STRING_LITERAL:filename ID:filename_directive_id
%target.cpp {
assert(m_add_codespec_list != NULL);
assert(m_add_directive_map != NULL);
AddDirective *add_directive = m_add_directive_map->Element(filename_directive_id->GetText());
if (add_directive == NULL)
EmitError("undeclared directive id \"" + filename_directive_id->GetText() + "\" in add_codespec directive", throwaway->GetFiLoc());
if (add_directive == NULL || !add_directive->IsRequired() || add_directive->m_param_type != Ast::AST_STRING)
EmitError("directive id \"" + filename_directive_id->GetText() + "\" in add_codespec directive must refer to a required directive accepting param type %string", throwaway->GetFiLoc());
if (filename->GetText().find_first_of(DIRECTORY_SLASH_STRING) != string::npos)
EmitError("filename portion \"" + filename->GetText() + "\" of %add_codespec directive may not contain slash (directory-delimiting) characters", throwaway->GetFiLoc());
m_add_codespec_list->Append(new AddCodespec(filename, filename_directive_id));
delete throwaway;
return NULL;
}
;
%nonterminal add_directive %type.cpp "AddDirective *"
:
DIRECTIVE_ADD_OPTIONAL_DIRECTIVE:throwaway ID:directive_to_add_id param_spec:param_type
%target.cpp {
assert(m_add_directive_map != NULL);
m_add_directive_map->Add(
directive_to_add_id->GetText(),
new AddOptionalDirective(directive_to_add_id, param_type->m_param_type, NULL));
delete throwaway;
delete param_type;
return NULL;
}
|
DIRECTIVE_ADD_OPTIONAL_DIRECTIVE:throwaway1 ID:directive_to_add_id param_spec:param_type DIRECTIVE_DEFAULT:throwaway2 default_value:default_value
%target.cpp {
assert(m_add_directive_map != NULL);
AddDirective *directive =
new AddOptionalDirective(
directive_to_add_id,
param_type->m_param_type, default_value);
m_add_directive_map->Add(
directive_to_add_id->GetText(),
directive);
if (param_type->m_param_type != default_value->GetAstType())
EmitError(
"type mismatch for default value for directive " + directive->DirectiveString() +
"; was expecting type " + Ast::TextBase::DirectiveTypeString(param_type->m_param_type) +
" but got type " + Ast::TextBase::DirectiveTypeString(default_value->GetAstType()),
throwaway1->GetFiLoc());
delete throwaway1;
delete param_type;
delete throwaway2;
return NULL;
}
|
DIRECTIVE_ADD_REQUIRED_DIRECTIVE:throwaway ID:directive_to_add_id param_spec:param_type
%target.cpp {
assert(m_add_directive_map != NULL);
m_add_directive_map->Add(
directive_to_add_id->GetText(),
new AddRequiredDirective(directive_to_add_id, param_type->m_param_type));
delete throwaway;
delete param_type;
return NULL;
}
;
%nonterminal param_spec %type.cpp "ParamType *"
:
%empty
%target.cpp {
return new ParamType(Ast::AST_NONE);
}
|
DIRECTIVE_ID:throwaway
%target.cpp {
delete throwaway;
return new ParamType(Ast::AST_ID);
}
|
DIRECTIVE_STRING:throwaway
%target.cpp {
delete throwaway;
return new ParamType(Ast::AST_STRING);
}
|
DIRECTIVE_DUMB_CODE_BLOCK:throwaway
%target.cpp {
delete throwaway;
return new ParamType(Ast::AST_DUMB_CODE_BLOCK);
}
|
DIRECTIVE_STRICT_CODE_BLOCK:throwaway
%target.cpp {
delete throwaway;
return new ParamType(Ast::AST_STRICT_CODE_BLOCK);
}
;
%nonterminal default_value %type.cpp "Ast::TextBase *"
:
ID:value %target.cpp { return value; }
| STRING_LITERAL:value %target.cpp { return value; }
| DUMB_CODE_BLOCK:value %target.cpp { return value; }
| STRICT_CODE_BLOCK:value %target.cpp { return value; }
;
// ///////////////////////////////////////////////////////////////////////////
// misc rules
// ///////////////////////////////////////////////////////////////////////////
%nonterminal at_least_one_newline
:
at_least_one_newline NEWLINE %target.cpp { return NULL; }
|
NEWLINE %target.cpp { return NULL; }
;