Skip to content

Commit

Permalink
Merge pull request #509 from ydah/use-rbs-inline
Browse files Browse the repository at this point in the history
Replace with rbs-inline style type definition in lexer directory
  • Loading branch information
ydah authored Jan 13, 2025
2 parents cc9ee80 + a6c21d7 commit ae0b85e
Show file tree
Hide file tree
Showing 19 changed files with 185 additions and 57 deletions.
9 changes: 8 additions & 1 deletion lib/lrama/lexer/grammar_file.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
# rbs_inline: enabled
# frozen_string_literal: true

module Lrama
class Lexer
class GrammarFile
class Text < String
# @rbs () -> String
def inspect
length <= 50 ? super : "#{self[0..47]}...".inspect
end
end

attr_reader :path, :text
attr_reader :path #: String
attr_reader :text #: String

# @rbs (String path, String text) -> void
def initialize(path, text)
@path = path
@text = Text.new(text).freeze
end

# @rbs () -> String
def inspect
"<#{self.class}: @path=#{path}, @text=#{text.inspect}>"
end

# @rbs (GrammarFile other) -> bool
def ==(other)
self.class == other.class &&
self.path == other.path
end

# @rbs () -> Array[String]
def lines
@lines ||= text.split("\n")
end
Expand Down
18 changes: 17 additions & 1 deletion lib/lrama/lexer/location.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# rbs_inline: enabled
# frozen_string_literal: true

module Lrama
class Lexer
class Location
attr_reader :grammar_file, :first_line, :first_column, :last_line, :last_column
attr_reader :grammar_file #: GrammarFile
attr_reader :first_line #: Integer
attr_reader :first_column #: Integer
attr_reader :last_line #: Integer
attr_reader :last_column #: Integer

# @rbs (grammar_file: GrammarFile, first_line: Integer, first_column: Integer, last_line: Integer, last_column: Integer) -> void
def initialize(grammar_file:, first_line:, first_column:, last_line:, last_column:)
@grammar_file = grammar_file
@first_line = first_line
Expand All @@ -13,6 +19,7 @@ def initialize(grammar_file:, first_line:, first_column:, last_line:, last_colum
@last_column = last_column
end

# @rbs (Location other) -> bool
def ==(other)
self.class == other.class &&
self.grammar_file == other.grammar_file &&
Expand All @@ -22,6 +29,7 @@ def ==(other)
self.last_column == other.last_column
end

# @rbs (Integer left, Integer right) -> Location
def partial_location(left, right)
offset = -first_column
new_first_line = -1
Expand Down Expand Up @@ -52,17 +60,20 @@ def partial_location(left, right)
)
end

# @rbs () -> String
def to_s
"#{path} (#{first_line},#{first_column})-(#{last_line},#{last_column})"
end

# @rbs (String error_message) -> String
def generate_error_message(error_message)
<<~ERROR.chomp
#{path}:#{first_line}:#{first_column}: #{error_message}
#{line_with_carets}
ERROR
end

# @rbs () -> String
def line_with_carets
<<~TEXT
#{text}
Expand All @@ -72,22 +83,27 @@ def line_with_carets

private

# @rbs () -> String
def path
grammar_file.path
end

# @rbs () -> String
def blanks
(text[0...first_column] or raise "#{first_column} is invalid").gsub(/[^\t]/, ' ')
end

# @rbs () -> String
def carets
blanks + '^' * (last_column - first_column)
end

# @rbs () -> String
def text
@text ||= _text.join("\n")
end

# @rbs () -> Array[String]
def _text
@_text ||=begin
range = (first_line - 1)...last_line
Expand Down
16 changes: 14 additions & 2 deletions lib/lrama/lexer/token.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# rbs_inline: enabled
# frozen_string_literal: true

require_relative 'token/char'
Expand All @@ -9,46 +10,57 @@
module Lrama
class Lexer
class Token
attr_reader :s_value, :location
attr_accessor :alias_name, :referred
attr_reader :s_value #: String
attr_reader :location #: Location
attr_accessor :alias_name #: String
attr_accessor :referred #: bool

# @rbs (s_value: String, ?alias_name: String, ?location: Location) -> void
def initialize(s_value:, alias_name: nil, location: nil)
s_value.freeze
@s_value = s_value
@alias_name = alias_name
@location = location
end

# @rbs () -> String
def to_s
"value: `#{s_value}`, location: #{location}"
end

# @rbs (String string) -> bool
def referred_by?(string)
[self.s_value, self.alias_name].compact.include?(string)
end

# @rbs (Token other) -> bool
def ==(other)
self.class == other.class && self.s_value == other.s_value
end

# @rbs () -> Integer
def first_line
location.first_line
end
alias :line :first_line

# @rbs () -> Integer
def first_column
location.first_column
end
alias :column :first_column

# @rbs () -> Integer
def last_line
location.last_line
end

# @rbs () -> Integer
def last_column
location.last_column
end

# @rbs (Lrama::Grammar::Reference ref, String message) -> bot
def invalid_ref(ref, message)
location = self.location.partial_location(ref.first_column, ref.last_column)
raise location.generate_error_message(message)
Expand Down
1 change: 1 addition & 0 deletions lib/lrama/lexer/token/char.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# rbs_inline: enabled
# frozen_string_literal: true

