Class: Ruleby::LeTigre::PatternParser

Inherits:
Object
  • Object
show all
Defined in:
lib/dsl/letigre.rb

Constant Summary collapse

@@head_error =
'Invalid type specification.'
@@method_error =
"No #method in expression: "
@@base_re =
/(For each|\w*\s*exists\??|not\??)(.*)/
@@mode_re =
/( (is a|kind of|instance of) )(.*)/
@@where_re =
/(.*)where (.*)/
@@head_re =
/(\w*)( as :(.*))?/
@@method_re =
/#((\w|\d|\_)*)\??/
@@bind_re =
/#:(\w*|\d*\_*)*/
@@and_re =
/#&&/
@@tag_re =
/(.*) as :(.*)/

Instance Method Summary collapse

Constructor Details

#initialize(rulebook) ⇒ PatternParser

Returns a new instance of PatternParser.



94
95
96
# File 'lib/dsl/letigre.rb', line 94

def initialize(rulebook)
  @rulebook = rulebook
end

Instance Method Details

#parse(lhs_strs) ⇒ Object



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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/dsl/letigre.rb', line 98

def parse(lhs_strs)
  pattern = nil
  lhs_strs.each do |lhs|  
    # match the quantifier
    if lhs =~ @@base_re
      base = $1
      tail = $2
    else
      base = 'For each'
      tail = lhs
    end
    
    raise 'The \'exists\' quantifier is not yet supported.' if base =~ /exists/
    
    if tail =~ @@mode_re
      mode = :inherits
      tail = $3
    else
      mode = :equals           
    end
    
    # check if there is a where clause
    if tail =~ @@where_re
      head = $1.strip
      tail = $2
    else
      head = tail.strip
      tail = nil
    end
    
    # match the class type and tag
    if head != ''
      head =~ @@head_re            
      clazz = @rulebook.__eval__ $1
      tag = $3 ? $3.to_sym : GeneratedTag.new 
    else
      clazz = Object
      tag = GeneratedTag.new
      mode = :inherits
    end
    
    deftemplate = Core::DefTemplate.new clazz, mode
    head = Core::HeadAtom.new tag, deftemplate

    atoms = []      
    atom_strs = tail ? tail.split(@@and_re) : []
    atom_strs.each do |a|
      # BUG we also need to pass in the head_tag with atoms!
      atoms.push parse_atom(a, deftemplate, atoms)
    end        
              
    if base =~ /not\??/
      p = mode==:inherits ? Core::NotInheritsPattern.new(head, atoms) :
                            Core::NotPattern.new(head, atoms) 
    else 
      p = mode==:inherits ? Core::InheritsPattern.new(head, atoms) :
                            Core::ObjectPattern.new(head, atoms) 
    end
                        
    pattern = pattern ? Core::AndPattern.new(pattern, p) : p
  end
  return pattern
end