Class: Sexpr::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/sexpr/processor.rb,
lib/sexpr/processor/helper.rb,
lib/sexpr/processor/null_helper.rb,
lib/sexpr/processor/sexpr_coercions.rb

Direct Known Subclasses

Rewriter

Defined Under Namespace

Classes: Helper, NullHelper, SexprCoercions

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Processor

Returns a new instance of Processor.



51
52
53
# File 'lib/sexpr/processor.rb', line 51

def initialize(options = nil)
  @options = options || {}
end

Instance Attribute Details

#optionsObject (readonly)

class << self



49
50
51
# File 'lib/sexpr/processor.rb', line 49

def options
  @options
end

Class Method Details

.build_helper_chain(helpers = self.helpers) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/sexpr/processor.rb', line 34

def build_helper_chain(helpers = self.helpers)
  return NullHelper.new if helpers.empty?
  helpers[0...-1].reverse.inject(helpers.last.new) do |chain, h_class|
    prepended = h_class.new
    prepended.next_in_chain = chain
    prepended
  end
end

.call(sexpr, options = nil) ⇒ Object



43
44
45
# File 'lib/sexpr/processor.rb', line 43

def call(sexpr, options = nil)
  new(options).call(sexpr)
end

.grammar(grammar = nil) ⇒ Object



9
10
11
12
13
# File 'lib/sexpr/processor.rb', line 9

def grammar(grammar = nil)
  @grammar = grammar if grammar
  @grammar ||= (@grammar || superclass.grammar) rescue Sexpr
  @grammar
end

.helper(helper_class) ⇒ Object



28
29
30
31
32
# File 'lib/sexpr/processor.rb', line 28

def helper(helper_class)
  methods = helper_class.const_get(:Methods) rescue nil
  module_eval{ include methods } if methods
  helpers << helper_class
end

.helpersObject



24
25
26
# File 'lib/sexpr/processor.rb', line 24

def helpers
  @helpers ||= superclass.helpers.dup rescue [ ]
end

.preprocessorsObject



15
16
17
# File 'lib/sexpr/processor.rb', line 15

def preprocessors
  @preprocessors ||= superclass.preprocessors.dup rescue [ ]
end

.use(preprocessor, options = nil) ⇒ Object



19
20
21
22
# File 'lib/sexpr/processor.rb', line 19

def use(preprocessor, options = nil)
  preprocessor.keys.each{|k| attr_reader(k)} if preprocessor.is_a?(Hash)
  preprocessors << [preprocessor, options]
end

Instance Method Details

#apply(sexpr) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/sexpr/processor.rb', line 63

def apply(sexpr)
  help(sexpr) do |n|
    meth = :"on_#{n.first}"
    meth = :"on_missing" unless respond_to?(meth)
    send(meth, n)
  end
end

#call(sexpr) ⇒ Object



59
60
61
# File 'lib/sexpr/processor.rb', line 59

def call(sexpr)
  apply(preprocess(sexpr))
end

#grammarObject



55
56
57
# File 'lib/sexpr/processor.rb', line 55

def grammar
  self.class.grammar
end

#on_missing(sexpr) ⇒ Object



71
72
73
# File 'lib/sexpr/processor.rb', line 71

def on_missing(sexpr)
  raise UnexpectedSexprError, "Unexpected sexpr: #{sexpr.inspect}"
end