lib/rubrowser/parser/file.rb in rubrowser-0.2.6 vs lib/rubrowser/parser/file.rb in rubrowser-0.2.7
- old
+ new
@@ -1,10 +1,10 @@
require 'parser/current'
require 'rubrowser/parser/definition/class'
require 'rubrowser/parser/definition/module'
require 'rubrowser/parser/relation/base'
-require 'rubrowser/parser/builder'
+require 'rubrowser/parser/file/builder'
module Rubrowser
module Parser
class File
FILE_SIZE_LIMIT = 2 * 1024 * 1024
@@ -16,26 +16,25 @@
@definitions = []
@relations = []
end
def parse
- if valid_file?(file)
- contents = ::File.read(file)
+ return unless valid_file?(file)
+ contents = ::File.read(file)
- buffer = ::Parser::Source::Buffer.new(file, 1)
- buffer.source = contents.force_encoding(Encoding::UTF_8)
+ buffer = ::Parser::Source::Buffer.new(file, 1)
+ buffer.source = contents.force_encoding(Encoding::UTF_8)
- parser = ::Parser::CurrentRuby.new(Builder.new)
- parser.diagnostics.ignore_warnings = true
- parser.diagnostics.all_errors_are_fatal = false
+ parser = ::Parser::CurrentRuby.new(Builder.new)
+ parser.diagnostics.ignore_warnings = true
+ parser.diagnostics.all_errors_are_fatal = false
- ast = parser.parse(buffer)
- constants = parse_block(ast)
+ ast = parser.parse(buffer)
+ constants = parse_block(ast)
- @definitions = constants[:definitions]
- @relations = constants[:relations]
- end
+ @definitions = constants[:definitions]
+ @relations = constants[:relations]
rescue ::Parser::SyntaxError
warn "SyntaxError in #{file}"
end
def valid_file?(file)
@@ -60,11 +59,12 @@
def parse_module(node, parents = [])
namespace = ast_consts_to_array(node.children.first, parents)
definition = Definition::Module.new(
namespace,
file: file,
- line: node.loc.line
+ line: node.loc.line,
+ lines: node.loc.last_line - node.loc.line + 1
)
constants = { definitions: [definition] }
children_constants = parse_array(node.children[1..-1], namespace)
merge_constants(children_constants, constants)
@@ -73,10 +73,11 @@
def parse_class(node, parents = [])
namespace = ast_consts_to_array(node.children.first, parents)
definition = Definition::Class.new(
namespace,
file: file,
- line: node.loc.line
+ line: node.loc.line,
+ lines: node.loc.last_line - node.loc.line + 1
)
constants = { definitions: [definition] }
children_constants = parse_array(node.children[1..-1], namespace)
merge_constants(children_constants, constants)