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/version.rb,
lib/syntax_tree/visitor.rb,
lib/syntax_tree/formatter.rb,
lib/syntax_tree/language_server.rb,
lib/syntax_tree/visitor/json_visitor.rb,
lib/syntax_tree/visitor/field_visitor.rb,
lib/syntax_tree/visitor/match_visitor.rb,
lib/syntax_tree/formatter/single_quotes.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: AssignFormatting, CLI, ContainsAssignment, HashKeyFormatter, Parentheses, Quotes, Ternaryable Classes: ARef, ARefField, Alias, ArgBlock, ArgParen, ArgStar, Args, ArgsForward, ArrayLiteral, AryPtn, Assign, Assoc, AssocSplat, BEGINBlock, Backref, Backtick, BareAssocHash, Begin, Binary, BlockArg, BlockFormatter, BlockVar, BodyStmt, BraceBlock, Break, CHAR, CVar, Call, CallChainFormatter, CallOperatorFormatter, Case, ClassDeclaration, Comma, Command, CommandCall, Comment, ConditionalFormatter, ConditionalModFormatter, Const, ConstPathField, ConstPathRef, ConstRef, Def, DefEndless, Defined, Defs, DoBlock, Dot2, Dot3, DotFormatter, DynaSymbol, ENDBlock, Else, Elsif, EmbDoc, EmbExprBeg, EmbExprEnd, EmbVar, EndContent, Ensure, ExcessedComma, FCall, Field, FloatLiteral, FlowControlFormatter, FndPtn, For, Formatter, GVar, HashLiteral, Heredoc, HeredocBeg, HshPtn, IVar, Ident, If, IfMod, IfOp, Imaginary, In, Int, Kw, KwRestParam, LBrace, LBracket, LParen, Label, LabelEnd, Lambda, LanguageServer, Location, LoopFormatter, MAssign, MLHS, MLHSParen, MRHS, MethodAddBlock, ModuleDeclaration, Next, Node, Not, Op, OpAssign, Params, Paren, Parser, Period, PinnedBegin, PinnedVarRef, Program, QSymbols, QSymbolsBeg, QWords, QWordsBeg, RAssign, RBrace, RBracket, RParen, RationalLiteral, Redo, RegexpBeg, RegexpContent, RegexpEnd, RegexpLiteral, Rescue, RescueEx, RescueMod, RestParam, Retry, Return, Return0, SClass, Statements, StringConcat, StringContent, StringDVar, StringEmbExpr, StringLiteral, Super, SymBeg, SymbolContent, SymbolLiteral, Symbols, SymbolsBeg, TLamBeg, TLambda, TStringBeg, TStringContent, TStringEnd, TopConstField, TopConstRef, Unary, Undef, Unless, UnlessMod, Until, UntilMod, VCall, VarAlias, VarField, VarRef, Visitor, VoidStmt, When, While, WhileMod, Word, Words, WordsBeg, XString, XStringLiteral, Yield, Yield0, 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.

{}
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 =
"2.5.0"

Class Method Summary collapse

Class Method Details

.format(source, maxwidth = 80) ⇒ Object

Parses the given source and returns the formatted source.



43
44
45
46
47
48
49
# File 'lib/syntax_tree.rb', line 43

def self.format(source, maxwidth = 80)
  formatter = Formatter.new(source, [], maxwidth)
  parse(source).format(formatter)

  formatter.flush
  formatter.output.join
end

.parse(source) ⇒ Object

Parses the given source and returns the syntax tree.



36
37
38
39
40
# File 'lib/syntax_tree.rb', line 36

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.



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/syntax_tree.rb', line 53

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.



31
32
33
# File 'lib/syntax_tree.rb', line 31

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