Skip to content

Lark | Creating A Grammar For YAML #1482

Open
@jordanparker6

Description

@jordanparker6

What is your question?

Trying to create a grammar that can parse any YAML file.

If you're having trouble with your code or grammar

Here is the code I am using.

%import common.NEWLINE
%import common.ESCAPED_STRING
%import common.SIGNED_NUMBER
%import common.WS_INLINE
%ignore WS_INLINE

start: document

document: element+

element: mapping
       | sequence
       | scalar

mapping: mapping_pair+

mapping_pair: key ":" WS_INLINE? value NEWLINE?

key: scalar

value: scalar
     | mapping_block
     | sequence_block

mapping_block: NEWLINE INDENT mapping_pair+ DEDENT

sequence: sequence_item+

sequence_item: "-" WS_INLINE? value NEWLINE?

sequence_block: NEWLINE INDENT sequence_item+ DEDENT

scalar: ESCAPED_STRING       -> string
      | SIGNED_NUMBER        -> number
      | BOOL                 -> boolean
      | NULL                 -> null
      | unquoted_string      -> unquoted

unquoted_string: /[^{}\[\],":\s\n][^{}\[\],":\n]*/

BOOL: "true" | "false"
NULL: "null" | "~"

%declare INDENT DEDENT
%ignore WS_INLINE

Explain what you're trying to do, and what is obstructing your progress.

I new to CFG and seem to struggle with the INDENT / DEDENTs.

What is going wrong with this and does anyone have a working example on how I could use a CFG to parse a YAML file?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions