Class: MotionBlender::Analyzer::Parser

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/motion_blender/analyzer/parser.rb

Constant Summary collapse

REQUIREMENT_TOKENS =
i(motion_require require_relative require)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Parser

Returns a new instance of Parser.



17
18
19
20
# File 'lib/motion_blender/analyzer/parser.rb', line 17

def initialize file
  @file = file.to_s
  @requires = []
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



15
16
17
# File 'lib/motion_blender/analyzer/parser.rb', line 15

def file
  @file
end

#last_traceObject (readonly)

Returns the value of attribute last_trace.



15
16
17
# File 'lib/motion_blender/analyzer/parser.rb', line 15

def last_trace
  @last_trace
end

#requiresObject (readonly)

Returns the value of attribute requires.



15
16
17
# File 'lib/motion_blender/analyzer/parser.rb', line 15

def requires
  @requires
end

Instance Method Details

#evaluate(ast, stack) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/motion_blender/analyzer/parser.rb', line 37

def evaluate ast, stack
  @last_trace = trace_for ast
  Evaluator.new(@file, ast, stack).parse_args.each do |req|
    req.trace = @last_trace
    @requires << req
  end
end

#parseObject



22
23
24
25
# File 'lib/motion_blender/analyzer/parser.rb', line 22

def parse
  ast = ::Parser::CurrentRuby.parse(File.read(@file))
  traverse ast if ast
end

#raketime_block?(ast) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/motion_blender/analyzer/parser.rb', line 49

def raketime_block? ast
  (ast.type == :block) &&
    (ast.children.first.loc.expression.source == 'MotionBlender.raketime')
end

#require_command?(ast) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/motion_blender/analyzer/parser.rb', line 45

def require_command? ast
  (ast.type == :send) && REQUIREMENT_TOKENS.include?(ast.children[1])
end

#trace_for(ast) ⇒ Object



54
55
56
# File 'lib/motion_blender/analyzer/parser.rb', line 54

def trace_for ast
  "#{@file}:#{ast.loc.line}:in `#{ast.children[1]}'"
end

#traverse(ast, stack = []) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/motion_blender/analyzer/parser.rb', line 27

def traverse ast, stack = []
  if require_command?(ast)
    evaluate ast, stack
  elsif !raketime_block?(ast)
    ast.children
      .select { |node| node.is_a?(::Parser::AST::Node) }
      .each { |node| traverse node, [*stack, ast] }
  end
end