Class: EBNFPegParser
- Inherits:
-
Object
- Object
- EBNFPegParser
- Includes:
- EBNF::PEG::Parser, EBNF::Terminals
- Defined in:
- examples/ebnf-peg-parser/parser.rb
Constant Summary
Constants included from EBNF::Terminals
EBNF::Terminals::CHAR, EBNF::Terminals::HEX, EBNF::Terminals::LHS, EBNF::Terminals::O_RANGE, EBNF::Terminals::PASS, EBNF::Terminals::POSTFIX, EBNF::Terminals::RANGE, EBNF::Terminals::RANGE_NOT_LHS, EBNF::Terminals::R_CHAR, EBNF::Terminals::STRING1, EBNF::Terminals::STRING2, EBNF::Terminals::SYMBOL, EBNF::Terminals::SYMBOL_BASE
Instance Attribute Summary collapse
-
#ast ⇒ Array<EBNF::Rule>
readonly
Abstract syntax tree from parse.
Attributes included from EBNF::PEG::Parser
#packrat, #scanner, #whitespace
Instance Method Summary collapse
-
#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
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'examples/ebnf-peg-parser/parser.rb', line 273 def initialize(input, **, &block) # If the `level` option is set, instantiate a logger for collecting trace information. if .key?(:level) [:logger] ||= Logger.new(STDERR). tap {|x| x.level = [: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 parsing_terminals = false @ast = [] parse(@input, :ebnf, EBNFPegMeta::RULES, # Use an optimized Regexp for whitespace whitespace: EBNF::Terminals::PASS, ** ) do |context, *data| rule = case context when :terminals # After parsing `@terminals` # This changes the state of the parser to treat subsequent rules as terminals. parsing_terminals = true rule = EBNF::Rule.new(nil, nil, data.first, kind: :terminals) when :pass # After parsing `@pass` # This defines a specific rule for whitespace. rule = EBNF::Rule.new(nil, nil, data.first, kind: :pass) when :rule # A rule which has already been turned into a `Rule` object. rule = data.first rule.kind = :terminal if parsing_terminals rule end @ast << rule if rule end @ast end |
Instance Attribute Details
#ast ⇒ Array<EBNF::Rule> (readonly)
Abstract syntax tree from parse
18 19 20 |
# File 'examples/ebnf-peg-parser/parser.rb', line 18 def ast @ast end |
Instance Method Details
#to_sxp(**options) ⇒ String
Output formatted S-Expression of grammar
315 316 317 318 319 |
# File 'examples/ebnf-peg-parser/parser.rb', line 315 def to_sxp(**) require 'sxp' unless defined?(SXP) # Output rules as a formatted S-Expression SXP::Generator.string(@ast.map(&:for_sxp)) end |