Module: Prism

Defined in:
lib/prism/dsl.rb,
lib/prism.rb,
lib/prism/ffi.rb,
lib/prism/node.rb,
lib/prism/pack.rb,
lib/prism/debug.rb,
lib/prism/pattern.rb,
lib/prism/visitor.rb,
lib/prism/compiler.rb,
lib/prism/node_ext.rb,
lib/prism/serialize.rb,
lib/prism/dispatcher.rb,
lib/prism/lex_compat.rb,
lib/prism/parse_result.rb,
lib/prism/ripper_compat.rb,
lib/prism/node_inspector.rb,
lib/prism/desugar_compiler.rb,
lib/prism/mutation_compiler.rb,
lib/prism/parse_result/comments.rb,
lib/prism/parse_result/newlines.rb,
ext/prism/api_pack.c,
ext/prism/extconf.rb,
ext/prism/extension.c

Overview

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

Defined Under Namespace

Modules: CallNodeFlags, DSL, ExtConf, IntegerBaseFlags, LoopFlags, Pack, RangeFlags, RegularExpressionFlags, Serialize, StringFlags Classes: AliasGlobalVariableNode, AliasMethodNode, AlternationPatternNode, AndNode, ArgumentsNode, ArrayNode, ArrayPatternNode, AssocNode, AssocSplatNode, BackReferenceReadNode, BasicVisitor, BeginNode, BlockArgumentNode, BlockLocalVariableNode, BlockNode, BlockParameterNode, BlockParametersNode, BreakNode, CallAndWriteNode, CallNode, CallOperatorWriteNode, CallOrWriteNode, CapturePatternNode, CaseNode, ClassNode, ClassVariableAndWriteNode, ClassVariableOperatorWriteNode, ClassVariableOrWriteNode, ClassVariableReadNode, ClassVariableTargetNode, ClassVariableWriteNode, Comment, Compiler, ConstantAndWriteNode, ConstantOperatorWriteNode, ConstantOrWriteNode, ConstantPathAndWriteNode, ConstantPathNode, ConstantPathOperatorWriteNode, ConstantPathOrWriteNode, ConstantPathTargetNode, ConstantPathWriteNode, ConstantReadNode, ConstantTargetNode, ConstantWriteNode, DefNode, DefinedNode, DesugarCompiler, Dispatcher, ElseNode, EmbeddedStatementsNode, EmbeddedVariableNode, EnsureNode, FalseNode, FindPatternNode, FlipFlopNode, FloatNode, ForNode, ForwardingArgumentsNode, ForwardingParameterNode, ForwardingSuperNode, GlobalVariableAndWriteNode, GlobalVariableOperatorWriteNode, GlobalVariableOrWriteNode, GlobalVariableReadNode, GlobalVariableTargetNode, GlobalVariableWriteNode, HashNode, HashPatternNode, IfNode, ImaginaryNode, ImplicitNode, InNode, InstanceVariableAndWriteNode, InstanceVariableOperatorWriteNode, InstanceVariableOrWriteNode, InstanceVariableReadNode, InstanceVariableTargetNode, InstanceVariableWriteNode, IntegerNode, InterpolatedMatchLastLineNode, InterpolatedRegularExpressionNode, InterpolatedStringNode, InterpolatedSymbolNode, InterpolatedXStringNode, KeywordHashNode, KeywordParameterNode, KeywordRestParameterNode, LambdaNode, LocalVariableAndWriteNode, LocalVariableOperatorWriteNode, LocalVariableOrWriteNode, LocalVariableReadNode, LocalVariableTargetNode, LocalVariableWriteNode, Location, MatchLastLineNode, MatchPredicateNode, MatchRequiredNode, MatchWriteNode, MissingNode, ModuleNode, MultiTargetNode, MultiWriteNode, MutationCompiler, NextNode, NilNode, NoKeywordsParameterNode, Node, NodeInspector, NumberedReferenceReadNode, OptionalParameterNode, OrNode, ParametersNode, ParenthesesNode, ParseError, ParseResult, ParseWarning, Pattern, 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 prism.h.

rb_str_new2(EXPECTED_PRISM_VERSION)

Class Method Summary collapse

Class Method Details

.dump(*args) ⇒ Object

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



187
188
189
# File 'lib/prism/ffi.rb', line 187

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

.dump_file(filepath) ⇒ Object

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



192
193
194
195
196
# File 'lib/prism/ffi.rb', line 192

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

.lex(*args) ⇒ Object

Return an array of tokens corresponding to the given string.



199
200
201
202
203
204
# File 'lib/prism/ffi.rb', line 199

def self.lex(code, filepath = nil)
  LibRubyParser::PrismBuffer.with do |buffer|
    LibRubyParser.pm_lex_serialize(code, code.bytesize, filepath, buffer.pointer)
    Serialize.load_tokens(Source.new(code), buffer.read)
  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.



33
34
35
# File 'lib/prism.rb', line 33

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.



207
208
209
210
211
# File 'lib/prism/ffi.rb', line 207

def self.lex_file(filepath)
  LibRubyParser::PrismString.with(filepath) do |string|
    lex(string.read, filepath)
  end
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.



40
41
42
# File 'lib/prism.rb', line 40

def self.lex_ripper(source)
  LexRipper.new(source).result
end

.load(source, serialized) ⇒ Object

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



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

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

.parse(*args) ⇒ Object

Parse the given string and return a ParseResult instance.



214
215
216
# File 'lib/prism/ffi.rb', line 214

def self.parse(code, filepath = nil)
  Prism.load(code, dump(code, filepath))
end

.parse_file(filepath) ⇒ Object

Parse the given file and return a ParseResult instance.



221
222
223
224
225
# File 'lib/prism/ffi.rb', line 221

def self.parse_file(filepath)
  LibRubyParser::PrismString.with(filepath) do |string|
    parse(string.read, filepath)
  end
end

.parse_lex(*args) ⇒ Object

Parse the given string and return a ParseResult instance.



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/prism/ffi.rb', line 228

def self.parse_lex(code, filepath = nil)
  LibRubyParser::PrismBuffer.with do |buffer|
     = [filepath.bytesize, filepath.b, 0].pack("LA*L") if filepath
    LibRubyParser.pm_parse_lex_serialize(code, code.bytesize, buffer.pointer, )

    source = Source.new(code)
    loader = Serialize::Loader.new(source, buffer.read)

    tokens = loader.load_tokens
    node, comments, errors, warnings = loader.load_nodes

    tokens.each { |token,| token.value.force_encoding(loader.encoding) }

    ParseResult.new([node, tokens], comments, errors, warnings, source)
  end
end

.parse_lex_file(filepath) ⇒ Object

Parse and lex the given file and return a ParseResult instance.



246
247
248
249
250
# File 'lib/prism/ffi.rb', line 246

def self.parse_lex_file(filepath)
  LibRubyParser::PrismString.with(filepath) do |string|
    parse_lex(string.read, filepath)
  end
end