Skip to content

Commit

Permalink
Merge pull request #312 from yui-knk/plain_object_codes
Browse files Browse the repository at this point in the history
Make Code to be plain object
  • Loading branch information
yui-knk authored Dec 24, 2023
2 parents ee2421e + c332168 commit 904318b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
24 changes: 18 additions & 6 deletions lib/lrama/grammar/code.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
2 changes: 1 addition & 1 deletion lib/lrama/grammar/code/printer_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/lrama/grammar/code/rule_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions sig/lrama/grammar/code.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion sig/lrama/grammar/code/printer_code.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 904318b

Please sign in to comment.