Skip to content

Commit

Permalink
parser: minor allocations reductions
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot committed Apr 4, 2024
1 parent 93c24ef commit 674ac6e
Showing 1 changed file with 19 additions and 48 deletions.
67 changes: 19 additions & 48 deletions lib/protoboeuf/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ def initialize(file_name, line_no, col_no)

def to_s
if @file_name
@file_name + "@" + @line_no.to_s + ":" + @col_no.to_s
"#{@file_name}@#{@line_no}:#{@col_no}"
else
@line_no.to_s + ":" + @col_no.to_s
"#{@line_no}:#{@col_no}"
end
end
end
Expand All @@ -42,7 +42,7 @@ def initialize(msg, pos)
end

def to_s
@msg.to_s + "@" + @pos.to_s
"#{@msg}@#{@pos}"
end
end

Expand Down Expand Up @@ -200,10 +200,9 @@ def self.check_enum_collision(enums)
names = Set.new
enums.each do |enum|
enum.constants.each do |const|
if names.include? const.name
unless names.add? const.name
raise ParseError.new("duplicate enum constant name #{const.name}", const.pos)
end
names.add(const.name)
end
end
end
Expand Down Expand Up @@ -294,33 +293,25 @@ def self.parse_field_type(input)
end

type_name = ident
loop do
if input.match '.'
type_name += '.' + input.read_ident
else
break
end
while input.match '.'
type_name << '.' << input.read_ident
end

type_name
end

# Parse a package name, e.g. foo.bar.bif
def self.parse_package_name(input)
name = ""
name = +""

if input.match '.'
name += '.' + input.read_ident
name << '.' << input.read_ident
else
name += input.read_ident
name << input.read_ident
end

loop do
if input.match '.'
name += '.' + input.read_ident
else
break
end
while input.match '.'
name << '.' << input.read_ident
end

name
Expand Down Expand Up @@ -387,11 +378,7 @@ def self.parse_body(input, pos, inside_message)

input.expect '{'

loop do
if input.match '}'
break
end

until input.match '}'
# Nested/local message and enum definitions
if inside_message && (input.match 'message')
msg_pos = input.pos
Expand Down Expand Up @@ -525,11 +512,7 @@ def self.parse_enum(input, pos)
enum_name = input.read_ident
input.expect '{'

loop do
if input.match '}'
break
end

until input.match '}'
if input.match 'option'
input.eat_ws
option_name = input.read_ident
Expand All @@ -553,7 +536,7 @@ def self.parse_enum(input, pos)
raise ParseError.new("enum constants should be uppercase identifiers", const_pos)
end

if constants.size == 0 && number != 0
if constants.empty? && number != 0
raise ParseError.new("the first enum constant should always have value 0", const_pos)
end

Expand Down Expand Up @@ -664,12 +647,8 @@ def eat_ch()
end

# Consume whitespace
def eat_ws()
loop do
if eof?
break
end

def eat_ws
until eof?
# Single-line comment
if match_exact("//")
loop do
Expand Down Expand Up @@ -721,11 +700,7 @@ def eat_ws()
def read_ident
name = +''

loop do
if eof?
break
end

until eof?
ch = peek_ch

if !ch.match(/[A-Za-z0-9_]/)
Expand All @@ -735,7 +710,7 @@ def read_ident
name << eat_ch
end

if name.size == 0
if name.empty?
raise ParseError.new("expected identifier", pos)
end

Expand Down Expand Up @@ -809,11 +784,7 @@ def read_int
end

# For each digit
loop do
if eof?
break
end

until eof?
ch = peek_ch

if ch >= '0' && ch <= '9'
Expand Down

0 comments on commit 674ac6e

Please sign in to comment.