Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add grammar/code directory types #417

Merged
merged 8 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Steepfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ target :lib do
repo_path '.gem_rbs_collection/'
signature "sig"

check "lib/lrama/grammar/code"
check "lib/lrama/grammar/parameterizing_rule"
check "lib/lrama/grammar/symbols"

check "lib/lrama/grammar/binding.rb"
check "lib/lrama/grammar/code/destructor_code.rb"
check "lib/lrama/grammar/code/printer_code.rb"
check "lib/lrama/grammar/code.rb"
check "lib/lrama/grammar/counter.rb"
check "lib/lrama/grammar/destructor.rb"
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/destructor.rb"
check "lib/lrama/grammar/printer.rb"
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"
Expand Down
2 changes: 2 additions & 0 deletions lib/lrama/grammar/code/rule_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def reference_to_c(ref)
when ref.type == :dollar && ref.name == "$" # $$
tag = ref.ex_tag || lhs.tag
raise_tag_not_found_error(ref) unless tag
# @type var tag: Lexer::Token::Tag
"(yyval.#{tag.member})"
when ref.type == :at && ref.name == "$" # @$
"(yyloc)"
Expand All @@ -50,6 +51,7 @@ def reference_to_c(ref)
i = -position_in_rhs + ref.index
tag = ref.ex_tag || rhs[ref.index - 1].tag
raise_tag_not_found_error(ref) unless tag
# @type var tag: Lexer::Token::Tag
"(yyvsp[#{i}].#{tag.member})"
when ref.type == :at # @n
i = -position_in_rhs + ref.index
Expand Down
6 changes: 3 additions & 3 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: Symbol
attr_accessor token_code: Lexer::Token::UserCode
attr_accessor type: ::Symbol
attr_accessor token_code: Grammar::Code

# delegated
def s_value: -> String
def line: -> Integer
def column: -> Integer
def references: -> Array[Lrama::Grammar::Reference]

def initialize: (type: Symbol, token_code: Lexer::Token::UserCode) -> void
def initialize: (type: ::Symbol, token_code: Grammar::Code) -> void

def translated_code: () -> String

Expand Down
7 changes: 3 additions & 4 deletions sig/lrama/grammar/code/destructor_code.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ module Lrama
class Grammar
class Code
class DestructorCode < Code
@tag: untyped
def initialize: (type: untyped, token_code: untyped, tag: untyped) -> void
@tag: Lexer::Token::Tag
def initialize: (type: ::Symbol, token_code: Grammar::Code, tag: Lexer::Token::Tag) -> void

private

# ref: Lrama::Grammar::Code.token_code.references
def reference_to_c: (untyped ref) -> untyped
def reference_to_c: (Reference ref) -> (String | bot)
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions sig/lrama/grammar/code/initial_action_code.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Lrama
class Grammar
class Code
class InitialActionCode < Code
private

# * ($$) yylval
# * (@$) yylloc
# * ($1) error
# * (@1) error
def reference_to_c: (Reference ref) -> (String | bot)
end
end
end
end
15 changes: 15 additions & 0 deletions sig/lrama/grammar/code/no_reference_code.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Lrama
class Grammar
class Code
class NoReferenceCode < Code
private

# * ($$) error
# * (@$) error
# * ($1) error
# * (@1) error
def reference_to_c: (Reference ref) -> bot
end
end
end
end
7 changes: 3 additions & 4 deletions sig/lrama/grammar/code/printer_code.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ module Lrama
class Grammar
class Code
class PrinterCode < Code
@tag: untyped
def initialize: (type: untyped, token_code: untyped, tag: untyped) -> void
@tag: Lexer::Token::Tag
def initialize: (type: ::Symbol, token_code: Grammar::Code, tag: Lexer::Token::Tag) -> void

private

# ref: Lrama::Grammar::Code.token_code.references
def reference_to_c: (untyped ref) -> untyped
def reference_to_c: (Reference ref) -> (String | bot)
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions sig/lrama/grammar/code/rule_action.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Lrama
class Grammar
class Code
class RuleAction < Code
@rule: Rule

def initialize: (type: ::Symbol, token_code: Grammar::Code, rule: Rule) -> void

private

