43#include "parser.tab.hh"
50#define YY_NO_UNISTD_H 1
56extern int yyerror(
const char *);
60const int max_include_depth = 10;
61YY_BUFFER_STATE include_stack[max_include_depth];
62std::vector<std::string> include_filename_stack(max_include_depth);
63int include_linenum_stack[max_include_depth];
64int include_stack_ptr = 0;
67std::string yyfilename;
Parser namespace for GMAD language. Combination of Geant4 and MAD.
81(([0-9]+)|([0-9]*\.[0-9]*))((e|E)[+|-]?[0-9]+)? { yylval.dval=atof(yytext);
return NUMBER; }
92marker {
return MARKER; }
93drift {
return DRIFT; }
97rfcavity {
return RF; }
98sbend {
return SBEND; }
100hkicker {
return HKICKER; }
101vkicker {
return VKICKER; }
102kicker {
return KICKER; }
103tkicker {
return TKICKER; }
104quadrupole {
return QUADRUPOLE; }
105sextupole {
return SEXTUPOLE; }
106octupole {
return OCTUPOLE; }
107decapole {
return DECAPOLE; }
108multipole {
return MULTIPOLE; }
109thinmultipole {
return THINMULT; }
110solenoid {
return SOLENOID; }
114muonspoiler {
return MUONSPOILER; }
115muspoiler {
return MUONSPOILER; }
116shield {
return SHIELD; }
117element {
return ELEMENT; }
118screen {
return SCREEN; }
119awakescreen {
return AWAKESCREEN; }
120awakespectrometer {
return AWAKESPECTROMETER; }
121transform3d {
return TRANSFORM3D ; }
122laser {
return LASER; }
123degrader {
return DEGRADER;}
125thinrmatrix {
return THINRMATRIX;}
126paralleltransporter {
return PARALLELTRANSPORTER;}
127rmatrix {
return RMATRIX;}
128crystalcol {
return CRYSTALCOL; }
129wirescanner {
return WIRESCANNER; }
130undulator {
return UNDULATOR; }
131usercomponent {
return USERCOMPONENT; }
134target {
return TARGET; }
136cavitymodel {
return CAVITYMODEL; }
137cutsregion {
return REGION; }
138newcolour {
return NEWCOLOUR; }
139crystal {
return CRYSTAL; }
140field {
return FIELD; }
141placement {
return PLACEMENT; }
142query {
return QUERY; }
143samplerplacement {
return SAMPLERPLACEMENT; }
144scorer {
return SCORER; }
145scorermesh {
return SCORERMESH; }
146tunnel {
return TUNNEL; }
147xsecbias {
return XSECBIAS; }
148xsecBias {
return XSECBIAS; }
149aperture {
return APERTURE; }
151modulator {
return MODULATOR;}
153matdef {
return MATERIAL; }
159period {
return PERIOD; }
160range {
return RANGE; }
164"else" {
return ELSE; }
165"begin" {
return BEGN; }
172option {
return OPTION; }
173beta0 {
return BEAM; }
174print {
return PRINT; }
175"return" {
return STOP; }
178sample {
return SAMPLE; }
179csample {
return CSAMPLE; }
187 yylval.str =
new std::string(yytext+1,strlen(yytext)-2);
static Parser * Instance()
Access method.
void AddVariable(std::string *name)
Add variable memory to variable list for memory management.
195 std::string errorstring =
"malformed string or unmatched quote";
196 yyerror(errorstring.c_str());
201[a-zA-Z#][A-Za-z0-9_#.]* {
202 std::string var(yytext);
209 case Symtab::symtabtype::FUNCTION:
211 case Symtab::symtabtype::ARRAY:
213 case Symtab::symtabtype::NUMBER:
215 case Symtab::symtabtype::STRING:
221 std::string* name =
new std::string(var);
Symtab * symlook(const std::string &s)
look up parser symbol
Common header for the lexer and the parser to share Symbol table for numeric variables,...
symtabtype GetType() const
Get type.
227. {
return yytext[0]; }
233 std::string bdsimpath = (std::string)getEnv(
"BDSIMPATH");
234 std::string includefilename =
"";
235 std::string mainfilename = yyfilename;
236 std::string mainfilepath =
"";
237 if(bdsimpath.length()>0){
239 std::cout <<
"parser> using BDSIMPATH to build included filepaths" << std::endl;
241 includefilename = bdsimpath + (std::string)yytext;
244 std::string::size_type found = mainfilename.rfind(
"/");
245 if (found != std::string::npos) {
246 mainfilepath = mainfilename.substr(0,found+1);
249 if ((mainfilename.substr(0,1)) ==
"/"){
251 includefilename = mainfilepath + (std::string)yytext;
256 std::string cwd = (std::string)getcwd(cwdchars,
sizeof(cwdchars)) +
"/";
257 includefilename = cwd + mainfilepath + (std::string)yytext;
261 std::cout <<
"parser> reading file " << includefilename << std::endl;
262 if (includefilename == yyfilename)
264 std::string errorstring =
"\nError: recursively including the same file inside itself!\n\n Problem";
265 yyerror(errorstring.c_str());
267 if(include_stack_ptr >= max_include_depth - 1)
269 std::string errorstring =
"Error : Include depth exceeds " + std::to_string(max_include_depth);
270 yyerror(errorstring.c_str());
274 yyin = fopen(includefilename.c_str(),
"r");
279 include_linenum_stack[include_stack_ptr] = line_num;
281 include_filename_stack[include_stack_ptr] = yyfilename;
282 yyfilename = includefilename;
283 include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
284 include_filename_stack[include_stack_ptr] = yyfilename;
285 yy_switch_to_buffer(yy_create_buffer( yyin, YY_BUF_SIZE ) );
290 std::string errorstring =
"Error : can't open " + includefilename;
291 yyerror(errorstring.c_str());
298 if (--include_stack_ptr < 0)
306 yy_delete_buffer(YY_CURRENT_BUFFER);
307 yy_switch_to_buffer(include_stack[include_stack_ptr]);
308 yyfilename = include_filename_stack[include_stack_ptr];
309 line_num = include_linenum_stack[include_stack_ptr];