Skip to content

Commit

Permalink
Merge pull request #368 from ydah/types
Browse files Browse the repository at this point in the history
Add rbs for `Grammar::Type` and `Grammar::Symbols::Resolver`
  • Loading branch information
yui-knk authored Feb 11, 2024
2 parents b0c29ea + be3a312 commit ed67d67
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Steepfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ target :lib do
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/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
16 changes: 12 additions & 4 deletions lib/lrama/grammar/symbols/resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def symbols
end

def sort_by_number!
@symbols.sort_by!(&:number)
symbols.sort_by!(&:number)
end

def add_term(id:, alias_name: nil, tag: nil, token_id: nil, replace: false)
Expand Down Expand Up @@ -198,9 +198,17 @@ def fill_terms_number
when "\\\\"
sym.token_id = 92
when /\A\\(\d+)\z/
sym.token_id = Integer($1, 8)
unless (id = Integer($1, 8)).nil?
sym.token_id = id
else
raise "Unknown Char s_value #{sym}"
end
when /\A(.)\z/
sym.token_id = $1.bytes.first
unless (id = $1&.bytes&.first).nil?
sym.token_id = id
else
raise "Unknown Char s_value #{sym}"
end
else
raise "Unknown Char s_value #{sym}"
end
Expand Down Expand Up @@ -237,7 +245,7 @@ def used_numbers
return @used_numbers if defined?(@used_numbers)

@used_numbers = {}
@symbols.map(&:number).each do |n|
symbols.map(&:number).each do |n|
@used_numbers[n] = true
end
@used_numbers
Expand Down
8 changes: 4 additions & 4 deletions sig/lrama/grammar/symbol.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Lrama
class Grammar
class Symbol
attr_accessor id: Lexer::Token
attr_accessor alias_name: String
attr_accessor alias_name: String?
attr_accessor number: Integer
attr_accessor tag: Lexer::Token?
attr_accessor term: bool
Expand All @@ -20,8 +20,8 @@ module Lrama
attr_writer accept_symbol: Symbol

def initialize: (
id: Lexer::Token, alias_name: String?, number: Integer?, tag: Lexer::Token?,
term: bool, token_id: Integer?, nullable: bool?, precedence: Precedence?, printer: Printer?) -> void
id: Lexer::Token, term: bool, ?alias_name: String?, ?number: Integer?, ?tag: Lexer::Token?,
?token_id: Integer?, ?nullable: bool?, ?precedence: Precedence?, ?printer: Printer?) -> void

def term?: () -> bool
def nterm?: () -> bool
Expand All @@ -31,7 +31,7 @@ module Lrama
def accept_symbol?: () -> bool
def display_name: () -> String
def enum_name: () -> String
def comment: () -> String
def comment: () -> String?
end
end
end
41 changes: 41 additions & 0 deletions sig/lrama/grammar/symbols/resolver.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module Lrama
class Grammar
class Symbols
class Resolver
attr_reader terms: Array[Grammar::Symbol]
attr_reader nterms: Array[Grammar::Symbol]

@symbols: Array[Grammar::Symbol]?
@number: Integer
@used_numbers: Hash[Integer, bool]

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 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?
def find_symbol_by_id!: (Lexer::Token id) -> Grammar::Symbol
def find_symbol_by_token_id: (Integer token_id) -> Grammar::Symbol?
def find_symbol_by_number!: (Integer number) -> Grammar::Symbol
def fill_symbol_number: () -> void
def fill_nterm_type: (Array[Grammar::Type] types) -> void
def fill_printer: (Array[Grammar::Printer] printers) -> void
def fill_error_token: (Array[Grammar::ErrorToken] error_tokens) -> void
def token_to_symbol: (Lexer::Token token) -> Grammar::Symbol
def validate!: () -> void

private

def find_nterm_by_id!: (Lexer::Token id) -> Grammar::Symbol
def fill_terms_number: () -> void
def fill_nterms_number: () -> void
def used_numbers: () -> Hash[Integer, bool]
def validate_number_uniqueness!: () -> void
def validate_alias_name_uniqueness!: () -> void
end
end
end
end
11 changes: 11 additions & 0 deletions sig/lrama/grammar/type.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Lrama
class Grammar
class Type
attr_reader id: Lexer::Token
attr_reader tag: Lexer::Token

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

0 comments on commit ed67d67

Please sign in to comment.