-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2378 from ruby/cli
- Loading branch information
Showing
11 changed files
with
311 additions
and
322 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,3 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
#!/bin/sh | ||
|
||
# Usage: | ||
# bin/lex # defaults to test.rb | ||
# bin/lex <filename> | ||
# bin/lex -e "<source-code>" | ||
|
||
$:.unshift(File.expand_path("../lib", __dir__)) | ||
require "ripper" | ||
require "prism" | ||
|
||
if ARGV[0] == "-e" | ||
source = ARGV[1] | ||
else | ||
filepath = ARGV.first || "test.rb" | ||
source = File.read(filepath) | ||
end | ||
|
||
pattern = "%-70s %-70s %-70s" | ||
|
||
ripper = | ||
begin | ||
Prism.lex_ripper(source) | ||
rescue ArgumentError, SyntaxError | ||
# If Ripper raises a syntax error, we want to continue as if it didn't | ||
# return any tokens at all. prism won't raise a syntax error, so it's nicer | ||
# to still be able to see the tokens that prism generated. | ||
[] | ||
end | ||
|
||
prism = Prism.lex_compat(source, filepath: filepath) | ||
prism_new = Prism.lex(source, filepath: filepath) | ||
if prism.errors.any? | ||
puts "Errors lexing:" | ||
prism.errors.map do |error| | ||
print "- [#{error.location.start_line},#{error.location.start_column}-" | ||
print "#{error.location.end_line},#{error.location.end_column}] " | ||
puts "\e[1;31m#{error.message}\e[0m" | ||
end | ||
puts "\n" | ||
end | ||
|
||
puts pattern % ["Ripper lex", "Prism compat lex", "Prism Lex"] | ||
puts pattern % ["-" * 70, "-" * 70, "-" * 70] | ||
|
||
prism_value = prism.value | ||
prism_new_value = prism_new.value | ||
|
||
[prism_value.length, ripper.length, prism_new_value.length].max.times do |index| | ||
left = ripper[index] | ||
right = prism_value[index] | ||
new = prism_new_value[index] | ||
|
||
color = left == right ? "38;5;102" : "1;31" | ||
|
||
if ENV["VERBOSE"] || (left != right) | ||
new_value = [new[0].type, [new[0].location.start_offset, new[0].location.length]] if new | ||
puts "\033[#{color}m#{pattern}\033[0m" % [left.inspect, right.inspect, new_value.inspect] | ||
end | ||
end | ||
exec bin/prism lex "$@" |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,3 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
#!/bin/sh | ||
|
||
# Usage: | ||
# bin/parse # defaults to test.rb | ||
# bin/parse <filename> | ||
# bin/parse -e "<source-code>" | ||
|
||
$:.unshift(File.expand_path("../lib", __dir__)) | ||
require "prism" | ||
|
||
if ARGV[0] == "-e" | ||
result = Prism.parse(ARGV[1]) | ||
else | ||
result = Prism.parse_file(ARGV[0] || "test.rb") | ||
end | ||
|
||
result.mark_newlines! if ENV["MARK_NEWLINES"] | ||
|
||
value = result.value | ||
value = value.accept(Prism::DesugarCompiler.new) if ENV["DESUGAR"] | ||
|
||
parts = {} | ||
parts["Comments"] = result.comments if result.comments.any? | ||
parts["Magic comments"] = result.magic_comments if result.magic_comments.any? | ||
parts["Warnings"] = result.warnings if result.warnings.any? | ||
parts["Errors"] = result.errors if result.errors.any? | ||
parts["DATA"] = result.data_loc if result.data_loc | ||
|
||
if parts.empty? | ||
puts value.inspect | ||
else | ||
parts["AST"] = value | ||
parts.each_with_index do |(key, value), index| | ||
puts if index > 0 | ||
puts "#{key}:" | ||
pp value | ||
end | ||
end | ||
exec bin/prism parse "$@" |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.