Debian Codegraph @

tests/run-pass/arithmetic_infix_ast_span.rs

raw symbols SCIP
1 extern crate peg;
2
3 peg::parser!( grammar arithmetic() for str {
4 rule ident() -> &'input str = $(['a'..='z']+)
5
6 pub rule expression() -> Node = precedence!{
7 start:position!() node:@ end:position!() { Node { start, node, end} }
8 --
9 x:(@) "+" y:@ { Op::Add(Box::new(x), Box::new(y)) }
10 --
11 x:(@) "*" y:@ { Op::Mul(Box::new(x), Box::new(y)) }
12 --
13 i:ident() [' '|'\n']* { Op::Ident(i.to_owned()) }
14 }
15 });
16
17 #[derive(Debug, PartialEq, Eq, Clone)]
18 pub struct Node {
19 node: Op,
20 start: usize,
21 end: usize,
22 }
23
24 #[derive(Debug, PartialEq, Eq, Clone)]
25 pub enum Op {
26 Ident(String),
27 Add(Box<Node>, Box<Node>),
28 Mul(Box<Node>, Box<Node>),
29 }
30
31 fn main(){
32 assert_eq!(arithmetic::expression("a+b*c").unwrap(),
33 Node {
34 start: 0,
35 end: 5,
36 node: Op::Add(
37 Box::new(Node {
38 start: 0,
39 end: 1,
40 node: Op::Ident("a".into())
41 }),
42 Box::new(Node {
43 start: 2,
44 end: 5,
45 node: Op::Mul(
46 Box::new(Node {
47 start: 2,
48 end: 3,
49 node: Op::Ident("b".into())
50 }),
51 Box::new(Node {
52 start: 4,
53 end: 5,
54 node: Op::Ident("c".into())
55 })
56 )
57 })
58 )
59 }
60 );
61 }