@@ -88,11 +88,23 @@ parens = wrapped tokLeftParen tokRightParen
8888braces :: forall a . Parser a -> Parser (Wrapped a )
8989braces = 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+
9197layoutNonEmpty :: 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
97109parseModule :: Parser (Recovered Module )
98110parseModule = do
@@ -678,20 +690,7 @@ parseDo = do
678690parseAdo :: Parser (Recovered Expr )
679691parseAdo = 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