eww/src/parser.lalrpop
2021-07-05 20:09:18 +02:00

56 lines
1.3 KiB
Text

use std::str::FromStr;
use crate::lexer::{Token, LexicalError};
use crate::expr::{Expr, Span};
grammar(file_id: usize);
extern {
type Location = usize;
type Error = LexicalError;
enum Token {
"(" => Token::LPren,
")" => Token::RPren,
"true" => Token::True,
"false" => Token::False,
"string" => Token::StrLit(<String>),
"number" => Token::NumLit(<String>),
"symbol" => Token::Symbol(<String>),
"keyword" => Token::Keyword(<String>),
"comment" => Token::Comment,
}
}
pub Expr: Expr = {
<l:@L> "(" <elems:(<Expr>)+> ")" <r:@R> => Expr::List(Span(l, r, file_id), elems),
<x:Keyword> => x,
<x:Symbol> => x,
<l:@L> <x:Value> <r:@R> => Expr::Value(Span(l, r, file_id), x),
<l:@L> "comment" <r:@R> => Expr::Comment(Span(l, r, file_id)),
};
Keyword: Expr = <l:@L> <x:"keyword"> <r:@R> => Expr::Keyword(Span(l, r, file_id), x.to_string());
Symbol: Expr = <l:@L> <x:"symbol"> <r:@R> => Expr::Symbol(Span(l, r, file_id), x.to_string());
Value: String = {
<StrLit> => <>,
<Num> => <>,
<Bool> => <>,
};
StrLit: String = {
<x:"string"> => {
x[1..x.len() - 1].to_owned()
},
};
Num: String = <"number"> => <>.to_string();
Bool: String = {
"true" => "true".to_string(),
"false" => "false".to_string(),
}
// vim:shiftwidth=4