Skip to content

Instantly share code, notes, and snippets.

@fero23
Created November 30, 2016 21:38
Show Gist options
  • Save fero23/9c34caaf34eb3d06d9fff4e16a3d7400 to your computer and use it in GitHub Desktop.
Save fero23/9c34caaf34eb3d06d9fff4e16a3d7400 to your computer and use it in GitHub Desktop.
Test grammar:
glurp_grammar! {
R: String => { Optional(R) ~ Char('R') } on {|opt, r| {
if Some(acc) = opt {
push(r);
acc
} else {
String::new()
}
}};
}
Test:
let mut parser = Parser::new();
parser.start_from(R);
println!("{:#?}", parser.parse("RRRRRR"));
Result:
Ok(
[
Node {
rule: 1,
alt: 0,
children: [
Node {
rule: 2,
alt: 0,
children: [
Node {
rule: 1,
alt: 0,
children: [
Node {
rule: 2,
alt: 0,
children: [
Node {
rule: 1,
alt: 0,
children: [
Node {
rule: 2,
alt: 0,
children: [
Node {
rule: 1,
alt: 0,
children: [
Node {
rule: 2,
alt: 0,
children: [
Node {
rule: 1,
alt: 0,
children: [
Node {
rule: 2,
alt: 0,
children: [
Node {
rule: 1,
alt: 0,
children: [
EmptyNode,
Node {
rule: 3,
alt: 0,
children: [
Leaf(
'R'
)
]
}
]
}
]
},
Node {
rule: 3,
alt: 0,
children: [
Leaf(
'R'
)
]
}
]
}
]
},
Node {
rule: 3,
alt: 0,
children: [
Leaf(
'R'
)
]
}
]
}
]
},
Node {
rule: 3,
alt: 0,
children: [
Leaf(
'R'
)
]
}
]
}
]
},
Node {
rule: 3,
alt: 0,
children: [
Leaf(
'R'
)
]
}
]
}
]
},
Node {
rule: 3,
alt: 0,
children: [
Leaf(
'R'
)
]
}
]
}
]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment