/
parser_helper.c
149 lines (131 loc) · 3.4 KB
/
parser_helper.c
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
/*
* Milkymist SoC (Software)
* Copyright (C) 2007, 2008, 2009, 2010 Sebastien Bourdeauducq
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdarg.h>
#define _GNU_SOURCE /* for asprintf */
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <malloc.h>
#include <fpvm/ast.h>
#include "symtab.h"
#include "scanner.h"
#include "parser.h"
#include "parser_itf.h"
#include "parser_helper.h"
static char printable_char(unsigned char c)
{
return c < ' ' || c > '~' ? '?' : c;
}
/*
* Since operators don't set "label" properly in unique(), we can't just print
* the whole string, but need to cut it at the first non-printable character.
*/
static int printable_label(const char *s)
{
const char *p;
if(isalnum(*s)) {
for(p = s; isalnum(*p); p++);
} else {
for(p = s; *p > ' '; p++);
}
return p-s;
}
const char *parse(const char *expr, int start_token, struct parser_comm *comm)
{
struct scanner *s;
struct parser_state state = {
.comm = comm,
.success = 0,
.error = NULL,
.error_label = NULL,
.id = NULL,
.style = unknown_style,
};
int tok;
struct id *identifier;
void *p;
char *error = NULL;
s = new_scanner((unsigned char *)expr);
p = ParseAlloc(malloc);
Parse(p, start_token, NULL, &state);
tok = scan(s);
while(tok != TOK_EOF) {
if(tok == TOK_ERROR) {
asprintf(&error,
"FPVM, line %d: scan error near '%c'",
s->lineno, printable_char(s->cursor[-1]));
ParseFree(p, free);
delete_scanner(s);
return error;
}
identifier = malloc(sizeof(struct id));
identifier->token = tok;
identifier->lineno = s->lineno;
switch(tok) {
case TOK_CONSTANT:
identifier->constant = get_constant(s);
identifier->label = "";
break;
case TOK_FNAME:
identifier->fname = get_name(s);
identifier->label = identifier->fname;
break;
case TOK_IDENT:
identifier->sym = get_symbol(s);
identifier->label = identifier->sym->fpvm_sym.name;
break;
default:
identifier->label = (const char *) s->old_cursor;
break;
}
state.id = identifier;
Parse(p, tok, identifier, &state);
tok = scan(s);
}
identifier = malloc(sizeof(struct id));
identifier->token = TOK_EOF;
identifier->lineno = s->lineno;
identifier->label = "EOF";
state.id = identifier;
Parse(p, TOK_EOF, NULL, &state);
ParseFree(p, free);
delete_scanner(s);
free(identifier);
if(!state.success) {
asprintf(&error,
"FPVM, line %d: %s near '%.*s'",
state.error_lineno,
state.error ? state.error : "parse error",
printable_label(state.error_label), state.error_label);
free((void *) state.error);
}
return error;
}
void parse_free_one(struct ast_node *node)
{
free(node);
}
void parse_free(struct ast_node *node)
{
if(node == NULL) return;
if(node_is_op(node)) {
parse_free(node->contents.branches.a);
parse_free(node->contents.branches.b);
parse_free(node->contents.branches.c);
}
free(node);
}