def reference_to_c: (Reference ref) -> String
def position_in_rhs: () -> Integer
def rhs: () -> Array[Grammar::Symbol]
def lhs: () -> Symbol
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any intention to use "Symbol" however rhs is Grammar::Symbol.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, seems like a simple mistake. I fixed.

def raise_tag_not_found_error: (Reference ref) -> bot
end
end
end
end
2 changes: 1 addition & 1 deletion sig/lrama/grammar/destructor.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Lrama
attr_accessor token_code: Grammar::Code
attr_accessor lineno: Integer

def translated_code: (Lexer::Token member) -> String
def translated_code: (Lexer::Token::Tag tag) -> String
end
end
end
4 changes: 2 additions & 2 deletions sig/lrama/grammar/error_token.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ module Lrama
class Grammar
class ErrorToken
attr_accessor ident_or_tags: Array[Lexer::Token::Ident | Lexer::Token::Tag]
attr_accessor token_code: Symbol
attr_accessor token_code: Code
attr_accessor lineno: Integer

def translated_code: (Lexer::Token? tag) -> String
def translated_code: (Lexer::Token::Tag tag) -> String
end
end
end
2 changes: 1 addition & 1 deletion sig/lrama/grammar/printer.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Lrama
attr_accessor token_code: Grammar::Code
attr_accessor lineno: Integer

def translated_code: (Lexer::Token member) -> String
def translated_code: (Lexer::Token::Tag tag) -> String
end
end
end
25 changes: 22 additions & 3 deletions sig/lrama/grammar/rule.rbs
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
module Lrama
class Grammar
class Rule
attr_accessor id: Integer
attr_accessor _lhs: Lexer::Token
attr_accessor lhs: Symbol
attr_accessor lhs_tag: untyped
attr_accessor _rhs: untyped
attr_accessor rhs: untyped
attr_accessor token_code: Lexer::Token::UserCode?
attr_accessor position_in_original_rule_rhs: untyped
attr_accessor nullable: bool
attr_accessor precedence_sym: Lexer::Token?
attr_accessor lineno: Integer?
attr_accessor original_rule: Rule

def initialize: (
?id: untyped, ?_lhs: untyped, ?lhs: untyped, ?lhs_tag: untyped, ?_rhs: untyped, ?rhs: untyped,
?token_code: untyped, ?position_in_original_rule_rhs: untyped, ?nullable: untyped,
?precedence_sym: untyped, ?lineno: untyped
?id: Integer, ?_lhs: Lexer::Token, ?lhs: Lexer::Token, ?lhs_tag: untyped, ?_rhs: untyped, ?rhs: untyped,
?token_code: Lexer::Token::UserCode?, ?position_in_original_rule_rhs: Integer?, ?nullable: bool,
?precedence_sym: Lexer::Token?, ?lineno: Integer?
) -> void

def as_comment: -> String

def empty_rule?: -> bool

def initial_rule?: -> bool

def with_actions: -> String
end
end
end
2 changes: 1 addition & 1 deletion sig/lrama/grammar/symbol.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Lrama
attr_accessor id: Lexer::Token
attr_accessor alias_name: String?
attr_accessor number: Integer
attr_accessor tag: Lexer::Token?
attr_accessor tag: Lexer::Token::Tag?
attr_accessor term: bool
attr_accessor token_id: Integer
attr_accessor nullable: bool
Expand Down
4 changes: 2 additions & 2 deletions sig/lrama/grammar/symbols/resolver.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ module Lrama
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 add_term: (id: Lexer::Token, ?alias_name: String?, ?tag: Lexer::Token::Tag?, ?token_id: Integer?, ?replace: bool) -> Grammar::Symbol
def add_nterm: (id: Lexer::Token, ?alias_name: String?, ?tag: Lexer::Token::Tag?) -> 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?
Expand Down
4 changes: 2 additions & 2 deletions sig/lrama/grammar/type.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module Lrama
class Grammar
class Type
attr_reader id: Lexer::Token
attr_reader tag: Lexer::Token
attr_reader tag: Lexer::Token::Tag

def initialize: (id: Lexer::Token, tag: Lexer::Token) -> void
def initialize: (id: Lexer::Token, tag: Lexer::Token::Tag) -> void
def ==: (Grammar::Type other) -> bool
end
end
Expand Down