Skip to content

Commit

Permalink
Revert "Remove regexp body mutation support"
Browse files Browse the repository at this point in the history
This reverts commit 21d3fef.

This was not a clean revert. Note that:
- The version of `regexp_parser` was 1.3.0, now it is 1.8.2 to accomodate our current `rubocop` version and because there were some relevant bugfixes implemented between 1.3.x and 1.8.x. We should eventually move to 2.0 but it is currently incompatible with this integration. There are some issues with the frozen Regexp classes getting mutated so we may have to open an issue.
- Since "expected exception" support was removed from the specs, I have had to exclude two files entirely. This seems unfortunate as it reduces our overall coverage.
- Since unsupported nodes are no longer explicitly tracked, I removed the code that used to handle that for regular expressions. See: #1021
- I had to change the example case for where we are more permissive than `regexp_parser` because `regexp_parser` has decided to become more permissive and try to match Ruby's semantics. It was actually very hard to find a case that failed--I brute-forced 50 million regexp strings that had perfect parity of being accepted and then stumbled onto the single hex escape case by accident. See: ammar/regexp_parser#75
- Changed an access pattern for regexp mutations which became equivalent based on this: https://github.com/ammar/regexp_parser/blame/4ca7cec03b210e3e00473b7b1a7308f963190c1e/lib/regexp_parser/expression/subexpression.rb#L30-L33
- Some other minor conflicts and small spec assertion changes were resolved as well.
  • Loading branch information
dgollahon committed Dec 20, 2020
1 parent fc8c82c commit 9c7fc4a
Show file tree
Hide file tree
Showing 43 changed files with 2,132 additions and 14 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Unreleased

