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
#![deny(missing_docs)]
extern crate read_token;
extern crate range;
pub use parse_error_handler::{
stderr_unwrap,
ParseErrorHandler
};
pub use parse_error::ParseError;
pub use meta_rules::{ parse, Rule };
pub type DebugId = usize;
use std::rc::Rc;
use std::fs::File;
use std::path::Path;
use range::Range;
pub mod bootstrap;
pub mod json;
pub mod meta_rules;
pub mod tokenizer;
mod parse_error;
mod parse_error_handler;
mod all {
pub use super::*;
}
#[derive(PartialEq, Clone, Debug)]
pub enum MetaData {
StartNode(Rc<String>),
EndNode(Rc<String>),
Bool(Rc<String>, bool),
F64(Rc<String>, f64),
String(Rc<String>, Rc<String>),
}
pub fn syntax(rules: &str) -> Result<Vec<(Rc<String>, Rule)>, (Range, ParseError)> {
match bootstrap::convert(
&try!(parse(&bootstrap::rules(), rules)),
&mut vec![]
) {
Ok(res) => Ok(res),
Err(()) => Err((Range::empty(0), ParseError::Conversion(
format!("Bootstrapping rules are incorrect"))))
}
}
pub fn load_syntax_data<A, B>(
syntax_path: A,
data_path: B
) -> Vec<(Range, MetaData)>
where A: AsRef<Path>, B: AsRef<Path>
{
use std::io::Read;
let mut syntax_file = File::open(syntax_path).unwrap();
let mut s = String::new();
syntax_file.read_to_string(&mut s).unwrap();
let rules = stderr_unwrap(&s, syntax(&s));
let mut data_file = File::open(data_path).unwrap();
let mut d = String::new();
data_file.read_to_string(&mut d).unwrap();
stderr_unwrap(&d, parse(&rules, &d))
}