Files
boring-lang/src/grammar.lalrpop
2021-04-18 08:51:21 -06:00

99 lines
3.2 KiB
Plaintext

use std::str::FromStr;
use crate::ast;
use crate::types;
grammar;
pub LiteralInt: ast::LiteralInt = {
<literal:r"[0-9]+"> => ast::LiteralInt{value: i64::from_str(literal).unwrap()}
};
pub Identifier: ast::Identifier = {
<i:r"[A-Za-z][A-Za-z0-9_]*"> => ast::Identifier{name: i.to_string()}
};
pub FunctionCall: ast::FunctionCall = {
<i:Spanned<Identifier>> "(" <args:Comma<Spanned<Expression>>> ")" => ast::FunctionCall{name:i, arguments: args}
}
pub Expression: Box<ast::Expression> = {
<l:Spanned<Expression>> "+" <r:Spanned<Factor>> => Box::new(ast::Expression::Op(ast::Operation{left: l, op: ast::Operator::Plus, right: r})),
<l:Spanned<Expression>> "-" <r:Spanned<Factor>> => Box::new(ast::Expression::Op(ast::Operation{left: l, op: ast::Operator::Minus, right: r})),
Factor,
}
pub Factor: Box<ast::Expression> = {
<l:Spanned<Factor>> "*" <r:Spanned<Term>> => Box::new(ast::Expression::Op(ast::Operation{left: l, op: ast::Operator::Mul, right: r})),
<l:Spanned<Factor>> "/" <r:Spanned<Term>> => Box::new(ast::Expression::Op(ast::Operation{left: l, op: ast::Operator::Div, right: r})),
Term,
}
pub Term: Box<ast::Expression> = {
Spanned<LiteralInt> => Box::new(ast::Expression::LiteralInt(<>)),
Spanned<Identifier> => Box::new(ast::Expression::Identifier(<>)),
Spanned<FunctionCall> => Box::new(ast::Expression::FunctionCall(<>)),
"(" <e:Expression> ")" => e
}
pub LetStatement: ast::Assignment = {
//TODO: support destructuring with tuples, when they exist.
//TODO: add mut, weak
"let" <n:Spanned<Identifier>> "=" <e:Spanned<Expression>> => ast::Assignment{name: n, type_usage: None, expression: e},
"let" <n:Spanned<Identifier>> ":" <t:Spanned<TypeUsage>> "=" <e:Spanned<Expression>> => ast::Assignment{name: n, type_usage: Some(t), expression: e},
}
pub Statement: ast::Statement = {
<l:Spanned<LetStatement>> => ast::Statement::Assignment(l),
<e:Spanned<Expression>> => ast::Statement::Expression(e),
}
pub Block: ast::Block = {
"{" <s:LineDelimiter<Spanned<Statement>>> "}" => ast::Block{statements: s}
}
pub TypeUsage: ast::TypeUsage = {
<n:Spanned<Identifier>> => ast::TypeUsage{name: n, ty: types::SpecifiedType::Unknown}
}
pub VariableDeclaration: ast::VariableDeclaration = {
<i:Spanned<Identifier>> ":" <t:Spanned<TypeUsage>> => ast::VariableDeclaration{name: i, type_usage: t},
}
pub Function: ast::Function = {
"fn" <n:Spanned<Identifier>> "(" <args:Comma<VariableDeclaration>> ")" <rt:Spanned<TypeUsage>> <b:Block> => ast::Function{name: n, arguments: args, block: b, return_type: rt}
}
pub Module: ast::Module = {
<fs:Function*> => ast::Module{functions: fs}
}
// From https://lalrpop.github.io/lalrpop/tutorial/006_macros.html
// Comma seperated list of T with optional trailing comma
Comma<T>: Vec<T> = {
<v:(<T> ",")*> <e:T?> => match e {
None => v,
Some(e) => {
let mut v = v;
v.push(e);
v
}
}
};
LineDelimiter<T>: Vec<T> = {
<v:(<T> r"\n|;")*> <e:T> => {
let mut v = v;
v.push(e);
v
}
};
Spanned<Rule>: ast::Spanned<Rule> = {
<l: @L> <rule: Rule> <r: @R> => ast::Spanned{span: ast::Span{left: l, right: r}, value: rule}
};