Class: Kaiseki::Rule

Inherits:
Object show all
Defined in:
lib/rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, grammar, &block) ⇒ Rule



5
6
7
8
9
10
11
# File 'lib/rule.rb', line 5

def initialize name, grammar, &block
  @name = name
  @grammar = grammar
  @parseable = nil
  
  instance_eval &block if block_given?
end

Instance Attribute Details

#parseableObject (readonly)

Returns the value of attribute parseable.



3
4
5
# File 'lib/rule.rb', line 3

def parseable
  @parseable
end

Instance Method Details

#action(node = @name, &block) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/rule.rb', line 113

def action node = @name, &block
  if @parseable
    unless @grammar.nodes.key? @name
      @grammar.nodes[@name] = Node.default
    end
    @parseable = @parseable.action node, &block
  else
    raise "parseable for rule #{@name.inspect} undefined"
  end
end

#cast(to_class) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/rule.rb', line 62

def cast to_class
  if @parseable
    @parseable = @parseable.cast to_class
  else
    raise "parseable for rule #{@name.inspect} undefined"
  end
end

#custom(is_predicate = false, &block) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/rule.rb', line 21

def custom is_predicate = false, &block
  if @parseable
    raise "parseable for rule #{@name.inspect} already defined"
  else
    @parseable = CustomParser.new is_predicate, &block
  end
end

#filter(node = @name, &block) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/rule.rb', line 102

def filter node = @name, &block
  if @parseable
    unless @grammar.nodes.key? @name
      @grammar.nodes[@name] = Node.default
    end
    @parseable = @parseable.filter node, &block
  else
    raise "parseable for rule #{@name.inspect} undefined"
  end
end

#mergeObject



54
55
56
57
58
59
60
# File 'lib/rule.rb', line 54

def merge
  if @parseable
    @parseable = @parseable.merge
  else
    raise "parseable for rule #{@name.inspect} undefined"
  end
end

#node(args, options = {}) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/rule.rb', line 124

def node args, options = {}
  args.must_be Array
  if @grammar.nodes.key? @name
    raise "node #{@name.inspect} already defined"
  else
    parent = options[:class] || Node
    @grammar.nodes[@name] = parent.subclass args, options
  end
end

#override(options) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/rule.rb', line 29

def override options
  if @parseable
    @parseable = @parseable.override options
  else
    raise "parseable for rule #{@name.inspect} undefined"
  end
end

#parses(parseable) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/rule.rb', line 13

def parses parseable
  if @parseable
    raise "parseable for rule #{@name.inspect} already defined"
  else
    @parseable = parseable.to_parseable
  end
end

#set(*vars) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/rule.rb', line 78

def set *vars
  if @parseable
    @parseable = @parseable.set *vars
  else
    raise "parseable for rule #{@name.inspect} undefined"
  end
end

#simplify(bool = true) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/rule.rb', line 46

def simplify bool = true
  if @parseable
    @parseable = @parseable.override :simplify => bool
  else
    raise "parseable for rule #{@name.inspect} undefined"
  end
end

#skipping(parseable) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/rule.rb', line 37

def skipping parseable
  if @parseable
    raise "skipping rule must not be a predicate" if parseable.predicate?
    @parseable = @parseable.override :skipping => parseable.to_parseable
  else
    raise "parseable for rule #{@name.inspect} undefined"
  end
end

#tag_error(name = @name) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/rule.rb', line 94

def tag_error name = @name
  if @parseable
    @parseable = @parseable.tag_error name
  else
    raise "parseable for rule #{@name.inspect} undefined"
  end
end

#tag_result(name = @name) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/rule.rb', line 86

def tag_result name = @name
  if @parseable
    @parseable = @parseable.tag_result name
  else
    raise "parseable for rule #{@name.inspect} undefined"
  end
end

#validates(validators) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/rule.rb', line 70

def validates validators
  if @parseable
    @parseable = @parseable.validate validators
  else
    raise "parseable for rule #{@name.inspect} undefined"
  end
end