Module: SyntaxTree

Defined in:
lib/syntax_tree.rb,
lib/syntax_tree/cli.rb,
lib/syntax_tree/node.rb,
lib/syntax_tree/parser.rb,
lib/syntax_tree/search.rb,
lib/syntax_tree/pattern.rb,
lib/syntax_tree/version.rb,
lib/syntax_tree/visitor.rb,
lib/syntax_tree/formatter.rb,
lib/syntax_tree/rake/task.rb,
lib/syntax_tree/basic_visitor.rb,
lib/syntax_tree/language_server.rb,
lib/syntax_tree/rake/check_task.rb,
lib/syntax_tree/rake/write_task.rb,
lib/syntax_tree/visitor/environment.rb,
lib/syntax_tree/plugin/single_quotes.rb,
lib/syntax_tree/visitor/json_visitor.rb,
lib/syntax_tree/plugin/trailing_comma.rb,
lib/syntax_tree/visitor/field_visitor.rb,
lib/syntax_tree/visitor/match_visitor.rb,
lib/syntax_tree/visitor/mutation_visitor.rb,
lib/syntax_tree/visitor/with_environment.rb,
lib/syntax_tree/language_server/inlay_hints.rb,
lib/syntax_tree/visitor/pretty_print_visitor.rb

Overview

Syntax Tree is a suite of tools built on top of the internal CRuby parser. It provides the ability to generate a syntax tree from source, as well as the tools necessary to inspect and manipulate that syntax tree. It can be used to build formatters, linters, language servers, and more.

Defined Under Namespace

Modules: ArrayMatch, AssignFormatting, CLI, ContainsAssignment, HashKeyFormatter, Parentheses, Quotes, Rake, Ternaryable, WithEnvironment Classes: ARef, ARefField, AliasNode, ArgBlock, ArgParen, ArgStar, Args, ArgsForward, ArrayLiteral, AryPtn, Assign, Assoc, AssocSplat, BEGINBlock, Backref, Backtick, BareAssocHash, BasicVisitor, Begin, Binary, BlockArg, BlockNode, BlockVar, BodyStmt, Break, CHAR, CVar, CallChainFormatter, CallNode, CallOperatorFormatter, Case, ClassDeclaration, Comma, Command, CommandCall, Comment, ConditionalFormatter, Const, ConstPathField, ConstPathRef, ConstRef, DefNode, Defined, DynaSymbol, ENDBlock, Else, Elsif, EmbDoc, EmbExprBeg, EmbExprEnd, EmbVar, EndContent, Ensure, Environment, ExcessedComma, Field, FloatLiteral, FlowControlFormatter, FndPtn, For, Formatter, GVar, HashLiteral, Heredoc, HeredocBeg, HeredocEnd, HshPtn, IVar, Ident, IfNode, IfOp, Imaginary, In, Int, Kw, KwRestParam, LBrace, LBracket, LParen, Label, LabelEnd, Lambda, LambdaVar, LanguageServer, Location, LoopFormatter, MAssign, MLHS, MLHSParen, MRHS, MethodAddBlock, ModuleDeclaration, Next, Node, Not, Op, OpAssign, Params, Paren, Parser, Pattern, Period, PinnedBegin, PinnedVarRef, Program, QSymbols, QSymbolsBeg, QWords, QWordsBeg, RAssign, RBrace, RBracket, RParen, RangeNode, RationalLiteral, Redo, RegexpBeg, RegexpContent, RegexpEnd, RegexpLiteral, Rescue, RescueEx, RescueMod, RestParam, Retry, ReturnNode, SClass, Search, Statements, StringConcat, StringContent, StringDVar, StringEmbExpr, StringLiteral, Super, SymBeg, SymbolContent, SymbolLiteral, Symbols, SymbolsBeg, TLamBeg, TLambda, TStringBeg, TStringContent, TStringEnd, TopConstField, TopConstRef, Unary, Undef, UnlessNode, UntilNode, VCall, VarField, VarRef, Visitor, VoidStmt, When, WhileNode, Word, Words, WordsBeg, XString, XStringLiteral, YieldNode, ZSuper

Constant Summary collapse

HANDLERS =

This holds references to objects that respond to both #parse and #format so that we can use them in the CLI.

{}
DEFAULT_PRINT_WIDTH =

This is the default print width when formatting. It can be overridden in the CLI by passing the –print-width option or here in the API by passing the optional second argument to ::format.

80
PATTERNS =

The list of nodes that represent patterns inside of pattern matching so that when a pattern is being printed it knows if it’s nested.

[AryPtn, Binary, FndPtn, HshPtn, RAssign].freeze
VERSION =
"5.0.0"

Class Method Summary collapse

Class Method Details

.format(source, maxwidth = DEFAULT_PRINT_WIDTH, options: Formatter::Options.new) ⇒ Object

Parses the given source and returns the formatted source.



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/syntax_tree.rb', line 57

def self.format(
  source,
  maxwidth = DEFAULT_PRINT_WIDTH,
  options: Formatter::Options.new
)
  formatter = Formatter.new(source, [], maxwidth, options: options)
  parse(source).format(formatter)

  formatter.flush
  formatter.output.join
end

.mutation {|visitor| ... } ⇒ Object

A convenience method for creating a new mutation visitor.

Yields:

  • (visitor)


70
71
72
73
74
# File 'lib/syntax_tree.rb', line 70

def self.mutation
  visitor = Visitor::MutationVisitor.new
  yield visitor
  visitor
end

.parse(source) ⇒ Object

Parses the given source and returns the syntax tree.



50
51
52
53
54
# File 'lib/syntax_tree.rb', line 50

def self.parse(source)
  parser = Parser.new(source)
  response = parser.parse
  response unless parser.error?
end

.read(filepath) ⇒ Object

Returns the source from the given filepath taking into account any potential magic encoding comments.



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/syntax_tree.rb', line 78

def self.read(filepath)
  encoding =
    File.open(filepath, "r") do |file|
      break Encoding.default_external if file.eof?

      header = file.readline
      header += file.readline if !file.eof? && header.start_with?("#!")
      Ripper.new(header).tap(&:parse).encoding
    end

  File.read(filepath, encoding: encoding)
end

.register_handler(extension, handler) ⇒ Object

This is a hook provided so that plugins can register themselves as the handler for a particular file type.



45
46
47
# File 'lib/syntax_tree.rb', line 45

def self.register_handler(extension, handler)
  HANDLERS[extension] = handler
end

.search(source, query, &block) ⇒ Object

Searches through the given source using the given pattern and yields each node in the tree that matches the pattern to the given block.



93
94
95
# File 'lib/syntax_tree.rb', line 93

def self.search(source, query, &block)
  Search.new(Pattern.new(query).compile).scan(parse(source), &block)
end