One Rule object stores all alternative patterns
This commit is contained in:
1 parent
e4f2fffe50
commit
9cc1890ddc
4 files changed
+50
-36
No files matched your search
@@ -3,9 +3,8 @@ class Imbecile
|
||||
class Parser
|
||||
|
||||
def initialize(tokens, rules)
|
||||
start_rules = rules.select {|rule| rule.name == "Start"}
|
||||
start_items = start_rules.map do |rule|
|
||||
Item.new(rule, 0)
|
||||
start_items = rules["Start"].patterns.map do |pattern|
|
||||
Item.new(pattern, 0)
|
||||
end
|
||||
start_item_set = ItemSet.new(start_items)
|
||||
start_item_set.close!
|
||||
|
||||
@@ -3,24 +3,24 @@ class Imbecile
|
||||
|
||||
class Item
|
||||
|
||||
attr_reader :rule
|
||||
attr_reader :pattern
|
||||
attr_reader :position
|
||||
|
||||
def initialize(rule, position)
|
||||
@rule = rule
|
||||
def initialize(pattern, position)
|
||||
@pattern = pattern
|
||||
@position = position
|
||||
end
|
||||
|
||||
def next_component
|
||||
@rule.components[@position]
|
||||
@pattern.components[@position]
|
||||
end
|
||||
|
||||
def hash
|
||||
[@rule, @position].hash
|
||||
[@pattern, @position].hash
|
||||
end
|
||||
|
||||
def ==(other)
|
||||
@rule == other.rule && @position == other.position
|
||||
@pattern == other.pattern && @position == other.position
|
||||
end
|
||||
|
||||
def eql?(other)
|
||||
@@ -28,9 +28,9 @@ class Imbecile
|
||||
end
|
||||
|
||||
def closed_items
|
||||
if @rule.components[@position].is_a?(Array)
|
||||
@rule.components[@position].map do |rule|
|
||||
Item.new(rule, 0)
|
||||
if @pattern.components[@position].is_a?(Rule)
|
||||
@pattern.components[@position].patterns.map do |pattern|
|
||||
Item.new(pattern, 0)
|
||||
end
|
||||
else
|
||||
[]
|
||||
|
||||
+20
-6
@@ -2,16 +2,30 @@ class Imbecile
|
||||
|
||||
class Rule
|
||||
|
||||
class Pattern
|
||||
|
||||
attr_reader :components
|
||||
|
||||
attr_reader :code
|
||||
|
||||
def initialize(components, code)
|
||||
@components = components
|
||||
@code = code
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
attr_reader :name
|
||||
|
||||
attr_reader :components
|
||||
attr_reader :patterns
|
||||
|
||||
attr_reader :code
|
||||
|
||||
def initialize(name, rule_components, code)
|
||||
def initialize(name)
|
||||
@name = name
|
||||
@components = rule_components
|
||||
@code = code
|
||||
@patterns = []
|
||||
end
|
||||
|
||||
def add_pattern(components, code)
|
||||
@patterns << Pattern.new(components, code)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user