summaryrefslogtreecommitdiff
path: root/src/taiga.lalrpop
blob: 6bf750d139a9f8ddee66035ba0087213f641d278 (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
55
use std::str::FromStr;
use ast::{self, WithLocation};

grammar;

pub Program: ast::Program = {
    Expression => ast::Program(<>),
};

Expression: Box<ast::WithLocation<ast::Expression>> = {
    <e1:Expression> "+" <e2:ExpressionMul> => Box::new(e1.join_map(*e2, |v1, v2| {
        ast::Expression::BinOp(ast::BinOp::Add, Box::new(v1), Box::new(v2))
    })),
    <e1:Expression> "-" <e2:ExpressionMul> => Box::new(e1.join_map(*e2, |v1, v2| {
        ast::Expression::BinOp(ast::BinOp::Sub, Box::new(v1), Box::new(v2))
    })),
    <ExpressionMul> => <>,
};

ExpressionMul: Box<ast::WithLocation<ast::Expression>> = {
    <e1:ExpressionMul> "*" <e2:ExpressionSign> => Box::new(e1.join_map(*e2, |v1, v2| {
        ast::Expression::BinOp(ast::BinOp::Mul, Box::new(v1), Box::new(v2))
    })),
    <e1:ExpressionMul> "/" <e2:ExpressionSign> => Box::new(e1.join_map(*e2, |v1, v2| {
        ast::Expression::BinOp(ast::BinOp::Div, Box::new(v1), Box::new(v2))
    })),
    <e1:ExpressionMul> "//" <e2:ExpressionSign> => Box::new(e1.join_map(*e2, |v1, v2| {
        ast::Expression::BinOp(ast::BinOp::FloorDiv, Box::new(v1), Box::new(v2))
    })),
    <ExpressionSign> => <>,
};

ExpressionSign: Box<ast::WithLocation<ast::Expression>> = {
    <l: @L> "+" <e: ExpressionBase> <r: @R> => Box::new(WithLocation::new(
        ast::Expression::UnaryOp(ast::UnaryOp::Pos, e),
        l, r
    )),
    <l: @L> "-" <e: ExpressionBase> <r: @R> => Box::new(WithLocation::new(
        ast::Expression::UnaryOp(ast::UnaryOp::Neg, e),
        l, r
    )),
    <ExpressionBase> => <>,
};

ExpressionBase: Box<ast::WithLocation<ast::Expression>> = {
    Num => Box::new(<>.map(|v| ast::Expression::Number(v))),
    Spanned<r"nil"> => Box::new(<>.map(|v| ast::Expression::Nil)),
    "(" <Expression> ")" => <>,
};

Num: WithLocation<u64> = <e: Spanned<r"[0-9]+">> => e.map(|v| u64::from_str(v).unwrap());

Spanned<T>: WithLocation<T> = {
    <l: @L> <v: T> <r: @R> => WithLocation::new(v, l, r)
};