Class: Gloss::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/gloss/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Parser

Returns a new instance of Parser.



7
8
9
# File 'lib/gloss/parser.rb', line 7

def initialize(str)
  @str = str
end

Instance Method Details

#runObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gloss/parser.rb', line 10

def run()
  tree_json = Gloss.parse_buffer(@str)
  begin
    JSON.parse(tree_json, symbolize_names: true)
  rescue JSON::ParserError
    error_message = tree_json
    error_message.match(/.+\s:(\d+)$/)
    (if $~.[](1)
      line_number = $~.[](1)
.to_i
      context = @str.lines
.[](((          line_number.-(2)
)..(line_number)))
.map
.with_index() { |line, index|
"#{index.-(1)
.+(line_number)}|  #{line}"          }
.join
      error_message = "#{context.rstrip}\n\n#{error_message}\n"
    end)
    throw(:"error", error_message)
  end
end