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
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//! Collection of all parsers and structs which represent tokens, operators and keywords

use crate::parser::literals::sp;
use crate::parser::{Res, Span};

use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::error::context;
use nom::sequence::preceded;

/// Reserved keywords for the liva lang, which can not be used as identifiers
pub const KEYWORDS: [&str; 14] = [
    "return", "class", "end", "fun", "do", "while", "for", "if", "let", "in", "else", "external",
    "as", "import",
];

lazy_static! {

    /// Vector of unary operators which show the precedence of the operators
    /// with which unary expressions should be evaluated
    pub static ref UNOPS: Vec<UnOperator> = {
        let table = &mut [UnOperator::Sub, UnOperator::Not];
        table.sort();
        table.to_vec()
    };

    /// Vector of binary operators which show the precedence of the operators
    /// with which binary expressions should be evaluated
    pub static ref BINOP_PRECEDENCE: Vec<Vec<Operator>> = {
        let table: &mut [&mut [Operator]] = &mut [
            &mut [Operator::Pow],
            &mut [Operator::Mul, Operator::Div, Operator::Mod],
            &mut [Operator::Add, Operator::Sub],
            // &mut [Operator::Concat],
            // &mut [Operator::BitShl, BinOp::BitShr],
            // &mut [Operator::BitAnd],
            // &mut [Operator::BitXor],
            // &mut [Operator::BitOr],
            &mut [
                Operator::Lt,
                Operator::Gt,
                Operator::Leq,
                Operator::Geq,
                Operator::Neq,
                Operator::EQ,
            ],
            &mut [Operator::And],
            &mut [Operator::Or],
        ];
        let mut acc = Vec::new();
        for s in table.iter_mut() {
            s.sort();
            acc.push(s.to_vec());
        }
        acc
    };
}

macro_rules! define_token {
    ( $( { $fn_name:ident, $name:literal, $token:literal } ), *) => {
        $(
            /// Macro generated function
            /// Token parser to parse token
            pub fn $fn_name(input: Span) -> Res<Span> {
                context(
                    $name,
                    tag($token)
                )(input)
            }
        )*
    }
}

define_token! {
    {add, "Add", "+"},
    {sub, "Sub", "-"},
    {mul, "Mul", "*"},
    {div, "Div", "/"},
    {modulo, "Modulu", "%"},
    {equal, "Equal", "=="},
    {unequal, "Unequal", "!="},
    {dot, "Dot", "."},
    {comma, "Comma", ","},
    {left_paren, "LeftParen", "("},
    {right_paren, "RightParen", ")"},
    {left_bracket, "LeftBracket", "["},
    {right_bracket, "RightBracket", "]"},
    {less_than, "LessThan", "<"},
    {greater_than, "GreaterThan", ">"},
    {less_eq_than, "LessEqThan", "<="},
    {greater_eq_than, "GreaterEqThan", ">="},
    {comment, "Comment", "//"},
    {newline, "Newline", "\n"}
}

define_token! {
    {lreturn, "Return", "return"},
    {class, "Class", "class"},
    {end, "End", "end"},
    {fun, "Fun", "fun"},
    {ldo, "Do", "do"},
    {lwhile, "While", "while"},
    {lfor, "For", "for"},
    {lif, "If", "if"},
    {lelse, "Else", "else"},
    {llet, "Let", "let"},
    {lin, "In", "in"},
    {lor, "Or", "or"},
    {land, "And", "and"},
    {external, "External", "external"},
    {import, "Import", "import"},
    {las, "As", "as"}
}

/// All Operators which are used in the language
/// for binary expressions
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum Operator {
    Add,
    Sub,
    Div,
    Mul,
    Pow,
    Mod,
    EQ,
    And,
    Lt,
    Gt,
    Or,
    Leq,
    Geq,
    Neq,
}

/// All Operators which are used in the language
/// for unary expressions
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum UnOperator {
    Add,
    Sub,
    Not,
}

pub(crate) fn parse_unary_operator(input: Span) -> Res<UnOperator> {
    context("UnaryOperator", preceded(sp, alt((add, sub, mul, div))))(input).map(
        |(next_input, res)| {
            match *res.fragment() {
                "+" => (next_input, UnOperator::Add),
                "-" => (next_input, UnOperator::Sub),
                "not" => (next_input, UnOperator::Not),
                // This should never reach!
                _ => (next_input, UnOperator::Add),
            }
        },
    )
}

pub(crate) fn parse_binary_operator(input: Span) -> Res<Operator> {
    context(
        "Operator",
        preceded(
            sp,
            alt((
                add,
                sub,
                mul,
                div,
                equal,
                unequal,
                less_than,
                greater_than,
                greater_eq_than,
                less_eq_than,
                land,
                modulo,
            )),
        ),
    )(input)
    .map(|(next_input, res)| {
        match *res.fragment() {
            "+" => (next_input, Operator::Add),
            "-" => (next_input, Operator::Sub),
            "*" => (next_input, Operator::Mul),
            "/" => (next_input, Operator::Div),
            "%" => (next_input, Operator::Mod),
            "==" => (next_input, Operator::EQ),
            "<" => (next_input, Operator::Lt),
            ">" => (next_input, Operator::Gt),
            "<=" => (next_input, Operator::Leq),
            ">=" => (next_input, Operator::Geq),
            "and" => (next_input, Operator::And),
            // This should never reach!
            _ => (next_input, Operator::Add),
        }
    })
}

pub(crate) fn parse_tokens(input: Span) -> Res<Span> {
    alt((
        add,
        sub,
        mul,
        div,
        dot,
        equal,
        unequal,
        left_paren,
        right_paren,
        less_than,
        greater_than,
        less_eq_than,
        greater_eq_than,
        modulo,
    ))(input)
}