Start on Item and ItemSet
This commit is contained in:
parent
6ce94e15af
commit
2e16b0bd6e
@ -8,6 +8,8 @@ require_relative "imbecile/fa/state/transition"
|
|||||||
require_relative "imbecile/lexer"
|
require_relative "imbecile/lexer"
|
||||||
require_relative "imbecile/lexer/dfa"
|
require_relative "imbecile/lexer/dfa"
|
||||||
require_relative "imbecile/parser"
|
require_relative "imbecile/parser"
|
||||||
|
require_relative "imbecile/parser/item"
|
||||||
|
require_relative "imbecile/parser/item_set"
|
||||||
require_relative "imbecile/regex"
|
require_relative "imbecile/regex"
|
||||||
require_relative "imbecile/regex/nfa"
|
require_relative "imbecile/regex/nfa"
|
||||||
require_relative "imbecile/regex/unit"
|
require_relative "imbecile/regex/unit"
|
||||||
|
@ -3,6 +3,12 @@ class Imbecile
|
|||||||
class Parser
|
class Parser
|
||||||
|
|
||||||
def initialize(tokens, rules)
|
def initialize(tokens, rules)
|
||||||
|
start_rules = rules.select {|rule| rule.name == "Start"}
|
||||||
|
start_items = start_rules.map do |rule|
|
||||||
|
Item.new(rule, 0)
|
||||||
|
end
|
||||||
|
start_item_set = ItemSet.new(start_items)
|
||||||
|
start_item_set.close!
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
33
lib/imbecile/parser/item.rb
Normal file
33
lib/imbecile/parser/item.rb
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
class Imbecile
|
||||||
|
class Parser
|
||||||
|
|
||||||
|
class Item
|
||||||
|
|
||||||
|
attr_reader :rule
|
||||||
|
attr_reader :position
|
||||||
|
|
||||||
|
def initialize(rule, position)
|
||||||
|
@rule = rule
|
||||||
|
@position = position
|
||||||
|
end
|
||||||
|
|
||||||
|
def next_component
|
||||||
|
@rule.components[@position]
|
||||||
|
end
|
||||||
|
|
||||||
|
def hash
|
||||||
|
[@rule, @position].hash
|
||||||
|
end
|
||||||
|
|
||||||
|
def ==(other)
|
||||||
|
@rule == other.rule && @position == other.position
|
||||||
|
end
|
||||||
|
|
||||||
|
def eql?(other)
|
||||||
|
self == other
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
26
lib/imbecile/parser/item_set.rb
Normal file
26
lib/imbecile/parser/item_set.rb
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
class Imbecile
|
||||||
|
class Parser
|
||||||
|
|
||||||
|
class ItemSet
|
||||||
|
|
||||||
|
def initialize(items)
|
||||||
|
@items = Set.new(items)
|
||||||
|
end
|
||||||
|
|
||||||
|
def close!
|
||||||
|
eval_items = @items
|
||||||
|
while eval_items.size > 0
|
||||||
|
this_eval_items = eval_items
|
||||||
|
eval_items = Set.new
|
||||||
|
this_eval_items.each do |item|
|
||||||
|
if item.next_component.is_a?(Rule)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
@items += eval_items
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
19
spec/imbecile/parser/item_spec.rb
Normal file
19
spec/imbecile/parser/item_spec.rb
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
class Imbecile
|
||||||
|
class Parser
|
||||||
|
|
||||||
|
describe Item do
|
||||||
|
|
||||||
|
it "operates properly with a set" do
|
||||||
|
rule = Object.new
|
||||||
|
item1 = Item.new(rule, 2)
|
||||||
|
item2 = Item.new(rule, 2)
|
||||||
|
expect(item1).to eq item2
|
||||||
|
expect(item1.eql?(item2)).to be_truthy
|
||||||
|
set = Set.new([item1, item2])
|
||||||
|
expect(set.size).to eq 1
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user