Remove Rule::Pattern, Item stores a Rule reference
This commit is contained in:
parent
bdb10e7afc
commit
5f7e548fe3
@ -81,8 +81,8 @@ class Imbecile
|
|||||||
elsif input.slice!(/\A(\S+)\s*:\s*\[(.*?)\] <<\n(.*?)^>>\n/m)
|
elsif input.slice!(/\A(\S+)\s*:\s*\[(.*?)\] <<\n(.*?)^>>\n/m)
|
||||||
rule_name, components, code = $1, $2, $3
|
rule_name, components, code = $1, $2, $3
|
||||||
components = components.strip.split(/\s+/)
|
components = components.strip.split(/\s+/)
|
||||||
@rules[rule_name] ||= Rule.new(rule_name)
|
@rules[rule_name] ||= []
|
||||||
@rules[rule_name].add_pattern(components, code)
|
@rules[rule_name] << Rule.new(rule_name, components, code)
|
||||||
else
|
else
|
||||||
if input.size > 25
|
if input.size > 25
|
||||||
input = input.slice(0..20) + "..."
|
input = input.slice(0..20) + "..."
|
||||||
@ -106,9 +106,9 @@ class Imbecile
|
|||||||
unless @rules["Start"]
|
unless @rules["Start"]
|
||||||
raise Error.new("Start rule not found")
|
raise Error.new("Start rule not found")
|
||||||
end
|
end
|
||||||
@rules.each do |rule_name, rule|
|
@rules.each do |rule_name, rules|
|
||||||
rule.patterns.each do |pattern|
|
rules.each do |rule|
|
||||||
pattern.components.map! do |component|
|
rule.components.map! do |component|
|
||||||
if token_names[component]
|
if token_names[component]
|
||||||
token_names[component]
|
token_names[component]
|
||||||
elsif @rules[component]
|
elsif @rules[component]
|
||||||
|
@ -6,9 +6,9 @@ class Imbecile
|
|||||||
token_eof = Token.new("$", nil, TOKEN_EOF)
|
token_eof = Token.new("$", nil, TOKEN_EOF)
|
||||||
@item_sets = []
|
@item_sets = []
|
||||||
item_sets_set = Set.new
|
item_sets_set = Set.new
|
||||||
start_items = rules["Start"].patterns.map do |pattern|
|
start_items = rules["Start"].map do |rule|
|
||||||
pattern.components << token_eof
|
rule.components << token_eof
|
||||||
Item.new(pattern, 0)
|
Item.new(rule, 0)
|
||||||
end
|
end
|
||||||
eval_item_sets = Set.new
|
eval_item_sets = Set.new
|
||||||
eval_item_sets << ItemSet.new(start_items)
|
eval_item_sets << ItemSet.new(start_items)
|
||||||
|
@ -3,24 +3,24 @@ class Imbecile
|
|||||||
|
|
||||||
class Item
|
class Item
|
||||||
|
|
||||||
attr_reader :pattern
|
attr_reader :rule
|
||||||
attr_reader :position
|
attr_reader :position
|
||||||
|
|
||||||
def initialize(pattern, position)
|
def initialize(rule, position)
|
||||||
@pattern = pattern
|
@rule = rule
|
||||||
@position = position
|
@position = position
|
||||||
end
|
end
|
||||||
|
|
||||||
def next_component
|
def next_component
|
||||||
@pattern.components[@position]
|
@rule.components[@position]
|
||||||
end
|
end
|
||||||
|
|
||||||
def hash
|
def hash
|
||||||
[@pattern, @position].hash
|
[@rule, @position].hash
|
||||||
end
|
end
|
||||||
|
|
||||||
def ==(other)
|
def ==(other)
|
||||||
@pattern == other.pattern && @position == other.position
|
@rule == other.rule && @position == other.position
|
||||||
end
|
end
|
||||||
|
|
||||||
def eql?(other)
|
def eql?(other)
|
||||||
@ -28,9 +28,9 @@ class Imbecile
|
|||||||
end
|
end
|
||||||
|
|
||||||
def closed_items
|
def closed_items
|
||||||
if @pattern.components[@position].is_a?(Rule)
|
if @rule.components[@position].is_a?(Array)
|
||||||
@pattern.components[@position].patterns.map do |pattern|
|
@rule.components[@position].map do |rule|
|
||||||
Item.new(pattern, 0)
|
Item.new(rule, 0)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
[]
|
[]
|
||||||
@ -38,7 +38,7 @@ class Imbecile
|
|||||||
end
|
end
|
||||||
|
|
||||||
def follow_symbol
|
def follow_symbol
|
||||||
@pattern.components[@position]
|
@rule.components[@position]
|
||||||
end
|
end
|
||||||
|
|
||||||
def followed_by?(symbol)
|
def followed_by?(symbol)
|
||||||
@ -46,21 +46,25 @@ class Imbecile
|
|||||||
end
|
end
|
||||||
|
|
||||||
def next_position
|
def next_position
|
||||||
Item.new(@pattern, @position + 1)
|
Item.new(@rule, @position + 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
parts = []
|
parts = []
|
||||||
@pattern.components.each_with_index do |symbol, index|
|
@rule.components.each_with_index do |symbol, index|
|
||||||
if @position == index
|
if @position == index
|
||||||
parts << "."
|
parts << "."
|
||||||
end
|
end
|
||||||
parts << symbol.name
|
if symbol.is_a?(Token)
|
||||||
|
parts << symbol.name
|
||||||
|
else
|
||||||
|
parts << symbol[0].name
|
||||||
|
end
|
||||||
end
|
end
|
||||||
if @position == @pattern.components.size
|
if @position == @rule.components.size
|
||||||
parts << "."
|
parts << "."
|
||||||
end
|
end
|
||||||
parts.join(" ")
|
"#{@rule.name} -> #{parts.join(" ")}"
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -2,30 +2,16 @@ class Imbecile
|
|||||||
|
|
||||||
class Rule
|
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 :name
|
||||||
|
|
||||||
attr_reader :patterns
|
attr_reader :components
|
||||||
|
|
||||||
def initialize(name)
|
attr_reader :code
|
||||||
|
|
||||||
|
def initialize(name, components, code)
|
||||||
@name = name
|
@name = name
|
||||||
@patterns = []
|
@components = components
|
||||||
end
|
@code = code
|
||||||
|
|
||||||
def add_pattern(components, code)
|
|
||||||
@patterns << Pattern.new(components, code)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user