From 43cad5573542da64f5a8b7b245d77d3946185dfa Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 14 Feb 2020 02:55:45 -0800 Subject: [PATCH] Expand parser more --- schala-lang/language/src/parser.rs | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/schala-lang/language/src/parser.rs b/schala-lang/language/src/parser.rs index b632e89..849fb7a 100644 --- a/schala-lang/language/src/parser.rs +++ b/schala-lang/language/src/parser.rs @@ -7,7 +7,7 @@ use nom::IResult; use nom::character::complete::{one_of, space0, alphanumeric0}; use nom::bytes::complete::{tag, take, take_while, take_until}; use nom::combinator::{map, map_res, value, opt, verify}; -use nom::multi::{separated_list, many1, many0}; +use nom::multi::{separated_list, separated_nonempty_list, many1, many0}; //use nom::error::{ParseError, ErrorKind}; use nom::branch::alt; use nom::sequence::{pair, delimited}; @@ -117,12 +117,27 @@ fn prefix_op(input: &str) -> IResult<&str, PrefixOp> { map(p, |sigil| PrefixOp::from_str(&sigil.to_string()).unwrap())(input) } +fn identifier_expr(text: &str) -> IResult<&str, ExpressionKind> { + let (text, qualified_identifier) = map( + qualified_identifier_list, + |components| QualifiedName { id: ItemId::new(0), components } + )(text)?; + //TODO handle struct literals + let exp = Expression::new(ItemId::new(0), ExpressionKind::Value(qualified_identifier)); + Ok((text, exp.kind)) +} + +fn qualified_identifier_list(text: &str) -> IResult<&str, Vec>> { + separated_nonempty_list(tag("::"), identifier)(text) +} + fn primary_expr(text: &str) -> IResult<&str, ExpressionKind> { // primary := literal | paren_expr | if_expr | for_expr | while_expr | identifier_expr | lambda_expr | anonymous_struct | list_expr alt(( literal, paren_expr, + identifier_expr, ))(text) } @@ -153,10 +168,7 @@ fn call_expr(text: &str) -> IResult<&str, ExpressionKind> { fn prefix_expr(text: &str) -> IResult<&str, ExpressionKind> { let (text, pfx) = delimited(space0, opt(prefix_op), space0)(text)?; - let (text, result) = alt(( - paren_expr, - literal, - ))(text)?; + let (text, result) = call_expr(text)?; match pfx { None => Ok((text, result)), Some(pfx) => { @@ -196,7 +208,6 @@ fn expression_kind(input: &str) -> IResult<&str, ExpressionKind> { } pub fn perform_parsing(input: &str) -> Result { - //let output = expression_kind(input); - let output = identifier(input); + let output = expression_kind(input); Ok(format!("{:?}", output)) }