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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
extern crate honeycomb;
use honeycomb::{
atoms::{any, eof, opt, rec, seq_no_ws, space, sym},
language::{array, identifier, number, string},
transform::to_number,
Parser,
};
use alloc::boxed::Box;
use alloc::vec::Vec;
use crate::tokens::{
Builtin, Expr, FnCall, Function, FunctionDef, Identifier, Literal, Name, Suite, Value,
};
pub fn string_literal() -> Parser<Literal> {
((space() >> string() << space()) - Literal::String) % "a string literal"
}
pub fn number_literal() -> Parser<Literal> {
((space() >> (number() - to_number) << space()) - Literal::Number) % "a number literal"
}
pub fn literal() -> Parser<Value> {
(string_literal() | number_literal()) - Value::Literal
}
pub fn builtin() -> Parser<Value> {
(((seq_no_ws("ls") | seq_no_ws("dir")) - |_| Builtin::List)
| ((seq_no_ws("clear") | seq_no_ws("cls")) - |_| Builtin::Clear)
| ((seq_no_ws("sh") | seq_no_ws("cmd")) - |_| Builtin::ShellOut)
| (seq_no_ws("mv") - |_| Builtin::Move)
| (seq_no_ws("cd") - |_| Builtin::ChangeDir)
| (seq_no_ws("rm") - |_| Builtin::Remove)
| (seq_no_ws("mkdir") - |_| Builtin::MakeDir)
| ((seq_no_ws("mkf") | seq_no_ws("touch")) - |_| Builtin::MakeFile)
| ((seq_no_ws("pwd") | seq_no_ws("cwd")) - |_| Builtin::WorkingDir)
| ((seq_no_ws("exit") | seq_no_ws("quit") | seq_no_ws("bye")) - |_| Builtin::Exit))
- Value::Builtin
}
pub fn ident() -> Parser<Identifier> {
((space() >> identifier() << space()) - Identifier) % "an identifier"
}
pub fn index_name(values: Parser<Value>) -> Parser<(Box<Value>, Vec<Value>)> {
((values & ((seq_no_ws("[") >> rec(value) << seq_no_ws("]")) * (1..)))
- |(head, indices)| (Box::new(head), indices))
% "a value followed by one or more indices"
}
pub fn dot_ident(values: Parser<Value>) -> Parser<(Box<Value>, Vec<Identifier>)> {
((values & ((sym('.') >> rec(ident)) * (1..)))
- |v: (Value, Vec<Identifier>)| (Box::new(v.0), v.1))
% "a dotted name"
}
pub fn name() -> Parser<Name> {
((dot_ident(group() | literal() | (ident() - Name::Name - Value::Name))
- |d| Name::DotName(d.0, d.1))
| (index_name(group() | literal() | (ident() - Name::Name - Value::Name))
- |d| Name::IndexName(d.0, d.1))
| (ident() - Name::Name))
% "a dotted name, an indexed value, or an identifier"
}
pub fn fncall() -> Parser<Value> {
(((builtin() & (rec(value) * (1..)))
- |call_data: (Value, Vec<Value>)| {
Value::FnCall(FnCall(Box::new(call_data.0), call_data.1))
})
| (((builtin() | (name() - Value::Name) | rec(group)) & array("(", rec(value), ")"))
- |call_data: (Value, Vec<Value>)| {
Value::FnCall(FnCall(Box::new(call_data.0), call_data.1))
}))
% "a value followed by comma arguments"
}
pub fn function() -> Parser<Function> {
(seq_no_ws("fn") >> (array("(", ident(), ")") & suite()))
- |(params, suite)| Function(params, suite)
}
pub fn function_def() -> Parser<FunctionDef> {
let body = array("(", ident(), ")") & rec(suite);
((seq_no_ws("fn") >> name() & body)
- |(n, (params, suite))| FunctionDef(n, Function(params, suite)))
% "a valid function definition"
}
pub fn group() -> Parser<Value> {
seq_no_ws("(") >> rec(value) << seq_no_ws(")")
}
pub fn flat_value() -> Parser<Value> {
literal()
}
pub fn recursive_value() -> Parser<Value> {
(function() - Value::Function) | rec(fncall) | builtin() | (name() - Value::Name) | rec(group)
}
pub fn value() -> Parser<Value> {
rec(recursive_value) | rec(flat_value)
}
pub fn assignment() -> Parser<Expr> {
((name() & (seq_no_ws("=") >> value())) - |(n, v)| Expr::Assignment(n, v))
% "a valid assignment"
}
pub fn while_loop() -> Parser<Expr> {
(((seq_no_ws("while") >> value()) & rec(suite)) - |(n, v)| Expr::WhileLoop(n, v))
% "a valid while loop"
}
pub fn if_then_else() -> Parser<Expr> {
((((seq_no_ws("if") >> value()) & rec(suite)) & opt(seq_no_ws("else") >> rec(suite)))
- |((condition, then_body), else_body_opt)| {
let else_body = match else_body_opt {
Some(body) => body,
None => Suite(Vec::new()),
};
Expr::IfThenElse(condition, then_body, else_body)
})
% "a valid if else statement"
}
pub fn expr() -> Parser<Expr> {
opt(comment() * (..))
>> (((assignment() << opt(seq_no_ws(";"))) % "a valid assignment")
| while_loop()
| if_then_else()
| (function_def() - Expr::FunctionDef)
| (((value() - Expr::Value) << opt(seq_no_ws(";"))) % "a value"))
<< opt(comment() * (..))
}
pub fn suite() -> Parser<Suite> {
((seq_no_ws("{") >> (expr() * (..)) << seq_no_ws("}")) - Suite)
% "a curly brace enclosed list of expressions"
}
pub fn comment() -> Parser<()> {
(seq_no_ws("#") >> ((sym('\n').isnt() >> any()) * (..))) - |_| ()
}
pub fn program() -> Parser<Suite> {
((expr() * (..)) - Suite) << eof()
}