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" 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})" 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..23ced680 --- /dev/null +++ b/sig/lrama/state/reduce_reduce_conflict.rbs @@ -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 diff --git a/sig/lrama/state/resolved_conflict.rbs b/sig/lrama/state/resolved_conflict.rbs new file mode 100644 index 00000000..76be9469 --- /dev/null +++ b/sig/lrama/state/resolved_conflict.rbs @@ -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 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..af878342 --- /dev/null +++ b/sig/lrama/state/shift_reduce_conflict.rbs @@ -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 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