From 6030a20068a24915a39f9273e5f82b7a53ddbb82 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Sun, 24 Dec 2023 12:27:30 +0900 Subject: [PATCH 1/2] Make Code to be plain object So that we can change type and token_code to be a mandatory argument. --- lib/lrama/grammar/code.rb | 24 ++++++++++++++++++------ lib/lrama/grammar/code/printer_code.rb | 2 +- lib/lrama/grammar/code/rule_action.rb | 2 +- sig/lrama/grammar/code.rbs | 2 +- sig/lrama/grammar/code/printer_code.rbs | 2 +- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/lrama/grammar/code.rb b/lib/lrama/grammar/code.rb index 81a03383..d0bef75e 100644 --- a/lib/lrama/grammar/code.rb +++ b/lib/lrama/grammar/code.rb @@ -1,12 +1,29 @@ require "forwardable" +require "lrama/grammar/code/initial_action_code" +require "lrama/grammar/code/no_reference_code" +require "lrama/grammar/code/printer_code" +require "lrama/grammar/code/rule_action" module Lrama class Grammar - class Code < Struct.new(:type, :token_code, keyword_init: true) + class Code extend Forwardable def_delegators "token_code", :s_value, :line, :column, :references + attr_reader :type, :token_code + + def initialize(type:, token_code:) + @type = type + @token_code = token_code + end + + def ==(other) + self.class == other.class && + self.type == other.type && + self.token_code == other.token_code + end + # $$, $n, @$, @n are translated to C code def translated_code t_code = s_value.dup @@ -31,8 +48,3 @@ def reference_to_c(ref) end end end - -require "lrama/grammar/code/initial_action_code" -require "lrama/grammar/code/no_reference_code" -require "lrama/grammar/code/printer_code" -require "lrama/grammar/code/rule_action" diff --git a/lib/lrama/grammar/code/printer_code.rb b/lib/lrama/grammar/code/printer_code.rb index a19646ad..2b1f127f 100644 --- a/lib/lrama/grammar/code/printer_code.rb +++ b/lib/lrama/grammar/code/printer_code.rb @@ -2,7 +2,7 @@ module Lrama class Grammar class Code class PrinterCode < Code - def initialize(type: nil, token_code: nil, tag: nil) + def initialize(type:, token_code:, tag:) super(type: type, token_code: token_code) @tag = tag end diff --git a/lib/lrama/grammar/code/rule_action.rb b/lib/lrama/grammar/code/rule_action.rb index 984c350b..8a67b732 100644 --- a/lib/lrama/grammar/code/rule_action.rb +++ b/lib/lrama/grammar/code/rule_action.rb @@ -2,7 +2,7 @@ module Lrama class Grammar class Code class RuleAction < Code - def initialize(type: nil, token_code: nil, rule: nil) + def initialize(type:, token_code:, rule:) super(type: type, token_code: token_code) @rule = rule end diff --git a/sig/lrama/grammar/code.rbs b/sig/lrama/grammar/code.rbs index 0a8ba403..3bf2ef25 100644 --- a/sig/lrama/grammar/code.rbs +++ b/sig/lrama/grammar/code.rbs @@ -12,7 +12,7 @@ module Lrama def column: -> untyped def references: -> untyped - def initialize: (?type: untyped, ?token_code: untyped) -> void + def initialize: (type: untyped, token_code: untyped) -> void def translated_code: () -> String diff --git a/sig/lrama/grammar/code/printer_code.rbs b/sig/lrama/grammar/code/printer_code.rbs index d067b3f0..0228698b 100644 --- a/sig/lrama/grammar/code/printer_code.rbs +++ b/sig/lrama/grammar/code/printer_code.rbs @@ -3,7 +3,7 @@ module Lrama class Code class PrinterCode < Code @tag: untyped - def initialize: (?type: untyped, ?token_code: untyped, ?tag: untyped) -> void + def initialize: (type: untyped, token_code: untyped, tag: untyped) -> void private From c332168ed53bd24d7f822a012f4b71f5307330cc Mon Sep 17 00:00:00 2001 From: yui-knk Date: Sun, 24 Dec 2023 12:31:12 +0900 Subject: [PATCH 2/2] Clarify untyped of Lrama::Grammar::Code --- sig/lrama/grammar/code.rbs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sig/lrama/grammar/code.rbs b/sig/lrama/grammar/code.rbs index 3bf2ef25..81aaa55d 100644 --- a/sig/lrama/grammar/code.rbs +++ b/sig/lrama/grammar/code.rbs @@ -3,16 +3,16 @@ module Lrama class Code extend Forwardable - attr_accessor type: untyped - attr_accessor token_code: untyped + attr_accessor type: Symbol + attr_accessor token_code: Lexer::Token::UserCode # delegated def s_value: -> String def line: -> Integer - def column: -> untyped - def references: -> untyped + def column: -> Integer + def references: -> Array[Lrama::Grammar::Reference] - def initialize: (type: untyped, token_code: untyped) -> void + def initialize: (type: Symbol, token_code: Lexer::Token::UserCode) -> void def translated_code: () -> String