Skip to content

Commit

Permalink
Merge pull request #458 from Little-Rubyist/add_types_to_state
Browse files Browse the repository at this point in the history
Add types to classes in state directory
  • Loading branch information
ydah authored Jul 20, 2024
2 parents c5a5c9c + 2f45b40 commit 23d308c
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 1 deletion.
1 change: 1 addition & 0 deletions Steepfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ target :lib do
check "lib/lrama/grammar"
check "lib/lrama/lexer"
check "lib/lrama/report"
check "lib/lrama/state"
check "lib/lrama/bitmap.rb"
check "lib/lrama/digraph.rb"
check "lib/lrama/grammar.rb"
Expand Down
1 change: 1 addition & 0 deletions lib/lrama/state/reduce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def add_not_selected_symbol(sym)

def selected_look_ahead
if @look_ahead
# @type ivar @look_ahead: Array<Grammar::Symbol>
@look_ahead - @not_selected_symbols
else
[]
Expand Down
2 changes: 1 addition & 1 deletion lib/lrama/state/resolved_conflict.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class State
class ResolvedConflict < Struct.new(:symbol, :reduce, :which, :same_prec, keyword_init: true)
def report_message
s = symbol.display_name
r = reduce.rule.precedence_sym.display_name
r = reduce.rule.precedence_sym&.display_name
case
when which == :shift && same_prec
msg = "resolved as #{which} (%right #{s})"
Expand Down
20 changes: 20 additions & 0 deletions sig/lrama/state/reduce.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Lrama
class State
class Reduce
@item: States::Item
@look_ahead: Array[Grammar::Symbol]?
@not_selected_symbols: Array[Grammar::Symbol]

attr_reader item: States::Item
attr_reader look_ahead: Array[Grammar::Symbol]?
attr_reader not_selected_symbols: Array[Grammar::Symbol]
attr_accessor default_reduction: bool

def initialize: (States::Item item) -> void
def rule: -> Grammar::Rule
def look_ahead=: (Array[Grammar::Symbol] look_ahead) -> Array[Grammar::Symbol]
def add_not_selected_symbol: (Grammar::Symbol sym) -> Array[Grammar::Symbol]
def selected_look_ahead: () -> (::Array[Grammar::Symbol?])
end
end
end
13 changes: 13 additions & 0 deletions sig/lrama/state/reduce_reduce_conflict.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Lrama
class State
class ReduceReduceConflict
attr_accessor symbols: Array[Grammar::Symbol?]
attr_accessor reduce1: State::Reduce
attr_accessor reduce2: State::Reduce

def initialize: (?symbols: Array[Grammar::Symbol?], ?reduce1: State::Reduce, ?reduce2: State::Reduce) -> void

def type: () -> :reduce_reduce
end
end
end
14 changes: 14 additions & 0 deletions sig/lrama/state/resolved_conflict.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Lrama
class State
class ResolvedConflict
attr_accessor symbol: Grammar::Symbol
attr_accessor reduce: State::Reduce
attr_accessor which: (:reduce | :shift)
attr_accessor same_prec: bool

def initialize: (?symbol: Grammar::Symbol, ?reduce: State::Reduce, ?which: (:reduce | :shift), ?same_prec: bool) -> void

def report_message: () -> (::String | bot)
end
end
end
14 changes: 14 additions & 0 deletions sig/lrama/state/shift.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Lrama
class State
class Shift
@next_sym: Grammar::Symbol
@next_items: Array[States::Item]

attr_reader next_sym: Grammar::Symbol
attr_reader next_items: Array[States::Item]
attr_accessor not_selected: bool

def initialize: (Grammar::Symbol next_sym, Array[States::Item] next_items) -> void
end
end
end
13 changes: 13 additions & 0 deletions sig/lrama/state/shift_reduce_conflict.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Lrama
class State
class ShiftReduceConflict
attr_accessor symbols: Array[Grammar::Symbol]
attr_accessor shift: State::Shift
attr_accessor reduce: State::Reduce

def initialize: (?symbols: Array[Grammar::Symbol], ?shift: State::Shift, ?reduce: State::Reduce) -> void

def type: () -> :shift_reduce
end
end
end
44 changes: 44 additions & 0 deletions sig/lrama/states/item.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module Lrama
class States
class Item
extend Forwardable

attr_accessor rule: untyped
attr_accessor position: Integer

# Optimization for States#setup_state
def hash: () -> untyped

def rule_id: () -> untyped

def empty_rule?: () -> untyped

def number_of_rest_symbols: () -> untyped

def next_sym: () -> untyped

def next_next_sym: () -> untyped

def previous_sym: () -> untyped

def end_of_rule?: () -> untyped

def beginning_of_rule?: () -> untyped

def start_item?: () -> untyped

def new_by_next_position: () -> untyped

def symbols_before_dot: () -> untyped

def symbols_after_dot: () -> untyped

def to_s: () -> ::String

def display_name: () -> ::String

# Right after position
def display_rest: () -> ::String
end
end
end

0 comments on commit 23d308c

Please sign in to comment.