Class: CoderCompanion::Java::JavaParser

Inherits:
LanguageParser show all
Defined in:
lib/codercompanion/java/java_parser.rb

Constant Summary collapse

JAVA_EXTENSIONS =
['.java']

Instance Attribute Summary

Attributes inherited from LanguageParser

#excluded, #tree_walker

Instance Method Summary collapse

Methods inherited from LanguageParser

#create_project_json

Constructor Details

#initializeJavaParser

Returns a new instance of JavaParser.



8
9
10
11
12
# File 'lib/codercompanion/java/java_parser.rb', line 8

def initialize
    Treetop.load(File.join(File.dirname(__FILE__), 'java_grammar'))
    @parser = CoderCompanion::JavaParser.new
    @tree_walker = CoderCompanion::Java::JavaTreeWalker.new
end

Instance Method Details

#build_tree(root_node) ⇒ Object



57
58
59
# File 'lib/codercompanion/java/java_parser.rb', line 57

def build_tree(root_node)
    root_node.build
end

#is_valid_file(ext) ⇒ Object



53
54
55
# File 'lib/codercompanion/java/java_parser.rb', line 53

def is_valid_file(ext)
    return JAVA_EXTENSIONS.include?(ext)
end

#parse(files) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/codercompanion/java/java_parser.rb', line 14

def parse(files)
    results = []
    files.each do |file|
        results << parse_file(file)
    end
    return results
end

#parse_file(file) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/codercompanion/java/java_parser.rb', line 22

def parse_file(file)
    data = File.open(file).read
    tree = @parser.parse(data)
    
    if tree
        #puts tree.inspect
    end
    
    # If the AST is nil then there was an error during parsing
    # we need to report a simple error message to help the user
    # if(tree.nil?)
    #   raise Exception, "Parse error at offset: #{@@parser.index}"
    # end

    if !tree
      #puts @parser.terminal_failures
      error_string = "\nParse error"
      error_string += "\nExpected: #{(@parser.terminal_failures.collect {|f| f.expected_string}).join(", ")}"
      error_string += "\nwhile parsing file: #{file}"
      error_string += "\nat line #{@parser.failure_line}"
      error_string += "\nat column #{@parser.failure_column}"
      error_string += "\nIf your code is compilable, report a bug at:"
      error_string += "\nhttp://codercompanion.com/contact"
      raise CoderCompanion::CoderCompanionException, error_string
    end
    
    ast = self.build_tree(tree)
    CoderCompanion::j_print_inline CoderCompanion::success_format '.'
    {:type => 'source', :body => ast, :file_path => file}
end