Class: ProcToAst::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/proc_to_ast/parser_gem.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



22
23
24
25
# File 'lib/proc_to_ast/parser_gem.rb', line 22

def initialize
  @parser = ::Parser::CurrentRuby.default_parser
  @parser.diagnostics.consumer = ->(diagnostic) {} # suppress error message
end

Instance Attribute Details

#parserObject (readonly)

Returns the value of attribute parser.



11
12
13
# File 'lib/proc_to_ast/parser_gem.rb', line 11

def parser
  @parser
end

Class Method Details

.highlight(source) ⇒ Object



17
18
19
# File 'lib/proc_to_ast/parser_gem.rb', line 17

def highlight(source)
  @formatter.format(@lexer.lex(source))
end

Instance Method Details

#parse(filename, linenum) ⇒ Parser::AST::Node

Read file and try parsing if success parse, find proc AST

Parameters:

  • filename (String)

    reading file path

  • linenum (Integer)

    start line number

Returns:

  • (Parser::AST::Node)

    Proc AST



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/proc_to_ast/parser_gem.rb', line 33

def parse(filename, linenum)
  @filename, @linenum = filename, linenum
  buf = []
  File.open(filename, "rb").each_with_index do |line, index|
    next if index < linenum - 1
    buf << line
    begin
      return do_parse(buf.join)
    rescue ::Parser::SyntaxError
      node = trim_and_retry(buf)
      return node if node
    end
  end
  fail(::Parser::SyntaxError, "Unknown error")
end