Files
bang/src/syntax/parser.lalrpop
2025-09-06 20:40:18 -07:00

71 lines
2.1 KiB
Plaintext

use crate::syntax::*;
use crate::syntax::error::ParserError;
use crate::syntax::tokens::*;
grammar(file_id: usize);
extern {
type Location = usize;
type Error = ParserError;
enum Token {
"(" => Token::OpenParen,
")" => Token::CloseParen,
"[" => Token::OpenSquare,
"]" => Token::CloseSquare,
"{" => Token::OpenBrace,
"}" => Token::CloseBrace,
";" => Token::Semi,
":" => Token::Colon,
"," => Token::Comma,
"`" => Token::BackTick,
"\\" => Token::Lambda(_),
"->" => Token::Arrow,
"<constructor>" => Token::TypeName(<String>),
"<value>" => Token::ValueName(<String>),
"<op>" => Token::OperatorName(<String>),
"<prim_constructor>" => Token::PrimitiveTypeName(<String>),
"<prim_value>" => Token::PrimitiveValueName(<String>),
"<integer>" => Token::Integer(<IntegerWithBase>),
"<char>" => Token::Character(<char>),
"<string>" => Token::String(<String>),
}
}
pub Type: Type = {
FunctionType,
}
FunctionType: Type = {
TypeApplication,
<argtype:FunctionType> "->" <ret:TypeApplication> =>
Type::Function(Box::new(argtype), Box::new(ret)),
}
TypeApplication: Type = {
BaseType,
<s:@L> <c:"<constructor>"> <e:@L> <arguments: BaseType*> => {
let constructor = Type::Constructor(Location::new(file_id, s..e), c);
Type::Application(Box::new(constructor), arguments)
},
<s:@L> <c:"<prim_constructor>"> <e:@L> <arguments: BaseType*> => {
let constructor = Type::Constructor(Location::new(file_id, s..e), c);
Type::Application(Box::new(constructor), arguments)
},
}
BaseType: Type = {
<s:@L> <v:"<value>"> <e:@L> =>
Type::Variable(Location::new(file_id, s..e), v),
<s:@L> <p: "<prim_value>"> <e:@L> =>
Type::Primitive(Location::new(file_id, s..e), p),
"(" <t:Type> ")" => t,
}
pub ConstantValue: ConstantValue = {
<s:@L> <x:"<integer>"> <e:@L> => ConstantValue::Integer(Location::new(file_id, s..e), x),
<s:@L> <x:"<char>"> <e:@L> => ConstantValue::Character(Location::new(file_id, s..e), x),
<s:@L> <x:"<string>"> <e:@L> => ConstantValue::String(Location::new(file_id, s..e), x),
}