This commit is contained in:
Greg Shuflin 2021-11-18 16:45:26 -08:00
parent 7a8ab3d571
commit 7f3b4a727f
3 changed files with 27 additions and 15 deletions

View File

@ -91,6 +91,8 @@ pub fn program(input: Span) -> ParseResult<AST> {
)), |(_, items, _)| items.into())
)(input)?;
println!("REST: {}", rest.fragment());
let ast = AST { id, statements };
Ok((rest, ast))
}
@ -118,8 +120,8 @@ fn statement(input: Span) -> ParseResult<Statement> {
let (rest, kind) = context(
"Parsing-statement",
alt((
map(expression, StatementKind::Expression),
map(declaration, StatementKind::Declaration),
map(expression, StatementKind::Expression),
))
)(input)?;
Ok((rest, Statement { id, location, kind }))
@ -144,25 +146,33 @@ fn module(input: Span) -> ParseResult<Declaration> {
pub fn expression(input: Span) -> ParseResult<Expression> {
let id = fresh_id(&input);
map(pair(expression_kind, opt(type_anno)), move |(kind, maybe_anno)| Expression::new(id, kind))(input)
map(pair(expression_kind, opt(type_anno)), move |(kind, type_anno)| Expression { id, type_anno, kind })(input)
}
fn type_anno(input: Span) -> ParseResult<TypeIdentifier> {
preceded(kw(":"), type_identifier)(input)
preceded(tok(char(':')), type_identifier)(input)
}
fn type_identifier(input: Span) -> ParseResult<TypeIdentifier> {
/*
alt((
tuple((kw("("), separated_list0(kw(","), type_identifier), kw(")"))),
type_singleton_name
map(delimited(tok(char('(')), separated_list0(tok(char(',')), type_identifier), tok(char(')'))),
TypeIdentifier::Tuple),
map(type_singleton_name, TypeIdentifier::Singleton),
))(input)
*/
unimplemented!()
}
fn type_singleton_name(input: Span) -> ParseResult<TypeSingletonName> {
unimplemented!()
map(pair(tok(identifier), opt(type_params)), |(name, params)| TypeSingletonName {
name: rc_string(name.fragment()), params: if let Some(params) = params { params } else { vec![] }
})(input)
}
fn type_params(input: Span) -> ParseResult<Vec<TypeIdentifier>> {
delimited(
tok(char('<')),
separated_list1(tok(char(',')), type_identifier),
tok(char('>'))
)(input)
}
pub fn expression_kind(input: Span) -> ParseResult<ExpressionKind> {

View File

@ -44,7 +44,9 @@ impl Parser {
let id_store: IdStore<ASTItem> = IdStore::new();
let span = Span::new_extra(input, Rc::new(RefCell::new(id_store)));
combinator::expression(span).map_err(|err| convert_err(input, err)).map(|(_, output)| output)
combinator::expression(span).map_err(|err| convert_err(input, err)).map(|(rest, output)| {
output
})
}
#[cfg(test)]

View File

@ -552,7 +552,7 @@ fn type_annotations() {
use ExpressionKind::*;
use TypeIdentifier::*;
assert_ast!(
assert_ast_comb!(
"let a = b : Int",
vec![decl(Declaration::Binding {
name: rc("a"),
@ -562,11 +562,11 @@ fn type_annotations() {
})]
);
assert_expr!(
assert_expr_comb!(
"a: Int",
expr_anno(Value(qn!(a)), Singleton(TypeSingletonName { name: rc("Int"), params: vec![] }))
);
assert_expr!(
assert_expr_comb!(
"a: Option<Int>",
expr_anno(
Value(qn!(a)),
@ -576,7 +576,7 @@ fn type_annotations() {
})
)
);
assert_expr!(
assert_expr_comb!(
"a: KoreanBBQSpecifier<Kimchi, Option<Bulgogi> >",
expr_anno(
Value(qn!(a)),
@ -592,7 +592,7 @@ fn type_annotations() {
})
)
);
assert_expr!(
assert_expr_comb!(
"a: (Int, Yolo<a>)",
expr_anno(
Value(qn!(a)),