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
147
148
149
150
151
152
153
154
155
156
157
158
159
#![warn(missing_docs)]
pub mod error;
#[doc(hidden)]
pub mod position;
#[doc(hidden)]
pub mod tokens;
use error::Error;
use position::{Pos, WithPos};
use tokens::{Token, Tokens, tokenize};
use tokens::Token::Eof;
use Command::*;
use Value::*;
macro_rules! peek {
($tokens:expr, { $($patterns:pat => $block:expr),* $(,)* }) => {
match $tokens.peek() {
$(Some(&Ok(WithPos { node: $patterns, .. })) => $block,)*
_ => Ok(None),
}
};
}
macro_rules! switch {
($tokens:expr, $expected:expr, { $($($patterns:pat)|* => $block:expr),* $(,)* }) => {
match $tokens.next() {
Some(Ok(WithPos { node: Eof, pos })) => return Err(Error::new(Eof.to_string(), $expected, pos)),
$($(Some(Ok(WithPos { node: $patterns, .. })))|* => $block,)*
Some(Err(error)) => return Err(error),
None => unreachable!(),
}
};
($tokens:expr, $expected:expr, $pos:ident, { $($($patterns:pat)|* => $block:expr),* $(,)* }) => {
match $tokens.next() {
Some(Ok(WithPos { node: Eof, pos })) => return Err(Error::new(Eof.to_string(), $expected, pos)),
$($(Some(Ok(WithPos { node: $patterns, pos: $pos })))|* => $block,)*
Some(Err(error)) => return Err(error),
None => unreachable!(),
}
};
}
#[derive(Debug, PartialEq)]
pub enum Command {
Set(String, Value),
}
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, PartialEq)]
pub enum Value {
Bool(bool),
Float(f64),
Int(i64),
Str(String),
}
pub fn parse(input: &str) -> Result<Vec<Command>> {
let mut tokens = tokenize(input);
let mut commands = vec![];
while let Some(command) = try!(line(&mut tokens)) {
commands.push(command);
}
try!(eof(&mut tokens));
Ok(commands)
}
fn check_ident(string: String, pos: &Pos) -> Result<String> {
if string.chars().all(|character| character.is_alphanumeric() || character == '-' || character == '_') {
Ok(string)
}
else {
Err(Error::new(string, "identifier".into(), pos.clone()))
}
}
fn eof(tokens: &mut Tokens) -> Result<()> {
match tokens.next() {
Some(Ok(WithPos { node: Eof, .. })) => Ok(()),
Some(Ok(token)) => Err(Error::new(token.to_string(), "command or comment".into(), token.pos)),
Some(Err(error)) => Err(error),
None => unreachable!(),
}
}
fn line(tokens: &mut Tokens) -> Result<Option<Command>> {
peek!(tokens, {
Token::Set => set_command(tokens).map(Some),
})
}
fn set_command(tokens: &mut Tokens) -> Result<Command> {
tokens.next();
let expected = "identifier".to_string();
let variable =
switch!(tokens, expected, pos, {
Token::Str(variable) => try!(check_ident(variable, &pos)),
token => return Err(Error::new(token.to_string(), expected, pos.clone())),
});
let value = try!(value(tokens));
Ok(Set(variable, value))
}
fn value(tokens: &mut Tokens) -> Result<Value> {
let expected = "value".to_string();
let val =
switch!(tokens, expected, pos, {
Token::Bool(boolean) => Bool(boolean),
Token::Float(num) => Float(num),
Token::Int(num) => Int(num),
Token::Str(string) | Token::QuotedStr(string) => Str(string),
token => return Err(Error::new(token.to_string(), expected, pos.clone())),
});
Ok(val)
}