module Lrama
Expand Down
1 change: 1 addition & 0 deletions lib/lrama/lexer/token/ident.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# rbs_inline: enabled
# frozen_string_literal: true

module Lrama
Expand Down
7 changes: 6 additions & 1 deletion lib/lrama/lexer/token/instantiate_rule.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
# rbs_inline: enabled
# frozen_string_literal: true

module Lrama
class Lexer
class Token
class InstantiateRule < Token
attr_reader :args, :lhs_tag
attr_reader :args #: Array[Lexer::Token]
attr_reader :lhs_tag #: Lexer::Token::Tag?

# @rbs (s_value: String, ?alias_name: String, ?location: Location, ?args: Array[Lexer::Token], ?lhs_tag: Lexer::Token::Tag?) -> void
def initialize(s_value:, alias_name: nil, location: nil, args: [], lhs_tag: nil)
super s_value: s_value, alias_name: alias_name, location: location
@args = args
@lhs_tag = lhs_tag
end

# @rbs () -> String
def rule_name
s_value
end

# @rbs () -> Integer
def args_count
args.count
end
Expand Down
4 changes: 3 additions & 1 deletion lib/lrama/lexer/token/tag.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# rbs_inline: enabled
# frozen_string_literal: true

module Lrama
class Lexer
class Token
class Tag < Token
# Omit "<>"
# @rbs () -> String
def member
# Omit "<>"
s_value[1..-2] or raise "Unexpected Tag format (#{s_value})"
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/lrama/lexer/token/user_code.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# rbs_inline: enabled
# frozen_string_literal: true

require "strscan"
Expand All @@ -6,14 +7,16 @@ module Lrama
class Lexer
class Token
class UserCode < Token
attr_accessor :tag
attr_accessor :tag #: Lexer::Token::Tag

# @rbs () -> Array[Lrama::Grammar::Reference]
def references
@references ||= _references
end

private

# @rbs () -> Array[Lrama::Grammar::Reference]
def _references
scanner = StringScanner.new(s_value)
references = [] #: Array[Grammar::Reference]
Expand All @@ -32,6 +35,7 @@ def _references
references
end

# @rbs (StringScanner scanner) -> Lrama::Grammar::Reference?
def scan_reference(scanner)
start = scanner.pos
case
Expand Down
6 changes: 3 additions & 3 deletions rbs_collection.lock.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ gems:
source:
type: git
name: ruby/gem_rbs_collection
revision: ccbd2bbc6be5c195df1d90aa68d64916a9e7d0ab
revision: 7651e7b92c15bd5b4bc11fd3dd455be0ea571fd0
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: erb
Expand All @@ -26,15 +26,15 @@ gems:
source:
type: git
name: ruby/gem_rbs_collection
revision: ccbd2bbc6be5c195df1d90aa68d64916a9e7d0ab
revision: 7651e7b92c15bd5b4bc11fd3dd455be0ea571fd0
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: stackprof
version: '0.2'
source:
type: git
name: ruby/gem_rbs_collection
revision: ccbd2bbc6be5c195df1d90aa68d64916a9e7d0ab
revision: 7651e7b92c15bd5b4bc11fd3dd455be0ea571fd0
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: strscan
Expand Down
28 changes: 28 additions & 0 deletions sig/generated/lrama/lexer/grammar_file.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated from lib/lrama/lexer/grammar_file.rb with RBS::Inline

module Lrama
class Lexer
class GrammarFile
class Text < String
# @rbs () -> String
def inspect: () -> String
end

attr_reader path: String

attr_reader text: String

# @rbs (String path, String text) -> void
def initialize: (String path, String text) -> void

# @rbs () -> String
def inspect: () -> String

# @rbs (GrammarFile other) -> bool
def ==: (GrammarFile other) -> bool

# @rbs () -> Array[String]
def lines: () -> Array[String]
end
end
end
52 changes: 52 additions & 0 deletions sig/generated/lrama/lexer/location.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Generated from lib/lrama/lexer/location.rb with RBS::Inline

module Lrama
class Lexer
class Location
attr_reader grammar_file: GrammarFile

attr_reader first_line: Integer

attr_reader first_column: Integer

attr_reader last_line: Integer

attr_reader last_column: Integer

# @rbs (grammar_file: GrammarFile, first_line: Integer, first_column: Integer, last_line: Integer, last_column: Integer) -> void
def initialize: (grammar_file: GrammarFile, first_line: Integer, first_column: Integer, last_line: Integer, last_column: Integer) -> void

# @rbs (Location other) -> bool
def ==: (Location other) -> bool

# @rbs (Integer left, Integer right) -> Location
def partial_location: (Integer left, Integer right) -> Location

# @rbs () -> String
def to_s: () -> String

# @rbs (String error_message) -> String
def generate_error_message: (String error_message) -> String

# @rbs () -> String
def line_with_carets: () -> String

private

# @rbs () -> String
def path: () -> String

# @rbs () -> String
def blanks: () -> String

# @rbs () -> String
def carets: () -> String

# @rbs () -> String
def text: () -> String

# @rbs () -> Array[String]
def _text: () -> Array[String]
end
end
end
Loading

0 comments on commit ae0b85e

Please sign in to comment.