schala/schala-lang/src/parsing/combinator.rs

979 lines
32 KiB
Rust
Raw Normal View History

2021-11-17 12:45:55 -08:00
use std::{cell::RefCell, rc::Rc};
2021-11-16 20:23:27 -08:00
use nom::{
branch::alt,
2021-11-18 17:16:39 -08:00
bytes::complete::{escaped_transform, tag, take_till, take_while},
character::{
2021-11-18 21:02:33 -08:00
complete::{alpha1, alphanumeric0, char, line_ending, none_of, not_line_ending, one_of, space1},
2021-11-18 17:16:39 -08:00
is_alphanumeric,
2021-11-17 12:45:55 -08:00
},
2021-11-19 09:59:08 -08:00
combinator::{cut, eof, map, not, opt, peek, recognize, value, verify},
2021-11-17 12:45:55 -08:00
error::{context, ParseError, VerboseError},
2021-11-18 21:02:33 -08:00
multi::{many0, many1, separated_list0, separated_list1},
2021-11-19 09:59:08 -08:00
sequence::{delimited, pair, preceded, separated_pair, terminated, tuple},
Finish, IResult, Parser,
2021-11-16 20:23:27 -08:00
};
2021-11-17 03:59:16 -08:00
use nom_locate::{position, LocatedSpan};
2021-11-17 12:45:55 -08:00
2021-11-17 12:45:42 -08:00
use crate::identifier::{Id, IdStore};
2021-11-16 20:23:27 -08:00
2021-11-17 12:45:42 -08:00
type StoreRef = Rc<RefCell<IdStore<ASTItem>>>;
2021-11-17 16:53:03 -08:00
pub type Span<'a> = LocatedSpan<&'a str, StoreRef>;
2021-11-17 03:59:16 -08:00
type ParseResult<'a, O> = IResult<Span<'a>, O, VerboseError<Span<'a>>>;
2021-11-16 20:23:27 -08:00
use crate::ast::*;
2021-11-17 01:54:35 -08:00
fn rc_string(s: &str) -> Rc<String> {
Rc::new(s.to_string())
2021-11-16 20:23:27 -08:00
}
2021-11-19 09:59:08 -08:00
fn is_keyword(input: &str) -> bool {
let keywords = [
"if",
"then",
"else",
"is",
"fn",
"for",
"while",
"in",
"true",
"false",
"let",
"in",
"mut",
"return",
"break",
"continue",
"type",
"alias",
"self",
"Self",
"interface",
"impl",
"module",
"import",
];
keywords.iter().any(|kw| kw == &input)
}
2021-11-17 12:45:42 -08:00
fn fresh_id(span: &Span) -> Id<ASTItem> {
let mut table_handle = span.extra.borrow_mut();
table_handle.fresh()
}
2021-11-16 20:23:27 -08:00
2021-11-17 16:44:09 -08:00
fn fresh_id_rc(store_ref: &StoreRef) -> Id<ASTItem> {
let mut table_handle = store_ref.borrow_mut();
table_handle.fresh()
}
2021-11-17 12:45:55 -08:00
fn tok<'a, O>(
input_parser: impl Parser<Span<'a>, O, VerboseError<Span<'a>>>,
) -> impl FnMut(Span<'a>) -> IResult<Span<'a>, O, VerboseError<Span<'a>>> {
context("tok", map(tuple((ws0, input_parser)), |(_, output)| output))
2021-11-17 01:54:35 -08:00
}
2021-11-16 20:23:27 -08:00
2021-11-17 03:59:16 -08:00
fn kw<'a>(keyword_str: &'static str) -> impl FnMut(Span<'a>) -> ParseResult<()> {
2021-11-17 12:45:55 -08:00
context("keyword", tok(value((), tag(keyword_str))))
2021-11-16 20:23:27 -08:00
}
// whitespace does consume at least one piece of whitespace - use ws0 for maybe none
2021-11-17 03:59:16 -08:00
fn whitespace(input: Span) -> ParseResult<()> {
2021-11-17 12:45:55 -08:00
context("whitespace", alt((block_comment, line_comment, value((), space1))))(input)
2021-11-16 20:23:27 -08:00
}
2021-11-17 03:59:16 -08:00
fn ws0(input: Span) -> ParseResult<()> {
2021-11-17 12:45:55 -08:00
context("WS0", value((), many0(whitespace)))(input)
2021-11-16 20:23:27 -08:00
}
2021-11-17 03:59:16 -08:00
fn line_comment(input: Span) -> ParseResult<()> {
2021-11-17 12:45:55 -08:00
value((), tuple((tag("//"), not_line_ending)))(input)
2021-11-16 20:23:27 -08:00
}
2021-11-17 03:59:16 -08:00
fn block_comment(input: Span) -> ParseResult<()> {
2021-11-17 12:45:55 -08:00
context(
"Block-comment",
value(
(),
tuple((
tag("/*"),
many0(alt((value((), none_of("*/")), value((), none_of("/*")), block_comment))),
tag("*/"),
)),
),
)(input)
2021-11-16 20:23:27 -08:00
}
2021-11-17 03:59:16 -08:00
fn statement_delimiter(input: Span) -> ParseResult<()> {
2021-11-17 12:45:55 -08:00
tok(alt((value((), line_ending), value((), char(';')))))(input)
2021-11-16 20:23:27 -08:00
}
2021-11-18 15:17:47 -08:00
pub fn program(input: Span) -> ParseResult<AST> {
let id = fresh_id(&input);
//TODO `rest` should be empty
2021-11-18 17:16:39 -08:00
let (rest, statements) = context(
"AST",
2021-11-19 10:00:16 -08:00
terminated(
2021-11-18 15:17:47 -08:00
map(
2021-11-18 17:16:39 -08:00
tuple((
many0(statement_delimiter),
2021-11-19 01:01:26 -08:00
separated_list0(many1(statement_delimiter), statement),
2021-11-18 17:16:39 -08:00
many0(statement_delimiter),
)),
|(_, items, _)| items.into(),
2021-11-19 10:00:16 -08:00
), tok(eof)),
2021-11-18 15:17:47 -08:00
)(input)?;
let ast = AST { id, statements };
Ok((rest, ast))
}
2021-11-18 21:02:33 -08:00
fn block_template<'a, O>(
input_parser: impl Parser<Span<'a>, O, VerboseError<Span<'a>>>,
) -> impl FnMut(Span<'a>) -> IResult<Span<'a>, Vec<O>, VerboseError<Span<'a>>> {
2021-11-18 19:12:28 -08:00
map(
delimited(
tok(char('{')),
tuple((
2021-11-18 21:02:33 -08:00
many0(statement_delimiter),
2021-11-19 01:01:26 -08:00
separated_list0(many1(statement_delimiter), input_parser),
2021-11-18 21:02:33 -08:00
many0(statement_delimiter),
2021-11-18 19:12:28 -08:00
)),
tok(char('}')),
2021-11-17 12:45:55 -08:00
),
2021-11-18 19:12:28 -08:00
|(_, items, _)| items,
)
}
pub fn block(input: Span) -> ParseResult<Block> {
map(block_template(statement), |items| items.into())(input)
2021-11-16 20:23:27 -08:00
}
2021-11-17 03:59:16 -08:00
fn statement(input: Span) -> ParseResult<Statement> {
2021-11-17 12:45:42 -08:00
let (input, pos) = position(input)?;
2021-11-18 15:17:47 -08:00
let location = pos.location_offset().into();
2021-11-17 12:45:42 -08:00
let id = fresh_id(&input);
2021-11-18 15:17:47 -08:00
let (rest, kind) = context(
2021-11-17 12:45:55 -08:00
"Parsing-statement",
2021-11-19 01:01:26 -08:00
alt((
map(flow, StatementKind::Flow),
2021-11-19 01:35:25 -08:00
map(import, StatementKind::Import),
2021-11-19 01:01:26 -08:00
map(declaration, StatementKind::Declaration),
map(expression, StatementKind::Expression),
)),
2021-11-18 15:17:47 -08:00
)(input)?;
Ok((rest, Statement { id, location, kind }))
}
2021-11-19 01:35:25 -08:00
fn import(input: Span) -> ParseResult<ImportSpecifier> {
fn path_components(input: Span) -> ParseResult<Vec<Rc<String>>> {
map(
tuple((opt(tag("::")), identifier, many0(preceded(tag("::"), identifier_span)))),
|(_maybe_root, first, rest)| {
let mut components = vec![rc_string(first.fragment())];
components.extend(rest.into_iter().map(|n| rc_string(n.fragment())));
components
},
)(input)
}
fn import_suffix(input: Span) -> ParseResult<ImportedNames> {
alt((
value(ImportedNames::All, tag("::*")),
map(
preceded(
tag("::"),
delimited(char('{'), separated_list0(tok(char(',')), identifier), char('}')),
),
|names| ImportedNames::List(names.into_iter().map(|n| rc_string(n.fragment())).collect()),
),
))(input)
}
let id = fresh_id(&input);
map(
preceded(kw("import"), cut(pair(path_components, opt(import_suffix)))),
move |(path_components, suffix)| ImportSpecifier {
id,
path_components,
imported_names: suffix.unwrap_or(ImportedNames::LastOfPath),
},
)(input)
}
2021-11-19 01:01:26 -08:00
fn flow(input: Span) -> ParseResult<FlowControl> {
alt((
map(kw("continue"), |_| FlowControl::Continue),
map(kw("break"), |_| FlowControl::Break),
map(preceded(kw("return"), opt(expression)), FlowControl::Return),
))(input)
}
2021-11-18 15:17:47 -08:00
fn declaration(input: Span) -> ParseResult<Declaration> {
2021-11-18 18:58:46 -08:00
alt((binding, type_decl, func, annotation, module, interface, implementation))(input)
}
fn implementation(input: Span) -> ParseResult<Declaration> {
alt((
map(
preceded(kw("impl"), tuple((type_singleton_name, kw("for"), type_identifier, decl_block))),
|(if_name, _, type_name, block)| Declaration::Impl {
type_name,
interface_name: Some(if_name),
block,
},
),
map(preceded(kw("impl"), pair(type_identifier, decl_block)), |(type_name, block)| {
Declaration::Impl { type_name, interface_name: None, block }
}),
))(input)
}
fn decl_block(input: Span) -> ParseResult<Vec<Declaration>> {
2021-11-18 19:12:28 -08:00
block_template(func_decl)(input)
2021-11-18 18:58:46 -08:00
}
fn interface(input: Span) -> ParseResult<Declaration> {
2021-11-18 22:21:52 -08:00
map(preceded(kw("interface"), pair(identifier, signature_block)), |(name, signatures)| {
2021-11-18 18:58:46 -08:00
Declaration::Interface { name: rc_string(name.fragment()), signatures }
})(input)
}
fn signature_block(input: Span) -> ParseResult<Vec<Signature>> {
2021-11-18 19:12:28 -08:00
block_template(func_signature)(input)
2021-11-18 18:31:33 -08:00
}
fn annotation(input: Span) -> ParseResult<Declaration> {
map(
tuple((
tok(char('@')),
identifier,
opt(delimited(tok(char('(')), separated_list1(tok(char(',')), expression), tok(char(')')))),
statement_delimiter,
statement,
)),
|(_, name, args, _, inner)| Declaration::Annotation {
name: rc_string(name.fragment()),
arguments: if let Some(args) = args { args } else { vec![] },
inner: Box::new(inner),
},
)(input)
2021-11-18 18:24:06 -08:00
}
fn func(input: Span) -> ParseResult<Declaration> {
alt((func_decl, map(func_signature, Declaration::FuncSig)))(input)
}
fn func_decl(input: Span) -> ParseResult<Declaration> {
map(pair(func_signature, block), |(sig, decl)| Declaration::FuncDecl(sig, decl))(input)
}
//TODO handle operators
fn func_signature(input: Span) -> ParseResult<Signature> {
2021-11-18 22:21:52 -08:00
map(tuple((kw("fn"), identifier, formal_params, opt(type_anno))), |(_, name, params, type_anno)| {
2021-11-18 18:24:06 -08:00
Signature { name: rc_string(name.fragment()), operator: false, params, type_anno }
})(input)
}
fn formal_params(input: Span) -> ParseResult<Vec<FormalParam>> {
delimited(tok(char('(')), separated_list0(tok(char(',')), formal_param), tok(char(')')))(input)
}
//TODO support 256-limit
fn formal_param(input: Span) -> ParseResult<FormalParam> {
map(
2021-11-18 22:21:52 -08:00
tuple((identifier, opt(type_anno), opt(preceded(tok(char('=')), expression)))),
2021-11-18 18:24:06 -08:00
|(name, anno, default)| FormalParam { name: rc_string(name.fragment()), anno, default },
)(input)
2021-11-18 17:16:39 -08:00
}
fn type_decl(input: Span) -> ParseResult<Declaration> {
alt((
map(
2021-11-18 22:21:52 -08:00
tuple((kw("type"), kw("alias"), identifier, tok(char('=')), identifier)),
2021-11-18 17:16:39 -08:00
|(_, _, alias, _, name)| Declaration::TypeAlias {
alias: rc_string(alias.fragment()),
original: rc_string(name.fragment()),
},
),
map(
tuple((kw("type"), opt(kw("mut")), type_singleton_name, tok(char('=')), type_body)),
|(_, mutable, name, _, body)| Declaration::TypeDecl { name, body, mutable: mutable.is_some() },
),
))(input)
}
fn type_body(input: Span) -> ParseResult<TypeBody> {
let id = fresh_id(&input);
alt((
map(
delimited(tok(char('{')), separated_list1(tok(char(',')), record_variant_item), tok(char('}'))),
2021-11-18 18:24:06 -08:00
move |items| TypeBody::ImmediateRecord { id, fields: items },
2021-11-18 17:16:39 -08:00
),
map(separated_list0(tok(char('|')), variant_spec), TypeBody::Variants),
))(input)
}
fn variant_spec(input: Span) -> ParseResult<Variant> {
fn record_variant(input: Span) -> ParseResult<VariantKind> {
map(
delimited(tok(char('{')), separated_list1(tok(char(',')), record_variant_item), tok(char('}'))),
VariantKind::Record,
)(input)
}
fn tuple_variant(input: Span) -> ParseResult<VariantKind> {
map(
delimited(tok(char('(')), separated_list1(tok(char(',')), type_identifier), tok(char(')'))),
VariantKind::TupleStruct,
)(input)
}
let id = fresh_id(&input);
let (rest, (name, kind)) = alt((
2021-11-18 22:21:52 -08:00
pair(identifier, record_variant),
pair(identifier, tuple_variant),
map(identifier, |ident| (ident, VariantKind::UnitStruct)),
2021-11-18 17:16:39 -08:00
))(input)?;
Ok((rest, Variant { id, name: rc_string(name.fragment()), kind }))
}
fn record_variant_item(input: Span) -> ParseResult<(Rc<String>, TypeIdentifier)> {
2021-11-18 22:21:52 -08:00
map(tuple((identifier, tok(char(':')), type_identifier)), |(name, _, ty)| {
2021-11-18 17:16:39 -08:00
(rc_string(name.fragment()), ty)
})(input)
2021-11-18 15:17:47 -08:00
}
fn binding(input: Span) -> ParseResult<Declaration> {
2021-11-18 22:21:52 -08:00
let parser = tuple((kw("let"), opt(kw("mut")), identifier, opt(type_anno), tok(char('=')), expression));
2021-11-18 17:16:39 -08:00
map(parser, |(_, maybe_mut, ident, type_anno, _, expr)| Declaration::Binding {
name: rc_string(ident.fragment()),
constant: maybe_mut.is_none(),
type_anno,
expr,
})(input)
2021-11-18 15:17:47 -08:00
}
fn module(input: Span) -> ParseResult<Declaration> {
2021-11-18 22:21:52 -08:00
map(tuple((kw("module"), identifier, block)), |(_, name, items)| Declaration::Module {
2021-11-18 17:16:39 -08:00
name: rc_string(name.fragment()),
items,
})(input)
2021-11-16 20:23:27 -08:00
}
2021-11-17 16:53:03 -08:00
pub fn expression(input: Span) -> ParseResult<Expression> {
2021-11-17 12:45:42 -08:00
let id = fresh_id(&input);
2021-11-18 21:02:33 -08:00
map(pair(expression_kind(true), opt(type_anno)), move |(kind, type_anno)| Expression {
id,
type_anno,
kind,
})(input)
}
fn expression_no_struct(input: Span) -> ParseResult<Expression> {
let id = fresh_id(&input);
map(pair(expression_kind(false), opt(type_anno)), move |(kind, type_anno)| Expression {
id,
type_anno,
kind,
})(input)
2021-11-17 03:40:43 -08:00
}
2021-11-19 00:40:10 -08:00
fn expr_or_block(input: Span) -> ParseResult<Block> {
let (input, pos) = position(input)?;
let id = fresh_id(&input);
let location = pos.location_offset().into();
alt((
block,
map(expression, move |expr| Statement { id, location, kind: StatementKind::Expression(expr) }.into()),
))(input)
}
2021-11-17 03:59:16 -08:00
fn type_anno(input: Span) -> ParseResult<TypeIdentifier> {
2021-11-18 16:45:26 -08:00
preceded(tok(char(':')), type_identifier)(input)
2021-11-17 03:40:43 -08:00
}
2021-11-17 03:59:16 -08:00
fn type_identifier(input: Span) -> ParseResult<TypeIdentifier> {
2021-11-17 03:40:43 -08:00
alt((
2021-11-18 17:16:39 -08:00
map(
delimited(tok(char('(')), separated_list0(tok(char(',')), type_identifier), tok(char(')'))),
TypeIdentifier::Tuple,
),
2021-11-18 16:45:26 -08:00
map(type_singleton_name, TypeIdentifier::Singleton),
2021-11-17 03:40:43 -08:00
))(input)
}
2021-11-17 03:59:16 -08:00
fn type_singleton_name(input: Span) -> ParseResult<TypeSingletonName> {
2021-11-18 22:21:52 -08:00
map(pair(identifier, opt(type_params)), |(name, params)| TypeSingletonName {
2021-11-18 17:16:39 -08:00
name: rc_string(name.fragment()),
params: if let Some(params) = params { params } else { vec![] },
2021-11-18 16:45:26 -08:00
})(input)
}
fn type_params(input: Span) -> ParseResult<Vec<TypeIdentifier>> {
2021-11-18 17:16:39 -08:00
delimited(tok(char('<')), separated_list1(tok(char(',')), type_identifier), tok(char('>')))(input)
2021-11-17 03:40:43 -08:00
}
2021-11-18 21:02:33 -08:00
pub fn expression_kind(allow_struct: bool) -> impl FnMut(Span) -> ParseResult<ExpressionKind> {
move |input: Span| context("expression-kind", precedence_expr(allow_struct))(input)
2021-11-17 16:44:09 -08:00
}
2021-11-18 21:02:33 -08:00
fn precedence_expr(allow_struct: bool) -> impl FnMut(Span) -> ParseResult<ExpressionKind> {
move |input: Span| {
let handle = input.extra.clone();
let precedence_continuation = pair(operator, prefix_expr(allow_struct));
map(
pair(prefix_expr(allow_struct), many0(precedence_continuation)),
move |(first, rest): (ExpressionKind, Vec<(BinOp, ExpressionKind)>)| {
let mut handle_ref = handle.borrow_mut();
BinopSequence { first, rest }.do_precedence(&mut handle_ref)
},
)(input)
}
2021-11-17 16:44:09 -08:00
}
fn operator(input: Span) -> ParseResult<BinOp> {
2021-11-17 16:53:03 -08:00
tok(map(
tuple((not(tag("*/")), recognize(many1(one_of("+-*/%<>=!$&|?^`"))))),
|(_, sigil_span): ((), Span)| BinOp::from_sigil(sigil_span.fragment()),
))(input)
2021-11-17 16:44:09 -08:00
}
fn prefix_op(input: Span) -> ParseResult<PrefixOp> {
2021-11-17 16:53:03 -08:00
tok(map(recognize(one_of("+-!")), |sigil: Span| PrefixOp::from_sigil(sigil.fragment())))(input)
2021-11-17 16:44:09 -08:00
}
2021-11-18 21:02:33 -08:00
fn prefix_expr(allow_struct: bool) -> impl FnMut(Span) -> ParseResult<ExpressionKind> {
move |input: Span| {
let handle = input.extra.clone();
context(
"prefix-expr",
map(pair(opt(prefix_op), extended_expr(allow_struct)), move |(prefix, expr)| {
if let Some(prefix) = prefix {
let expr = Expression::new(fresh_id_rc(&handle), expr);
ExpressionKind::PrefixExp(prefix, Box::new(expr))
} else {
expr
}
}),
)(input)
}
2021-11-17 16:44:09 -08:00
}
2021-11-18 14:38:29 -08:00
#[derive(Debug)]
enum ExtendedPart<'a> {
Index(Vec<Expression>),
Call(Vec<InvocationArgument>),
Accessor(&'a str),
}
2021-11-18 21:02:33 -08:00
fn extended_expr(allow_struct: bool) -> impl FnMut(Span) -> ParseResult<ExpressionKind> {
move |input: Span| {
let (s, (primary, parts)) =
context("extended-expr", pair(primary_expr(allow_struct), many0(extended_expr_part)))(input)?;
let mut expression = Expression::new(fresh_id(&s), primary);
for part in parts.into_iter() {
let kind = match part {
ExtendedPart::Index(indexers) =>
ExpressionKind::Index { indexee: Box::new(expression), indexers },
ExtendedPart::Call(arguments) => ExpressionKind::Call { f: Box::new(expression), arguments },
ExtendedPart::Accessor(name) => {
let name = rc_string(name);
ExpressionKind::Access { name, expr: Box::new(expression) }
}
};
expression = Expression::new(fresh_id(&s), kind);
}
2021-11-18 14:38:29 -08:00
2021-11-18 21:02:33 -08:00
Ok((s, expression.kind))
2021-11-18 14:38:29 -08:00
}
2021-11-17 01:54:35 -08:00
}
2021-11-18 14:38:29 -08:00
fn extended_expr_part(input: Span) -> ParseResult<ExtendedPart> {
fn index_part(input: Span) -> ParseResult<Vec<Expression>> {
2021-11-18 17:16:39 -08:00
delimited(tok(char('[')), separated_list1(tok(char(',')), expression), tok(char(']')))(input)
2021-11-18 14:38:29 -08:00
}
fn call_part(input: Span) -> ParseResult<Vec<InvocationArgument>> {
2021-11-18 17:16:39 -08:00
delimited(tok(char('(')), separated_list0(tok(char(',')), invocation_argument), tok(char(')')))(input)
2021-11-18 14:38:29 -08:00
}
fn access_part(input: Span) -> ParseResult<&str> {
2021-11-18 17:16:39 -08:00
preceded(tok(char('.')), map(identifier, |item| *item.fragment()))(input)
2021-11-18 14:38:29 -08:00
}
alt((
map(index_part, ExtendedPart::Index),
map(call_part, ExtendedPart::Call),
2021-11-18 17:16:39 -08:00
map(access_part, ExtendedPart::Accessor),
2021-11-18 14:38:29 -08:00
))(input)
}
2021-11-18 17:16:39 -08:00
//TODO this shouldn't be an expression b/c type annotations disallowed here
2021-11-18 14:38:29 -08:00
fn invocation_argument(input: Span) -> ParseResult<InvocationArgument> {
alt((
map(tok(char('_')), |_| InvocationArgument::Ignored),
2021-11-18 22:21:52 -08:00
map(tuple((identifier, tok(char('=')), expression)), |(name, _, expr)| InvocationArgument::Keyword {
name: rc_string(name.fragment()),
expr,
2021-11-18 17:16:39 -08:00
}),
2021-11-18 14:38:29 -08:00
map(expression, InvocationArgument::Positional),
))(input)
}
2021-11-18 21:02:33 -08:00
fn primary_expr(allow_struct: bool) -> impl FnMut(Span) -> ParseResult<ExpressionKind> {
move |input: Span| {
if allow_struct {
context("primary-expr", alt((named_struct, primary_expr_no_struct)))(input)
} else {
context("primary-expr", primary_expr_no_struct)(input)
}
}
2021-11-18 20:08:46 -08:00
}
fn primary_expr_no_struct(input: Span) -> ParseResult<ExpressionKind> {
context(
"primary-expr-no-struct",
alt((
while_expr,
2021-11-18 22:12:13 -08:00
for_expr,
2021-11-19 00:40:10 -08:00
if_expr,
2021-11-19 00:52:00 -08:00
lambda_expr,
2021-11-18 17:16:39 -08:00
list_expr,
paren_expr,
string_literal,
float_literal,
number_literal,
bool_literal,
identifier_expr,
)),
2021-11-18 03:16:43 -08:00
)(input)
}
2021-11-18 21:02:33 -08:00
fn named_struct(input: Span) -> ParseResult<ExpressionKind> {
map(pair(qualified_identifier, record_block), |(name, fields)| ExpressionKind::NamedStruct {
name,
fields,
})(input)
}
//TODO support anonymous structs and Elm-style update syntax for structs
fn record_block(input: Span) -> ParseResult<Vec<(Rc<String>, Expression)>> {
let record_entry =
2021-11-18 22:21:52 -08:00
separated_pair(map(identifier, |span| rc_string(span.fragment())), tok(char(':')), expression);
2021-11-18 21:02:33 -08:00
delimited(tok(char('{')), separated_list0(tok(char(',')), record_entry), tok(char('}')))(input)
}
2021-11-19 00:52:00 -08:00
fn lambda_expr(input: Span) -> ParseResult<ExpressionKind> {
alt((
2021-11-19 01:01:26 -08:00
map(
preceded(tok(char('\\')), tuple((formal_params, opt(type_anno), block))),
|(params, type_anno, body)| ExpressionKind::Lambda { params, type_anno, body },
),
map(
preceded(tok(char('\\')), tuple((formal_param, opt(type_anno), block))),
|(param, type_anno, body)| ExpressionKind::Lambda { params: vec![param], type_anno, body },
),
2021-11-19 00:52:00 -08:00
))(input)
}
2021-11-18 20:08:46 -08:00
fn while_expr(input: Span) -> ParseResult<ExpressionKind> {
let id = fresh_id(&input);
2021-11-18 21:02:33 -08:00
map(preceded(kw("while"), pair(opt(expression_no_struct), block)), move |(condition, body)| {
ExpressionKind::WhileExpression { condition: condition.map(Box::new), body }
2021-11-18 20:08:46 -08:00
})(input)
}
2021-11-19 00:40:10 -08:00
fn if_expr(input: Span) -> ParseResult<ExpressionKind> {
fn else_case(input: Span) -> ParseResult<Option<Block>> {
opt(preceded(kw("else"), expr_or_block))(input)
}
fn cond_block(input: Span) -> ParseResult<IfExpressionBody> {
map(block_template(cond_arm), IfExpressionBody::CondList)(input)
}
fn cond_arm(input: Span) -> ParseResult<ConditionArm> {
let condition = map(preceded(kw("is"), pattern), Condition::Pattern);
let condition_guard = opt(preceded(kw("if"), expression));
alt((
map(preceded(kw("else"), expr_or_block), |body| ConditionArm {
condition: Condition::Else,
guard: None,
body,
}),
map(
tuple((condition, condition_guard, kw("then"), expr_or_block)),
|(condition, guard, _, body)| ConditionArm { condition, guard, body },
),
))(input)
}
fn simple_pattern_match(input: Span) -> ParseResult<IfExpressionBody> {
map(
tuple((preceded(kw("is"), pattern), preceded(kw("then"), pair(expr_or_block, else_case)))),
|(pattern, (then_case, else_case))| IfExpressionBody::SimplePatternMatch {
pattern,
then_case,
else_case,
},
)(input)
}
fn simple_conditional(input: Span) -> ParseResult<IfExpressionBody> {
map(preceded(kw("then"), pair(expr_or_block, else_case)), |(then_case, else_case)| {
IfExpressionBody::SimpleConditional { then_case, else_case }
})(input)
}
fn if_expr_body(input: Span) -> ParseResult<IfExpressionBody> {
alt((cond_block, simple_pattern_match, simple_conditional))(input)
}
map(preceded(kw("if"), pair(opt(expression_no_struct), if_expr_body)), |(discriminator, body)| {
ExpressionKind::IfExpression { discriminator: discriminator.map(Box::new), body: Box::new(body) }
})(input)
}
fn pattern(input: Span) -> ParseResult<Pattern> {
alt((
map(
delimited(tok(char('(')), separated_list1(tok(char(',')), pattern), tok(char(')'))),
Pattern::TuplePattern,
),
simple_pattern,
))(input)
}
fn simple_pattern(input: Span) -> ParseResult<Pattern> {
fn record_pattern_entry(input: Span) -> ParseResult<(Rc<String>, Pattern)> {
let id = fresh_id(&input);
alt((
map(separated_pair(identifier, tok(char(':')), pattern), |(ident, pat)| {
(rc_string(ident.fragment()), pat)
}),
map(identifier, move |ident| {
let qn = QualifiedName { id, components: vec![rc_string(ident.fragment())] };
(rc_string(ident.fragment()), Pattern::VarOrName(qn))
}),
))(input)
}
alt((
pattern_literal,
map(
pair(
qualified_identifier,
delimited(tok(char('(')), separated_list0(tok(char(',')), pattern), tok(char(')'))),
),
|(qn, members)| Pattern::TupleStruct(qn, members),
),
map(
pair(
qualified_identifier,
delimited(
tok(char('{')),
separated_list0(tok(char(',')), record_pattern_entry),
tok(char('}')),
),
),
|(qn, members)| Pattern::Record(qn, members),
),
map(qualified_identifier, Pattern::VarOrName),
))(input)
}
fn pattern_literal(input: Span) -> ParseResult<Pattern> {
alt((
value(Pattern::Ignored, kw("_")),
value(Pattern::Literal(PatternLiteral::BoolPattern(true)), kw("true")),
value(Pattern::Literal(PatternLiteral::BoolPattern(false)), kw("false")),
map(tok(bare_string_literal), |s| Pattern::Literal(PatternLiteral::StringPattern(Rc::new(s)))),
map(pair(opt(tok(char('-'))), alt((float_literal, number_literal))), |(sign, num)| {
Pattern::Literal(PatternLiteral::NumPattern { neg: sign.is_some(), num })
}),
))(input)
}
2021-11-18 22:12:13 -08:00
fn for_expr(input: Span) -> ParseResult<ExpressionKind> {
fn for_enumerators(input: Span) -> ParseResult<Vec<Enumerator>> {
alt((
delimited(tok(char('{')), separated_list0(tok(char(',')), enumerator), tok(char('}'))),
map(enumerator, |enumerator| vec![enumerator]),
))(input)
}
//TODO add guards, etc.
fn enumerator(input: Span) -> ParseResult<Enumerator> {
alt((
2021-11-18 22:21:52 -08:00
map(separated_pair(identifier, kw("<-"), expression_no_struct), |(ident, generator)| {
2021-11-18 22:12:13 -08:00
Enumerator { id: rc_string(ident.fragment()), generator }
}),
//TODO distinguish these two cases in AST
2021-11-18 22:21:52 -08:00
map(separated_pair(identifier, kw("="), expression_no_struct), |(ident, generator)| Enumerator {
id: rc_string(ident.fragment()),
generator,
2021-11-18 22:12:13 -08:00
}),
))(input)
}
fn for_body(input: Span) -> ParseResult<Box<ForBody>> {
alt((
preceded(kw("return"), map(expression_no_struct, |expr| Box::new(ForBody::MonadicReturn(expr)))),
map(block, |body| Box::new(ForBody::StatementBlock(body))),
))(input)
}
map(preceded(kw("for"), pair(for_enumerators, for_body)), |(enumerators, body)| {
ExpressionKind::ForExpression { enumerators, body }
})(input)
}
2021-11-18 04:01:40 -08:00
fn paren_expr(input: Span) -> ParseResult<ExpressionKind> {
delimited(
tok(char('(')),
2021-11-18 17:16:39 -08:00
map(separated_list0(tok(char(',')), expression), |mut exprs| match exprs.len() {
2021-11-18 04:01:40 -08:00
1 => exprs.pop().unwrap().kind,
_ => ExpressionKind::TupleLiteral(exprs),
}),
2021-11-18 17:16:39 -08:00
tok(char(')')),
2021-11-18 04:01:40 -08:00
)(input)
}
2021-11-18 03:32:01 -08:00
fn list_expr(input: Span) -> ParseResult<ExpressionKind> {
2021-11-18 17:16:39 -08:00
map(delimited(tok(char('[')), separated_list0(tok(char(',')), expression), tok(char(']'))), |items| {
ExpressionKind::ListLiteral(items)
})(input)
2021-11-18 03:32:01 -08:00
}
2021-11-18 03:16:43 -08:00
//TODO need to do something with prefix in the AST
fn string_literal(input: Span) -> ParseResult<ExpressionKind> {
2021-11-18 17:16:39 -08:00
tok(map(pair(opt(identifier), bare_string_literal), |(_maybe_prefix, s)| {
ExpressionKind::StringLiteral(Rc::new(s))
}))(input)
2021-11-17 01:54:35 -08:00
}
2021-11-18 03:16:43 -08:00
fn bare_string_literal(input: Span) -> ParseResult<String> {
2021-11-18 17:16:39 -08:00
let string_escape_transforms =
alt((value("\\", tag("\\")), value("\"", tag("\"")), value("\n", tag("n")), value("\t", tag("t"))));
alt((
map(tag(r#""""#), |_| String::new()),
2021-11-19 01:35:25 -08:00
delimited(char('"'), escaped_transform(none_of(r#""\"#), '\\', string_escape_transforms), char('"')),
2021-11-18 17:16:39 -08:00
))(input)
2021-11-18 03:16:43 -08:00
}
2021-11-17 03:59:16 -08:00
fn identifier_expr(input: Span) -> ParseResult<ExpressionKind> {
2021-11-17 01:54:35 -08:00
context("identifier-expr", map(qualified_identifier, ExpressionKind::Value))(input)
}
2021-11-17 03:59:16 -08:00
fn qualified_identifier(input: Span) -> ParseResult<QualifiedName> {
2021-11-17 12:45:42 -08:00
let id = fresh_id(&input);
2021-11-18 22:21:52 -08:00
tok(map(separated_list1(tag("::"), map(identifier_span, |x| rc_string(x.fragment()))), move |items| {
2021-11-17 12:45:55 -08:00
QualifiedName { id, components: items }
}))(input)
2021-11-17 01:54:35 -08:00
}
2021-11-17 03:59:16 -08:00
fn identifier(input: Span) -> ParseResult<Span> {
2021-11-18 22:21:52 -08:00
tok(identifier_span)(input)
}
fn identifier_span(input: Span) -> ParseResult<Span> {
2021-11-19 09:59:08 -08:00
fn check(input: &Span) -> bool {
!is_keyword(input.fragment())
}
verify(
recognize(tuple((
alt((tag("_"), alpha1)),
take_while(|ch: char| is_alphanumeric(ch as u8) || ch == '_'),
))),
check,
)(input)
2021-11-16 20:23:27 -08:00
}
2021-11-17 03:59:16 -08:00
fn bool_literal(input: Span) -> ParseResult<ExpressionKind> {
2021-11-17 12:45:55 -08:00
context(
"bool-literal",
alt((
map(kw("true"), |_| ExpressionKind::BoolLiteral(true)),
map(kw("false"), |_| ExpressionKind::BoolLiteral(false)),
)),
)(input)
2021-11-16 20:23:27 -08:00
}
2021-11-17 16:53:03 -08:00
fn float_literal(input: Span) -> ParseResult<ExpressionKind> {
tok(map(
alt((
recognize(tuple((digits(digit_group_dec), char('.'), opt(digits(digit_group_dec))))),
recognize(tuple((char('.'), digits(digit_group_dec)))),
)),
|ds| ExpressionKind::FloatLiteral(ds.fragment().parse().unwrap()),
))(input)
}
2021-11-17 03:59:16 -08:00
fn number_literal(input: Span) -> ParseResult<ExpressionKind> {
2021-11-16 20:23:27 -08:00
map(alt((tok(hex_literal), tok(bin_literal), tok(dec_literal))), ExpressionKind::NatLiteral)(input)
}
2021-11-17 03:59:16 -08:00
fn dec_literal(input: Span) -> ParseResult<u64> {
2021-11-16 20:23:27 -08:00
map(digits(digit_group_dec), |chars: Vec<char>| {
let s: String = chars.into_iter().collect();
s.parse().unwrap()
})(input)
}
2021-11-17 03:59:16 -08:00
fn hex_literal(input: Span) -> ParseResult<u64> {
2021-11-16 20:23:27 -08:00
map(preceded(alt((tag("0x"), tag("0X"))), digits(digit_group_hex)), |chars: Vec<char>| {
let s: String = chars.into_iter().collect();
parse_hex(&s).unwrap()
})(input)
}
2021-11-17 03:59:16 -08:00
fn bin_literal(input: Span) -> ParseResult<u64> {
2021-11-16 20:23:27 -08:00
map(preceded(alt((tag("0b"), tag("0B"))), digits(digit_group_bin)), |chars: Vec<char>| {
let s: String = chars.into_iter().collect();
parse_binary(&s).unwrap()
})(input)
}
2021-11-17 03:59:16 -08:00
fn digits<'a, E: ParseError<Span<'a>>>(
digit_type: impl Parser<Span<'a>, Vec<char>, E>,
) -> impl FnMut(Span<'a>) -> IResult<Span<'a>, Vec<char>, E> {
2021-11-16 20:23:27 -08:00
map(separated_list1(many1(char('_')), digit_type), |items: Vec<Vec<char>>| {
items.into_iter().flatten().collect()
})
}
2021-11-17 03:59:16 -08:00
fn digit_group_dec(input: Span) -> ParseResult<Vec<char>> {
2021-11-16 20:23:27 -08:00
many1(one_of("0123456789"))(input)
}
2021-11-17 03:59:16 -08:00
fn digit_group_hex(input: Span) -> ParseResult<Vec<char>> {
2021-11-16 20:23:27 -08:00
many1(one_of("0123456789abcdefABCDEF"))(input)
}
2021-11-17 03:59:16 -08:00
fn digit_group_bin(input: Span) -> ParseResult<Vec<char>> {
2021-11-16 20:23:27 -08:00
many1(one_of("01"))(input)
}
fn parse_binary(digits: &str) -> Result<u64, &'static str> {
let mut result: u64 = 0;
let mut multiplier = 1;
for d in digits.chars().rev() {
match d {
'1' => result += multiplier,
'0' => (),
'_' => continue,
_ => unreachable!(),
}
multiplier = match multiplier.checked_mul(2) {
Some(m) => m,
None => return Err("Binary expression will overflow"),
}
}
Ok(result)
}
fn parse_hex(digits: &str) -> Result<u64, &'static str> {
let mut result: u64 = 0;
let mut multiplier: u64 = 1;
for d in digits.chars().rev() {
if d == '_' {
continue;
}
match d.to_digit(16) {
Some(n) => result += n as u64 * multiplier,
None => return Err("Internal parser error: invalid hex digit"),
}
multiplier = match multiplier.checked_mul(16) {
Some(m) => m,
None => return Err("Hexadecimal expression will overflow"),
}
}
Ok(result)
}
2021-11-18 04:01:40 -08:00
#[derive(Debug)]
struct BinopSequence {
first: ExpressionKind,
rest: Vec<(BinOp, ExpressionKind)>,
}
impl BinopSequence {
fn do_precedence(self, store: &mut IdStore<ASTItem>) -> ExpressionKind {
fn helper(
precedence: i32,
lhs: ExpressionKind,
rest: &mut Vec<(BinOp, ExpressionKind)>,
store: &mut IdStore<ASTItem>,
) -> Expression {
let mut lhs = Expression::new(store.fresh(), lhs);
while let Some((next_op, next_rhs)) = rest.pop() {
let new_precedence = next_op.get_precedence();
if precedence >= new_precedence {
rest.push((next_op, next_rhs));
break;
}
let rhs = helper(new_precedence, next_rhs, rest, store);
lhs = Expression::new(
store.fresh(),
ExpressionKind::BinExp(next_op, Box::new(lhs), Box::new(rhs)),
);
}
lhs
}
let mut as_stack = self.rest.into_iter().rev().collect();
helper(BinOp::min_precedence(), self.first, &mut as_stack, store).kind
}
}
2021-11-16 20:23:27 -08:00
#[cfg(test)]
mod test {
use pretty_assertions::assert_eq;
use super::*;
2021-11-17 03:59:16 -08:00
macro_rules! span {
2021-11-17 12:45:55 -08:00
($func:expr, $input:expr) => {{
2021-11-17 12:45:42 -08:00
let id_store: IdStore<ASTItem> = IdStore::new();
2021-11-17 12:45:55 -08:00
let span = Span::new_extra($input, Rc::new(RefCell::new(id_store)));
2021-11-17 12:45:42 -08:00
$func(span).map(|(span, x)| (*span.fragment(), x))
2021-11-17 12:45:55 -08:00
}};
2021-11-17 01:54:35 -08:00
}
2021-11-16 20:23:27 -08:00
#[test]
fn combinator_test1() {
2021-11-17 03:59:16 -08:00
assert_eq!(span!(digits(digit_group_dec), "342"), Ok(("", vec!['3', '4', '2'])));
assert_eq!(span!(bin_literal, "0b1111qsdf"), Ok(("qsdf", 15)));
2021-11-18 03:16:43 -08:00
assert_eq!(span!(bare_string_literal, r#""fah""#), Ok(("", "fah".to_string())));
assert_eq!(span!(bare_string_literal, r#""""#), Ok(("", "".to_string())));
2021-11-19 09:59:08 -08:00
assert_eq!(*span!(identifier_span, "modulek").unwrap().1.fragment(), "modulek");
assert!(span!(identifier_span, "module").is_err());
2021-11-16 20:23:27 -08:00
}
2021-11-17 01:54:35 -08:00
#[test]
fn combinator_test_ws0() {
2021-11-17 03:59:16 -08:00
assert_eq!(span!(block_comment, "/*yolo*/"), Ok(("", ())));
assert_eq!(span!(block_comment, "/*yolo*/ jumpy /*nah*/"), Ok((" jumpy /*nah*/", ())));
assert_eq!(span!(ws0, "/* yolo */ "), Ok(("", ())));
assert_eq!(span!(ws0, "/* /* no */ yolo */ "), Ok(("", ())));
2021-11-17 01:54:35 -08:00
}
2021-11-16 20:23:27 -08:00
#[test]
fn combinator_test2() {
2021-11-17 01:54:35 -08:00
for s in [" 15", " 0b1111", " 1_5_", "0XF__", "0Xf"].iter() {
2021-11-18 21:02:33 -08:00
assert_eq!(span!(expression_kind(true), s).unwrap().1, ExpressionKind::NatLiteral(15));
2021-11-16 20:23:27 -08:00
}
2021-11-17 01:54:35 -08:00
2021-11-17 12:45:55 -08:00
assert_eq!(
2021-11-18 21:02:33 -08:00
span!(expression_kind(true), " /*gay*/ true").unwrap().1,
ExpressionKind::BoolLiteral(true)
);
2021-11-16 20:23:27 -08:00
}
}