Expand parser more

This commit is contained in:
greg 2020-02-14 02:55:45 -08:00
parent a6d065864c
commit 43cad55735

View File

@ -7,7 +7,7 @@ use nom::IResult;
use nom::character::complete::{one_of, space0, alphanumeric0}; use nom::character::complete::{one_of, space0, alphanumeric0};
use nom::bytes::complete::{tag, take, take_while, take_until}; use nom::bytes::complete::{tag, take, take_while, take_until};
use nom::combinator::{map, map_res, value, opt, verify}; 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::error::{ParseError, ErrorKind};
use nom::branch::alt; use nom::branch::alt;
use nom::sequence::{pair, delimited}; 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) 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<Rc<String>>> {
separated_nonempty_list(tag("::"), identifier)(text)
}
fn primary_expr(text: &str) -> IResult<&str, ExpressionKind> { 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 // primary := literal | paren_expr | if_expr | for_expr | while_expr | identifier_expr | lambda_expr | anonymous_struct | list_expr
alt(( alt((
literal, literal,
paren_expr, paren_expr,
identifier_expr,
))(text) ))(text)
} }
@ -153,10 +168,7 @@ fn call_expr(text: &str) -> IResult<&str, ExpressionKind> {
fn prefix_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, pfx) = delimited(space0, opt(prefix_op), space0)(text)?;
let (text, result) = alt(( let (text, result) = call_expr(text)?;
paren_expr,
literal,
))(text)?;
match pfx { match pfx {
None => Ok((text, result)), None => Ok((text, result)),
Some(pfx) => { Some(pfx) => {
@ -196,7 +208,6 @@ fn expression_kind(input: &str) -> IResult<&str, ExpressionKind> {
} }
pub fn perform_parsing(input: &str) -> Result<String, String> { pub fn perform_parsing(input: &str) -> Result<String, String> {
//let output = expression_kind(input); let output = expression_kind(input);
let output = identifier(input);
Ok(format!("{:?}", output)) Ok(format!("{:?}", output))
} }