Class: ABNFParser

Inherits:
Object
  • Object
show all
Includes:
EBNF::PEG::Parser
Defined in:
examples/abnf/parser.rb

Constant Summary collapse

ALPHA =

Regular expressions for both “Core” and ABNF-specific terminals.

%r{[\x41-\x5A\x61-\x7A]}
VCHAR =
%r{[\x20-\x7E]}
WSP =
%r{[\x20\x09]}
CRLF =
%r{\x0D?\x0A}
COMMENT =
%r{;(?:#{WSP}|#{VCHAR})*#{CRLF}}
C_NL =
%r{#{COMMENT}|#{CRLF}}
C_WSP =
%r{#{WSP}|(?:#{C_NL}#{WSP})}

Instance Attribute Summary collapse

Attributes included from EBNF::PEG::Parser

#packrat, #scanner, #whitespace

Instance Method Summary collapse

Methods included from EBNF::PEG::Parser

#clear_packrat, #debug, #depth, #error, #find_rule, #onFinish, #onStart, #onTerminal, #parse, #prod_data, #progress, #terminal_options, #terminal_regexp, #update_furthest_failure, #warn

Constructor Details

#initialize(input, **options, &block) ⇒ EBNFParser

Parser invocation.

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

Parameters:

  • input (#read, #to_s)
  • options (Hash{Symbol => Object})

Options Hash (**options):

  • :level (Boolean)

    Trace level. 0(debug), 1(info), 2(warn), 3(error).



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'examples/abnf/parser.rb', line 239

def initialize(input, **options, &block)
  # If the `level` option is set, instantiate a logger for collecting trace information.
  if options.key?(:level)
    options[:logger] ||= Logger.new(STDERR).
      tap {|x| x.level = options[:level]}.
      tap {|x| x.formatter = lambda {|severity, datetime, progname, msg| "#{severity} #{msg}\n"}}
  end

  # Read input, if necessary, which will be used in a Scanner.
  @input = input.respond_to?(:read) ? input.read : input.to_s

  @parsed_rules = {}

  # Parses into `@parsed_rules`
  parse(@input,
        :rulelist,        # Starting rule
        ABNFMeta::RULES,  # PEG rules
        whitespace: '',   # No implicit whitespace
        **options)
end

Instance Attribute Details

#parsed_rulesHash{Symbol => EBNF::Rule} (readonly)

Hash of generated EBNF::Rule objects by symbol

Returns:



28
29
30
# File 'examples/abnf/parser.rb', line 28

def parsed_rules
  @parsed_rules
end

Instance Method Details

#astArray<EBNF::Rule>

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

Returns:



264
265
266
267
268
269
270
271
272
# File 'examples/abnf/parser.rb', line 264

def ast
  # Add built-in rules for standard ABNF rules not 
  parsed_rules.values.map(&:symbols).flatten.uniq.each do |sym|
    rule = ABNFCore::RULES.detect {|r| r.sym == sym}
    parsed_rules[sym] ||= rule if rule
  end

  parsed_rules.values
end

#to_sxp(**options) ⇒ String

Output formatted S-Expression of grammar

Returns:

  • (String)


277
278
279
280
281
# File 'examples/abnf/parser.rb', line 277

def to_sxp(**options)
  require 'sxp' unless defined?(SXP)
  # Output rules as a formatted S-Expression
  SXP::Generator.string(ast.map(&:for_sxp))
end