Class: Peggy::ABNF

Inherits:
Builder show all
Defined in:
lib/abnc.rb

Overview

Implements the RFC 4234 ABNF, one of several grammars supported.

Keep in mind, though, that the ABNF semantics is that of a BNF, i.e., non-deterministic; while the packrat parser underlying peggy is a PEG parser, which cuts decision points once a successful parse is made. You may have to exchange alternatives, e.g., for parsing ABNF itself using ABNF, you have to change RFC 4234’s rule

repeat         =  1*DIGIT / (*DIGIT "*" *DIGIT)

into

repeat         =  (*DIGIT "*" *DIGIT) / 1*DIGIT

as otherwise “1*(…)” will start to parse as the first alternative and never try the second.

Defined Under Namespace

Classes: ABNFParser

Instance Attribute Summary

Attributes inherited from Builder

#parent, #productions

Attributes inherited from Parser

#debug_flag, #ignore_productions, #parse_results, #source_text

Instance Method Summary collapse

Methods inherited from Builder

#[], #alt, #eof, #initialize, #lit, #many, #method_missing, #neg, #opt, #parse?, #pos, #reset!, #seq, #some, #to_s

Methods inherited from Parser

#[], #_memoize, #allow?, #ast?, #check?, #correct_regexp!, #dissallow?, #eof, #ignore?, #literal?, #match?, #parse?, #query?, #regexp?, #string?

Constructor Details

This class inherits a constructor from Peggy::Builder

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Peggy::Builder

Instance Method Details

#compile!(text, options = {}) ⇒ Object

ABNFParser



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/abnc.rb', line 73

def compile! text, options={}
  reset!
  compiler = ABNFParser.new
# puts compiler
  # compiler.debug_flag = true
  result = compiler.parse? :grammar, text
#pp compiler.parse_results
  # raise "Invalid ABNF grammar" unless result
  grammar = compiler.ast? :ignore=>:s #options
###puts grammar
  raise "Invalid ABNF grammar at char #{compiler.parse_results.keys.max}" unless result
  grammar.each :prod do |definition|
    send(symbolize(definition.prodname.to_s)) do
      build_prodalt definition.prodalt
    end
  end
#puts to_s
end