summary refs log tree commit diff
path: root/18/src/expression.lalrpop
blob: 222a7e64488a7551882f9a75d86c002840e4bedb (plain)
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
grammar;

pub Expression: i64 = {
  <Term>,
  <left:Expression> PLUS <right:Term> => {
    left + right
  },
  <left:Expression> STAR <right:Term> => {
    left * right
  },
};

Term: i64 = {
  NUMBER => {
    <>.parse::<i64>().unwrap()
  },
  LEFT_PARENTHESIS <Expression> RIGHT_PARENTHESIS,
};

pub ExpressionPro: i64 = {
  <Expression2Pro>,
  <left:ExpressionPro> STAR <right:Expression2Pro> => {
    left * right
  },
};

Expression2Pro: i64 = {
  <TermPro>,
  <left:Expression2Pro> PLUS <right:TermPro> => {
    left + right
  },
};

TermPro: i64 = {
  NUMBER => {
    <>.parse::<i64>().unwrap()
  },
  LEFT_PARENTHESIS <ExpressionPro> RIGHT_PARENTHESIS,
};

match {
  r"\p{Zs}+" => { },

  r"[0-9]+" => NUMBER,

  "+" => PLUS,

  "*" => STAR,

  "(" => LEFT_PARENTHESIS,

  ")" => RIGHT_PARENTHESIS,
}