Module: Walrus::Grammar::LocationTracking

Included in:
ArrayResult, MatchDataWrapper, Node, ParseError, SkippedSubstringException, StringResult
Defined in:
lib/walrus/grammar/location_tracking.rb

Overview

Methods for embedding location information in objects returned (or exceptions raised) from parse methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#outer_endObject

For occasions where a single item must serve as a carrier for array-like information (that is, its own start, end and source_text, as well as the “outer” equivalents). This can happen where a single node appears in a list context surrounded only by skipped content.



28
29
30
# File 'lib/walrus/grammar/location_tracking.rb', line 28

def outer_end
  @outer_end
end

#outer_source_textObject

For occasions where a single item must serve as a carrier for array-like information (that is, its own start, end and source_text, as well as the “outer” equivalents). This can happen where a single node appears in a list context surrounded only by skipped content.



28
29
30
# File 'lib/walrus/grammar/location_tracking.rb', line 28

def outer_source_text
  @outer_source_text
end

#outer_startObject

For occasions where a single item must serve as a carrier for array-like information (that is, its own start, end and source_text, as well as the “outer” equivalents). This can happen where a single node appears in a list context surrounded only by skipped content.



28
29
30
# File 'lib/walrus/grammar/location_tracking.rb', line 28

def outer_start
  @outer_start
end

#source_textObject

Returns the value of attribute source_text.



23
24
25
# File 'lib/walrus/grammar/location_tracking.rb', line 23

def source_text
  @source_text
end

Instance Method Details

#column_endObject



81
82
83
# File 'lib/walrus/grammar/location_tracking.rb', line 81

def column_end
  @column_end || 0
end

#column_end=(column_end) ⇒ Object



77
78
79
# File 'lib/walrus/grammar/location_tracking.rb', line 77

def column_end=(column_end)
  @column_end = column_end.to_i
end

#column_startObject

Returns 0 if @column_start is nil (for ease of use, users of classes that mix-in this module don’t have to worry about special casing nil values).



41
42
43
# File 'lib/walrus/grammar/location_tracking.rb', line 41

def column_start
  @column_start || 0
end

#column_start=(column_start) ⇒ Object

Sets @column_start to col. Sets @column_start to 0 if passed nil (for ease of use, users of classes that mix-in this module don’t have to worry about special casing nil values).



36
37
38
# File 'lib/walrus/grammar/location_tracking.rb', line 36

def column_start=(column_start)
  @column_start = column_start.to_i
end

#endObject

Convenience method for getting both line_end and column_end at once.



86
87
88
# File 'lib/walrus/grammar/location_tracking.rb', line 86

def end
  [self.line_end, self.column_end]
end

#end=(array) ⇒ Object

Convenience method for setting both line_end and column_end at once.

Raises:

  • (ArgumentError)


91
92
93
94
95
96
# File 'lib/walrus/grammar/location_tracking.rb', line 91

def end=(array)
  raise ArgumentError if array.nil?
  raise ArgumentError if array.length != 2
  self.line_end   = array[0]
  self.column_end = array[1]
end

#line_endObject



73
74
75
# File 'lib/walrus/grammar/location_tracking.rb', line 73

def line_end
  @line_end || 0
end

#line_end=(line_end) ⇒ Object



69
70
71
# File 'lib/walrus/grammar/location_tracking.rb', line 69

def line_end=(line_end)
  @line_end = line_end.to_i
end

#line_startObject

Returns 0 if @line_start is nil (for ease of use, users of classes that mix-in this module don’t have to worry about special casing nil values).



52
53
54
# File 'lib/walrus/grammar/location_tracking.rb', line 52

def line_start
  @line_start || 0
end

#line_start=(line_start) ⇒ Object

Sets @line_start to line. Sets @line_start to 0 if passed nil (for ease of use, users of classes that mix-in this module don’t have to worry about special casing nil values).



47
48
49
# File 'lib/walrus/grammar/location_tracking.rb', line 47

def line_start=(line_start)
  @line_start = line_start.to_i
end

#rightmost?(other) ⇒ Boolean

Given another object that responds to column_end and line_end, returns true if the receiver is rightmost or equal. If the other object is farther to the right returns false.

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
108
109
110
# File 'lib/walrus/grammar/location_tracking.rb', line 100

def rightmost?(other)
  if self.line_end > other.line_end
    true
  elsif other.line_end > self.line_end
    false
  elsif self.column_end >= other.column_end
    true
  else
    false
  end
end

#startObject

Convenience method for getting both line_start and column_start at once.



57
58
59
# File 'lib/walrus/grammar/location_tracking.rb', line 57

def start
  [self.line_start, self.column_start]
end

#start=(array) ⇒ Object

Convenience method for setting both line_start and column_start at once.

Raises:

  • (ArgumentError)


62
63
64
65
66
67
# File 'lib/walrus/grammar/location_tracking.rb', line 62

def start=(array)
  raise ArgumentError if array.nil?
  raise ArgumentError if array.length != 2
  self.line_start    = array[0]
  self.column_start  = array[1]
end