diff options
Diffstat (limited to 'crates/rebel-parse/src/grammar')
-rw-r--r-- | crates/rebel-parse/src/grammar/mod.rs | 3 | ||||
-rw-r--r-- | crates/rebel-parse/src/grammar/recipe.rs | 277 | ||||
-rw-r--r-- | crates/rebel-parse/src/grammar/task_ref.rs | 65 | ||||
-rw-r--r-- | crates/rebel-parse/src/grammar/tokenize.rs | 137 |
4 files changed, 482 insertions, 0 deletions
diff --git a/crates/rebel-parse/src/grammar/mod.rs b/crates/rebel-parse/src/grammar/mod.rs new file mode 100644 index 0000000..de06991 --- /dev/null +++ b/crates/rebel-parse/src/grammar/mod.rs @@ -0,0 +1,3 @@ +pub mod recipe; +pub mod task_ref; +pub mod tokenize; diff --git a/crates/rebel-parse/src/grammar/recipe.rs b/crates/rebel-parse/src/grammar/recipe.rs new file mode 100644 index 0000000..81b47c9 --- /dev/null +++ b/crates/rebel-parse/src/grammar/recipe.rs @@ -0,0 +1,277 @@ +use std::borrow::Cow; + +use crate::{ + ast::{ + self, + expr::{self, Expr}, + pat::{DestrPat, Pat}, + typ::{self, Type}, + }, + token::*, +}; + +pub use rules::*; + +peg::parser! { + pub grammar rules<'a>() for TokenStream<'a> { + use expr::OpBinary::*; + use expr::OpUnary::*; + + pub rule recipe() -> ast::Recipe<'a> + = recipe:recipe_stmt()* { recipe } + + pub rule recipe_stmt() -> ast::RecipeStmt<'a> + = [Token::Keyword(Keyword::Fetch)] name:ident() p('{') entries:delimited(<struct_field()>, <p(',')>) p('}') { + ast::RecipeStmt::Fetch { name, entries } + } + / [Token::Keyword(Keyword::Task)] name:ident() p('(') params:func_params() p(')') + p('{') block:block() p('}') { + ast::RecipeStmt::Task { name, params, block } + } + / stmt:block_stmt() p(';') { + ast::RecipeStmt::BlockStmt(stmt) + } + + pub rule block() -> ast::Block<'a> + = block:block_stmt() ++ p(';') { ast::Block(block) } + + pub rule block_stmt() -> ast::BlockStmt<'a> + = [Token::Keyword(Keyword::Let)] dest:typed_pat() p('=') expr:expr() { + ast::BlockStmt::let_assign(dest, Some(expr)) + } + / [Token::Keyword(Keyword::Let)] dest:typed_pat() { + ast::BlockStmt::let_assign(dest, None) + } + / [Token::Keyword(Keyword::Fn)] ident:ident() p('(') params:func_params() p(')') + ret:tagged(<p2('-', '>')>, <typ()>)? p('{') block:block() p('}') + { + ast::BlockStmt::Fn { + ident, + params, + ret: ret.map(Box::new), + block, + } + } + / dest:destr_pat() op:assign_op() expr:expr() { + ast::BlockStmt::assign(dest, op, false, expr) + } + / dest:destr_pat() p2('=', '+') expr:expr() { + ast::BlockStmt::assign(dest, Some(Add), true, expr) + } + / expr:expr() { + ast::BlockStmt::Expr { expr: Box::new(expr) } + } + / { ast::BlockStmt::Empty } + + rule assign_op() -> Option<expr::OpBinary> + = p('=') { None } + / p2('+', '=') { Some(Add) } + / p2('-', '=') { Some(Sub) } + / p2('*', '=') { Some(Mul) } + / p2('/', '=') { Some(Div) } + / p2('%', '=') { Some(Rem) } + + rule typed_pat() -> ast::TypedPat<'a> + = pat:pat() typ:tagged(<p(':')>, <typ()>)? { ast::TypedPat { pat, typ } } + + pub rule typ() -> Type<'a> = precedence! { + t:@ p('?') { Type::Option(Box::new(t)) } + -- + t:typ_atom() { t } + } + + rule typ_atom() -> Type<'a> + = p('(') t:typ() p(')') { Type::Paren(Box::new(t)) } + / lit:typ_literal() { Type::Literal(lit) } + / path:path() { Type::Path(path) } + + rule typ_literal() -> typ::Literal<'a> + = p('(') p(')') { typ::Literal::Unit } + / p('(') elements:(typ() ++ p(',')) p(',')? p(')') { + typ::Literal::Tuple(elements) + } + / p('[') typ:typ() p(']') { + typ::Literal::Array(Box::new(typ)) + } + / [Token::Keyword(Keyword::Map)] p('{') key:typ() p2('=', '>') value:typ() p('}') { + typ::Literal::Map(Box::new(key), Box::new(value)) + } + / p('{') entries:delimited(<struct_field_typ()>, <p(',')>) p('}') { + typ::Literal::Struct(entries) + } + + pub rule pat() -> ast::pat::Pat<'a> + = p('(') pat:pat() p(')') { Pat::Paren(Box::new(pat)) } + / ident:ident() { Pat::Ident(ident) } + + pub rule destr_pat() -> DestrPat<'a> = precedence! { + base:@ p('[') index:expr() p(']') { + DestrPat::Index { base: Box::new(base), index: Box::new(index) } + } + -- + base:@ p('.') field:field() { + DestrPat::Field { base: Box::new(base), field } + } + -- + p('(') pat:destr_pat() p(')') { DestrPat::Paren(Box::new(pat)) } + path:path() { DestrPat::Path(path) } + } + + rule struct_field_typ() -> typ::StructField<'a> + = field:field() p(':') typ:typ() { + typ::StructField { name: field.name, typ } + } + + pub rule expr() -> Expr<'a> = precedence! { + left:(@) p2('|', '|') right:@ { Expr::binary(left, Or, right) } + -- + left:(@) p2('&', '&') right:@ { Expr::binary(left, And, right) } + -- + left:(@) p2('=', '=') right:@ { Expr::binary(left, Eq, right) } + left:(@) p2('!', '=') right:@ { Expr::binary(left, Ne, right) } + left:(@) p('<') right:@ { Expr::binary(left, Lt, right) } + left:(@) p('>') right:@ { Expr::binary(left, Gt, right) } + left:(@) p2('<', '=') right:@ { Expr::binary(left, Le, right) } + left:(@) p2('>', '=') right:@ { Expr::binary(left, Ge, right) } + -- + left:(@) p('+') right:@ { Expr::binary(left, Add, right) } + left:(@) p('-') right:@ { Expr::binary(left, Sub, right) } + -- + left:(@) p('*') right:@ { Expr::binary(left, Mul, right) } + left:(@) p('/') right:@ { Expr::binary(left, Div, right) } + left:(@) p('%') right:@ { Expr::binary(left, Rem, right) } + -- + p('-') expr:@ { Expr::unary(Neg, expr) } + p('!') expr:@ { Expr::unary(Not, expr) } + -- + expr:@ p('(') params:call_params() p(')') { + Expr::apply(expr, params) + } + base:@ p('[') index:expr() p(']') { Expr::index(base, index) } + -- + expr:@ p('.') method:field() p('(') params:call_params() p(')') { + Expr::method(expr, method, params) + } + base:@ p('.') field:field() { Expr::field(base, field) } + -- + e:atom() { e } + } + + rule atom() -> Expr<'a> + = p('(') e:expr() p(')') { Expr::paren(e) } + / [Token::Keyword(Keyword::If)] + if_blocks:(cond_block() ++ ([Token::Keyword(Keyword::Else)] [Token::Keyword(Keyword::If)])) + else_block:([Token::Keyword(Keyword::Else)] p('{') block:block() p('}') { Box::new(block) })? + { + Expr::IfElse { if_blocks, else_block } + } + / lit:literal() { Expr::Literal(lit) } + / p('{') block:block() p('}') { Expr::Block(block) } + / path:path() { Expr::Path(path) } + + rule cond_block() -> (Expr<'a>, ast::Block<'a>) + = cond:expr() p('{') block:block() p('}') { (cond, block) } + + rule call_params() -> Vec<expr::Expr<'a>> + = args:delimited(<expr()>, <p(',')>) { args } + + rule func_params() -> Vec<ast::FuncParam<'a>> + = params:delimited(<func_param()>, <p(',')>) { params } + + rule func_param() -> ast::FuncParam<'a> + = name:ident() p(':') typ:typ() { ast::FuncParam { name, typ } } + + rule literal() -> expr::Literal<'a> + = [Token::Keyword(Keyword::True)] { expr::Literal::Bool(true) } + / [Token::Keyword(Keyword::False)] { expr::Literal::Bool(false) } + / [Token::Keyword(Keyword::None)] { expr::Literal::None } + / n:number() { expr::Literal::Int(n) } + / [Token::Str(Str { pieces, kind })] { ? + let pieces = pieces + .iter() + .map(|piece| piece.try_into()) + .collect::<Result<_, _>>()?; + Ok(expr::Literal::Str{ pieces, kind: *kind }) + } + / p('(') p(')') { expr::Literal::Unit } + / p('(') elements:(expr() ++ p(',')) p(',')? p(')') { + expr::Literal::Tuple(elements) + } + / p('[') elements:delimited(<expr()>, <p(',')>) p(']') { + expr::Literal::Array(elements) + } + / [Token::Keyword(Keyword::Map)] p('{') entries:delimited(<map_entry()>, <p(',')>) p('}') { + expr::Literal::Map(entries) + } + / p('{') entries:delimited(<struct_field()>, <p(',')>) p('}') { + expr::Literal::Struct(entries) + } + + rule map_entry() -> expr::MapEntry<'a> + = key:expr() p2('=', '>') value:expr() { + expr::MapEntry { key, value } + } + + rule struct_field() -> expr::StructField<'a> + = field:field() value:tagged(<p(':')>, <expr()>)? { + expr::StructField::new(field, value) + } + + rule path() -> ast::Path<'a> + = components:(ident() ++ p2(':', ':')) { + ast::Path { root: ast::PathRoot::Relative, components } + } + / components:(p2(':', ':') ident:ident() { ident })+ { + ast::Path { root: ast::PathRoot::Absolute, components } + } + / [Token::Keyword(Keyword::Recipe)] components:(p2(':', ':') ident:ident() { ident })* { + ast::Path { root: ast::PathRoot::Recipe, components } + } + / [Token::Keyword(Keyword::Task)] components:(p2(':', ':') ident:ident() { ident })* { + ast::Path { root: ast::PathRoot::Task, components } + } + + rule field() -> ast::Ident<'a> + = ident() + / [Token::Number(content)] { + ast::Ident { name: Cow::Borrowed(content) } + } + + rule number() -> i64 + = neg:p('-')? [Token::Number(s)] { ? + let (radix, rest) = if let Some(rest) = s.strip_prefix("0x") { + (16, rest) + } else if let Some(rest) = s.strip_prefix("0o") { + (8, rest) + } else if let Some(rest) = s.strip_prefix("0b") { + (2, rest) + } else { + (10, *s) + }; + let mut digits = rest.replace('_', ""); + if neg.is_some() { + digits = format!("-{digits}"); + } + i64::from_str_radix(&digits, radix).or(Err("number")) + } + + rule p_(ch: char) + = [Token::Punct(Punct(c, Spacing::Joint)) if *c == ch] {} + + rule p(ch: char) -> () + = [Token::Punct(Punct(c, _)) if *c == ch] {} + + rule p2(ch1: char, ch2: char) -> () + = p_(ch1) p(ch2) + + rule ident() -> ast::Ident<'a> + = [Token::Ident(name)] { ast::Ident { name: Cow::Borrowed(name) } } + + rule delimited<T>(expr: rule<T>, delim: rule<()>) -> Vec<T> + = values:(expr() ++ delim()) delim()? { values } + / { Vec::new() } + + rule tagged<T>(tag: rule<()>, value: rule<T>) -> T + = tag() v:value() { v } + } +} diff --git a/crates/rebel-parse/src/grammar/task_ref.rs b/crates/rebel-parse/src/grammar/task_ref.rs new file mode 100644 index 0000000..77d6c5f --- /dev/null +++ b/crates/rebel-parse/src/grammar/task_ref.rs @@ -0,0 +1,65 @@ +pub use rules::*; + +use rebel_common::types::TaskIDRef; + +#[derive(Debug, Clone, Copy)] +pub struct TaskRef<'a> { + pub id: TaskIDRef<'a>, + pub args: TaskArgs<'a>, +} + +#[derive(Debug, Clone, Copy, Default)] +pub struct TaskArgs<'a> { + pub host: Option<&'a str>, + pub target: Option<&'a str>, +} + +#[derive(Debug, Clone, Copy)] +pub struct TaskFlags { + pub force_run: bool, +} + +peg::parser! { + pub grammar rules() for str { + pub rule task_ref_with_flags() -> (TaskRef<'input>, TaskFlags) + = task:task_ref() flags:task_flags() { (task, flags) } + + pub rule task_ref() -> TaskRef<'input> + = id:task_id() args:task_args() { + TaskRef { + id, + args, + } + } + + rule recipe_id() -> &'input str + = $(name() ("/" name())?) + + rule task_id() -> TaskIDRef<'input> + = recipe:recipe_id() "::" task:name() { + TaskIDRef { recipe, task } + } + + rule task_args() -> TaskArgs<'input> + = "@" host:name()? target:tagged(<":">, <name()>)? { + TaskArgs { + host, + target, + } + } + / { Default::default() } + + rule task_flags() -> TaskFlags + = force_run:force_run() { TaskFlags { force_run } } + + rule force_run() -> bool + = "+" { true } + / { false } + + rule name() -> &'input str + = $(['a'..='z' | 'A' ..='Z' | '0'..='9' | '_' | '-']+) + + rule tagged<T>(tag: rule<()>, value: rule<T>) -> T + = tag() v:value() { v } + } +} diff --git a/crates/rebel-parse/src/grammar/tokenize.rs b/crates/rebel-parse/src/grammar/tokenize.rs new file mode 100644 index 0000000..eb8a900 --- /dev/null +++ b/crates/rebel-parse/src/grammar/tokenize.rs @@ -0,0 +1,137 @@ +use crate::token::*; + +pub use rules::*; + +static KEYWORDS: phf::Map<&'static str, Keyword> = phf::phf_map! { + "else" => Keyword::Else, + "false" => Keyword::False, + "fetch" => Keyword::Fetch, + "fn" => Keyword::Fn, + "for" => Keyword::For, + "if" => Keyword::If, + "let" => Keyword::Let, + "map" => Keyword::Map, + "mut" => Keyword::Mut, + "none" => Keyword::None, + "recipe" => Keyword::Recipe, + "set" => Keyword::Set, + "task" => Keyword::Task, + "true" => Keyword::True, +}; + +peg::parser! { + pub grammar rules() for str { + pub rule token_stream() -> TokenStream<'input> + = _ tokens:(token() ** _) _ { TokenStream(tokens) } + + pub rule token() -> Token<'input> + = number:number() { Token::Number(number) } + / string:string() { Token::Str(string) } + / token:ident_or_keyword() { token } + / punct:punct() { Token::Punct(punct) } + + rule ident_or_keyword() -> Token<'input> + = s:$( + ['a'..='z' | 'A' ..='Z' | '_' ] + ['a'..='z' | 'A' ..='Z' | '_' | '0'..='9']* + ) { + if let Some(kw) = KEYWORDS.get(s) { + Token::Keyword(*kw) + } else { + Token::Ident(s) + } + } + + rule punct() -> Punct + = ch:punct_char() spacing:spacing() { Punct(ch, spacing) } + + rule punct_char() -> char + = !comment_start() ch:[ + | '~' | '!' | '@' | '#' | '$' | '%' | '^' | '&' + | '*' | '-' | '=' | '+' | '|' | ';' | ':' | ',' + | '<' | '.' | '>' | '/' | '\'' | '?' | '(' | ')' + | '[' | ']' | '{' | '}' + ] { ch } + + rule spacing() -> Spacing + = &punct_char() { Spacing::Joint } + / { Spacing::Alone } + + rule number() -> &'input str + = $(['0'..='9'] ['0'..='9' | 'a'..='z' | 'A'..='Z' | '_']*) + + rule string() -> Str<'input> + = "\"" pieces:string_piece()* "\"" { + Str { + pieces, + kind: StrKind::Regular, + } + } + / "r\"" chars:$([^'"']*) "\"" { + Str { + pieces: vec![StrPiece::Chars(chars)], + kind: StrKind::Raw, + } + } + / "```" newline() pieces:script_string_piece()* "```" { + Str { + pieces, + kind: StrKind::Script, + } + } + + rule string_piece() -> StrPiece<'input> + = chars:$((!"{{" [^'"' | '\\'])+) { StrPiece::Chars(chars) } + / "\\" escape:string_escape() { StrPiece::Escape(escape) } + / string_interp() + + rule string_escape() -> char + = "n" { '\n' } + / "r" { '\r' } + / "t" { '\t' } + / "\\" { '\\' } + / "\"" { '"' } + / "{" { '{' } + / "0" { '\0' } + / "x" digits:$(['0'..='7'] hex_digit()) { + u8::from_str_radix(digits, 16).unwrap().into() + } + / "u{" digits:$(hex_digit()*<1,6>) "}" { ? + u32::from_str_radix(digits, 16).unwrap().try_into().or(Err("Invalid unicode escape")) + } + + rule script_string_piece() -> StrPiece<'input> + = chars:$((!"{{" !"```" [_])+) { StrPiece::Chars(chars) } + / string_interp() + + rule string_interp() -> StrPiece<'input> + = "{{" _ tokens:(subtoken() ++ _) _ "}}" { + StrPiece::Interp(TokenStream(tokens)) + } + + rule subtoken() -> Token<'input> + = !"}}" token:token() { token } + + rule hex_digit() + = ['0'..='9' | 'a'..='f' | 'A'..='F'] + + /// Mandatory whitespace + rule __ + = ([' ' | '\t'] / quiet!{newline()} / quiet!{comment()})+ + + /// Optional whitespace + rule _ + = quiet!{__?} + + rule comment_start() + = "//" + / "/*" + + rule comment() + = "//" (!newline() [_])* (newline() / ![_]) + / "/*" (!"*/" [_])* "*/" + + rule newline() + = ['\n' | '\r'] + } +} |