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
pub use self::lines::Lines;
pub use self::node::Node;
pub use self::number::Number;
pub use self::optional::Optional;
pub use self::repeat::Repeat;
pub use self::rule::Rule;
pub use self::select::Select;
pub use self::separate_by::SeparateBy;
pub use self::sequence::Sequence;
pub use self::text::Text;
pub use self::token::Token;
pub use self::until_any::UntilAny;
pub use self::until_any_or_whitespace::UntilAnyOrWhitespace;
pub use self::whitespace::Whitespace;
use range::Range;
use {
MetaData,
ParseError,
Syntax,
};
use tokenizer::TokenizerState;
mod lines;
mod node;
mod number;
mod optional;
mod repeat;
mod rule;
mod select;
mod separate_by;
mod sequence;
mod text;
mod token;
mod until_any;
mod until_any_or_whitespace;
mod whitespace;
pub fn parse(
rules: &Syntax,
text: &str,
tokens: &mut Vec<Range<MetaData>>
) -> Result<(), Range<ParseError>> {
let chars: Vec<char> = text.chars().collect();
let s = TokenizerState(tokens.len());
let n = match rules.rules.len() {
0 => { return Err(Range::empty(0).wrap(ParseError::NoRules)); }
x => x
};
let res = rules.rules[n - 1].parse(tokens, &s, &chars, 0, &rules.rules);
match res {
Ok((range, s, opt_error)) => {
if range.next_offset() < text.chars().count() {
Err(ret_err(
Range::empty(range.next_offset()).wrap(
ParseError::ExpectedEnd),
opt_error
))
} else {
tokens.truncate(s.0);
Ok(())
}
}
Err(range_err) => Err(range_err)
}
}
pub fn update_refs(&mut Syntax { ref mut rules, ref names }: &mut Syntax) {
for r in rules {
r.update_refs(names);
}
}
pub type ParseResult<S> = Result<(Range, S, Option<Range<ParseError>>),
Range<ParseError>>;
#[inline(always)]
fn update<'a>(
range: Range,
err: Option<Range<ParseError>>,
chars: &mut &'a [char],
offset: &mut usize,
opt_error: &mut Option<Range<ParseError>>
) {
let next_offset = range.next_offset();
*chars = &chars[next_offset - *offset..];
*offset = next_offset;
err_update(err, opt_error);
}
#[inline(always)]
fn err_update(
err: Option<Range<ParseError>>,
opt_error: &mut Option<Range<ParseError>>
) {
if let &mut Some(ref mut opt_error) = opt_error {
if let Some(err) = err {
if opt_error.next_offset() <= err.next_offset() {
*opt_error = err;
}
}
} else {
*opt_error = err;
};
}
#[inline(always)]
fn ret_err(a: Range<ParseError>, b: Option<Range<ParseError>>)
-> Range<ParseError> {
if let Some(b) = b {
if b.next_offset() > a.next_offset() {
b
} else {
a
}
} else {
a
}
}
#[cfg(test)]
mod tests{
use range::Range;
use all::*;
#[test]
fn no_rules() {
assert_eq!(parse(&Syntax::new(), "", &mut vec![]),
Err(Range::empty(0).wrap(ParseError::NoRules)));
}
}