Class: Mutineer::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/mutineer/parser.rb

Overview

Thin boundary around Prism.

The parse methods return a Prism::ParseResult so all callers use result.value, result.source.source (raw bytes), and result.errors uniformly. No wrapping struct.

Constant Summary collapse

COMPILE_LOCK =

Serializes compiles?: it swaps the process-wide $VERBOSE, and --daemon checks mutants on several threads.

Mutex.new

Class Method Summary collapse

Class Method Details

.comments(source) ⇒ Array<Prism::Comment>

The comments of source text, without building its syntax tree: a few times cheaper than parse_string for a caller that needs only these.

Parameters:

  • source (String) —

    source text.

Returns:

  • (Array<Prism::Comment>) —

    its comments, in source order.



94
95
96
# File 'lib/mutineer/parser.rb', line 94

def self.comments(source)
  Prism.parse_comments(source)
end

.compiles?(source) ⇒ Boolean (private)

Whether the running Ruby compiles source, without running it. Mutineer forks, so it runs on CRuby, which has RubyVM. Warnings are off for the compile: a mutant can trigger one (if a = 1).

Parameters:

  • source (String) —

    source text.

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mutineer/parser.rb', line 75

def self.compiles?(source)
  COMPILE_LOCK.synchronize do
    verbose = $VERBOSE
    $VERBOSE = nil
    RubyVM::InstructionSequence.compile(source)
    true
  rescue SyntaxError
    false
  ensure
    $VERBOSE = verbose
  end
end

.invalid?(source) ⇒ Boolean

True when Ruby cannot load source: the loaded Prism reports a parse error and the running Ruby cannot compile the text either. Prism alone is not enough. Prism 2 reports a bare-constant operator write inside a method (T += 1) as a dynamic constant assignment, while the Prism 1 that Ruby 3.4 and 4.0 load files with accepts it (#392). Compiling does not run the code.

Parameters:

  • source (String) —

    source text.

Returns:

  • (Boolean)


59
60
61
62
63
# File 'lib/mutineer/parser.rb', line 59

def self.invalid?(source)
  return false if parse_string(source).errors.empty? # the usual case costs one parse

  !compiles?(source)
end

.parse_file(path) ⇒ Prism::ParseResult

Parses a file with Prism.

Parameters:

  • path (String) —

    source file path.

Returns:

  • (Prism::ParseResult) —

    Prism parse result.

Raises:



23
24
25
26
27
# File 'lib/mutineer/parser.rb', line 23

def self.parse_file(path)
  Prism.parse_file(path)
rescue SystemCallError => e
  raise ParseError, e.message
end

.parse_string(source) ⇒ Prism::ParseResult

Parses source text with Prism.

Parameters:

  • source (String) —

    source text.

Returns:

  • (Prism::ParseResult) —

    Prism parse result.



46
47
48
# File 'lib/mutineer/parser.rb', line 46

def self.parse_string(source)
  Prism.parse(source)
end

.read(path) ⇒ String

Reads a Ruby source file in the encoding its magic comment names, or UTF-8 when it names none (#346). The bytes are not transcoded, so Prism's offsets still address them and a mutant written back stays valid for that encoding. An unknown encoding name stays UTF-8, as Ruby does. I/O errors propagate (Errno::ENOENT and the rest).

Parameters:

  • path (String) —

    source file path.

Returns:

  • (String) —

    source text, tagged with its encoding.



37
38
39
40
# File 'lib/mutineer/parser.rb', line 37

def self.read(path)
  bytes = File.binread(path)
  bytes.force_encoding(SourceBytes.encoding_for(bytes))
end