1#![crate_type="rlib"]
2#![allow(dead_code)] extern crate regex;
5
6use std::collections::HashMap;
7
8#[derive(Copy,Clone)]
9pub struct Input<'a> {
10 pub text: &'a str,
11 pub offset: usize,
12}
13
14pub trait Symbol<'input, G> {
15 type Output;
16
17 fn pretty_print(&self) -> String;
18
19 fn parse_complete(&self, grammar: &mut G, text: &'input str)
20 -> Result<Self::Output, Error<'input>>
21 {
22 let (mid, result) = try!(self.parse_prefix(grammar, text));
23 let end = util::skip_whitespace(mid);
24 if end.offset == text.len() {
25 Ok(result)
26 } else {
27 Err(Error { expected: "end of input",
28 offset: end.offset })
29 }
30 }
31
32 fn parse_prefix(&self, grammar: &mut G, text: &'input str)
33 -> ParseResult<'input,Self::Output>
34 {
35 let input = Input { text: text, offset: 0 };
36 self.parse(grammar, input)
37 }
38
39 fn parse(&self, grammar: &mut G, input: Input<'input>)
40 -> ParseResult<'input,Self::Output>;
41}
42
43pub type Cache<'input,T> = HashMap<usize, ParseResult<'input,T>>;
44
45pub type ParseResult<'input,O> = Result<(Input<'input>, O), Error<'input>>;
46
47pub enum Kind<NT> {
48 Text,
49 Option,
50 Repeat,
51 Elem,
52 Group,
53 Symbol(NT)
54}
55
56#[derive(Clone, Debug)]
57pub struct Error<'input> {
58 pub expected: &'input str,
59 pub offset: usize
60}
61
62#[macro_use]
63pub mod macros;
64pub mod util;
65pub mod tree;
66
67impl<'input> Input<'input> {
68 fn offset_by(&self, amount: usize) -> Input<'input> {
69 Input { text: self.text, offset: self.offset + amount }
70 }
71}
72
73#[cfg(test)]
74mod test;