Module: YARP

Defined in:
lib/yarp/node.rb,
lib/yarp.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, RangeNodeFlags, 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, 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

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.



78
79
80
81
82
83
84
85
86
87
# File 'ext/yarp/extension.c', line 78

static VALUE
dump(int argc, VALUE *argv, VALUE self) {
    VALUE string;
    VALUE filepath;
    rb_scan_args(argc, argv, "11", &string, &filepath);

    yp_string_t input;
    input_load_string(&input, string);
    return dump_input(&input, check_string(filepath));
}

.dump_file(filepath) ⇒ Object

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



90
91
92
93
94
95
96
97
98
99
100
101
# File 'ext/yarp/extension.c', line 90

static VALUE
dump_file(VALUE self, VALUE filepath) {
    yp_string_t input;

    const char *checked = check_string(filepath);
    if (!yp_string_mapped_init(&input, checked)) return Qnil;

    VALUE value = dump_input(&input, checked);
    yp_string_free(&input);

    return value;
}

.lex(*args) ⇒ Object

Return an array of tokens corresponding to the given string.



276
277
278
279
280
281
282
283
284
285
# File 'ext/yarp/extension.c', line 276

static VALUE
lex(int argc, VALUE *argv, VALUE self) {
    VALUE string;
    VALUE filepath;
    rb_scan_args(argc, argv, "11", &string, &filepath);

    yp_string_t input;
    input_load_string(&input, string);
    return lex_input(&input, check_string(filepath));
}

.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.



721
722
723
# File 'lib/yarp/lex_compat.rb', line 721

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.



288
289
290
291
292
293
294
295
296
297
298
299
# File 'ext/yarp/extension.c', line 288

static VALUE
lex_file(VALUE self, VALUE filepath) {
    yp_string_t input;

    const char *checked = check_string(filepath);
    if (!yp_string_mapped_init(&input, checked)) return Qnil;

    VALUE value = lex_input(&input, checked);
    yp_string_free(&input);

    return value;
}

.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.



728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
# File 'lib/yarp/lex_compat.rb', line 728

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.



302
303
304
# File 'lib/yarp.rb', line 302

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

.parse(*args) ⇒ Object

Parse the given string and return a ParseResult instance.



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'ext/yarp/extension.c', line 332

static VALUE
parse(int argc, VALUE *argv, VALUE self) {
    VALUE string;
    VALUE filepath;
    rb_scan_args(argc, argv, "11", &string, &filepath);

    yp_string_t input;
    input_load_string(&input, string);

#ifdef YARP_DEBUG_MODE_BUILD
    size_t length = yp_string_length(&input);
    char* dup = malloc(length);
    memcpy(dup, yp_string_source(&input), length);
    yp_string_constant_init(&input, dup, length);
#endif

    VALUE value = parse_input(&input, check_string(filepath));

#ifdef YARP_DEBUG_MODE_BUILD
    free(dup);
#endif

    return value;
}

.parse_file(filepath) ⇒ Object

Parse the given file and return a ParseResult instance.



358
359
360
361
362
363
364
365
366
367
368
369
# File 'ext/yarp/extension.c', line 358

static VALUE
parse_file(VALUE self, VALUE filepath) {
    yp_string_t input;

    const char *checked = check_string(filepath);
    if (!yp_string_mapped_init(&input, checked)) return Qnil;

    VALUE value = parse_input(&input, checked);
    yp_string_free(&input);

    return value;
}