Class: ProcToAst::Parser
- Inherits:
-
Object
- Object
- ProcToAst::Parser
- Defined in:
- lib/proc_to_ast/parser_gem.rb
Instance Attribute Summary collapse
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ Parser
constructor
A new instance of Parser.
-
#parse(filename, linenum) ⇒ Parser::AST::Node
Read file and try parsing if success parse, find proc AST.
Constructor Details
#initialize ⇒ Parser
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
#parser ⇒ Object (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
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 |