Class: PositionHandler

Inherits:
Psych::TreeBuilder
  • Object
show all
Defined in:
lib/traceable.rb

Overview

Extend the parser to remember the position of each object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ PositionHandler

filename is the name of the source file the YAML structure will be parsed from. It is only copied into parsed objects, not interpreted.



128
129
130
131
# File 'lib/traceable.rb', line 128

def initialize(filename)
  super()
  @file = filename
end

Instance Attribute Details

#parserObject

The handler needs access to the parser in order to call mark



123
124
125
# File 'lib/traceable.rb', line 123

def parser
  @parser
end

Instance Method Details

#record_position(node, mark) ⇒ Object

Copy a parser position from a Psych::Parser::Mark to a parse tree node.



135
136
137
138
139
140
141
142
# File 'lib/traceable.rb', line 135

def record_position(node, mark)
  node.extend Traceable
  node.file = @file
  node.byte = mark.index
  node.line = mark.line
  node.column = mark.column
  node
end

#scalar(value, anchor, tag, plain, quoted, style) ⇒ Object



152
153
154
# File 'lib/traceable.rb', line 152

def scalar(value, anchor, tag, plain, quoted, style)
  record_position(super, parser.mark)
end

#start_mapping(anchor, tag, implicit, style) ⇒ Object



144
145
146
# File 'lib/traceable.rb', line 144

def start_mapping(anchor, tag, implicit, style)
  record_position(super, parser.mark)
end

#start_sequence(anchor, tag, implicit, style) ⇒ Object



148
149
150
# File 'lib/traceable.rb', line 148

def start_sequence(anchor, tag, implicit, style)
  record_position(super, parser.mark)
end