-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression.rs
More file actions
211 lines (188 loc) · 6.59 KB
/
expression.rs
File metadata and controls
211 lines (188 loc) · 6.59 KB
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
206
207
208
209
210
211
use noa_parser::acceptor::Acceptor;
use noa_parser::bytes::components::groups::GroupKind;
use noa_parser::bytes::matchers::match_pattern;
use noa_parser::bytes::primitives::number::Number;
use noa_parser::bytes::primitives::whitespace::OptionalWhitespaces;
use noa_parser::errors::{ParseError, ParseResult};
use noa_parser::matcher::{Match, MatchSize};
use noa_parser::peek::peek;
use noa_parser::recognizer::{Recognizable, Recognizer};
use noa_parser::scanner::Scanner;
use noa_parser::visitor::Visitor;
// ------------------------------------------------------------
// ExpressionInternal
// ------------------------------------------------------------
#[allow(dead_code)]
#[derive(Debug)]
enum ExpressionInternal {
Reducted(Reducted),
RightExpression(RightExpression),
}
// ------------------------------------------------------------
#[derive(Debug)]
struct Reducted {
lhs: usize,
op: BinaryOperator,
rhs: usize,
}
impl<'a> Visitor<'a, u8> for Reducted {
fn accept(scanner: &mut Scanner<'a, u8>) -> ParseResult<Self> {
OptionalWhitespaces::accept(scanner)?;
let lhs = Number::accept(scanner)?.0;
OptionalWhitespaces::accept(scanner)?;
let op = Recognizer::<u8, BinaryOperator>::new(scanner)
.try_or(BinaryOperator::Add)?
.try_or(BinaryOperator::Mul)?
.finish()
.ok_or(ParseError::UnexpectedToken)?;
OptionalWhitespaces::accept(scanner)?;
let rhs = Number::accept(scanner)?.0;
OptionalWhitespaces::accept(scanner)?;
Ok(Reducted { lhs, op, rhs })
}
}
// ------------------------------------------------------------
// +++ RightExpression
// ------------------------------------------------------------
#[derive(Debug)]
struct RightExpression {
lhs: usize,
op: BinaryOperator,
rhs: Box<Expression>,
}
impl<'a> Visitor<'a, u8> for RightExpression {
fn accept(scanner: &mut Scanner<'a, u8>) -> ParseResult<Self> {
OptionalWhitespaces::accept(scanner)?;
let lhs = Number::accept(scanner)?.0;
OptionalWhitespaces::accept(scanner)?;
let op = Recognizer::<u8, BinaryOperator>::new(scanner)
.try_or(BinaryOperator::Add)?
.try_or(BinaryOperator::Mul)?
.finish()
.ok_or(ParseError::UnexpectedToken)?;
OptionalWhitespaces::accept(scanner)?;
let rhs = Expression::accept(scanner)?;
OptionalWhitespaces::accept(scanner)?;
Ok(RightExpression {
lhs,
op,
rhs: Box::new(rhs),
})
}
}
// ------------------------------------------------------------
// BinaryOperator
// ------------------------------------------------------------
#[derive(Debug)]
enum BinaryOperator {
Add,
Mul,
}
impl Match<u8> for BinaryOperator {
fn matcher(&self, data: &[u8]) -> (bool, usize) {
match self {
BinaryOperator::Add => match_pattern(b"+", data),
BinaryOperator::Mul => match_pattern(b"*", data),
}
}
}
impl MatchSize for BinaryOperator {
fn size(&self) -> usize {
match self {
BinaryOperator::Add => 1,
BinaryOperator::Mul => 1,
}
}
}
impl<'a> Recognizable<'a, u8, BinaryOperator> for BinaryOperator {
fn recognize(self, scanner: &mut Scanner<'a, u8>) -> ParseResult<Option<BinaryOperator>> {
if scanner.is_empty() {
return Ok(None);
}
let (matched, size) = self.matcher(scanner.remaining());
if matched {
scanner.bump_by(size);
return Ok(Some(self));
}
Ok(None)
}
}
// ------------------------------------------------------------
// Expression
// ------------------------------------------------------------
/// Final result of the expression.
#[allow(dead_code)]
#[derive(Debug)]
enum Expression {
/// Both lhs and rhs are reduced.
Reduced {
lhs: usize,
op: BinaryOperator,
rhs: usize,
},
/// Only lhs is reduced.
RightExpression {
lhs: usize,
op: BinaryOperator,
rhs: Box<Expression>,
},
}
impl From<ExpressionInternal> for Expression {
fn from(value: ExpressionInternal) -> Self {
match value {
ExpressionInternal::Reducted(reduced) => Expression::Reduced {
lhs: reduced.lhs,
op: reduced.op,
rhs: reduced.rhs,
},
ExpressionInternal::RightExpression(right) => Expression::RightExpression {
lhs: right.lhs,
op: right.op,
rhs: right.rhs,
},
}
}
}
impl<'a> Visitor<'a, u8> for Expression {
fn accept(scanner: &mut Scanner<'a, u8>) -> ParseResult<Self> {
OptionalWhitespaces::accept(scanner)?;
// Check if there is a parenthesis
let result = peek(GroupKind::Parenthesis, scanner)?;
match result {
Some(peeked) => {
// Parse the inner expression
let mut inner_scanner = Scanner::new(peeked.peeked_slice());
let inner_result = Expression::accept(&mut inner_scanner)?;
scanner.bump_by(peeked.end_slice);
Ok(inner_result)
}
None => {
// Parse the reduced expression or the right expression
let accepted = Acceptor::new(scanner)
.try_or(ExpressionInternal::RightExpression)?
.try_or(ExpressionInternal::Reducted)?
.finish()
.ok_or(ParseError::UnexpectedToken)?;
Ok(accepted.into())
}
}
}
}
fn main() {
let data = b"1 + 2";
let mut scanner = Scanner::new(data);
let result = Expression::accept(&mut scanner);
println!("{:?}", result); // Ok(Reduced { lhs: 1, op: Add, rhs: 2 })
let data = b"1 + (2 * 3)";
let mut scanner = Scanner::new(data);
let result = Expression::accept(&mut scanner);
println!("{:?}", result); // Ok(RightExpression { lhs: 1, op: Add, rhs: Reduced { lhs: 2, op: Mul, rhs: 3 } })
let data = b"1 + (2 * 3 * ( 7 + 8))";
let mut scanner = Scanner::new(data);
let result = Expression::accept(&mut scanner);
println!("{:?}", result); //Ok(RightExpression { lhs: 1, op: Add, rhs: RightExpression { lhs: 2, op: Mul, rhs: RightExpression { lhs: 3, op: Mul, rhs: Reduced { lhs: 7, op: Add, rhs: 8 } } } })
let data = b"1 + 2 + 3";
let mut scanner = Scanner::new(data);
let result = Expression::accept(&mut scanner);
println!("{:?}", result); // Ok(RightExpression { lhs: 1, op: Add, rhs: Reduced { lhs: 2, op: Add, rhs: 3 } })
}