This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathparser.rs
More file actions
259 lines (235 loc) · 9.39 KB
/
parser.rs
File metadata and controls
259 lines (235 loc) · 9.39 KB
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
// parser for the lexer description
// invoked by Rustc when compiling a lexer file
// uses libsyntax to parse what Rustc gives us
use collections::hashmap::HashMap;
use lexer::Condition;
use lexer::LexerDef;
use lexer::Rule;
use regex;
use regex::Regex;
use regex::{Cat, Char, Closure, Or, Maybe, Var};
use std::rc::Rc;
use std::vec_ng::Vec;
use syntax::ast::Ident;
use syntax::ast::Name;
use syntax::parse::token;
use syntax::parse::token::keywords;
use syntax::parse::parser::Parser;
// the "lexical" environment of regular expression definitions
type Env = HashMap<Name, Rc<Regex>>;
// recursively parses a character class, e.g. ['a'-'z''0'-'9''_']
// basically creates an or-expression per character in the class
fn getCharClass(parser: &mut Parser) -> ~Regex {
let ch = match parser.bump_and_get() {
token::LIT_CHAR(ch) => ch as u8,
_ => parser.unexpected()
};
match parser.token {
token::RBRACKET => {
parser.bump();
~Char(ch)
}
token::BINOP(token::MINUS) => {
// a char seq, e.g. 'a' - 'Z'
parser.bump();
let ch2 = match parser.bump_and_get() {
token::LIT_CHAR(ch) => ch as u8,
_ => parser.unexpected()
};
let reg = match regex::seq(ch, ch2) {
Some(r) => r,
None => parser.span_fatal(parser.last_span,
"invalid character range")
};
match parser.token {
token::RBRACKET => {
parser.bump();
reg
}
_ => ~Or(reg, getCharClass(parser))
}
}
_ => ~Or(~Char(ch), getCharClass(parser))
}
}
// parses a "constant" in an regular expression, i.e. either
// - a literal character
// - a character class
// - an identifier refering to another expression
// parenthesized subexpressions are also parsed here since the have
// the same operator precedence as the constants
fn getConst(parser: &mut Parser, env: &Env) -> ~Regex {
let tok = parser.bump_and_get();
// here we expect either
// the start of a character-class, '['
// the start of a parenthesized expression, '('
// a literal char constant, 'a'
match tok {
token::LPAREN => getRegex(parser, &token::RPAREN, env),
token::LBRACKET => getCharClass(parser),
token::LIT_CHAR(ch) => ~Char(ch as u8),
token::LIT_STR(id) => match regex::string(token::get_name(id.name).get()) {
Some(reg) => reg,
None => parser.span_fatal(parser.last_span,
"bad string constant in regular expression")
},
token::IDENT(id, _) => match env.find_copy(&id.name) {
Some(value) => ~Var(value),
None => parser.span_fatal(parser.last_span,
format!("unknown identifier: {:s}",
token::get_name(id.name).get()))
},
_ => parser.unexpected_last(&tok)
}
}
// a "closure" in a regular expression, i.e. expr*
// the * operator has lower precedence that concatenation
fn getClosure(parser: &mut Parser, env: &Env) -> ~Regex {
let reg = getConst(parser, env);
if parser.eat(&token::BINOP(token::STAR)) { ~Closure(reg) }
else if parser.eat(&token::BINOP(token::PLUS)) {
~Cat(reg.clone(), ~Closure(reg))
}
else if parser.eat(&token::BINOP(token::PERCENT)) { ~Maybe(reg) }
else { reg }
}
// recursively parses a sequence of concatenations
// continues until it reaches the end of the current subexpr,
// indicated by the end parameter or an or operator, which has
// higher precedence. Concatenation is left-assoc
fn getConcat(parser: &mut Parser, end: &token::Token, env: &Env) -> ~Regex {
let opl = getClosure(parser, env);
if &parser.token == end ||
parser.token == token::BINOP(token::OR) {
opl
} else {
let opr = getConcat(parser, end, env);
~Cat(opl, opr)
}
}
// entry point of the regex parser, parses an or-expression
// tries to parse a concat expression as the left operation, and then
// if we are not at the end of the current subexpression as indicated by
// the end parameter, we try to read a | operator followed by another
// expression which is parsed recursively (or is left-assoc)
fn getRegex(parser: &mut Parser, end: &token::Token, env: &Env) -> ~Regex {
if parser.eat(end) {
parser.unexpected();
}
let left = getConcat(parser, end, env);
if parser.eat(end) { left }
else {
parser.expect(&token::BINOP(token::OR));
let right = getRegex(parser, end, env);
~Or(left, right)
}
}
// a pattern is an association of a name to a regular expression
// this function expects the next tokens to be id = reg, with id
// being a non-keyword identifier and reg a literal constant
fn getPattern(parser: &mut Parser, env: &Env) -> (Ident, ~Regex) {
let name = parser.parse_ident();
parser.expect(&token::EQ);
let reg = getRegex(parser, &token::SEMI, env);
(name, reg)
}
// a definition is of the form let pattern; see the function above
// for a description of pattern. This function just tries to parse
// as much definitions as possible, until it sees something that
// does not match. returns an error if it encounters a malformed
// definition, otherwise return the "environment" containing named
// regular expression definitions
fn getDefinitions(parser: &mut Parser) -> ~Env {
let mut ret = ~HashMap::new();
while parser.eat_keyword(keywords::Let) {
let (id, pat) = getPattern(parser, ret);
ret.insert(id.name, Rc::new(*pat));
}
ret
}
// parses the contents of a "condition" body, i.e. simply a
// list of rules of the form regex => action
// stops as soon as we encounter a closing brace } which
// indicates the end of the condition body
fn getCondition(parser: &mut Parser, env: &Env) -> ~[Rule] {
let mut ret = ~[];
while !parser.eat(&token::RBRACE) {
let reg = getRegex(parser, &token::FAT_ARROW, env);
let stmt = parser.parse_stmt(Vec::new());
ret.push(Rule { pattern: reg, action: stmt });
}
ret
}
// parses the main body of the lexer description
// entries here may be either rules of the gorm regex => action
// or "conditions" of the form condition { ... } that contains rules
// rules outside conditions implicitely belong to the "INITIAL" condition
fn getConditions(parser: &mut Parser, env: &Env) -> ~[Condition] {
// remember the names of the conditions we already
// encountered and where we stored their rules in
// the conditions array
let mut cond_names: HashMap<Name, uint> = HashMap::new();
let mut ret = ~[];
let initial = Condition {
name: token::intern("INITIAL"),
rules: ~[]
};
cond_names.insert(initial.name, 0);
ret.push(initial);
while parser.token != token::EOF {
// here we can expect either a condition declaration
// or simply a rule, which is then implicitly in the
// "Initial" condition
// in any case, we expect an ident or a regex first
match parser.token {
token::IDENT(id, _) => {
// this may be either the start of a regexp followed
// by an arrow and an action or a condition followed
// by an opening brace.
// if we see an opening brace '{' here then it's a
// condition whose name is the id we just parsed
if parser.look_ahead(1, |tok| tok == &token::LBRACE) {
// ok it's a condition
// bump 2 times: the identifier and the lbrace
parser.bump();
parser.bump();
// parse the condition body
let rules = getCondition(parser, env);
// have we seen this condition before ?
match cond_names.find_copy(&id.name) {
Some(i) => {
ret[i].rules.push_all_move(rules);
continue
}
None => ()
}
// nope, create it
ret.push(Condition { rules: rules, name: id.name });
cond_names.insert(id.name, ret.len() - 1);
} else {
// ok, it's not a condition, so it's a rule of the form
// regex => action, with regex beginning by an identifier
let reg = getRegex(parser, &token::FAT_ARROW, env);
let stmt = parser.parse_stmt(Vec::new());
ret[0].rules.push(Rule { pattern: reg, action: stmt });
}
}
_ => {
// it's not an ident, but it may still be the
// beginning of a regular expression
let reg = getRegex(parser, &token::FAT_ARROW, env);
let stmt = parser.parse_stmt(Vec::new());
ret[0].rules.push(Rule { pattern: reg, action: stmt });
}
}
}
ret
}
// runs the parsing of the full analyser description
// - first gets an environment of the regular expression definitions
// - then parses the definitions of the rules and the conditions
pub fn parse(parser: &mut Parser) -> LexerDef {
let defs = getDefinitions(parser);
let conditions = getConditions(parser, defs);
LexerDef { conditions: conditions }
}