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
#![doc(test(attr(allow(unused_variables), deny(warnings))))]
#[macro_use]
extern crate nom;
#[macro_use]
extern crate hexf_parse;
#[cfg(feature="graphviz")]
extern crate dot;
macro_rules! ast_panic_test {
($name: ident, $func: ident, $input: expr) => {
#[test]
#[should_panic]
fn $name () {
use super::*;
$func($input.as_bytes()).unwrap().1;
}
}
}
macro_rules! ast_test {
($name: ident, $func: ident, $input: expr, $output: expr) => {
#[test]
fn $name () {
use super::*;
assert_eq!($func($input.as_bytes()).unwrap().1, $output);
}
}
}
macro_rules! ast_valid {
($name: ident, $func: ident, $input: expr) => {
#[test]
fn $name () {
use super::*;
assert!(match $func($input.as_bytes()).unwrap().1 {
_ => true,
});
}
}
}
macro_rules! ast_invalid {
($name: ident, $func: ident, $input: expr) => {
#[test]
#[should_panic]
fn $name () {
use super::*;
$func($input.as_bytes()).unwrap().1;
}
}
}
macro_rules! astb {
($name: ident, $($a: expr),*) => {
$name($(Box::new($a)),*)
};
}
macro_rules! ast {
($name: ident) => {
$name
};
($name: ident, $($a: expr),*) => {
$name($($a),*)
};
}
use function::parse_block;
pub use ast::ASTNode;
use std::io::Read;
pub mod ast;
pub mod op;
pub mod number;
pub mod exp;
pub mod string;
pub mod name;
pub mod var;
pub mod field;
pub mod statement;
pub mod function;
pub use nom::IResult;
use exp::parse_exp;
named!(pub parse_chunk<ASTNode>, dbg_dmp!(ws!(parse_exp)));
pub fn parse_string<'a, T: Into<&'a [u8]>>(s: T) -> Option<ASTNode> {
match parse_chunk(s.into()) {
IResult::Done(_, a) => Some(a),
_ => None
}
}
pub fn parse<T: Read>(mut s: T) -> Option<ASTNode> {
let mut buf = vec![];
s.read_to_end(&mut buf);
buf.pop();
match parse_chunk(&buf) {
IResult::Done(_, a) => Some(a),
_ => None
}
}