Skip to content

Allow rules in templates #1566

@Sporarum

Description

@Sporarum

Suggestion

_separated{x, sep}: x (sep x)*  // Define a sequence of 'x sep x sep x ...'

a_list: "[" _separated{"a", ","} "]" // works 
ab_list1: "[" _separated{"a" | "b", ","} "]" // doesn't work
ab_list2: "[" _separated{("a" | "b"), ","} "]" // doesn't work

This forces us to create intermediary rules that are only used inside a single template

ab: "a" | "b"
ab_list3: "[" _separated{ab, ","} "]" // works

Describe alternatives you've considered
Other solution would be to implement expanding templates:

__separated{x, sep}: x (sep x)*  // Define a sequence of 'x sep x sep x ...'

ab_list4: "[" __separated{("a" | "b"), ","} "]" // works, expands to
ab_list5: "[" ("a" | "b") ( "," ("a" | "b") )* "]"

Additional context
This is shortcomming is not documented at https://lark-parser.readthedocs.io/en/latest/grammar.html#templates

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions