Skip to content

Commit 91beb67

Browse files
committed
Cleanup ado parsing
With the fixes to indentation recovery in #63, the workaround for empty ado parsing is no longer necessary. This effectively reverts #46.
1 parent 4913300 commit 91beb67

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

src/PureScript/CST/Parser.purs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,23 @@ parens = wrapped tokLeftParen tokRightParen
8888
braces :: forall a. Parser a -> Parser (Wrapped a)
8989
braces = wrapped tokLeftBrace tokRightBrace
9090

91+
layoutStatements :: forall f a. (a -> Array a -> f a) -> Parser a -> Parser (f a)
92+
layoutStatements f statementParser = ado
93+
head <- statementParser
94+
tail <- many (tokLayoutSep *> statementParser)
95+
in f head tail
96+
9197
layoutNonEmpty :: forall a. Parser a -> Parser (NonEmptyArray a)
92-
layoutNonEmpty valueParser = ado
93-
head <- tokLayoutStart *> valueParser
94-
tail <- many (tokLayoutSep *> valueParser) <* tokLayoutEnd
95-
in NonEmptyArray.cons' head tail
98+
layoutNonEmpty statementParser =
99+
tokLayoutStart *> layoutStatements NonEmptyArray.cons' statementParser <* tokLayoutEnd
100+
101+
layout :: forall a. Parser a -> Parser (Array a)
102+
layout statementParser =
103+
tokLayoutStart *> statements <* tokLayoutEnd
104+
where
105+
statements =
106+
layoutStatements Array.cons statementParser
107+
<|> pure []
96108

97109
parseModule :: Parser (Recovered Module)
98110
parseModule = do
@@ -678,20 +690,7 @@ parseDo = do
678690
parseAdo :: Parser (Recovered Expr)
679691
parseAdo = do
680692
keyword <- tokQualifiedKeyword "ado"
681-
-- A possibly-empty version of `layoutNonEmpty` to handle empty `ado in`
682-
statements <- do
683-
let
684-
-- `recoverDoStatement` recovers too much if it is immediately
685-
-- confronted with `TokLayoutEnd`, since that is associated with a
686-
-- `layoutStack` _of the parent_ as opposed to the stuff we actually
687-
-- want to recover, which we would correctly guess if we saw a statement
688-
-- or two inside the block
689-
valueParser = recoverDoStatement parseDoStatement
690-
nonEmptyCase =
691-
Array.cons <$> valueParser <*> many (tokLayoutSep *> valueParser)
692-
_ <- tokLayoutStart
693-
-- So we explicitly handle `TokLayoutEnd` ahead of time:
694-
[] <$ tokLayoutEnd <|> nonEmptyCase <* tokLayoutEnd
693+
statements <- layout (recoverDoStatement parseDoStatement)
695694
in_ <- tokKeyword "in"
696695
result <- parseExpr
697696
pure $ ExprAdo { keyword, statements, in: in_, result }

0 commit comments

Comments
 (0)