Class: Mirah::Parser

Inherits:
Object
  • Object
show all
Includes:
Util::ProcessErrors
Defined in:
lib/mirah/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::ProcessErrors

#process_errors

Constructor Details

#initialize(state, logging) ⇒ Parser

Returns a new instance of Parser.



24
25
26
27
28
29
# File 'lib/mirah/parser.rb', line 24

def initialize(state, logging)
  @transformer = Mirah::Transform::Transformer.new(state)
  Java::MirahImpl::Builtin.initialize_builtins(@transformer)
  @logging = logging
  @verbose = state.verbose
end

Instance Attribute Details

#loggingObject

Returns the value of attribute logging.



31
32
33
# File 'lib/mirah/parser.rb', line 31

def logging
  @logging
end

#transformerObject

Returns the value of attribute transformer.



31
32
33
# File 'lib/mirah/parser.rb', line 31

def transformer
  @transformer
end

Instance Method Details

#expand_files(files_or_scripts) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/mirah/parser.rb', line 72

def expand_files(files_or_scripts)
  expanded = []
  files_or_scripts.each do |filename|
    if File.directory?(filename)
      Dir[File.join(filename, '*')].each do |child|
        if File.directory?(child)
          files_or_scripts << child
        elsif child =~ /\.(duby|mirah)$/
          expanded << child
        end
      end
    else
      expanded << filename
    end
  end
  expanded
end

#parse_and_transform(filename, src) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/mirah/parser.rb', line 62

def parse_and_transform(filename, src)
  parser_ast = Mirah::AST.parse_ruby(src, filename)
  
  transformer.filename = filename
  mirah_ast = transformer.transform(parser_ast, nil)
  process_errors(transformer.errors)
  
  mirah_ast
end

#parse_file(filename) ⇒ Object



57
58
59
60
# File 'lib/mirah/parser.rb', line 57

def parse_file(filename)
  puts "  #{filename}" if logging
  parse_and_transform(filename, File.read(filename))
end

#parse_from_args(files_or_scripts) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mirah/parser.rb', line 33

def parse_from_args(files_or_scripts)
  nodes = []
  inline = false
  puts "Parsing..." if logging
  expand_files(files_or_scripts).each do |script|
    if script == '-e'
      inline = true
      next
    elsif inline
      nodes << parse_inline(script)
      break
    else
      nodes << parse_file(script)
    end
  end
  raise 'nothing to parse? ' + files_or_scripts.inspect unless nodes.length > 0
  nodes
end

#parse_inline(source) ⇒ Object



52
53
54
55
# File 'lib/mirah/parser.rb', line 52

def parse_inline(source)
  puts "  <inline script>" if logging
  parse_and_transform('DashE', source)
end