From 478437add4fcd3aa3a203b8fed0f0d3b84b6fa5a Mon Sep 17 00:00:00 2001 From: ydah <13041216+ydah@users.noreply.github.com> Date: Wed, 7 Feb 2024 18:13:02 +0900 Subject: [PATCH 1/5] Add sig/lrama/grammar/type.rbs --- Steepfile | 1 + sig/lrama/grammar/type.rbs | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 sig/lrama/grammar/type.rbs diff --git a/Steepfile b/Steepfile index 6e455bbe..7d37c283 100644 --- a/Steepfile +++ b/Steepfile @@ -17,6 +17,7 @@ target :lib do 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/type.rbs b/sig/lrama/grammar/type.rbs new file mode 100644 index 00000000..2cdd8b13 --- /dev/null +++ b/sig/lrama/grammar/type.rbs @@ -0,0 +1,11 @@ +module Lrama + class Grammar + class Type + attr_reader id: Lexer::Token + attr_reader tag: Lexer::Token + + def initialize: (id: Lexer::Token, tag: Lexer::Token) -> void + def ==: (Grammar::Type other) -> bool + end + end +end From 35f0cb476b436c896d61f2f39624af9b49c285f3 Mon Sep 17 00:00:00 2001 From: ydah <13041216+ydah@users.noreply.github.com> Date: Wed, 7 Feb 2024 18:14:33 +0900 Subject: [PATCH 2/5] Add sig/lrama/grammar/symbols/resolver.rbs --- Steepfile | 1 + sig/lrama/grammar/symbols/resolver.rbs | 41 ++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 sig/lrama/grammar/symbols/resolver.rbs diff --git a/Steepfile b/Steepfile index 7d37c283..a8789d38 100644 --- a/Steepfile +++ b/Steepfile @@ -11,6 +11,7 @@ target :lib do 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/printer.rb" diff --git a/sig/lrama/grammar/symbols/resolver.rbs b/sig/lrama/grammar/symbols/resolver.rbs new file mode 100644 index 00000000..8f16cd16 --- /dev/null +++ b/sig/lrama/grammar/symbols/resolver.rbs @@ -0,0 +1,41 @@ +module Lrama + class Grammar + class Symbols + class Resolver + attr_reader terms: Array[Grammar::Symbol] + attr_reader nterms: Array[Grammar::Symbol] + + @symbols: Array[Grammar::Symbol]? + @number: Integer + @used_numbers: Hash[Integer, bool] + + 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 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? + def find_symbol_by_id!: (Lexer::Token id) -> Grammar::Symbol + def find_symbol_by_token_id: (Integer token_id) -> Grammar::Symbol? + def find_symbol_by_number!: (Integer number) -> Grammar::Symbol + def fill_symbol_number: () -> void + def fill_nterm_type: (Array[Grammar::Type] types) -> void + def fill_printer: (Array[Grammar::Printer] printers) -> void + def fill_error_token: (Array[Grammar::ErrorToken] error_tokens) -> void + def token_to_symbol: (Lexer::Token token) -> Grammar::Symbol + def validate!: () -> void + + private + + def find_nterm_by_id!: (Lexer::Token id) -> Grammar::Symbol + def fill_terms_number: () -> void + def fill_nterms_number: () -> void + def used_numbers: () -> Hash[Integer, bool] + def validate_number_uniqueness!: () -> void + def validate_alias_name_uniqueness!: () -> void + end + end + end +end From 985a96e5218d7d0467ca089022e794e452563acf Mon Sep 17 00:00:00 2001 From: ydah <13041216+ydah@users.noreply.github.com> Date: Wed, 7 Feb 2024 20:07:40 +0900 Subject: [PATCH 3/5] Use symbols instead of @instead --- lib/lrama/grammar/symbols/resolver.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/lrama/grammar/symbols/resolver.rb b/lib/lrama/grammar/symbols/resolver.rb index 456993b8..f34b9b2e 100644 --- a/lib/lrama/grammar/symbols/resolver.rb +++ b/lib/lrama/grammar/symbols/resolver.rb @@ -14,7 +14,7 @@ def symbols end def sort_by_number! - @symbols.sort_by!(&:number) + symbols.sort_by!(&:number) end def add_term(id:, alias_name: nil, tag: nil, token_id: nil, replace: false) @@ -237,7 +237,7 @@ def used_numbers return @used_numbers if defined?(@used_numbers) @used_numbers = {} - @symbols.map(&:number).each do |n| + symbols.map(&:number).each do |n| @used_numbers[n] = true end @used_numbers From c499a0fbbc8de797807d924b3fe8a2281f70a76d Mon Sep 17 00:00:00 2001 From: ydah <13041216+ydah@users.noreply.github.com> Date: Wed, 7 Feb 2024 20:09:49 +0900 Subject: [PATCH 4/5] Fix steep check offense MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` ❯ bundle exec rake steep bundle exec steep check # Type checking files: ...................................................................................................................................F................... lib/lrama/grammar/symbols/resolver.rb:24:31: [error] Cannot pass a value of type `(::String | nil)` as an argument of type `::String` │ (::String | nil) <: ::String │ nil <: ::String │ │ Diagnostic ID: Ruby::ArgumentTypeMismatch │ └ sym.alias_name = alias_name ~~~~~~~~~~ lib/lrama/grammar/symbols/resolver.rb:36:24: [error] More keyword arguments are required: precedence, printer │ Diagnostic ID: Ruby::InsufficientKeywordArguments │ └ term = Symbol.new( ~~~~ lib/lrama/grammar/symbols/resolver.rb:48:25: [error] More keyword arguments are required: precedence, printer │ Diagnostic ID: Ruby::InsufficientKeywordArguments │ └ nterm = Symbol.new( ~~~~ Detected 3 problems from 1 file ``` --- sig/lrama/grammar/symbol.rbs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sig/lrama/grammar/symbol.rbs b/sig/lrama/grammar/symbol.rbs index 6c345bdb..0d066934 100644 --- a/sig/lrama/grammar/symbol.rbs +++ b/sig/lrama/grammar/symbol.rbs @@ -2,7 +2,7 @@ module Lrama class Grammar class Symbol attr_accessor id: Lexer::Token - attr_accessor alias_name: String + attr_accessor alias_name: String? attr_accessor number: Integer attr_accessor tag: Lexer::Token? attr_accessor term: bool @@ -20,8 +20,8 @@ module Lrama attr_writer accept_symbol: Symbol def initialize: ( - id: Lexer::Token, alias_name: String?, number: Integer?, tag: Lexer::Token?, - term: bool, token_id: Integer?, nullable: bool?, precedence: Precedence?, printer: Printer?) -> void + id: Lexer::Token, term: bool, ?alias_name: String?, ?number: Integer?, ?tag: Lexer::Token?, + ?token_id: Integer?, ?nullable: bool?, ?precedence: Precedence?, ?printer: Printer?) -> void def term?: () -> bool def nterm?: () -> bool @@ -31,7 +31,7 @@ module Lrama def accept_symbol?: () -> bool def display_name: () -> String def enum_name: () -> String - def comment: () -> String + def comment: () -> String? end end end From be3a312ca7e5f7e0a24c3a6eb52e721d8af47e41 Mon Sep 17 00:00:00 2001 From: ydah <13041216+ydah@users.noreply.github.com> Date: Wed, 7 Feb 2024 20:10:33 +0900 Subject: [PATCH 5/5] Fix steep check offense MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` ❯ bundle exec rake steep bundle exec steep check # Type checking files: .............................................................................................................................F......................... lib/lrama/grammar/symbols/resolver.rb:201:33: [error] Cannot pass a value of type `(::Integer | nil)` as an argument of type `::Integer` │ (::Integer | nil) <: ::Integer │ nil <: ::Integer │ │ Diagnostic ID: Ruby::ArgumentTypeMismatch │ └ sym.token_id = Integer($1, 8) ~~~~~~~~~~~~~~ lib/lrama/grammar/symbols/resolver.rb:203:36: [error] Type `(::String | nil)` does not have method `bytes` │ Diagnostic ID: Ruby::NoMethod │ └ sym.token_id = $1.bytes.first ~~~~~ Detected 2 problems from 1 file ``` --- lib/lrama/grammar/symbols/resolver.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/lrama/grammar/symbols/resolver.rb b/lib/lrama/grammar/symbols/resolver.rb index f34b9b2e..07e03e70 100644 --- a/lib/lrama/grammar/symbols/resolver.rb +++ b/lib/lrama/grammar/symbols/resolver.rb @@ -198,9 +198,17 @@ def fill_terms_number when "\\\\" sym.token_id = 92 when /\A\\(\d+)\z/ - sym.token_id = Integer($1, 8) + unless (id = Integer($1, 8)).nil? + sym.token_id = id + else + raise "Unknown Char s_value #{sym}" + end when /\A(.)\z/ - sym.token_id = $1.bytes.first + unless (id = $1&.bytes&.first).nil? + sym.token_id = id + else + raise "Unknown Char s_value #{sym}" + end else raise "Unknown Char s_value #{sym}" end