Class: Peggy::PEG
Overview
Implements the Parser Expression Grammar (PEG), one of several grammars supported.
Defined Under Namespace
Classes: PEGCompiler
Instance Attribute Summary
Attributes inherited from Parser
#debug_flag, #ignore_productions, #parse_results, #source_text
Instance Method Summary collapse
- #build_prefix(prefix) ⇒ Object
- #build_sequence(sequence) ⇒ Object
- #build_suffix(suffix) ⇒ Object
-
#compile?(text) ⇒ Boolean
PEGCompiler.
-
#initialize ⇒ PEG
constructor
A new instance of PEG.
Methods inherited from Parser
#[], #allow?, #check?, #correct_regexp!, #dissallow?, #eof, #ignore?, #literal?, #match?, #parse?, #query?, #regexp?, #string?
Constructor Details
#initialize ⇒ PEG
Returns a new instance of PEG.
10 11 12 13 |
# File 'lib/peg.rb', line 10 def initialize super build end |
Instance Method Details
#build_prefix(prefix) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/Copy of peg.rb', line 104 def build_prefix prefix if sequence.peek pos do build_suffix prefix.suffix end elsif sequence.disallow neg do build_suffix prefix.suffix end else build_suffix prefix.suffix end end |
#build_sequence(sequence) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/Copy of peg.rb', line 92 def build_sequence sequence if sequence.count('prefix') == 1 build_prefix sequence.prefix else each do sequence.find_each 'prefix' do |prefix| build_prefix prefix end end end end |
#build_suffix(suffix) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/Copy of peg.rb', line 118 def build_suffix suffix if suffix.question opt do build_primary suffix.primary end elsif suffix. neg do build_suffix prefix.suffix end else build_suffix prefix.suffix end end |
#compile?(text) ⇒ Boolean
PEGCompiler
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/Copy of peg.rb', line 71 def compile? text reset! compiler = PEGCompiler.new raise "Invalid grammar" unless compiler.parse! :grammar, text ast = compiler.ast ast.find_each 'definition' do |definition| send definition.identifier.to_sym do if definition.count('sequence') == 1 build_sequence definition.expression.sequence else one do definition.find_each 'sequence' do |sequence| build_sequence sequence end end end end end private def build_sequence sequence if sequence.count('prefix') == 1 build_prefix sequence.prefix else each do sequence.find_each 'prefix' do |prefix| build_prefix prefix end end end end def build_prefix prefix if sequence.peek pos do build_suffix prefix.suffix end elsif sequence.disallow neg do build_suffix prefix.suffix end else build_suffix prefix.suffix end end def build_suffix suffix if suffix.question opt do build_primary suffix.primary end elsif suffix. neg do build_suffix prefix.suffix end else build_suffix prefix.suffix end end end |