Class: ABNFParser
- Inherits:
-
Object
- Object
- ABNFParser
- 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
-
#parsed_rules ⇒ Hash{Symbol => EBNF::Rule}
readonly
Hash of generated EBNF::Rule objects by symbol.
Attributes included from EBNF::PEG::Parser
#packrat, #scanner, #whitespace
Instance Method Summary collapse
-
#ast ⇒ Array<EBNF::Rule>
The AST includes the parsed rules along with built-in rules for ABNF used within the parsed grammar.
-
#initialize(input, **options, &block) ⇒ EBNFParser
constructor
Parser invocation.
-
#to_sxp(**options) ⇒ String
Output formatted S-Expression of grammar.
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
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, **, &block) # If the `level` option is set, instantiate a logger for collecting trace information. if .has_key?(:level) [:logger] = Logger.new(STDERR) [:logger].level = [:level] [: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 **) end |
Instance Attribute Details
#parsed_rules ⇒ Hash{Symbol => EBNF::Rule} (readonly)
Hash of generated EBNF::Rule objects by symbol
28 29 30 |
# File 'examples/abnf/parser.rb', line 28 def parsed_rules @parsed_rules end |
Instance Method Details
#ast ⇒ Array<EBNF::Rule>
The AST includes the parsed rules along with built-in rules for ABNF used within the parsed grammar.
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
277 278 279 280 281 |
# File 'examples/abnf/parser.rb', line 277 def to_sxp(**) require 'sxp' unless defined?(SXP) # Output rules as a formatted S-Expression SXP::Generator.string(ast.map(&:for_sxp)) end |