Class: EBNF::ABNF

Inherits:
Object
  • Object
show all
Includes:
PEG::Parser
Defined in:
lib/ebnf/abnf.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 PEG::Parser

#packrat, #scanner, #whitespace

Instance Method Summary collapse

Methods included from PEG::Parser

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

Constructor Details

#initialize(input, **options) ⇒ 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).



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/ebnf/abnf.rb', line 235

def initialize(input, **options)
  # If the `level` option is set, instantiate a logger for collecting trace information.
  if options.has_key?(:level)
    options[:logger] = Logger.new(STDERR)
    options[:logger].level = options[:level]
    options[:logger].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)
rescue EBNF::PEG::Parser::Error => e
  raise SyntaxError, e.message
end

Instance Attribute Details

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

Hash of generated Rule objects by symbol

Returns:



24
25
26
# File 'lib/ebnf/abnf.rb', line 24

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:



262
263
264
265
266
267
268
269
270
# File 'lib/ebnf/abnf.rb', line 262

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