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

View File

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