Module: YARP

Defined in:
lib/yarp/node.rb,
lib/yarp.rb,
lib/yarp/ffi.rb,
lib/yarp/pack.rb,
lib/yarp/serialize.rb,
lib/yarp/lex_compat.rb,
lib/yarp/ripper_compat.rb,
ext/yarp/api_pack.c,
ext/yarp/extension.c

Overview

This file is generated by the bin/template script and should not be modified manually. See templates/lib/yarp/node.rb.erb if you are looking to modify the template

Defined Under Namespace

Modules: CallNodeFlags, DSL, LoopFlags, Pack, RangeFlags, RegularExpressionFlags, Serialize Classes: AliasNode, AlternationPatternNode, AndNode, ArgumentsNode, ArrayNode, ArrayPatternNode, AssocNode, AssocSplatNode, BackReferenceReadNode, BasicVisitor, BeginNode, BlockArgumentNode, BlockNode, BlockParameterNode, BlockParametersNode, BreakNode, CallNode, CallOperatorAndWriteNode, CallOperatorOrWriteNode, CallOperatorWriteNode, CapturePatternNode, CaseNode, ClassNode, ClassVariableOperatorAndWriteNode, ClassVariableOperatorOrWriteNode, ClassVariableOperatorWriteNode, ClassVariableReadNode, ClassVariableWriteNode, Comment, ConstantOperatorAndWriteNode, ConstantOperatorOrWriteNode, ConstantOperatorWriteNode, ConstantPathNode, ConstantPathOperatorAndWriteNode, ConstantPathOperatorOrWriteNode, ConstantPathOperatorWriteNode, ConstantPathWriteNode, ConstantReadNode, ConstantWriteNode, DefNode, DefinedNode, ElseNode, EmbeddedStatementsNode, EmbeddedVariableNode, EnsureNode, FalseNode, FindPatternNode, FlipFlopNode, FloatNode, ForNode, ForwardingArgumentsNode, ForwardingParameterNode, ForwardingSuperNode, GlobalVariableOperatorAndWriteNode, GlobalVariableOperatorOrWriteNode, GlobalVariableOperatorWriteNode, GlobalVariableReadNode, GlobalVariableWriteNode, HashNode, HashPatternNode, IfNode, ImaginaryNode, InNode, InstanceVariableOperatorAndWriteNode, InstanceVariableOperatorOrWriteNode, InstanceVariableOperatorWriteNode, InstanceVariableReadNode, InstanceVariableWriteNode, IntegerNode, InterpolatedRegularExpressionNode, InterpolatedStringNode, InterpolatedSymbolNode, InterpolatedXStringNode, KeywordHashNode, KeywordParameterNode, KeywordRestParameterNode, LambdaNode, LocalVariableOperatorAndWriteNode, LocalVariableOperatorOrWriteNode, LocalVariableOperatorWriteNode, LocalVariableReadNode, LocalVariableWriteNode, Location, MatchPredicateNode, MatchRequiredNode, MissingNode, ModuleNode, MultiWriteNode, NextNode, NilNode, NoKeywordsParameterNode, Node, NumberedReferenceReadNode, OptionalParameterNode, OrNode, ParametersNode, ParenthesesNode, ParseError, ParseResult, ParseWarning, PinnedExpressionNode, PinnedVariableNode, PostExecutionNode, PreExecutionNode, ProgramNode, RangeNode, RationalNode, RedoNode, RegularExpressionNode, RequiredDestructuredParameterNode, RequiredParameterNode, RescueModifierNode, RescueNode, RestParameterNode, RetryNode, ReturnNode, RipperCompat, SelfNode, SingletonClassNode, Source, SourceEncodingNode, SourceFileNode, SourceLineNode, SplatNode, StatementsNode, StringConcatNode, StringNode, SuperNode, SymbolNode, Token, TrueNode, UndefNode, UnlessNode, UntilNode, Visitor, WhenNode, WhileNode, XStringNode, YieldNode

Constant Summary collapse

BACKEND =
ID2SYM(rb_intern("CExtension"))
VERSION =

in yarp.h.

rb_str_new2(EXPECTED_YARP_VERSION)

Class Method Summary collapse

Class Method Details

.dump(*args) ⇒ Object

Dump the AST corresponding to the given string to a string.



174
175
176
# File 'lib/yarp/ffi.rb', line 174

def self.dump(code, filepath = nil)
  dump_internal(code, code.bytesize, filepath)
end

.dump_file(filepath) ⇒ Object

Dump the AST corresponding to the given file to a string.



179
180
181
182
183
# File 'lib/yarp/ffi.rb', line 179

def self.dump_file(filepath)
  LibRubyParser.with_string(filepath) do |string|
    dump_internal(string.source, string.length, filepath)
  end
end

.lex(*args) ⇒ Object

Return an array of tokens corresponding to the given string.



186
187
188
189
190
191
192
193
# File 'lib/yarp/ffi.rb', line 186

def self.lex(code, filepath = nil)
  LibRubyParser.with_buffer do |buffer|
    LibRubyParser.yp_lex_serialize(code, code.bytesize, filepath, buffer)

    source = Source.new(code)
    Serialize.load_tokens(source, buffer.to_ruby_string).with_source(source)
  end
end

.lex_compat(source, filepath = "") ⇒ Object

Returns an array of tokens that closely resembles that of the Ripper lexer. The only difference is that since we don’t keep track of lexer state in the same way, it’s going to always return the NONE state.



736
737
738
# File 'lib/yarp/lex_compat.rb', line 736

def self.lex_compat(source, filepath = "")
  LexCompat.new(source, filepath).result
end

.lex_file(filepath) ⇒ Object

Return an array of tokens corresponding to the given file.



196
197
198
# File 'lib/yarp/ffi.rb', line 196

def self.lex_file(filepath)
  LibRubyParser.with_string(filepath) { |string| lex(string.read, filepath) }
end

.lex_ripper(source) ⇒ Object

This lexes with the Ripper lex. It drops any space events but otherwise returns the same tokens. Raises SyntaxError if the syntax in source is invalid.



743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
# File 'lib/yarp/lex_compat.rb', line 743

def self.lex_ripper(source)
  previous = []
  results = []

  Ripper.lex(source, raise_errors: true).each do |token|
    case token[1]
    when :on_sp
      # skip
    when :on_tstring_content
      if previous[1] == :on_tstring_content && (token[2].start_with?("\#$") || token[2].start_with?("\#@"))
        previous[2] << token[2]
      else
        results << token
        previous = token
      end
    when :on_words_sep
      if previous[1] == :on_words_sep
        previous[2] << token[2]
      else
        results << token
        previous = token
      end
    else
      results << token
      previous = token
    end
  end

  results
end

.load(source, serialized) ⇒ Object

Load the serialized AST using the source as a reference into a tree.



323
324
325
# File 'lib/yarp.rb', line 323

def self.load(source, serialized)
  Serialize.load(source, serialized)
end

.parse(*args) ⇒ Object

Parse the given string and return a ParseResult instance.



201
202
203
# File 'lib/yarp/ffi.rb', line 201

def self.parse(code, filepath = nil)
  YARP.load(code, dump(code, filepath)).with_source(Source.new(code))
end

.parse_file(filepath) ⇒ Object

Parse the given file and return a ParseResult instance.



208
209
210
# File 'lib/yarp/ffi.rb', line 208

def self.parse_file(filepath)
  LibRubyParser.with_string(filepath) { |string| parse(string.read, filepath) }
end