From dc50dd1545e5554acf2a10bd92879310b173dfd6 Mon Sep 17 00:00:00 2001 From: Alex Bethel Date: Sat, 6 Aug 2022 20:04:11 -0500 Subject: [PATCH] Basic pattern parsing --- axc/src/parser.rs | 35 +++++++++++++++++++++++++---------- axc/src/syntax.rs | 11 +++++------ 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/axc/src/parser.rs b/axc/src/parser.rs index 8fc0b23..fffc1c2 100644 --- a/axc/src/parser.rs +++ b/axc/src/parser.rs @@ -214,13 +214,9 @@ impl Default for ParserMeta { /// Parser for AlexScript code. pub fn parser<'a>(m: &'a ParserMeta) -> impl Parser> + 'a { - // parse_statement(m) - // .repeated() - // .map(SyntaxTree) - // .then_ignore(end()) - parse_expression(m) - .then_ignore(just(";").then(whitespace())) + parse_statement(m) .repeated() + .map(SyntaxTree) .then_ignore(end()) .map(|exprs| { println!("{:#?}", exprs); @@ -288,7 +284,8 @@ fn parse_func_decl<'a>( m: &'a ParserMeta, ) -> impl Parser> + 'a { keyword("def") - .ignore_then(ident()) + .then_ignore(whitespace()) + .ignore_then(ident().then_ignore(whitespace())) .then(parse_pattern(m).repeated()) .then( just('=') @@ -479,13 +476,31 @@ fn parse_literal(_m: &ParserMeta) -> impl Parser impl Parser> { todo() } -fn parse_pattern(_m: &ParserMeta) -> impl Parser> { - todo() +fn parse_pattern(m: &ParserMeta) -> impl Parser> { + choice((parse_capture_pattern(m),)) +} + +fn parse_capture_pattern(_m: &ParserMeta) -> impl Parser> { + ident() + .try_map(|iden: String, span| { + if iden.starts_with(|c: char| c.is_lowercase()) { + Ok(iden) + } else { + Err(Simple::custom( + span, + "capture must start with lowercase letter", + )) + } + }) + .then_ignore(whitespace()) + .map(Pattern::Exact) } diff --git a/axc/src/syntax.rs b/axc/src/syntax.rs index 0c555d3..2ab0cf5 100644 --- a/axc/src/syntax.rs +++ b/axc/src/syntax.rs @@ -194,9 +194,8 @@ pub enum Type { /// The function being applied. This must be a generic type. function: Box, - /// The expression given as an argument to the type. This can be any expression, to allow - /// const generics; in most cases, though, it should be just a normal type. - expression: Box, + /// The type given as an argument to the type. + expression: Box, }, /// `(a, b)` @@ -210,6 +209,9 @@ pub enum Type { /// statements. #[derive(Clone, Debug)] pub enum Pattern { + /// `a` + Capture(String), + /// `(a, b)` Tuple(Vec), @@ -228,9 +230,6 @@ pub enum Pattern { /// `Foo { a: x, b: y, ... }` Destructure(String, Record), - /// `a` - Capture(String), - /// `_` Ignore,