Skip to content

Commit e9ebf56

Browse files
Move grammar file and fixup some test cases
1 parent b717695 commit e9ebf56

File tree

3 files changed

+39
-23
lines changed

3 files changed

+39
-23
lines changed

src/parser/grammar.rs

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use pest::{error::Error, iterators::Pairs, Parser};
22

33
#[derive(Parser)]
4-
#[grammar = "data/redcode.pest"]
4+
#[grammar = "parser/grammar/redcode.pest"]
55
struct Grammar;
66

77
pub fn parse(rule: Rule, input: &str) -> Result<Pairs<Rule>, Error<Rule>> {
@@ -15,6 +15,28 @@ mod tests {
1515

1616
use super::*;
1717

18+
// NOTE: these "doctests" are not actually compiled or run
19+
// since they are in a #[cfg(test)] module
20+
21+
/// A macro to assert on the way a certain input string parses
22+
/// Two forms are allowed. One has no identifier:
23+
/// ```
24+
/// match_parse!(Field {
25+
/// "123" | "4567" => [
26+
/// // This should look like the `tokens` field of `parses_to!`
27+
/// ],
28+
/// });
29+
/// ```
30+
///
31+
/// The other allows you to bind the input string so you can use it in your
32+
/// ```
33+
/// match_parse!(input, Field {
34+
/// "123" | "4567" => [
35+
/// // You can do something with e.g. `input.len()` here, which
36+
/// // will be either 3 or 4 depending on the test case
37+
/// ],
38+
/// });
39+
/// ```
1840
macro_rules! match_parse {
1941
($rule:ident $args:tt) => {
2042
match_parse!(_input, $rule $args)
@@ -78,14 +100,24 @@ mod tests {
78100
#[test]
79101
fn parse_label_expr() {
80102
match_parse!(Expr {
81-
"foo"| "fo2"| "f_2" => [
103+
"foo" | "fo2" | "f_2" => [
82104
Expr(0, 3, [
83105
Label(0, 3),
84106
]),
85107
]
86108
});
87109
}
88110

111+
#[test]
112+
fn parse_label() {
113+
match_parse!(label_input, LabelDeclaration {
114+
"some_label" | "some_label2" => [Label(0, label_input.len())],
115+
"a: " => [Label(0, 1)],
116+
" a " => [Label(1, 2)],
117+
"a :" => [Label(0, 1)],
118+
});
119+
}
120+
89121
#[test]
90122
fn parse_opcode_modifier() {
91123
match_parse!(input, Operation {
@@ -228,24 +260,4 @@ mod tests {
228260
],
229261
});
230262
}
231-
232-
#[test]
233-
fn parse_label() {
234-
for &(label_input, start, end) in [
235-
("some_label", 0, 10),
236-
("some_label2", 0, 11),
237-
("a: ", 0, 1),
238-
(" a ", 1, 2),
239-
("a :", 0, 1),
240-
]
241-
.iter()
242-
{
243-
parses_to! {
244-
parser: Grammar,
245-
input: label_input,
246-
rule: Rule::LabelDeclaration,
247-
tokens: [Label(start, end)]
248-
}
249-
}
250-
}
251263
}
File renamed without changes.

tests/data/test.red

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,8 @@ loop
3535
mov.x 1, 2
3636
mov.i 1, 2
3737

38-
jmp loop
38+
jmp loop
39+
40+
end foo bar baz
41+
42+
Some Junk Here mov.ba 1,2

0 commit comments

Comments
 (0)