Class: LydownParser

Inherits:
Object
  • Object
show all
Defined in:
lib/lydown/parsing.rb

Class Method Summary collapse

Class Method Details

.do_parse(source, opts) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/lydown/parsing.rb', line 20

def self.do_parse(source, opts)
  parser, ast = self.new
  ast = parser.parse(source)

  unless ast
    error_msg = format_parser_error(source, parser, opts)
    raise LydownError, error_msg
  else
    stream = []
    ast.to_stream(stream, opts.merge(progress_base: source.length))
    # insert source ref event into stream if we have a filename ref
    if opts[:filename] && !stream.empty?
      stream.unshift({type: :source_ref}.merge(opts))
    end
    stream
  end
end

.format_parser_error(source, parser, opts) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lydown/parsing.rb', line 55

def self.format_parser_error(source, parser, opts)
  msg = opts[:filename] ? "#{Pathname.relative_pwd(opts[:filename])}: " : ""
  if opts[:nice_error]
    msg << "Unexpected character at line #{parser.failure_line} column #{parser.failure_column}:\n"
  else
    msg << "#{parser.failure_reason}:\n"
  end
  msg << "  #{source.lines[parser.failure_line - 1].chomp}\n #{' ' * parser.failure_column}^"

  msg
end

.parse(source, opts = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/lydown/parsing.rb', line 8

def self.parse(source, opts = {})
  if opts[:no_progress_bar]
    do_parse(source, opts)
  else
    Lydown::CLI.show_progress('Parse', source.length * 2) do |bar|
      stream = do_parse(source, opts)
      bar.finish
      stream
    end
  end
end

.parse_macro_group(source, opts) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lydown/parsing.rb', line 38

def self.parse_macro_group(source, opts)
  parser, ast = self.new, ast = nil
  old_bar, $progress_bar = $progress_bar, nil
  ast = parser.parse(source)
  unless ast
    error_msg = format_parser_error(source, parser, opts)
    $stderr.puts error_msg
    raise LydownError, error_msg
  else
    stream = []
    ast.to_stream(stream, opts)
    stream
  end
ensure
  $progress_bar = old_bar
end