Module: Grammar::Molecules

Included in:
Grammar
Defined in:
lib/grammar.rb

Overview

include this somewhere to have access to methods that

Constant Summary collapse

NULL =

Zero-width Grammar that always passes and matches nothing

Grammar.new { |e| e[true] }
EOF =

Zero-width Grammar that matches the end-of-file (or end-of-input)

Grammar.new { |e| e.eof }
ANY =

Grammar that matches any single element (not EOF)

Grammar.new { |e| e.any }

Instance Method Summary collapse

Instance Method Details

#Always(&block) ⇒ Object

Grammar that yields an engine to a Grammar block and expects that the result always passes.



232
233
234
# File 'lib/grammar.rb', line 232

def Always(&block) # :yield: engine
    Grammar { |e| e.always(&block) }
end

#Chain(pattern_sequence) ⇒ Object

Grammar that matches the elements in pattern_sequence. Element is used for each pattern in pattern_sequence. Starting from index 0 #[] is used to access pattern_sequence until it returns nil.



179
180
181
182
183
184
185
# File 'lib/grammar.rb', line 179

def Chain(pattern_sequence)
    p = pattern_sequence[0] or return NULL
    g = E(p)
    i = 0
    g += E(p) while p = pattern_sequence[i+=1]
    g
end

#Check(message = nil, &block) ⇒ Object

Grammar that fails with a message when the Grammar block doesn’t pass.



226
227
228
# File 'lib/grammar.rb', line 226

def Check(message=nil, &block) # :yield: engine
    Grammar(&block) | Fail(message)
end

#CommonObject

Grammar that is the result of yielding an engine. This adds a convenience so that action blocks don’t need to receive the engine.



238
239
240
# File 'lib/grammar.rb', line 238

def Common # :yield: engine
    Grammar { |e| yield(e)[e] }
end

#Element(pattern) ⇒ Object Also known as: E

Grammar that matches to a single element. An object responding to #=== is used to do the matching.



170
171
172
# File 'lib/grammar.rb', line 170

def Element(pattern)
    Grammar { |e| e.match(pattern) }
end

#Fail(message = nil) ⇒ Object

Grammar that always fails (with a message)



188
189
190
# File 'lib/grammar.rb', line 188

def Fail(message=nil)
    Grammar { |e| e.failure(message) }
end

#Grammar(&block) ⇒ Object

Eliminate the need for the “.new”. Would be better if objects were callable so we wouldn’t need this.



164
165
166
# File 'lib/grammar.rb', line 164

def Grammar(&block)
    Grammar.new(&block)
end

#Output(&block) ⇒ Object

Grammar that modifies the output buffer. The current buffer and optionally the engine are yielded to a block which should return what the new output buffer should be. WARNING: only use this inside of a Grammar where #group or #redirect has been applied. It probably won’t work as expected in other places.



248
249
250
# File 'lib/grammar.rb', line 248

def Output(&block) # :yield: buf[, engine]
    Grammar { |e| e.output(&block) }
end

#Recurse(inner = Grammar()) ⇒ Object

Grammar that handles recursion. inner represents a call back to the resulting Grammar. An inner may be given or it will be automatically generated (as an empty/invalid Grammar). inner is yielded to the block which should return the resulting Grammar (and be based on inner). Middle, right, and left recursion should be handled by the engine properly, but there may be restrictions on left recursion (i.e. must be the very first thing in the resulting Grammar).



219
220
221
222
# File 'lib/grammar.rb', line 219

def Recurse(inner=Grammar()) # :yield: inner
    outer = yield(inner)
    Grammar { |e| e.recurse(inner, &outer) }
end

#Variables(*vals, &block) ⇒ Object

Grammar that shares/uses one or more variables. Optional initial values can be given for the variables. The block should take variable reference objects (one or more) and should return a Grammar that uses the variables in action blocks. Use var#[] to get the value in a variable reference object and var#<< to set the value.



198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/grammar.rb', line 198

def Variables(*vals, &block) # :yield: *vars
    Grammar { |e|
        e.variables(block.arity) { |*vars|
            init = []
            vals.each_with_index { |val, i|
                init << (vars[i] << e[val,true])
            }
            init << e[true]
            Grammar { e.steps(*init) } + yield(*vars)
        }
    }
end