Class: Mutineer::Parser
- Inherits:
-
Object
- Object
- Mutineer::Parser
- 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--daemonchecks mutants on several threads. Mutex.new
Class Method Summary collapse
-
.comments(source) ⇒ Array<Prism::Comment>
The comments of source text, without building its syntax tree: a few times cheaper than Parser.parse_string for a caller that needs only these.
-
.compiles?(source) ⇒ Boolean
private
Whether the running Ruby compiles
source, without running it. -
.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. -
.parse_file(path) ⇒ Prism::ParseResult
Parses a file with Prism.
-
.parse_string(source) ⇒ Prism::ParseResult
Parses source text with Prism.
-
.read(path) ⇒ String
Reads a Ruby source file in the encoding its magic comment names, or UTF-8 when it names none (#346).
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.
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).
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.
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.
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. end |
.parse_string(source) ⇒ Prism::ParseResult
Parses source text with Prism.
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).
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 |