Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add types to classes in state directory #458

Merged
merged 4 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
ydah marked this conversation as resolved.
Show resolved Hide resolved
@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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seemed to be nullable. is it acceptable to have nil in the r variable?
Currently allows nil to pass type checking.

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
ydah marked this conversation as resolved.
Show resolved Hide resolved
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