Skip to content

Commit 48dfc99

Browse files
committed
Kore.Parser.Parser: De-sugar \right-assoc
1 parent ce3fa79 commit 48dfc99

File tree

2 files changed

+80
-12
lines changed

2 files changed

+80
-12
lines changed

kore/src/Kore/Parser/Parser.hs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,39 @@ parseSymbolOrAliasRemainder symbolOrAliasConstructor = do
275275

276276
{- | Parse the @\\left-assoc@ syntactic sugar.
277277
278+
@parseLeftAssoc@ assumes that the initial identifier has already been parsed.
279+
278280
@
279-
"\\left-assoc" '{' '}' '(' <application-pattern> ')'
281+
_ '{' '}' '(' <application-pattern> ')'
280282
@
281283
-}
282284
parseLeftAssoc :: Parser ParsedPattern
283-
parseLeftAssoc = do
285+
parseLeftAssoc = parseAssoc foldl1
286+
287+
{- | Parse the @\\right-assoc@ syntactic sugar.
288+
289+
@parseRightAssoc@ assumes that the initial identifier has already been parsed.
290+
291+
@
292+
_ '{' '}' '(' <application-pattern> ')'
293+
@
294+
-}
295+
parseRightAssoc :: Parser ParsedPattern
296+
parseRightAssoc = parseAssoc foldr1
297+
298+
{- | Parse the @\\left-assoc@ or @\\right-assoc@ syntactic sugar.
299+
300+
@parseAssoc@ assumes that the initial identifier has already been parsed.
301+
302+
@
303+
_ '{' '}' '(' <application-pattern> ')'
304+
@
305+
-}
306+
parseAssoc
307+
:: (forall r. (r -> r -> r) -> [r] -> r)
308+
-- ^ folding function: 'foldl1' or 'foldr1'
309+
-> Parser ParsedPattern
310+
parseAssoc foldAssoc = do
284311
braces $ pure ()
285312
application <- parens $ parseApplication parsePattern
286313
let mkApplication child1 child2 =
@@ -289,7 +316,7 @@ parseLeftAssoc = do
289316
& embedParsedPattern
290317
case applicationChildren application of
291318
[] -> fail "expected one or more arguments"
292-
children -> pure (foldl1 mkApplication children)
319+
children -> pure (foldAssoc mkApplication children)
293320

294321
{- | Parse a built-in Kore (matching logic) pattern.
295322
@@ -361,6 +388,7 @@ parseKoreRemainder identifier =
361388
"dv" -> embedParsedPattern . DomainValueF <$> parseDomainValue
362389
-- Syntax sugar
363390
"left-assoc" -> parseLeftAssoc
391+
"right-assoc" -> parseRightAssoc
364392

365393
_ -> empty
366394

kore/test/Test/Kore/Parser/Parser.hs

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,11 @@ applicationPatternParserTests =
365365
, symbolOrAliasParams = []
366366
}
367367
, applicationChildren =
368+
map embedParsedPattern
368369
[ StringLiteral "a"
369370
& Const & StringLiteralF
370-
& embedParsedPattern
371371
, StringLiteral "b"
372372
& Const & StringLiteralF
373-
& embedParsedPattern
374373
]
375374
}
376375
)
@@ -390,21 +389,62 @@ applicationPatternParserTests =
390389
, symbolOrAliasParams = []
391390
}
392391
, applicationChildren =
393-
[ StringLiteral "a"
394-
& Const & StringLiteralF
395-
& embedParsedPattern
396-
, StringLiteral "b"
397-
& Const & StringLiteralF
398-
& embedParsedPattern
392+
map embedParsedPattern
393+
[ StringLiteral "a" & Const & StringLiteralF
394+
, StringLiteral "b" & Const & StringLiteralF
399395
]
400396
}
401397
, StringLiteral "c" & Const & StringLiteralF
402398
]
403399
}
404400
)
401+
, success "\\right-assoc{}(c{}(\"a\"))"
402+
( StringLiteral "a"
403+
& Const & StringLiteralF
404+
& embedParsedPattern
405+
)
406+
, success "\\right-assoc{}(c{}(\"a\", \"b\"))"
407+
( embedParsedPattern $ ApplicationF Application
408+
{ applicationSymbolOrAlias =
409+
SymbolOrAlias
410+
{ symbolOrAliasConstructor = testId "c"
411+
, symbolOrAliasParams = []
412+
}
413+
, applicationChildren =
414+
map embedParsedPattern
415+
[ StringLiteral "a" & Const & StringLiteralF
416+
, StringLiteral "b" & Const & StringLiteralF
417+
]
418+
}
419+
)
420+
, success "\\right-assoc{}(c{}(\"a\", \"b\", \"c\"))"
421+
( embedParsedPattern $ ApplicationF Application
422+
{ applicationSymbolOrAlias =
423+
SymbolOrAlias
424+
{ symbolOrAliasConstructor = testId "c"
425+
, symbolOrAliasParams = []
426+
}
427+
, applicationChildren =
428+
map embedParsedPattern
429+
[ StringLiteral "a" & Const & StringLiteralF
430+
, ApplicationF Application
431+
{ applicationSymbolOrAlias =
432+
SymbolOrAlias
433+
{ symbolOrAliasConstructor = testId "c"
434+
, symbolOrAliasParams = []
435+
}
436+
, applicationChildren =
437+
map embedParsedPattern
438+
[ StringLiteral "b" & Const & StringLiteralF
439+
, StringLiteral "c" & Const & StringLiteralF
440+
]
441+
}
442+
]
443+
}
444+
)
405445
, FailureWithoutMessage
406446
[ "", "var", "v:", ":s", "c(s)", "c{s}"
407-
, "\\left-assoc{}(c{}())"
447+
, "\\left-assoc{}(c{}())", "\\right-assoc{}(c{}())"
408448
]
409449
]
410450
bottomPatternParserTests :: [TestTree]

0 commit comments

Comments
 (0)