From b25483fefebb28730d01ec2711ef0652bd5ef692 Mon Sep 17 00:00:00 2001 From: Little_Rubyist Date: Mon, 22 Apr 2024 22:35:20 +0900 Subject: [PATCH 1/8] add rule_action.rbs --- lib/lrama/grammar/code/rule_action.rb | 2 ++ sig/lrama/grammar/code/rule_action.rbs | 19 +++++++++++++++++++ sig/lrama/grammar/rule.rbs | 2 +- sig/lrama/grammar/symbol.rbs | 2 +- sig/lrama/grammar/symbols/resolver.rbs | 4 ++-- sig/lrama/grammar/type.rbs | 4 ++-- 6 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 sig/lrama/grammar/code/rule_action.rbs diff --git a/lib/lrama/grammar/code/rule_action.rb b/lib/lrama/grammar/code/rule_action.rb index d3c0eab6..27db7044 100644 --- a/lib/lrama/grammar/code/rule_action.rb +++ b/lib/lrama/grammar/code/rule_action.rb @@ -41,6 +41,7 @@ def reference_to_c(ref) when ref.type == :dollar && ref.name == "$" # $$ tag = ref.ex_tag || lhs.tag raise_tag_not_found_error(ref) unless tag + # @type var tag: Lexer::Token::Tag "(yyval.#{tag.member})" when ref.type == :at && ref.name == "$" # @$ "(yyloc)" @@ -50,6 +51,7 @@ def reference_to_c(ref) i = -position_in_rhs + ref.index tag = ref.ex_tag || rhs[ref.index - 1].tag raise_tag_not_found_error(ref) unless tag + # @type var tag: Lexer::Token::Tag "(yyvsp[#{i}].#{tag.member})" when ref.type == :at # @n i = -position_in_rhs + ref.index diff --git a/sig/lrama/grammar/code/rule_action.rbs b/sig/lrama/grammar/code/rule_action.rbs new file mode 100644 index 00000000..1daf879d --- /dev/null +++ b/sig/lrama/grammar/code/rule_action.rbs @@ -0,0 +1,19 @@ +module Lrama + class Grammar + class Code + class RuleAction < Code + @rule: untyped + + def initialize: (type: Symbol, token_code: Lexer::Token::UserCode, rule: Rule) -> void + + private + + def reference_to_c: (Reference ref) -> String + def position_in_rhs: () -> Integer + def rhs: () -> Array[Grammar::Symbol] + def lhs: () -> Grammar::Symbol + def raise_tag_not_found_error: (Reference ref) -> void + end + end + end +end diff --git a/sig/lrama/grammar/rule.rbs b/sig/lrama/grammar/rule.rbs index c181be5b..9950ead0 100644 --- a/sig/lrama/grammar/rule.rbs +++ b/sig/lrama/grammar/rule.rbs @@ -5,7 +5,7 @@ module Lrama def initialize: ( ?id: untyped, ?_lhs: untyped, ?lhs: untyped, ?lhs_tag: untyped, ?_rhs: untyped, ?rhs: untyped, - ?token_code: untyped, ?position_in_original_rule_rhs: untyped, ?nullable: untyped, + ?token_code: untyped, ?position_in_original_rule_rhs: Integer?, ?nullable: untyped, ?precedence_sym: untyped, ?lineno: untyped ) -> void end diff --git a/sig/lrama/grammar/symbol.rbs b/sig/lrama/grammar/symbol.rbs index aa68e596..3522c355 100644 --- a/sig/lrama/grammar/symbol.rbs +++ b/sig/lrama/grammar/symbol.rbs @@ -4,7 +4,7 @@ module Lrama attr_accessor id: Lexer::Token attr_accessor alias_name: String? attr_accessor number: Integer - attr_accessor tag: Lexer::Token? + attr_accessor tag: Lexer::Token::Tag? attr_accessor term: bool attr_accessor token_id: Integer attr_accessor nullable: bool diff --git a/sig/lrama/grammar/symbols/resolver.rbs b/sig/lrama/grammar/symbols/resolver.rbs index 8f16cd16..b6120efa 100644 --- a/sig/lrama/grammar/symbols/resolver.rbs +++ b/sig/lrama/grammar/symbols/resolver.rbs @@ -12,8 +12,8 @@ module Lrama def initialize: () -> void def symbols: () -> Array[Grammar::Symbol] def sort_by_number!: () -> void - def add_term: (id: Lexer::Token, ?alias_name: String?, ?tag: Lexer::Token?, ?token_id: Integer?, ?replace: bool) -> Grammar::Symbol - def add_nterm: (id: Lexer::Token, ?alias_name: String?, ?tag: Lexer::Token?) -> Grammar::Symbol? + def add_term: (id: Lexer::Token, ?alias_name: String?, ?tag: Lexer::Token::Tag?, ?token_id: Integer?, ?replace: bool) -> Grammar::Symbol + def add_nterm: (id: Lexer::Token, ?alias_name: String?, ?tag: Lexer::Token::Tag?) -> Grammar::Symbol? def find_symbol_by_s_value: (Grammar::Symbol s_value) -> Grammar::Symbol? def find_symbol_by_s_value!: (Grammar::Symbol s_value) -> Grammar::Symbol def find_symbol_by_id: (Lexer::Token id) -> Grammar::Symbol? diff --git a/sig/lrama/grammar/type.rbs b/sig/lrama/grammar/type.rbs index 2cdd8b13..4d74eb17 100644 --- a/sig/lrama/grammar/type.rbs +++ b/sig/lrama/grammar/type.rbs @@ -2,9 +2,9 @@ module Lrama class Grammar class Type attr_reader id: Lexer::Token - attr_reader tag: Lexer::Token + attr_reader tag: Lexer::Token::Tag - def initialize: (id: Lexer::Token, tag: Lexer::Token) -> void + def initialize: (id: Lexer::Token, tag: Lexer::Token::Tag) -> void def ==: (Grammar::Type other) -> bool end end From b7e288f4913715974b9c92aa100289c5fe783816 Mon Sep 17 00:00:00 2001 From: Little_Rubyist Date: Mon, 22 Apr 2024 22:36:11 +0900 Subject: [PATCH 2/8] add no_reference_code.rbs --- sig/lrama/grammar/code/no_reference_code.rbs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 sig/lrama/grammar/code/no_reference_code.rbs diff --git a/sig/lrama/grammar/code/no_reference_code.rbs b/sig/lrama/grammar/code/no_reference_code.rbs new file mode 100644 index 00000000..950e32ff --- /dev/null +++ b/sig/lrama/grammar/code/no_reference_code.rbs @@ -0,0 +1,15 @@ +module Lrama + class Grammar + class Code + class NoReferenceCode < Code + private + + # * ($$) error + # * (@$) error + # * ($1) error + # * (@1) error + def reference_to_c: (Reference ref) -> void + end + end + end +end From 616998e997c1a37a2b0b783491be428d2fa55f19 Mon Sep 17 00:00:00 2001 From: Little_Rubyist Date: Mon, 22 Apr 2024 22:42:49 +0900 Subject: [PATCH 3/8] add initial_action_code.rbs --- sig/lrama/grammar/code/initial_action_code.rbs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 sig/lrama/grammar/code/initial_action_code.rbs diff --git a/sig/lrama/grammar/code/initial_action_code.rbs b/sig/lrama/grammar/code/initial_action_code.rbs new file mode 100644 index 00000000..de7ab875 --- /dev/null +++ b/sig/lrama/grammar/code/initial_action_code.rbs @@ -0,0 +1,15 @@ +module Lrama + class Grammar + class Code + class InitialActionCode < Code + private + + # * ($$) yylval + # * (@$) yylloc + # * ($1) error + # * (@1) error + def reference_to_c: (Reference ref) -> (String | bot) + end + end + end +end From e16f0fbacff70a2ec8637f3df2a2f65aa5bf6300 Mon Sep 17 00:00:00 2001 From: Little_Rubyist Date: Mon, 22 Apr 2024 22:57:55 +0900 Subject: [PATCH 4/8] fix destructor_code.rbs --- sig/lrama/grammar/code.rbs | 6 +++--- sig/lrama/grammar/code/destructor_code.rbs | 7 +++---- sig/lrama/grammar/code/no_reference_code.rbs | 2 +- sig/lrama/grammar/code/printer_code.rbs | 3 +-- sig/lrama/grammar/code/rule_action.rbs | 4 ++-- sig/lrama/grammar/destructor.rbs | 2 +- 6 files changed, 11 insertions(+), 13 deletions(-) diff --git a/sig/lrama/grammar/code.rbs b/sig/lrama/grammar/code.rbs index 81aaa55d..04eb2c25 100644 --- a/sig/lrama/grammar/code.rbs +++ b/sig/lrama/grammar/code.rbs @@ -3,8 +3,8 @@ module Lrama class Code extend Forwardable - attr_accessor type: Symbol - attr_accessor token_code: Lexer::Token::UserCode + attr_accessor type: ::Symbol + attr_accessor token_code: Grammar::Code # delegated def s_value: -> String @@ -12,7 +12,7 @@ module Lrama def column: -> Integer def references: -> Array[Lrama::Grammar::Reference] - def initialize: (type: Symbol, token_code: Lexer::Token::UserCode) -> void + def initialize: (type: ::Symbol, token_code: Grammar::Code) -> void def translated_code: () -> String diff --git a/sig/lrama/grammar/code/destructor_code.rbs b/sig/lrama/grammar/code/destructor_code.rbs index 8dc4784f..4cbebb5a 100644 --- a/sig/lrama/grammar/code/destructor_code.rbs +++ b/sig/lrama/grammar/code/destructor_code.rbs @@ -2,13 +2,12 @@ module Lrama class Grammar class Code class DestructorCode < Code - @tag: untyped - def initialize: (type: untyped, token_code: untyped, tag: untyped) -> void + @tag: Lexer::Token::Tag + def initialize: (type: ::Symbol, token_code: Grammar::Code, tag: Lexer::Token::Tag) -> void private - # ref: Lrama::Grammar::Code.token_code.references - def reference_to_c: (untyped ref) -> untyped + def reference_to_c: (Reference ref) -> (String | bot) end end end diff --git a/sig/lrama/grammar/code/no_reference_code.rbs b/sig/lrama/grammar/code/no_reference_code.rbs index 950e32ff..3238ab63 100644 --- a/sig/lrama/grammar/code/no_reference_code.rbs +++ b/sig/lrama/grammar/code/no_reference_code.rbs @@ -8,7 +8,7 @@ module Lrama # * (@$) error # * ($1) error # * (@1) error - def reference_to_c: (Reference ref) -> void + def reference_to_c: (Reference ref) -> bot end end end diff --git a/sig/lrama/grammar/code/printer_code.rbs b/sig/lrama/grammar/code/printer_code.rbs index 0228698b..07b4ce55 100644 --- a/sig/lrama/grammar/code/printer_code.rbs +++ b/sig/lrama/grammar/code/printer_code.rbs @@ -7,8 +7,7 @@ module Lrama private - # ref: Lrama::Grammar::Code.token_code.references - def reference_to_c: (untyped ref) -> untyped + def reference_to_c: (Reference ref) -> (String | bot) end end end diff --git a/sig/lrama/grammar/code/rule_action.rbs b/sig/lrama/grammar/code/rule_action.rbs index 1daf879d..9b6796af 100644 --- a/sig/lrama/grammar/code/rule_action.rbs +++ b/sig/lrama/grammar/code/rule_action.rbs @@ -4,7 +4,7 @@ module Lrama class RuleAction < Code @rule: untyped - def initialize: (type: Symbol, token_code: Lexer::Token::UserCode, rule: Rule) -> void + def initialize: (type: ::Symbol, token_code: Grammar::Code, rule: Rule) -> void private @@ -12,7 +12,7 @@ module Lrama def position_in_rhs: () -> Integer def rhs: () -> Array[Grammar::Symbol] def lhs: () -> Grammar::Symbol - def raise_tag_not_found_error: (Reference ref) -> void + def raise_tag_not_found_error: (Reference ref) -> bot end end end diff --git a/sig/lrama/grammar/destructor.rbs b/sig/lrama/grammar/destructor.rbs index b00f3308..1374c1c3 100644 --- a/sig/lrama/grammar/destructor.rbs +++ b/sig/lrama/grammar/destructor.rbs @@ -5,7 +5,7 @@ module Lrama attr_accessor token_code: Grammar::Code attr_accessor lineno: Integer - def translated_code: (Lexer::Token member) -> String + def translated_code: (Lexer::Token::Tag tag) -> String end end end From 9d0a37f334dcede3c65107c6af7b409fbfcb5787 Mon Sep 17 00:00:00 2001 From: Little_Rubyist Date: Mon, 22 Apr 2024 23:01:47 +0900 Subject: [PATCH 5/8] remove untyped type in grammar/code --- sig/lrama/grammar/code/printer_code.rbs | 4 ++-- sig/lrama/grammar/code/rule_action.rbs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sig/lrama/grammar/code/printer_code.rbs b/sig/lrama/grammar/code/printer_code.rbs index 07b4ce55..d8a3541b 100644 --- a/sig/lrama/grammar/code/printer_code.rbs +++ b/sig/lrama/grammar/code/printer_code.rbs @@ -2,8 +2,8 @@ module Lrama class Grammar class Code class PrinterCode < Code - @tag: untyped - def initialize: (type: untyped, token_code: untyped, tag: untyped) -> void + @tag: Lexer::Token::Tag + def initialize: (type: ::Symbol, token_code: Grammar::Code, tag: Lexer::Token::Tag) -> void private diff --git a/sig/lrama/grammar/code/rule_action.rbs b/sig/lrama/grammar/code/rule_action.rbs index 9b6796af..fda64170 100644 --- a/sig/lrama/grammar/code/rule_action.rbs +++ b/sig/lrama/grammar/code/rule_action.rbs @@ -2,7 +2,7 @@ module Lrama class Grammar class Code class RuleAction < Code - @rule: untyped + @rule: Rule def initialize: (type: ::Symbol, token_code: Grammar::Code, rule: Rule) -> void From 6c23e5c510fb6c2129832acebd3a0cc4dd244e09 Mon Sep 17 00:00:00 2001 From: Little_Rubyist Date: Mon, 22 Apr 2024 23:02:23 +0900 Subject: [PATCH 6/8] steep check grammar/code directory --- Steepfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Steepfile b/Steepfile index a44c3e4e..e8059ca2 100644 --- a/Steepfile +++ b/Steepfile @@ -6,7 +6,7 @@ target :lib do check "lib/lrama/grammar/binding.rb" check "lib/lrama/grammar/code/destructor_code.rb" - check "lib/lrama/grammar/code/printer_code.rb" + check "lib/lrama/grammar/code" check "lib/lrama/grammar/code.rb" check "lib/lrama/grammar/counter.rb" check "lib/lrama/grammar/error_token.rb" From cd31cd30bf7e896abf15d7c4f35fc3e59af31b55 Mon Sep 17 00:00:00 2001 From: Little_Rubyist Date: Wed, 8 May 2024 00:42:34 +0900 Subject: [PATCH 7/8] fix types and refactor steepfile --- Steepfile | 12 ++++++------ sig/lrama/grammar/code/rule_action.rbs | 2 +- sig/lrama/grammar/error_token.rbs | 4 ++-- sig/lrama/grammar/printer.rbs | 2 +- sig/lrama/grammar/rule.rbs | 25 ++++++++++++++++++++++--- 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/Steepfile b/Steepfile index e8059ca2..4bf2d153 100644 --- a/Steepfile +++ b/Steepfile @@ -4,23 +4,23 @@ target :lib do repo_path '.gem_rbs_collection/' signature "sig" - check "lib/lrama/grammar/binding.rb" - check "lib/lrama/grammar/code/destructor_code.rb" check "lib/lrama/grammar/code" + check "lib/lrama/grammar/parameterizing_rule" + check "lib/lrama/grammar/symbols" + + check "lib/lrama/grammar/binding.rb" check "lib/lrama/grammar/code.rb" check "lib/lrama/grammar/counter.rb" + check "lib/lrama/grammar/destructor.rb" check "lib/lrama/grammar/error_token.rb" - check "lib/lrama/grammar/parameterizing_rule" - check "lib/lrama/grammar/parameterizing_rules" - check "lib/lrama/grammar/symbols" check "lib/lrama/grammar/percent_code.rb" check "lib/lrama/grammar/precedence.rb" - check "lib/lrama/grammar/destructor.rb" check "lib/lrama/grammar/printer.rb" check "lib/lrama/grammar/reference.rb" check "lib/lrama/grammar/rule_builder.rb" check "lib/lrama/grammar/symbol.rb" check "lib/lrama/grammar/type.rb" + check "lib/lrama/lexer" check "lib/lrama/report" check "lib/lrama/bitmap.rb" diff --git a/sig/lrama/grammar/code/rule_action.rbs b/sig/lrama/grammar/code/rule_action.rbs index fda64170..6c24b071 100644 --- a/sig/lrama/grammar/code/rule_action.rbs +++ b/sig/lrama/grammar/code/rule_action.rbs @@ -11,7 +11,7 @@ module Lrama def reference_to_c: (Reference ref) -> String def position_in_rhs: () -> Integer def rhs: () -> Array[Grammar::Symbol] - def lhs: () -> Grammar::Symbol + def lhs: () -> Symbol def raise_tag_not_found_error: (Reference ref) -> bot end end diff --git a/sig/lrama/grammar/error_token.rbs b/sig/lrama/grammar/error_token.rbs index 9b6a06dc..6b82296a 100644 --- a/sig/lrama/grammar/error_token.rbs +++ b/sig/lrama/grammar/error_token.rbs @@ -2,10 +2,10 @@ module Lrama class Grammar class ErrorToken attr_accessor ident_or_tags: Array[Lexer::Token::Ident | Lexer::Token::Tag] - attr_accessor token_code: Symbol + attr_accessor token_code: Code attr_accessor lineno: Integer - def translated_code: (Lexer::Token? tag) -> String + def translated_code: (Lexer::Token::Tag tag) -> String end end end diff --git a/sig/lrama/grammar/printer.rbs b/sig/lrama/grammar/printer.rbs index 947273dc..6048cda7 100644 --- a/sig/lrama/grammar/printer.rbs +++ b/sig/lrama/grammar/printer.rbs @@ -5,7 +5,7 @@ module Lrama attr_accessor token_code: Grammar::Code attr_accessor lineno: Integer - def translated_code: (Lexer::Token member) -> String + def translated_code: (Lexer::Token::Tag tag) -> String end end end diff --git a/sig/lrama/grammar/rule.rbs b/sig/lrama/grammar/rule.rbs index 9950ead0..5a639022 100644 --- a/sig/lrama/grammar/rule.rbs +++ b/sig/lrama/grammar/rule.rbs @@ -1,13 +1,32 @@ module Lrama class Grammar class Rule + attr_accessor id: Integer + attr_accessor _lhs: Lexer::Token + attr_accessor lhs: Symbol + attr_accessor lhs_tag: untyped + attr_accessor _rhs: untyped + attr_accessor rhs: untyped + attr_accessor token_code: Lexer::Token::UserCode? + attr_accessor position_in_original_rule_rhs: untyped + attr_accessor nullable: bool + attr_accessor precedence_sym: Lexer::Token? + attr_accessor lineno: Integer? attr_accessor original_rule: Rule def initialize: ( - ?id: untyped, ?_lhs: untyped, ?lhs: untyped, ?lhs_tag: untyped, ?_rhs: untyped, ?rhs: untyped, - ?token_code: untyped, ?position_in_original_rule_rhs: Integer?, ?nullable: untyped, - ?precedence_sym: untyped, ?lineno: untyped + ?id: Integer, ?_lhs: Lexer::Token, ?lhs: Lexer::Token, ?lhs_tag: untyped, ?_rhs: untyped, ?rhs: untyped, + ?token_code: Lexer::Token::UserCode?, ?position_in_original_rule_rhs: Integer?, ?nullable: bool, + ?precedence_sym: Lexer::Token?, ?lineno: Integer? ) -> void + + def as_comment: -> String + + def empty_rule?: -> bool + + def initial_rule?: -> bool + + def with_actions: -> String end end end From 43cd9ea71c638d2da1d49d440d5af2e76553872b Mon Sep 17 00:00:00 2001 From: Little_Rubyist Date: Fri, 17 May 2024 15:24:03 +0900 Subject: [PATCH 8/8] Corrected the way the type is written --- sig/lrama/grammar/code/rule_action.rbs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sig/lrama/grammar/code/rule_action.rbs b/sig/lrama/grammar/code/rule_action.rbs index 6c24b071..fda64170 100644 --- a/sig/lrama/grammar/code/rule_action.rbs +++ b/sig/lrama/grammar/code/rule_action.rbs @@ -11,7 +11,7 @@ module Lrama def reference_to_c: (Reference ref) -> String def position_in_rhs: () -> Integer def rhs: () -> Array[Grammar::Symbol] - def lhs: () -> Symbol + def lhs: () -> Grammar::Symbol def raise_tag_not_found_error: (Reference ref) -> bot end end