Skip to content

Commit

Permalink
Merge pull request #50 from alitaso345/refactor_states_item
Browse files Browse the repository at this point in the history
Refactor states.rb and grammar.rb
  • Loading branch information
yui-knk authored Jul 8, 2023
2 parents 3811b3a + 8352028 commit 0983285
Show file tree
Hide file tree
Showing 12 changed files with 345 additions and 323 deletions.
8 changes: 8 additions & 0 deletions lib/lrama.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
require "lrama/bitmap"
require "lrama/code"
require "lrama/command"
require "lrama/context"
require "lrama/digraph"
require "lrama/error_token"
require "lrama/grammar"
require "lrama/lexer"
require "lrama/output"
require "lrama/parser"
require "lrama/precedence"
require "lrama/printer"
require "lrama/reference"
require "lrama/report"
require "lrama/rule"
require "lrama/state"
require "lrama/states"
require "lrama/states_reporter"
require "lrama/symbol"
require "lrama/union"
require "lrama/version"
require "lrama/warning"
121 changes: 121 additions & 0 deletions lib/lrama/code.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
require "forwardable"

module Lrama
class Code < Struct.new(:type, :token_code, keyword_init: true)
extend Forwardable

def_delegators "token_code", :s_value, :line, :column, :references

# $$, $n, @$, @n is translated to C code
def translated_code
case type
when :user_code
translated_user_code
when :initial_action
translated_initial_action_code
end
end

# * ($1) error
# * ($$) *yyvaluep
# * (@1) error
# * (@$) *yylocationp
def translated_printer_code(tag)
t_code = s_value.dup

references.reverse.each do |ref|
first_column = ref.first_column
last_column = ref.last_column

case
when ref.value == "$" && ref.type == :dollar # $$
# Omit "<>"
member = tag.s_value[1..-2]
str = "((*yyvaluep).#{member})"
when ref.value == "$" && ref.type == :at # @$
str = "(*yylocationp)"
when ref.type == :dollar # $n
raise "$#{ref.value} can not be used in %printer."
when ref.type == :at # @n
raise "@#{ref.value} can not be used in %printer."
else
raise "Unexpected. #{self}, #{ref}"
end

t_code[first_column..last_column] = str
end

return t_code
end
alias :translated_error_token_code :translated_printer_code


private

# * ($1) yyvsp[i]
# * ($$) yyval
# * (@1) yylsp[i]
# * (@$) yyloc
def translated_user_code
t_code = s_value.dup

references.reverse.each do |ref|
first_column = ref.first_column
last_column = ref.last_column

case
when ref.value == "$" && ref.type == :dollar # $$
# Omit "<>"
member = ref.tag.s_value[1..-2]
str = "(yyval.#{member})"
when ref.value == "$" && ref.type == :at # @$
str = "(yyloc)"
when ref.type == :dollar # $n
i = -ref.position_in_rhs + ref.value
# Omit "<>"
member = ref.tag.s_value[1..-2]
str = "(yyvsp[#{i}].#{member})"
when ref.type == :at # @n
i = -ref.position_in_rhs + ref.value
str = "(yylsp[#{i}])"
else
raise "Unexpected. #{self}, #{ref}"
end

t_code[first_column..last_column] = str
end

return t_code
end

# * ($1) error
# * ($$) yylval
# * (@1) error
# * (@$) yylloc
def translated_initial_action_code
t_code = s_value.dup

references.reverse.each do |ref|
first_column = ref.first_column
last_column = ref.last_column

case
when ref.value == "$" && ref.type == :dollar # $$
str = "yylval"
when ref.value == "$" && ref.type == :at # @$
str = "yylloc"
when ref.type == :dollar # $n
raise "$#{ref.value} can not be used in initial_action."
when ref.type == :at # @n
raise "@#{ref.value} can not be used in initial_action."
else
raise "Unexpected. #{self}, #{ref}"
end

t_code[first_column..last_column] = str
end

return t_code
end
end
end
7 changes: 7 additions & 0 deletions lib/lrama/error_token.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Lrama
class ErrorToken < Struct.new(:ident_or_tags, :code, :lineno, keyword_init: true)
def translated_code(member)
code.translated_error_token_code(member)
end
end
end
Loading

0 comments on commit 0983285

Please sign in to comment.