Module: Maccro::DSL

Defined in:
lib/maccro/dsl.rb,
lib/maccro/dsl/node.rb,
lib/maccro/dsl/value.rb,
lib/maccro/dsl/assign.rb,
lib/maccro/dsl/literal.rb,
lib/maccro/dsl/expression.rb

Defined Under Namespace

Modules: ASTNodeWrapper Classes: AndAssignment, AndExp, AnyNode, Array, ArrayAssignmentWithOperator, Assignment, AttributeAssignment, AttributeAssignmentWithOperator, BackRefVariable, CallExp, CaseExp, ClassVariable, ClassVariableAssignment, Colon2Exp, Colon3Exp, Constant, ConstantDeclaration, ConstantDeclarationWithOperator, DRegexp, DString, DSymbol, DXString, DefineMethod, DefineSingletonMethod, DefinedExp, Dot2Exp, Dot3Exp, DynamicVariableAssignmentCurrentScope, DynamicVariableAssignmentOutOfScope, Expression, FString, FalseValue, Flip2Exp, Flip3Exp, FunctionCallExp, GlobalVariable, GlobalVariableAssignment, Hash, IfExp, InstanceVariable, InstanceVariableAssignment, Lambda, Literal, LocalVariable, LocalVariableAssignment, MatchExp, MultiAssignment, NilValue, Node, NthRefVariable, Number, OperatorCallExp, OrAssignment, OrExp, RegexpCompiledOnce, RegularExpression, SafeCallExp, Self, SpecialVariable, String, SuperExp, Symbol, TrueValue, UnlessExp, VCallExp, Value, Variable, XString, YieldExp

Class Method Summary collapse

Class Method Details

.ast_node_to_dsl_node(ast_node) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/maccro/dsl.rb', line 16

def self.ast_node_to_dsl_node(ast_node)
  unless ast_node.is_a?(RubyVM::AbstractSyntaxTree::Node)
    if ast_node.is_a?(Array)
      ast_node.times do |i|
        ast_node[i] = ast_node_to_dsl_node(ast_node[i])
      end
    end
    return ast_node
  end

  # ast_node.is_a?(RubyVM::AbstractSyntaxTree::Node)
  ast_node.extend ASTNodeWrapper
  if is_placeholder?(ast_node)
    return placeholder_to_matcher_node(ast_node)
  end

  ast_node.children.each_with_index do |n, i|
    ast_node.children[i] = ast_node_to_dsl_node(n)
  end
  ast_node
end

.is_placeholder?(node) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
# File 'lib/maccro/dsl.rb', line 38

def self.is_placeholder?(node)
  if node.type == :VCALL && placeholder_name?(node.children.first)
    true
  elsif node.type == :GVAR && node.children.first == :'$TARGET'
    true
  else
    false
  end
end

.matcher(code_snippet) ⇒ Object



10
11
12
13
14
# File 'lib/maccro/dsl.rb', line 10

def self.matcher(code_snippet)
  ast = CodeUtil.parse_to_ast(code_snippet)
  # Top level node should be SCOPE, and children[2] will be the first expression node
  return ast_node_to_dsl_node(ast.children[2])
end

.placeholder_name?(sym) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
# File 'lib/maccro/dsl.rb', line 48

def self.placeholder_name?(sym)
  # Expression: "eN"
  # Value: "vN"
  # String: "sN"
  # Symbol: "yN"
  # Number: "nN"
  # Regular expression: "rN"
  # N index is 1 origin
  (sym.to_s =~ /^[evsynr][1-9]\d*$/).!.!
end

.placeholder_to_matcher_node(placeholder_node) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/maccro/dsl.rb', line 59

def self.placeholder_to_matcher_node(placeholder_node)
  name = placeholder_node.children.first.to_s
  nodeClass = case name
              when '$TARGET' then AnyNode
              when /^s([1-9]\d*)$/ then String
              when /^y([1-9]\d*)$/ then Symbol
              when /^n([1-9]\d*)$/ then Number
              when /^r([1-9]\d*)$/ then RegularExpression
              when /^v([1-9]\d*)$/ then Value
              when /^e([1-9]\d*)$/ then Expression
              else
                raise "BUG: unregistered placeholder name `#{name}`"
              end
  nodeClass.new(name, placeholder_node.to_code_range)
end