Exception: TSJSON::TSJSONSyntaxError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/language/lexer/syntax_error.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, nodes, source, positions, path = nil, originalError = nil) ⇒ TSJSONSyntaxError

Returns a new instance of TSJSONSyntaxError.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/language/lexer/syntax_error.rb', line 13

def initialize(
  message, nodes, source, positions, path = nil, originalError = nil
)
  self.message = message

  _nodes =
    if nodes.is_a?(Array)
      nodes.length != 0 ? nodes : nil
    else
      nodes ? [nodes] : nil
    end

  # Compute locations in the source for the given nodes/positions.
  _source = source
  _source = _nodes[0].loc&.source if (!_source && _nodes)

  _positions = positions

  if (!_positions && _nodes)
    _positions =
      _nodes.reduce([]) do |list, node|
        list.push(node.loc.start) if (node.loc)
        return list
      end
  end
  _positions = undefined if (_positions && _positions.length === 0)

  if (positions && source)
    _locations = positions.map { |pos| Location.get_location(source, pos) }
  elsif (_nodes)
    _locations =
      _nodes.reduce([]) do |list, node|
        if (node.loc)
          list.push(
            Location.get_location(node.loc.source, node.loc.start_pos)
          )
        end
        return list
      end
  end

  self.source = _source
  self.positions = _positions
  self.locations = _locations
  self.nodes = _nodes
end

Instance Attribute Details

#locationsObject

Returns the value of attribute locations.



5
6
7
# File 'lib/language/lexer/syntax_error.rb', line 5

def locations
  @locations
end

#messageObject

Returns the value of attribute message.



5
6
7
# File 'lib/language/lexer/syntax_error.rb', line 5

def message
  @message
end

#nodesObject

Returns the value of attribute nodes.



5
6
7
# File 'lib/language/lexer/syntax_error.rb', line 5

def nodes
  @nodes
end

#originalErrorObject

Returns the value of attribute originalError.



5
6
7
# File 'lib/language/lexer/syntax_error.rb', line 5

def originalError
  @originalError
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/language/lexer/syntax_error.rb', line 5

def path
  @path
end

#positionsObject

Returns the value of attribute positions.



5
6
7
# File 'lib/language/lexer/syntax_error.rb', line 5

def positions
  @positions
end

#sourceObject

Returns the value of attribute source.



5
6
7
# File 'lib/language/lexer/syntax_error.rb', line 5

def source
  @source
end

Class Method Details



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/language/lexer/syntax_error.rb', line 60

def self.print_error(error)
  output = error.message

  if (error.nodes)
    error.nodes.each do |node|
      output += "\n\n" + printLocation(node.loc) if (node.loc)
    end
  elsif (error.source && error.locations)
    error.locations.each do |location|
      output +=
        "\n\n" + Source.print_source_location(error.source, location)
    end
  end

  return output
end

.syntax_error(source, position, description) ⇒ Object



77
78
79
# File 'lib/language/lexer/syntax_error.rb', line 77

def self.syntax_error(source, position, description)
  return new("Syntax Error: #{description}", nil, source, [position])
end

Instance Method Details

#to_sObject



85
86
87
# File 'lib/language/lexer/syntax_error.rb', line 85

def to_s
  return TSJSONSyntaxError.print_error(self)
end

#toJSONObject



81
82
83
# File 'lib/language/lexer/syntax_error.rb', line 81

def toJSON
  return { message: self.message, locations: self.locations }
end