* Reintroduce regexp mutation support [#TODO]

# v0.10.20 2020-12-16

[#1159](https://github.com/mbj/mutant/pull/1159)
Expand Down
3 changes: 2 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ PATH
mprelude (~> 0.1.0)
parser (~> 2.7.1)
procto (~> 0.0.2)
regexp_parser (~> 1.8)
unparser (~> 0.5.4)
variable (~> 0.0.1)

Expand Down Expand Up @@ -51,7 +52,7 @@ GEM
ast (~> 2.4.1)
procto (0.0.3)
rainbow (3.0.0)
regexp_parser (2.0.0)
regexp_parser (1.8.2)
rexml (3.2.4)
rspec (3.10.0)
rspec-core (~> 3.10.0)
Expand Down
17 changes: 17 additions & 0 deletions lib/mutant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
require 'parser'
require 'parser/current'
require 'pathname'
require 'regexp_parser'
require 'set'
require 'singleton'
require 'stringio'
Expand Down Expand Up @@ -52,6 +53,15 @@ module Mutant
require 'mutant/ast/named_children'
require 'mutant/ast/node_predicates'
require 'mutant/ast/find_metaclass_containing'
require 'mutant/ast/regexp'
require 'mutant/ast/regexp/transformer'
require 'mutant/ast/regexp/transformer/direct'
require 'mutant/ast/regexp/transformer/named_group'
require 'mutant/ast/regexp/transformer/options_group'
require 'mutant/ast/regexp/transformer/quantifier'
require 'mutant/ast/regexp/transformer/recursive'
require 'mutant/ast/regexp/transformer/root'
require 'mutant/ast/regexp/transformer/text'
require 'mutant/ast/meta'
require 'mutant/ast/meta/send'
require 'mutant/ast/meta/const'
Expand All @@ -74,6 +84,13 @@ module Mutant
require 'mutant/mutator/util/symbol'
require 'mutant/mutator/node'
require 'mutant/mutator/node/generic'
require 'mutant/mutator/node/regexp'
require 'mutant/mutator/node/regexp/alternation_meta'
require 'mutant/mutator/node/regexp/capture_group'
require 'mutant/mutator/node/regexp/character_type'
require 'mutant/mutator/node/regexp/end_of_line_anchor'
require 'mutant/mutator/node/regexp/end_of_string_or_before_end_of_line_anchor'
require 'mutant/mutator/node/regexp/greedy_zero_or_more'
require 'mutant/mutator/node/literal'
require 'mutant/mutator/node/literal/boolean'
require 'mutant/mutator/node/literal/range'
Expand Down
42 changes: 42 additions & 0 deletions lib/mutant/ast/regexp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

module Mutant
module AST
# Regexp source mapper
module Regexp
# Parse regex string into expression
#
# @param regexp [String]
#
# @return [Regexp::Expression, nil]
#
# rubocop:disable Lint/SuppressedException
def self.parse(regexp)
::Regexp::Parser.parse(regexp)
# regexp_parser is more strict than MRI
rescue ::Regexp::Scanner::PrematureEndError
end
# rubocop:enable Lint/SuppressedException

# Convert expression into ast node
#
# @param expression [Regexp::Expression]
#
# @return [Parser::AST::Node]
def self.to_ast(expression)
ast_type = :"regexp_#{expression.token}_#{expression.type}"

Transformer.lookup(ast_type).to_ast(expression)
end

# Convert node into expression
#
# @param node [Parser::AST::Node]
#
# @return [Regexp::Expression]
def self.to_expression(node)
Transformer.lookup(node.type).to_expression(node)
end
end # Regexp
end # AST
end # Mutant
187 changes: 187 additions & 0 deletions lib/mutant/ast/regexp/transformer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
# frozen_string_literal: true

module Mutant
module AST
module Regexp
# Regexp bijective mapper
#
# Transforms parsed regular expression representation from
# `Regexp::Expression` instances (provided by `regexp_parser`) into
# equivalent representations using `Parser::AST::Node`
class Transformer
include AbstractType

REGISTRY = Registry.new

# Lookup transformer class for regular expression node type
#
# @param type [Symbol]
#
# @return [Class<Transformer>]
def self.lookup(type)
REGISTRY.lookup(type)
end

# Register transformer class as responsible for handling node type
#
# @param type [Symbol]
#
# @return [undefined]
def self.register(type)
REGISTRY.register(type, self)
end
private_class_method :register

# Transform expression
#
# @param expression [Regexp::Expression]
#
# @return [Parser::AST::Node]
def self.to_ast(expression)
self::ExpressionToAST.call(expression)
end

# Transform node
#
# @param node [Parser::AST::Node]
#
# @return [Regexp::Expression]
def self.to_expression(node)
self::ASTToExpression.call(node)
end

# Abstract expression transformer
class ExpressionToAST
PREFIX = :regexp

include Concord.new(:expression), Procto.call, AST::Sexp, AbstractType, Adamantium

private

# Node with provided children using node type constructed in `type`
#
# @param [Object,Parser::AST::Node] child of node
#
# @return [Parser::AST::Node]
def ast(*children)
s(type, *children)
end

# Wrap provided node in a quantifier
#
# @param node [Parser::AST::Node]
#
# @return [Parser::AST::Node]
# quantifier node wrapping provided node if expression is quantified
#
# @return [Parser::AST::Node]
# original node otherwise
def quantify(node)
return node unless expression.quantified?

Quantifier.to_ast(expression.quantifier).append(node)
end

# Transformed children of expression
#
# @return [Array<Parser::AST::Node>]
def children
expression.map(&Regexp.public_method(:to_ast))
end

# Node type constructed from token and type of `Regexp::Expression`
#
# @return [Symbol]
def type
:"#{PREFIX}_#{expression.token}_#{expression.type}"
end
end # ExpressionToAST

# Abstract node transformer
class ASTToExpression
include Concord.new(:node), Procto.call, AbstractType, Adamantium

# Call generic transform method and freeze result
#
# @return [Regexp::Expression]
def call
transform.freeze
end

private

# Transformation of ast into expression
#
# @return [Regexp::Expression]
abstract_method :transform

# Transformed children of node
#
# @return [Array<Regexp::Expression>]
def subexpressions
node.children.map(&Regexp.public_method(:to_expression))
end
end # ASTToExpression

# Mixin for node transformers
#
# Helps construct a mapping from Parser::AST::Node domain to
# Regexp::Expression domain
module LookupTable
Mapping = Class.new.include(Concord::Public.new(:token, :regexp_class))

# Table mapping ast types to object information for regexp domain
class Table

# Coerce array of mapping information into structured table
#
# @param [Array(Symbol, Array, Class<Regexp::Expression>)]
#
# @return [Table]
def self.create(*rows)
table = rows.map do |ast_type, token, klass|
[ast_type, Mapping.new(::Regexp::Token.new(*token), klass)]
end.to_h

new(table)
end

include Concord.new(:table), Adamantium

# Types defined by the table
#
# @return [Array<Symbol>]
def types
table.keys
end

# Lookup mapping information given an ast node type
#
# @param type [Symbol]
#
# @return [Mapping]
def lookup(type)
table.fetch(type)
end
end # Table

private

# Lookup expression token given node type
#
# @return [Regexp::Token]
def expression_token
self.class::TABLE.lookup(node.type).token
end

# Lookup regexp class given node type
#
# @return [Class<Regexp::Expression>]
def expression_class
self.class::TABLE.lookup(node.type).regexp_class
end
end # LookupTable
end # Transformer
end # Regexp
end # AST
end # Mutant
Loading

0 comments on commit 9c7fc4a

Please sign in to comment.