From 1797136156d9a9ea388f27efe275c2ee79e84c95 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Sat, 20 Nov 2021 23:09:40 -0800 Subject: [PATCH] Some refactoring in parser combinator --- schala-lang/src/parsing/combinator.rs | 29 ++++++++++----------------- schala-lang/src/symbol_table/test.rs | 6 ++++-- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/schala-lang/src/parsing/combinator.rs b/schala-lang/src/parsing/combinator.rs index 57d4c8c..c829f54 100644 --- a/schala-lang/src/parsing/combinator.rs +++ b/schala-lang/src/parsing/combinator.rs @@ -74,7 +74,7 @@ fn tok<'a, O>( fn toknl<'a, O>( input_parser: impl Parser, O, VerboseError>>, ) -> impl FnMut(Span<'a>) -> IResult, O, VerboseError>> { - 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,31 +330,24 @@ fn type_body(input: Span) -> ParseResult { "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 { +fn record_variant(input: Span) -> ParseResult, 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(','))), - ), - pair(many0(statement_delimiter), tok(char('}'))), + delimited( + pair(tok(char('{')), many0(statement_delimiter)), + terminated( + separated_list1(toknl(char(',')), toknl(record_variant_item)), + opt(toknl(char(','))), ), - VariantKind::Record, + pair(many0(statement_delimiter), tok(char('}'))), ), )(input) } @@ -369,7 +362,7 @@ fn variant_spec(input: Span) -> ParseResult { 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)?; diff --git a/schala-lang/src/symbol_table/test.rs b/schala-lang/src/symbol_table/test.rs index 381eb5e..450bb91 100644 --- a/schala-lang/src/symbol_table/test.rs +++ b/schala-lang/src/symbol_table/test.rs @@ -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 + , } "#;