Some refactoring in parser combinator

This commit is contained in:
Greg Shuflin 2021-11-20 23:09:40 -08:00
parent 3e422291f4
commit 1797136156
2 changed files with 15 additions and 20 deletions

View File

@ -74,7 +74,7 @@ fn tok<'a, O>(
fn toknl<'a, O>(
input_parser: impl Parser<Span<'a>, O, VerboseError<Span<'a>>>,
) -> impl FnMut(Span<'a>) -> IResult<Span<'a>, O, VerboseError<Span<'a>>> {
context("token/newline", preceded(ws0, input_parser))
context("token/newline", preceded(pair(many0(tok(statement_delimiter)), ws0), input_parser))
}
fn kw<'a>(keyword_str: &'static str) -> impl FnMut(Span<'a>) -> ParseResult<()> {
@ -330,32 +330,25 @@ fn type_body(input: Span) -> ParseResult<TypeBody> {
"type-body",
alt((
map(
delimited(
tok(char('{')),
separated_list1(tok(char(',')), record_variant_item),
tok(char('}')),
),
move |items| TypeBody::ImmediateRecord { id, fields: items },
record_variant,
move |fields| TypeBody::ImmediateRecord { id, fields },
),
map(separated_list0(tok(char('|')), variant_spec), TypeBody::Variants),
)),
)(input)
}
fn record_variant(input: Span) -> ParseResult<VariantKind> {
fn record_variant(input: Span) -> ParseResult<Vec<(Rc<String>, TypeIdentifier)>> {
context(
"record-variant",
map(
delimited(
pair(tok(char('{')), many0(statement_delimiter)),
terminated(
separated_list1(pair(tok(char(',')), many0(statement_delimiter)), record_variant_item),
opt(tok(char(','))),
separated_list1(toknl(char(',')), toknl(record_variant_item)),
opt(toknl(char(','))),
),
pair(many0(statement_delimiter), tok(char('}'))),
),
VariantKind::Record,
),
)(input)
}
@ -369,7 +362,7 @@ fn variant_spec(input: Span) -> ParseResult<Variant> {
let id = fresh_id(&input);
let (rest, (name, kind)) = alt((
pair(identifier, record_variant),
pair(identifier, map(record_variant, VariantKind::Record)),
pair(identifier, tuple_variant),
map(identifier, |ident| (ident, VariantKind::UnitStruct)),
))(input)?;

View File

@ -254,9 +254,11 @@ fn duplicate_modules() {
fn duplicate_struct_members() {
let source = r#"
type Tarak = Tarak {
loujet: i32,
mets: i32,
loujet: i32
,
mets: i32,
mets: i32
,
}
"#;