List literal

This commit is contained in:
Greg Shuflin 2021-11-18 03:32:01 -08:00
parent 37e85c417e
commit a666ac985b
2 changed files with 20 additions and 32 deletions

View File

@ -10,7 +10,7 @@ use nom::{
combinator::{map, not, opt, peek, recognize, value},
error::{context, ParseError, VerboseError},
multi::{fold_many1, many0, many1, separated_list0, separated_list1},
sequence::{pair, preceded, tuple},
sequence::{delimited, pair, preceded, tuple},
Err, IResult, Parser,
};
use nom_locate::{position, LocatedSpan};
@ -178,10 +178,21 @@ fn extended_expr(input: Span) -> ParseResult<ExpressionKind> {
fn primary_expr(input: Span) -> ParseResult<ExpressionKind> {
context("primary-expr", alt((
string_literal, float_literal, number_literal, bool_literal, identifier_expr))
list_expr,
string_literal, float_literal, number_literal, bool_literal, identifier_expr
))
)(input)
}
fn list_expr(input: Span) -> ParseResult<ExpressionKind> {
map(
delimited(
tok(char('[')),
separated_list0(tok(char(',')), expression),
tok(char(']')),
), |items| ExpressionKind::ListLiteral(items))(input)
}
//TODO need to do something with prefix in the AST
fn string_literal(input: Span) -> ParseResult<ExpressionKind> {
tok(
@ -380,29 +391,5 @@ mod test {
span!(expression_kind, " /*yolo*/ barnaby").unwrap().1,
ExpressionKind::Value(qn!(barnaby))
);
let source = "!4";
let parsed = span!(expression_kind, source).map_err(|err| match err {
Err::Error(err) | Err::Failure(err) => {
let err = VerboseError {
errors: err.errors.into_iter().map(|(sp, kind)| (*sp.fragment(), kind)).collect(),
};
nom::error::convert_error(source, err)
}
_ => panic!(),
});
if let Err(err) = parsed {
println!("{}", err);
panic!("parse error desu!");
}
assert_eq!(
parsed.unwrap().1,
ExpressionKind::PrefixExp(
PrefixOp::from_sigil("!"),
Box::new(Expression::new(Default::default(), ExpressionKind::NatLiteral(4)))
)
);
}
}

View File

@ -168,8 +168,9 @@ fn string_literals() {
fn list_literals() {
use ExpressionKind::*;
assert_expr!("[]", expr(ListLiteral(vec![])));
assert_expr!("[1,2]", expr(ListLiteral(vec![expr(NatLiteral(1)), expr(NatLiteral(2)),])));
assert_expr_comb!("[]", expr(ListLiteral(vec![])));
assert_expr_comb!("[1,2]", expr(ListLiteral(vec![expr(NatLiteral(1)), expr(NatLiteral(2)),])));
assert_expr_comb!("[1, /*no*/2]", expr(ListLiteral(vec![expr(NatLiteral(1)), expr(NatLiteral(2)),])));
assert_fail_expr!("[1,,2]", "some failure");
}
@ -217,10 +218,10 @@ fn binexps() {
fn prefix_exps() {
use ExpressionKind::*;
assert_expr!("-3", prefixop("-", expr(NatLiteral(3))));
assert_expr!("-0.2", prefixop("-", expr(FloatLiteral(0.2))));
assert_expr!("!3", prefixop("!", expr(NatLiteral(3))));
assert_expr!("!t", prefixop("!", expr(Value(qn!(t)))));
assert_expr_comb!("-3", prefixop("-", expr(NatLiteral(3))));
assert_expr_comb!("-0.2", prefixop("-", expr(FloatLiteral(0.2))));
assert_expr_comb!("!3", prefixop("!", expr(NatLiteral(3))));
assert_expr_comb!("!t", prefixop("!", expr(Value(qn!(t)))));
assert_expr!("a <- -b", binop("<-", expr(Value(qn!(a))), prefixop("-", expr(Value(qn!(b))))));
assert_expr!("a <--b", binop("<--", expr(Value(qn!(a))), expr(Value(qn!(b)))));
}