Class: SexpGrammar::Rule

Inherits:
Object
  • Object
show all
Includes:
Element
Defined in:
lib/sexp_grammar/rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Element

#===

Constructor Details

#initialize(name, defn) ⇒ Rule

Returns a new instance of Rule.



8
9
10
11
# File 'lib/sexp_grammar/rule.rb', line 8

def initialize(name, defn)
  @name = name
  @defn = defn
end

Instance Attribute Details

#defnObject (readonly)

Returns the value of attribute defn.



6
7
8
# File 'lib/sexp_grammar/rule.rb', line 6

def defn
  @defn
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/sexp_grammar/rule.rb', line 5

def name
  @name
end

Instance Method Details

#eat(sexp) ⇒ Object



19
20
21
22
# File 'lib/sexp_grammar/rule.rb', line 19

def eat(sexp)
  return nil unless match?(sexp.first)
  sexp[1..-1]
end

#inspectObject



24
25
26
# File 'lib/sexp_grammar/rule.rb', line 24

def inspect
  "(rule #{name}, #{defn.inspect})"
end

#match?(sexp) ⇒ Boolean

Returns:

  • (Boolean)


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

def match?(sexp)
  return nil unless sexp.is_a?(Array)
  return false unless sexp.first && (sexp.first == name)
  defn.match?(sexp[1..-1])
end