CombinedParsers.BNF
โ Module(Extended) Backus-Naur Form CombinedParser
Defining a EBNF parser can be done with the CombinedParsers.BNF.ebnf
string macro. substitute
is used to combine parts of the definition.
Left recursion is not yet supported (will lead to a stack overflow).
CombinedParsers.BNF.ebnf
โ Constantebnf
Parser to create a CombinedParser
from EBNF syntax:
julia> p = ebnf"""
digit excluding zero = "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
digit = "0" | digit excluding zero ;
natural number = digit excluding zero, { digit } ;
integer = "0" | [ "-" ], natural number ;
"""
|๐ Either
โโ |๐ Either |> with_name(:integer)
โ โโ 0
โ โโ ๐ Sequence
โ โโ \-? |
โ โโ ๐ Sequence |> with_name(:natural number) # branches hidden
โโ ๐ Sequence |> with_name(:natural number)
โ โโ |๐ Either |> with_name(:digit excluding zero) # branches hidden
โ โโ |๐* Either |> with_name(:digit) |> Repeat
โ โโ 0
โ โโ |๐ Either |> with_name(:digit excluding zero) # branches hidden
โโ |๐ Either |> with_name(:digit)
โ โโ 0
โ โโ |๐ Either |> with_name(:digit excluding zero) # branches hidden
โโ |๐ Either |> with_name(:digit excluding zero)
โโ 1
โโ 2
โโ 3
โโ 4
โโ 5
โโ 6
โโ 7
โโ 8
โโ 9
::Union{SubString{String}, Tuple{SubString{String}, Vector{SubString{String}}}, Tuple{AbstractString, Tuple{SubString{String}, Vector{SubString{String}}}}}
julia> p[:integer]("42")
("", ("4", SubString{String}["2"]))
A (too complicated) result type is derived implicitly. You can map transform results of parts of a EBNF parser with the deepmap
function:
julia> deepmap(MatchedSubSequence, p, :integer)[:integer]("42")
"42"
I want to support more BNF variants. Contributions of test cases are welcome! A EBNF Syntax draft built from Wikimedia Ebnf-syntax-diagram.
Left recursion is not yet supported (will lead to a stack overflow).
CombinedParsers.BNF.defining_symbol
โ ConstantSupports BNF and EBNF variants
julia> CombinedParsers.BNF.defining_symbol
CombinedParsers.BNF.concatenate_symbol
โ ConstantSupports BNF and EBNF variants
julia> CombinedParsers.BNF.concatenate_symbol