Start building following item sets
This commit is contained in:
1 parent
9cc1890ddc
commit
6026bf1514
4 files changed
+77
-2
No files matched your search
@@ -37,6 +37,32 @@ class Imbecile
|
||||
end
|
||||
end
|
||||
|
||||
def follow_symbol
|
||||
@pattern.components[@position]
|
||||
end
|
||||
|
||||
def followed_by?(symbol)
|
||||
follow_symbol == symbol
|
||||
end
|
||||
|
||||
def next_position
|
||||
Item.new(@pattern, @position + 1)
|
||||
end
|
||||
|
||||
def to_s
|
||||
parts = []
|
||||
@pattern.components.each_with_index do |symbol, index|
|
||||
if @position == index
|
||||
parts << "."
|
||||
end
|
||||
parts << symbol.name
|
||||
end
|
||||
if @position == @pattern.components.size
|
||||
parts << "."
|
||||
end
|
||||
parts.join(" ")
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -3,10 +3,39 @@ class Imbecile
|
||||
|
||||
class ItemSet
|
||||
|
||||
attr_reader :items
|
||||
|
||||
def initialize(items)
|
||||
@items = Set.new(items)
|
||||
close!
|
||||
end
|
||||
|
||||
def follow_symbols
|
||||
Set.new(@items.map(&:follow_symbol))
|
||||
end
|
||||
|
||||
def follow_set(symbol)
|
||||
ItemSet.new(items_followed_by(symbol).map(&:next_position))
|
||||
end
|
||||
|
||||
def hash
|
||||
@items.hash
|
||||
end
|
||||
|
||||
def ==(other)
|
||||
@items.eql?(other.items)
|
||||
end
|
||||
|
||||
def eql?(other)
|
||||
self == other
|
||||
end
|
||||
|
||||
def to_s
|
||||
@items.map(&:to_s).join("\n")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def close!
|
||||
eval_items = @items
|
||||
while eval_items.size > 0
|
||||
@@ -19,6 +48,12 @@ class Imbecile
|
||||
end
|
||||
end
|
||||
|
||||
def items_followed_by(symbol)
|
||||
@items.select do |item|
|
||||
item.followed_by?(symbol)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user