Rename "following" to "next" - #25

The term "following" could potentially imply an association with the
"follow set", however it was used in a non-closed manner.
This commit is contained in:
Josh Holtrop 2024-07-08 10:14:09 -04:00
parent 911e9505b7
commit d76e12fea1
3 changed files with 46 additions and 46 deletions

View File

@ -23,10 +23,10 @@ class Propane
item_set.id = @item_sets.size item_set.id = @item_sets.size
@item_sets << item_set @item_sets << item_set
@item_sets_set[item_set] = item_set @item_sets_set[item_set] = item_set
item_set.following_symbols.each do |following_symbol| item_set.next_symbols.each do |next_symbol|
unless following_symbol.name == "$EOF" unless next_symbol.name == "$EOF"
following_set = item_set.build_following_item_set(following_symbol) next_item_set = item_set.build_next_item_set(next_symbol)
eval_item_sets << following_set eval_item_sets << next_item_set
end end
end end
end end
@ -48,15 +48,15 @@ class Propane
@shift_table = [] @shift_table = []
@reduce_table = [] @reduce_table = []
@item_sets.each do |item_set| @item_sets.each do |item_set|
shift_entries = item_set.following_symbols.map do |following_symbol| shift_entries = item_set.next_symbols.map do |next_symbol|
state_id = state_id =
if following_symbol.name == "$EOF" if next_symbol.name == "$EOF"
0 0
else else
item_set.following_item_set[following_symbol].id item_set.next_item_set[next_symbol].id
end end
{ {
symbol_id: following_symbol.id, symbol_id: next_symbol.id,
state_id: state_id, state_id: state_id,
} }
end end
@ -87,11 +87,11 @@ class Propane
end end
def process_item_set(item_set) def process_item_set(item_set)
item_set.following_symbols.each do |following_symbol| item_set.next_symbols.each do |next_symbol|
unless following_symbol.name == "$EOF" unless next_symbol.name == "$EOF"
following_set = @item_sets_set[item_set.build_following_item_set(following_symbol)] next_item_set = @item_sets_set[item_set.build_next_item_set(next_symbol)]
item_set.following_item_set[following_symbol] = following_set item_set.next_item_set[next_symbol] = next_item_set
following_set.in_sets << item_set next_item_set.in_sets << item_set
end end
end end
end end
@ -183,9 +183,9 @@ class Propane
# tokens to form the lookahead token set. # tokens to form the lookahead token set.
item_sets.each do |item_set| item_sets.each do |item_set|
item_set.items.each do |item| item_set.items.each do |item|
if item.following_symbol == rule_set if item.next_symbol == rule_set
(1..).each do |offset| (1..).each do |offset|
case symbol = item.following_symbol(offset) case symbol = item.next_symbol(offset)
when nil when nil
rule_set = item.rule.rule_set rule_set = item.rule.rule_set
unless checked_rule_sets.include?(rule_set) unless checked_rule_sets.include?(rule_set)
@ -242,8 +242,8 @@ class Propane
@log.puts @log.puts
@log.puts " Incoming states: #{incoming_ids.join(", ")}" @log.puts " Incoming states: #{incoming_ids.join(", ")}"
@log.puts " Outgoing states:" @log.puts " Outgoing states:"
item_set.following_item_set.each do |following_symbol, following_item_set| item_set.next_item_set.each do |next_symbol, next_item_set|
@log.puts " #{following_symbol.name} => #{following_item_set.id}" @log.puts " #{next_symbol.name} => #{next_item_set.id}"
end end
@log.puts @log.puts
@log.puts " Reduce actions:" @log.puts " Reduce actions:"

View File

