From 46c9605b9f27176c10a17da0ba3c8ba90a50a183 Mon Sep 17 00:00:00 2001 From: Little_Rubyist Date: Sat, 13 Jul 2024 03:15:35 +0900 Subject: [PATCH 1/4] Add types in state directory classes --- sig/lrama/state/reduce.rbs | 20 ++++++++++ sig/lrama/state/reduce_reduce_conflict.rbs | 11 ++++++ sig/lrama/state/resolved_conflict.rbs | 12 ++++++ sig/lrama/state/shift.rbs | 14 +++++++ sig/lrama/state/shift_reduce_conflict.rbs | 11 ++++++ sig/lrama/states/item.rbs | 44 ++++++++++++++++++++++ 6 files changed, 112 insertions(+) create mode 100644 sig/lrama/state/reduce.rbs create mode 100644 sig/lrama/state/reduce_reduce_conflict.rbs create mode 100644 sig/lrama/state/resolved_conflict.rbs create mode 100644 sig/lrama/state/shift.rbs create mode 100644 sig/lrama/state/shift_reduce_conflict.rbs create mode 100644 sig/lrama/states/item.rbs diff --git a/sig/lrama/state/reduce.rbs b/sig/lrama/state/reduce.rbs new file mode 100644 index 00000000..be98d5bc --- /dev/null +++ b/sig/lrama/state/reduce.rbs @@ -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 diff --git a/sig/lrama/state/reduce_reduce_conflict.rbs b/sig/lrama/state/reduce_reduce_conflict.rbs new file mode 100644 index 00000000..2d8ecf9a --- /dev/null +++ b/sig/lrama/state/reduce_reduce_conflict.rbs @@ -0,0 +1,11 @@ +module Lrama + class State + class ReduceReduceConflict + attr_accessor symbols: Array[Grammar::Symbol?] + attr_accessor reduce1: State::Reduce + attr_accessor reduce2: State::Reduce + + def type: () -> :reduce_reduce + end + end +end diff --git a/sig/lrama/state/resolved_conflict.rbs b/sig/lrama/state/resolved_conflict.rbs new file mode 100644 index 00000000..4856608f --- /dev/null +++ b/sig/lrama/state/resolved_conflict.rbs @@ -0,0 +1,12 @@ +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 report_message: () -> (::String | bot) + end + end +end diff --git a/sig/lrama/state/shift.rbs b/sig/lrama/state/shift.rbs new file mode 100644 index 00000000..f1dd0e89 --- /dev/null +++ b/sig/lrama/state/shift.rbs @@ -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 diff --git a/sig/lrama/state/shift_reduce_conflict.rbs b/sig/lrama/state/shift_reduce_conflict.rbs new file mode 100644 index 00000000..74e5c102 --- /dev/null +++ b/sig/lrama/state/shift_reduce_conflict.rbs @@ -0,0 +1,11 @@ +module Lrama + class State + class ShiftReduceConflict + attr_accessor symbols: Array[Grammar::Symbol] + attr_accessor shift: State::Shift + attr_accessor reduce: State::Reduce + + def type: () -> :shift_reduce + end + end +end diff --git a/sig/lrama/states/item.rbs b/sig/lrama/states/item.rbs new file mode 100644 index 00000000..45a58ca2 --- /dev/null +++ b/sig/lrama/states/item.rbs @@ -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 From b778966c28a329837c921a150cb9644d2a8ac0c8 Mon Sep 17 00:00:00 2001 From: Little_Rubyist Date: Tue, 16 Jul 2024 11:48:06 +0900 Subject: [PATCH 2/4] Add state directory in Steepfile --- Steepfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Steepfile b/Steepfile index d929e524..3c4f357c 100644 --- a/Steepfile +++ b/Steepfile @@ -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" From 7a6eed36f53c641606b6ccb11fda7b177e62ed4b Mon Sep 17 00:00:00 2001 From: Little_Rubyist Date: Tue, 16 Jul 2024 11:49:01 +0900 Subject: [PATCH 3/4] Fix steep errors --- lib/lrama/state/reduce.rb | 1 + lib/lrama/state/resolved_conflict.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/lrama/state/reduce.rb b/lib/lrama/state/reduce.rb index 45d509f7..a2b7c26c 100644 --- a/lib/lrama/state/reduce.rb +++ b/lib/lrama/state/reduce.rb @@ -27,6 +27,7 @@ def add_not_selected_symbol(sym) def selected_look_ahead if @look_ahead + # @type ivar @look_ahead: Array @look_ahead - @not_selected_symbols else [] diff --git a/lib/lrama/state/resolved_conflict.rb b/lib/lrama/state/resolved_conflict.rb index 6b67f36b..3bb3d144 100644 --- a/lib/lrama/state/resolved_conflict.rb +++ b/lib/lrama/state/resolved_conflict.rb @@ -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})" From 2f45b40d771f128e90ac7303582678120ef077ad Mon Sep 17 00:00:00 2001 From: Little_Rubyist Date: Tue, 16 Jul 2024 19:30:54 +0900 Subject: [PATCH 4/4] Add types for initialize method --- sig/lrama/state/reduce_reduce_conflict.rbs | 8 +++++--- sig/lrama/state/resolved_conflict.rbs | 2 ++ sig/lrama/state/shift_reduce_conflict.rbs | 8 +++++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/sig/lrama/state/reduce_reduce_conflict.rbs b/sig/lrama/state/reduce_reduce_conflict.rbs index 2d8ecf9a..23ced680 100644 --- a/sig/lrama/state/reduce_reduce_conflict.rbs +++ b/sig/lrama/state/reduce_reduce_conflict.rbs @@ -1,9 +1,11 @@ module Lrama class State class ReduceReduceConflict - attr_accessor symbols: Array[Grammar::Symbol?] - attr_accessor reduce1: State::Reduce - attr_accessor reduce2: State::Reduce + 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 diff --git a/sig/lrama/state/resolved_conflict.rbs b/sig/lrama/state/resolved_conflict.rbs index 4856608f..76be9469 100644 --- a/sig/lrama/state/resolved_conflict.rbs +++ b/sig/lrama/state/resolved_conflict.rbs @@ -6,6 +6,8 @@ module Lrama 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 diff --git a/sig/lrama/state/shift_reduce_conflict.rbs b/sig/lrama/state/shift_reduce_conflict.rbs index 74e5c102..af878342 100644 --- a/sig/lrama/state/shift_reduce_conflict.rbs +++ b/sig/lrama/state/shift_reduce_conflict.rbs @@ -1,9 +1,11 @@ module Lrama class State class ShiftReduceConflict - attr_accessor symbols: Array[Grammar::Symbol] - attr_accessor shift: State::Shift - attr_accessor reduce: State::Reduce + 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