Class: Mirah::Parser

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

Defined Under Namespace

Classes: AstPrinter

Constant Summary

Constants included from Logging::Logged

Logging::Logged::VLEVELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging::Logged

#error, #info, #log, #logger, #logger_name, #logging?, #vlog, #warning

Methods included from Util::ProcessErrors

#process_errors, #process_inference_errors

Constructor Details

#initialize(state, typer, logging) ⇒ Parser

Returns a new instance of Parser.



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

def initialize(state, typer, logging)
  @transformer = Mirah::Transform::Transformer.new(state, typer)
  @logging = logging
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



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

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

#format_ast(ast) ⇒ Object



70
71
72
# File 'lib/mirah/parser.rb', line 70

def format_ast(ast)
  AstPrinter.new.scan(ast, ast)
end

#parse_and_transform(filename, src) ⇒ Object



66
67
68
# File 'lib/mirah/parser.rb', line 66

def parse_and_transform(filename, src)
  Mirah::AST.parse_ruby(transformer, src, filename)
end

#parse_file(filename) ⇒ Object



61
62
63
64
# File 'lib/mirah/parser.rb', line 61

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
51
52
53
54
# 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 = nodes.compact
  if self.logging
    nodes.each {|node| log "Parsed:\n #{format_ast(node)}"}
  end
  nodes
end

#parse_inline(source) ⇒ Object



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

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