Sha256: bb9b03d585cc646ae47d5f2fa5e21e035069f8f35ec1bd393a3645231eab9d10

Contents?: true

Size: 1.87 KB

Versions: 6

Compression:

Stored size: 1.87 KB

Contents

#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "parser/current"

$:.unshift(File.expand_path("../lib", __dir__))
require "syntax_tree"

# First, opt in to every AST feature.
Parser::Builders::Default.modernize

# Modify the source map == check so that it doesn't check against the node
# itself so we don't get into a recursive loop.
Parser::Source::Map.prepend(
  Module.new {
    def ==(other)
      self.class == other.class &&
        (instance_variables - %i[@node]).map do |ivar|
          instance_variable_get(ivar) == other.instance_variable_get(ivar)
        end.reduce(:&)
    end
  }
)

# Next, ensure that we're comparing the nodes and also comparing the source
# ranges so that we're getting all of the necessary information.
Parser::AST::Node.prepend(
  Module.new {
    def ==(other)
      super && (location == other.location)
    end
  }
)

source = ARGF.read

parser = Parser::CurrentRuby.new
parser.diagnostics.all_errors_are_fatal = true

buffer = Parser::Source::Buffer.new("(string)", 1)
buffer.source = source.dup.force_encoding(parser.default_encoding)

stree = SyntaxTree::Translation.to_parser(SyntaxTree.parse(source), buffer)
ptree = parser.parse(buffer)

if stree == ptree
  puts "Syntax trees are equivalent."
elsif stree.inspect == ptree.inspect
  warn "Syntax tree locations are different."

  queue = [[stree, ptree]]
  while (left, right = queue.shift)
    if left.location != right.location
      warn "Different node:"
      pp left

      warn "Different location:"

      warn "Syntax Tree:"
      pp left.location

      warn "whitequark/parser:"
      pp right.location

      exit
    end

    left.children.zip(right.children).each do |left_child, right_child|
      queue << [left_child, right_child] if left_child.is_a?(Parser::AST::Node)
    end
  end
else
  warn "Syntax Tree:"
  pp stree

  warn "whitequark/parser:"
  pp ptree
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
syntax_tree-6.2.0 bin/whitequark
syntax_tree-6.1.1 bin/whitequark
syntax_tree-6.1.0 bin/whitequark
syntax_tree-6.0.2 bin/whitequark
syntax_tree-6.0.1 bin/whitequark
syntax_tree-6.0.0 bin/whitequark