Skip to content

Commit

Permalink
Add source position tracking to fields and constants
Browse files Browse the repository at this point in the history
  • Loading branch information
maximecb committed Feb 8, 2024
1 parent a2ebd3b commit 641bb41
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/protobuff/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ def to_s
Message = Struct.new(:name, :fields, :pos)

# Qualifier is :optional, :required or :repeated
Field = Struct.new(:qualifier, :type, :name, :number)
Field = Struct.new(:qualifier, :type, :name, :number, :pos)

# Enum and enum constants
Enum = Struct.new(:name, :constants, :pos)
Constant = Struct.new(:name, :number)
Constant = Struct.new(:name, :number, :pos)

# Parse a source string
def self.parse_string(str)
Expand Down Expand Up @@ -173,6 +173,7 @@ def self.parse_message(input, pos)

# Field type and name
input.eat_ws
field_pos = input.pos
type = input.read_ident
input.eat_ws
name = input.read_ident
Expand All @@ -185,7 +186,7 @@ def self.parse_message(input, pos)
raise "field number should be in uint32 range"
end

fields << Field.new(qualifier, type, name, number)
fields << Field.new(qualifier, type, name, number, field_pos)
end

Message.new(message_name, fields, pos)
Expand All @@ -206,6 +207,7 @@ def self.parse_enum(input, pos)

# Constant name and number
input.eat_ws
const_pos = input.pos
name = input.read_ident
input.expect '='
input.eat_ws
Expand All @@ -224,7 +226,7 @@ def self.parse_enum(input, pos)
raise "enum constants should be in uint32 range"
end

constants << Constant.new(name, number)
constants << Constant.new(name, number, const_pos)
end

Enum.new(enum_name, constants, pos)
Expand Down

0 comments on commit 641bb41

Please sign in to comment.