module Maccro::DSL
Public Class Methods
ast_node_to_dsl_node(ast_node)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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