-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from alitaso345/refactor_states_item
Refactor states.rb and grammar.rb
- Loading branch information
Showing
12 changed files
with
345 additions
and
323 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.