parser.rb

EBNF Parser for ABNF.

Produces an Abstract Synatx Tree in S-Expression form for the input grammar file

Regular expressions for both "Core" and ABNF-specific terminals.

Hash of generated {EBNF::Rule} objects by symbol

@return [Hash{Symbol => EBNF::Rule}]

The following ABNF grammar rules are treated as terminals.

rulename ::= ALPHA (ALPHA | DIGIT | "-")*

defined_as ::= c_wsp* ("=" | "=/") c_wsp*

quoted_string ::= DQUOTE [#x20-#x21#x23-#x7E]* DQUOTE

bin_val ::= "b" BIT+ (("." BIT+)+ | ("-" BIT+))?

Interpret segments in binary creating a sequence of hex characters or a string

Interpret as a range

Interpret as a single HEX character

dec_val ::= "d" DIGIT+ (("." DIGIT+)+ | ("-" DIGIT+))?

Interpret segments in decimal creating a sequence of hex characters or a string

Interpret as a range

Interpret as a single HEX character

hex_val ::= "x" HEXDIG+ (("." HEXDIG+)+ | ("-" HEXDIG+))?

Interpret segments in hexadecimal creating a sequence of hex characters or a string

Interpret as a range

Interpret as a single HEX character

c_wsp ::= WSP | (c_nl WSP)

c_nl ::= comment | CRLF

DIGIT ::= [#x30-#x39]

Non-terminal productions

The start_production on :rule allows the parser to present the value as a single Hash, rather than an array of individual hashes.

rule ::= rulename defined_as elements c_nl

value contains an expression. Invoke callback

append to rule alternate

append alternatives to rule

add elements as last alternative

There shouldn't be an existing rule

elements ::= alternation c_wsp*

alternation ::= concatenation (c_wsp* "/" c_wsp* concatenation)*

The _aleteration_2 rule comes from the expanded PEG grammar and serves as an opportunity to custommize the values presented to the aleteration rule.

concatenation::= repetition (c_wsp+ repetition)*

repetition ::= repeat? element

repeat ::= DIGIT+ | (DIGIT* "*" DIGIT*)

element ::= rulename | group | option | char_val | num_val | prose_val

group ::= "(" c_wsp* alternation c_wsp* ")"

option ::= "[" c_wsp* alternation c_wsp* "]"

case_insensitive_string ::= "%i"? quoted_string

Only need to use case-insensitive if there are alphabetic characters in the string.

case_sensitive_string ::= "%s" quoted_string

num_val ::= "%" (bin_val | dec_val | hex_val)

Parser invocation.

On start, yield ourselves if a block is given, otherwise, return this parser instance

@param [#read, #to_s] input @param [Hash{Symbol => Object}] options @option options [Boolean] :level Trace level. 0(debug), 1(info), 2(warn), 3(error). @return [EBNFParser]

If the level option is set, instantiate a logger for collecting trace information.

Read input, if necessary, which will be used in a Scanner.

Parses into @parsed_rules

The AST includes the parsed rules along with built-in rules for ABNF used within the parsed grammar.

@return [ArrayEBNF::Rule]

Add built-in rules for standard ABNF rules not

Output formatted S-Expression of grammar

@return [String]

Output rules as a formatted S-Expression

Generate a combination of seq and string to represent a sequence of characters

@param [Array] characters @return [String,Array]

Either return the sequence, or a string