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
peg::parser! {
pub grammar g_code() for str{
use super::super::token::*;
use super::super::ast::*;
use rust_decimal::Decimal;
pub rule newline() -> Newline = pos:position!() inner:(quiet!{ $("\r\n" / "\r" / "\n") } / expected!("newline")) {
Newline {
pos
}
};
pub rule dot() -> &'input str = quiet! { $(".") } / expected!("decimal point");
pub rule star() -> &'input str = quiet!{ $("*") } / expected!("checksum asterisk");
pub rule minus() -> &'input str = quiet!{ $("-") } / expected!("minus sign");
pub rule percent() -> &'input str = quiet! { $("%") } / expected!("percent sign");
rule quotation_mark() -> &'input str = quiet! { $("\"") } / expected!("quotation mark");
rule ascii_except_quote_or_newline() -> &'input str = quiet! { $(['\t' | ' '..='!'| '#'..='~']*) } / expected!("ASCII character except quote or newline");
pub rule string() -> &'input str = $(quotation_mark() ascii_except_quote_or_newline() ((quotation_mark() quotation_mark())+ ascii_except_quote_or_newline())* quotation_mark());
rule ascii_except_closing_parenthesis_or_newline() -> &'input str = quiet! { $(['\t' | ' '..='(' | '*'..='~']*) } / expected!("ASCII character except closing parenthesis or newline");
rule opening_parenthesis() -> &'input str = quiet! { $("(") } / expected!("opening parenthesis");
rule closing_parenthesis() -> &'input str = quiet! { $(")") } / expected!("closing parenthesis");
pub rule inline_comment() -> InlineComment<'input> = pos:position!() inner:$(opening_parenthesis() ascii_except_closing_parenthesis_or_newline() closing_parenthesis()) {
InlineComment {
inner,
pos,
}
};
rule ascii_character_except_newline() -> &'input str = quiet!{ $(['\t' | ' '..='~']*) } / expected!("ASCII character");
rule semicolon() -> &'input str = quiet! { $(";") } / expected!("semicolon");
pub rule comment() -> Comment<'input> = pos:position!() inner:$(semicolon() ascii_character_except_newline()) {
Comment {
inner,
pos,
}
};
pub rule integer() -> &'input str = quiet! { $(['0'..='9']+) } / expected!("integer");
pub rule letters() -> &'input str = quiet! { $(['a'..='z' | 'A'..='Z']+) } / expected!("letters");
pub rule whitespace() -> Whitespace<'input> = pos:position!() inner:(quiet! { $([' ' | '\t' ]+) } / expected!("whitespace")) {
Whitespace {
inner,
pos,
}
};
pub rule checksum() -> Checksum = left:position!() star:star() checksum:integer() right:position!() {?
Ok(Checksum {
inner: checksum.parse::<u8>().map_err(|e| "checksum is not byte-sized")?,
span: Span(left, right)
})
};
pub rule field() -> Field<'input>
= left:position!() letters:letters() neg:minus()? lhs:integer() dot:dot() rhs:integer()? right:position!() {?
let lhs_start = left + letters.len() + neg.as_ref().map(|_| 1).unwrap_or(0);
let lhs_end = lhs_start + lhs.len();
let rhs_start = lhs_end + 1;
let rhs_end = rhs_start + rhs.map(|x| x.len()).unwrap_or(0);
Ok(Field {
letters,
value: Value::Rational(lhs.parse::<Decimal>()
.map_err(|e| "integer part does not fit in an i64")
.and_then(|lhs| if let Some(rhs_str) = rhs {
rhs_str.parse::<i64>()
.map(|rhs| Decimal::new(rhs, rhs_str.len() as u32))
.map(|rhs| lhs + rhs)
.map(|value| if neg.is_some() { -value } else { value })
.map_err(|e| "fractional part does not fit in an i64")
} else {
Ok(lhs)
})?),
raw_value: if neg.is_some() { vec!["-", lhs, ".", rhs.unwrap_or("")] } else { vec![lhs, ".", rhs.unwrap_or("")] },
span: Span(left, right)
})
}
/ left:position!() letters:letters() neg:minus()? dot:dot() rhs_str:integer() right:position!() {?
let rhs_start = left + letters.len() + neg.as_ref().map(|_| 1).unwrap_or(0) + 1;
let rhs_end = right;
Ok(Field {
letters,
value: Value::Rational(rhs_str.parse::<i64>()
.map(|rhs| Decimal::new(rhs, rhs_str.len() as u32))
.map(|rhs| if neg.is_some() { -rhs } else { rhs })
.map_err(|e| "fractional part does not fit in an i64")?),
raw_value: if neg.is_some() { vec!["-", ".", rhs_str] } else { vec![".", rhs_str] },
span: Span(left, right)
})
}
/ left:position!() letters:letters() value:integer() right:position!() {?
Ok(Field {
letters,
value: Value::Integer(value.parse::<usize>().map_err(|e| "integer does not fit in usize")?),
raw_value: vec![value],
span: Span(left, right)
})
}
/ left:position!() letters:letters() minus:minus() value:integer() right:position!() {?
let value_start = left + letters.len() + 1;
let value_end = right;
Ok(Field {
letters,
value: Value::Rational(-value.parse::<Decimal>().map_err(|e| "integer does not fit in i64")?),
raw_value: vec!["-", value],
span: Span(left, right)
})
}
/ left:position!() letters:letters() string:string() right:position!() {
Field {
letters,
value: Value::String(string),
raw_value: vec![string],
span: Span(left, right)
}
};
rule line_component() -> LineComponent<'input>
= field:field() { LineComponent { field: Some(field), ..Default::default() } }
/ whitespace:whitespace() { LineComponent { whitespace: Some(whitespace), ..Default::default() } }
/ inline_comment:inline_comment() { LineComponent { inline_comment: Some(inline_comment), ..Default::default() } };
pub rule line() -> Line<'input> =
left:position!()
line_components:line_component()*
checksum:checksum()?
comment:comment()?
right:position!() {
Line {
line_components,
checksum,
comment,
span: Span(left, right)
}
};
pub rule file_parser() -> File<'input>
= left:position!() start_percent:percent() lines:(a:line() b:newline() { (a, b) })* last_line:line() end_percent:percent() right:position!() {
File {
start_percent: true,
lines,
last_line: if last_line.line_components.is_empty() && last_line.checksum.is_none() && last_line.comment.is_none() {
None
} else {
Some(last_line)
},
end_percent: true,
span: Span(left, right)
}
}
/ left:position!() lines:(a:line() b:newline() { (a, b) })* last_line:line() right:position!() {
File {
start_percent: false,
lines,
last_line: if last_line.line_components.is_empty() && last_line.checksum.is_none() && last_line.comment.is_none() {
None
} else {
Some(last_line)
},
end_percent: false,
span: Span(left, right)
}
};
pub rule snippet_parser() -> Snippet<'input> = left:position!() lines:(a:line() b:newline() { (a, b) })* last_line:line() right:position!() {
Snippet {
lines,
last_line: if last_line.line_components.is_empty() && last_line.checksum.is_none() && last_line.comment.is_none() {
None
} else {
Some(last_line)
},
span: Span(left, right)
}
}
}
}