@ -56,7 +56,7 @@ class Propane
# Return the set of Items obtained by "closing" the current item. # Return the set of Items obtained by "closing" the current item.
# #
# If the following symbol for the current item is another Rule name, then # If the next symbol for the current item is another Rule name, then
# this method will return all Items for that Rule with a position of 0. # this method will return all Items for that Rule with a position of 0.
# Otherwise, an empty Array is returned. # Otherwise, an empty Array is returned.
# #
@ -81,17 +81,17 @@ class Propane
@position == @rule.components.size @position == @rule.components.size
end end
# Get the following symbol for the Item. # Get the next symbol for the Item.
# #
# That is, the symbol which follows the parse position marker in the # That is, the symbol which is after the parse position marker in the
# current Item. # current Item.
# #
# @param offset [Integer] # @param offset [Integer]
# Offset from current parse position to examine. # Offset from current parse position to examine.
# #
# @return [Token, RuleSet, nil] # @return [Token, RuleSet, nil]
# Following symbol for the Item. # Next symbol for the Item.
def following_symbol(offset = 0) def next_symbol(offset = 0)
@rule.components[@position + offset] @rule.components[@position + offset]
end end
@ -108,25 +108,25 @@ class Propane
end end
end end
# Get whether this Item is followed by the provided symbol. # Get whether this Item's next symbol is the given symbol.
# #
# @param symbol [Token, RuleSet] # @param symbol [Token, RuleSet]
# Symbol to query. # Symbol to query.
# #
# @return [Boolean] # @return [Boolean]
# Whether this Item is followed by the provided symbol. # Whether this Item's next symbol is the given symbol.
def followed_by?(symbol) def next_symbol?(symbol)
following_symbol == symbol next_symbol == symbol
end end
# Get the following item for this Item. # Get the next item for this Item.
# #
# That is, the Item formed by moving the parse position marker one place # That is, the Item formed by moving the parse position marker one place
# forward from its position in this Item. # forward from its position in this Item.
# #
# @return [Item] # @return [Item]
# The following item for this Item. # The next item for this Item.
def following_item def next_item
Item.new(@rule, @position + 1) Item.new(@rule, @position + 1)
end end

View File

@ -14,8 +14,8 @@ class Propane
attr_accessor :id attr_accessor :id
# @return [Hash] # @return [Hash]
# Maps a following symbol to its ItemSet. # Maps a next symbol to its ItemSet.
attr_reader :following_item_set attr_reader :next_item_set
# @return [Set<ItemSet>] # @return [Set<ItemSet>]
# ItemSets leading to this item set. # ItemSets leading to this item set.
@ -31,28 +31,28 @@ class Propane
# Items in this ItemSet. # Items in this ItemSet.
def initialize(items) def initialize(items)
@items = Set.new(items) @items = Set.new(items)
@following_item_set = {} @next_item_set = {}
@in_sets = Set.new @in_sets = Set.new
close! close!
end end
# Get the set of following symbols for all Items in this ItemSet. # Get the set of next symbols for all Items in this ItemSet.
# #
# @return [Set<Token, RuleSet>] # @return [Set<Token, RuleSet>]
# Set of following symbols for all Items in this ItemSet. # Set of next symbols for all Items in this ItemSet.
def following_symbols def next_symbols
Set.new(@items.map(&:following_symbol).compact) Set.new(@items.map(&:next_symbol).compact)
end end
# Build a following ItemSet for the given following symbol. # Build a next ItemSet for the given next symbol.
# #
# @param symbol [Token, RuleSet] # @param symbol [Token, RuleSet]
# Following symbol to build the following ItemSet for. # Next symbol to build the next ItemSet for.
# #
# @return [ItemSet] # @return [ItemSet]
# Following ItemSet for the given following symbol. # Next ItemSet for the given next symbol.
def build_following_item_set(symbol) def build_next_item_set(symbol)
ItemSet.new(items_followed_by(symbol).map(&:following_item)) ItemSet.new(items_with_next(symbol).map(&:next_item))
end end
# Hash function. # Hash function.
@ -137,16 +137,16 @@ class Propane
end end
end end
# Get the Items followed by the given following symbol. # Get the Items with the given next symbol.
# #
# @param symbol [Token, RuleSet] # @param symbol [Token, RuleSet]
# Following symbol. # Next symbol.
# #
# @return [Array<Item>] # @return [Array<Item>]
# Items followed by the given following symbol. # Items with the given next symbol.
def items_followed_by(symbol) def items_with_next(symbol)
@items.select do |item| @items.select do |item|
item.followed_by?(symbol) item.next_symbol?(symbol)
end end
end end