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
#![crate_type="rlib"]
#![allow(dead_code)]
extern crate regex;
use std::collections::HashMap;
#[derive(Copy,Clone)]
pub struct Input<'a> {
pub text: &'a str,
pub offset: usize,
}
pub trait Symbol<'input, G> {
type Output;
fn pretty_print(&self) -> String;
fn parse_complete(&self, grammar: &mut G, text: &'input str)
-> Result<Self::Output, Error<'input>>
{
let (mid, result) = try!(self.parse_prefix(grammar, text));
let end = util::skip_whitespace(mid);
if end.offset == text.len() {
Ok(result)
} else {
Err(Error { expected: "end of input",
offset: end.offset })
}
}
fn parse_prefix(&self, grammar: &mut G, text: &'input str)
-> ParseResult<'input,Self::Output>
{
let input = Input { text: text, offset: 0 };
self.parse(grammar, input)
}
fn parse(&self, grammar: &mut G, input: Input<'input>)
-> ParseResult<'input,Self::Output>;
}
pub type Cache<'input,T> = HashMap<usize, ParseResult<'input,T>>;
pub type ParseResult<'input,O> = Result<(Input<'input>, O), Error<'input>>;
pub enum Kind<NT> {
Text,
Option,
Repeat,
Elem,
Group,
Symbol(NT)
}
#[derive(Clone, Debug)]
pub struct Error<'input> {
pub expected: &'input str,
pub offset: usize
}
#[macro_use]
pub mod macros;
pub mod util;
pub mod tree;
impl<'input> Input<'input> {
fn offset_by(&self, amount: usize) -> Input<'input> {
Input { text: self.text, offset: self.offset + amount }
}
}
#[cfg(test)]
mod test;