Skip to content

Commit 23d308c

Browse files
authored
Merge pull request #458 from Little-Rubyist/add_types_to_state
Add types to classes in state directory
2 parents c5a5c9c + 2f45b40 commit 23d308c

9 files changed

+121
-1
lines changed

Steepfile

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ target :lib do
99
check "lib/lrama/grammar"
1010
check "lib/lrama/lexer"
1111
check "lib/lrama/report"
12+
check "lib/lrama/state"
1213
check "lib/lrama/bitmap.rb"
1314
check "lib/lrama/digraph.rb"
1415
check "lib/lrama/grammar.rb"

lib/lrama/state/reduce.rb

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def add_not_selected_symbol(sym)
2727

2828
def selected_look_ahead
2929
if @look_ahead
30+
# @type ivar @look_ahead: Array<Grammar::Symbol>
3031
@look_ahead - @not_selected_symbols
3132
else
3233
[]

lib/lrama/state/resolved_conflict.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class State
88
class ResolvedConflict < Struct.new(:symbol, :reduce, :which, :same_prec, keyword_init: true)
99
def report_message
1010
s = symbol.display_name
11-
r = reduce.rule.precedence_sym.display_name
11+
r = reduce.rule.precedence_sym&.display_name
1212
case
1313
when which == :shift && same_prec
1414
msg = "resolved as #{which} (%right #{s})"

sig/lrama/state/reduce.rbs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Lrama
2+
class State
3+
class Reduce
4+
@item: States::Item
5+
@look_ahead: Array[Grammar::Symbol]?
6+
@not_selected_symbols: Array[Grammar::Symbol]
7+
8+
attr_reader item: States::Item
9+
attr_reader look_ahead: Array[Grammar::Symbol]?
10+
attr_reader not_selected_symbols: Array[Grammar::Symbol]
11+
attr_accessor default_reduction: bool
12+
13+
def initialize: (States::Item item) -> void
14+
def rule: -> Grammar::Rule
15+
def look_ahead=: (Array[Grammar::Symbol] look_ahead) -> Array[Grammar::Symbol]
16+
def add_not_selected_symbol: (Grammar::Symbol sym) -> Array[Grammar::Symbol]
17+
def selected_look_ahead: () -> (::Array[Grammar::Symbol?])
18+
end
19+
end
20+
end
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Lrama
2+
class State
3+
class ReduceReduceConflict
4+
attr_accessor symbols: Array[Grammar::Symbol?]
5+
attr_accessor reduce1: State::Reduce
6+
attr_accessor reduce2: State::Reduce
7+
8+
def initialize: (?symbols: Array[Grammar::Symbol?], ?reduce1: State::Reduce, ?reduce2: State::Reduce) -> void
9+
10+
def type: () -> :reduce_reduce
11+
end
12+
end
13+
end

sig/lrama/state/resolved_conflict.rbs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Lrama
2+
class State
3+
class ResolvedConflict
4+
attr_accessor symbol: Grammar::Symbol
5+
attr_accessor reduce: State::Reduce
6+
attr_accessor which: (:reduce | :shift)
7+
attr_accessor same_prec: bool
8+
9+
def initialize: (?symbol: Grammar::Symbol, ?reduce: State::Reduce, ?which: (:reduce | :shift), ?same_prec: bool) -> void
10+
11+
def report_message: () -> (::String | bot)
12+
end
13+
end
14+
end

sig/lrama/state/shift.rbs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Lrama
2+
class State
3+
class Shift
4+
@next_sym: Grammar::Symbol
5+
@next_items: Array[States::Item]
6+
7+
attr_reader next_sym: Grammar::Symbol
8+
attr_reader next_items: Array[States::Item]
9+
attr_accessor not_selected: bool
10+
11+
def initialize: (Grammar::Symbol next_sym, Array[States::Item] next_items) -> void
12+
end
13+
end
14+
end
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Lrama
2+
class State
3+
class ShiftReduceConflict
4+
attr_accessor symbols: Array[Grammar::Symbol]
5+
attr_accessor shift: State::Shift
6+
attr_accessor reduce: State::Reduce
7+
8+
def initialize: (?symbols: Array[Grammar::Symbol], ?shift: State::Shift, ?reduce: State::Reduce) -> void
9+
10+
def type: () -> :shift_reduce
11+
end
12+
end
13+
end

sig/lrama/states/item.rbs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module Lrama
2+
class States
3+
class Item
4+
extend Forwardable
5+
6+
attr_accessor rule: untyped
7+
attr_accessor position: Integer
8+
9+
# Optimization for States#setup_state
10+
def hash: () -> untyped
11+
12+
def rule_id: () -> untyped
13+
14+
def empty_rule?: () -> untyped
15+
16+
def number_of_rest_symbols: () -> untyped
17+
18+
def next_sym: () -> untyped
19+
20+
def next_next_sym: () -> untyped
21+
22+
def previous_sym: () -> untyped
23+
24+
def end_of_rule?: () -> untyped
25+
26+
def beginning_of_rule?: () -> untyped
27+
28+
def start_item?: () -> untyped
29+
30+
def new_by_next_position: () -> untyped
31+
32+
def symbols_before_dot: () -> untyped
33+
34+
def symbols_after_dot: () -> untyped
35+
36+
def to_s: () -> ::String
37+
38+
def display_name: () -> ::String
39+
40+
# Right after position
41+
def display_rest: () -> ::String
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)