"BEGIN", "CHAR" => "CHAR", "ENDBlock" => "END", "EndContent" => "__end__", "ArrayLiteral" => "array", "ARef" => "aref", "ARefField" => "aref_field", "AryPtn" => "aryptn", "BodyStmt" => "bodystmt", "RAssign" => "rassign", "ClassDeclaration" => "class", "CVar" => "cvar", "EmbDoc" => "embdoc", "EmbExprBeg" => "embexpr_beg", "EmbExprEnd" => "embexpr_end", "EmbVar" => "embvar", "FCall" => "fcall", "FloatLiteral" => "float", "FndPtn" => "fndptn", "GVar" => "gvar", "HashLiteral" => "hash", "HshPtn" => "hshptn", "IVar" => "ivar", "KwRestParam" => "kwrest_param", "LBrace" => "lbrace", "LBracket" => "lbracket", "LParen" => "lparen", "MAssign" => "massign", "MLHS" => "mlhs", "MLHSParen" => "mlhs_paren", "ModuleDeclaration" => "module", "MRHS" => "mrhs", "QSymbols" => "qsymbols", "QSymbolsBeg" => "qsymbols_beg", "QWords" => "qwords", "QWordsBeg" => "qwords_beg", "RationalLiteral" => "rational", "RBrace" => "rbrace", "RBracket" => "rbracket", "RParen" => "rparen", "SClass" => "sclass", "StringDVar" => "string_dvar", "StringEmbExpr" => "string_embexpr", "TLambda" => "tlambda", "TLamBeg" => "tlambeg", "TStringBeg" => "tstring_beg", "TStringContent" => "tstring_content", "TStringEnd" => "tstring_end", "VCall" => "vcall", "XString" => "xstring", "XStringLiteral" => "xstring_literal", "ZSuper" => "zsuper" } NAMES.default_proc = -> (hash, key) do value = key.dup value.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2') value.gsub!(/([a-z\d])([A-Z])/, '\1_\2') value.tr!("-", "_") value.downcase! hash[key] = value end filepath = File.expand_path("../lib/syntax_tree/node.rb", __dir__) source = SyntaxTree.read(filepath) ast = SyntaxTree.parse(source) ast => SyntaxTree::Program[statements: { body: [*, SyntaxTree::ModuleDeclaration[bodystmt: { statements: { body: classes } }]] }] classes.each do |node| next unless node in SyntaxTree::ClassDeclaration[superclass: { value: { value: "Node" } }] body = node.bodystmt.statements.body index = body.index { _1 in SyntaxTree::Def[name: { value: "child_nodes" }] } index ||= body.length - 1 body.insert(index, SyntaxTree.parse(<<~RUBY).statements.body.first) def accept(visitor) visitor.visit_#{NAMES[node.constant.constant.value]}(self) end RUBY body[index].location.instance_variable_set(:@start_line, node.location.end_line) end NAMES.sort.to_h.each do |original, change| puts <<~RUBY # Visit a#{original.match?(/^[AEIOU]/) ? "n" : ""} #{original} node. alias visit_#{change} visit_child_nodes RUBY end formatter = SyntaxTree::Formatter.new(source, []) ast.format(formatter) formatter.flush File.write(filepath, formatter.output.join)">
Visitors #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Visitors #29
Changes from all commits
15075db
2a914c9
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing
Uh oh!
There was an error while loading. Please reload this page.