mirror of
https://github.com/AdaCore/langkit.git
synced 2026-02-12 12:28:12 -08:00
PB08-014: Add Pick utility parser
Change-Id: I6755a1753bd5312620cdcbf92b4d332a57e2037f
This commit is contained in:
@@ -633,6 +633,32 @@ def always_make_progress(parser):
|
||||
return not isinstance(parser, (Opt, Null))
|
||||
|
||||
|
||||
def Pick(*parsers):
|
||||
"""
|
||||
Utility around Row and Extract, that will automatically scan a Row, remove
|
||||
tokens and ignored sub parses, and extract the only significant sub-result.
|
||||
|
||||
If there are several significant sub-results, raises an error.
|
||||
"""
|
||||
parsers = [resolve(p) for p in parsers if p]
|
||||
pick_parser_idx = -1
|
||||
ignore = (Tok, Discard)
|
||||
for i, p in enumerate(parsers):
|
||||
if (isinstance(p, ignore)
|
||||
or isinstance(p, Opt) and isinstance(p.parser, ignore)):
|
||||
continue
|
||||
check_source_language(
|
||||
pick_parser_idx == -1,
|
||||
"Pick parser can have only one sub-parser that is not a token"
|
||||
)
|
||||
pick_parser_idx = i
|
||||
|
||||
if pick_parser_idx == -1:
|
||||
return Row(*parsers)
|
||||
else:
|
||||
return Row(*parsers)[pick_parser_idx]
|
||||
|
||||
|
||||
class Row(Parser):
|
||||
"""Parser that matches a what sub-parsers match in sequence."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user