16 std::vector<ASTNode*> children;
30 static bool machine(ParserState *ps);
31 static bool expect(ParserState *ps, char c);
32 static bool expect_alpha(ParserState *ps);
33 static bool expect_digit(ParserState *ps);
34 static bool expect_alnum(ParserState *ps);
35 static bool match_str(ParserState *ps, const char *str);
36 static bool match_char(ParserState *ps, char c);
37 static bool mstatement(ParserState *ps);
38 static bool ident(ParserState *ps, std::string *s);
39 static bool gear(ParserState *ps);
40 static bool expression(ParserState *ps);
42 static bool nextchar(ParserState *ps);
43 static void putback(ParserState *ps, char c);
45 bool parse_machine(Machine *mcn, const char *fname)
47 FILE *fp = fopen(fname, "rb");
55 pstate.nextc = fgetc(fp); // prime the parser
58 bool res = machine(&pstate);
63 static bool machine(ParserState *ps)
65 matchstr(ps, "machine");
68 while(mstatement(ps));
74 static bool expect(ParserState *ps, int c)
84 static bool expect_alpha(ParserState *ps)
86 if(!isalpha(ps->nextc)) {
94 static bool expect_digit(ParserState *ps)
96 if(!isdigit(ps->nextc)) {
104 static bool expect_alnum(ParserState *ps)
106 if(!isalnum(ps->nextc)) {
114 static bool match_str(ParserState *ps, const char *str)
116 while(*str && match_char(ps, *str++));
117 return !feof(ps->fp);
120 static bool match_char(ParserState *ps, int c)
129 static bool expression(ParserState *ps);
131 #define CHECK(x) if(!(x)) return false
133 static bool mstatement(ParserState *ps)
137 CHECK(ident(ps, &id));
146 static bool expression(ParserState *ps)
150 static bool ident(ParserState *ps, std::string *s)
152 CHECK(expect_alpha(ps));
153 while(isalnum(ps->nextc) || ps->nextc == '_') {
154 s->append((char)ps->nextc);
160 static bool skip_line(FILE *fp)
163 while((c = fgetc(fp)) != -1 && c != '\n');
167 static bool nextchar(ParserState *ps)
169 if(ps->savedc != -1) {
170 ps->nextc = ps->savedc;
175 while((ps->nextc = fgetc(ps->fp)) != -1) {
176 if(ps->nextc == '#') {
177 CHECK(skip_line(ps->fp));
179 if(!isspace(ps->nextc)) {
184 return ps->nextc != -1;
187 static void putback(ParserState *ps, char c)
189 ps->savedc = ps->nextc;