mutant 0.8.24 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +3 -3
- data/Changelog.md +14 -654
- data/Gemfile +13 -0
- data/Gemfile.lock +59 -64
- data/LICENSE +271 -20
- data/README.md +73 -140
- data/Rakefile +0 -21
- data/bin/mutant +7 -2
- data/config/reek.yml +2 -1
- data/config/rubocop.yml +5 -9
- data/docs/incremental.md +76 -0
- data/docs/known-problems.md +0 -14
- data/docs/mutant-minitest.md +1 -1
- data/docs/mutant-rspec.md +2 -24
- data/lib/mutant.rb +45 -53
- data/lib/mutant/ast/nodes.rb +0 -2
- data/lib/mutant/ast/types.rb +1 -117
- data/lib/mutant/base.rb +192 -0
- data/lib/mutant/bootstrap.rb +145 -0
- data/lib/mutant/cli.rb +68 -54
- data/lib/mutant/config.rb +119 -6
- data/lib/mutant/env.rb +94 -8
- data/lib/mutant/expression.rb +6 -1
- data/lib/mutant/expression/parser.rb +9 -31
- data/lib/mutant/integration.rb +64 -36
- data/lib/mutant/isolation.rb +16 -1
- data/lib/mutant/isolation/fork.rb +105 -40
- data/lib/mutant/license.rb +34 -0
- data/lib/mutant/license/subscription.rb +47 -0
- data/lib/mutant/license/subscription/commercial.rb +57 -0
- data/lib/mutant/license/subscription/opensource.rb +77 -0
- data/lib/mutant/loader.rb +27 -4
- data/lib/mutant/matcher.rb +48 -1
- data/lib/mutant/matcher/chain.rb +1 -1
- data/lib/mutant/matcher/config.rb +0 -2
- data/lib/mutant/matcher/filter.rb +1 -1
- data/lib/mutant/matcher/method.rb +11 -7
- data/lib/mutant/matcher/methods.rb +1 -1
- data/lib/mutant/matcher/namespace.rb +1 -1
- data/lib/mutant/matcher/null.rb +1 -1
- data/lib/mutant/matcher/scope.rb +1 -1
- data/lib/mutant/meta/example/dsl.rb +0 -8
- data/lib/mutant/mutation.rb +1 -2
- data/lib/mutant/mutator/node.rb +2 -9
- data/lib/mutant/mutator/node/arguments.rb +1 -1
- data/lib/mutant/mutator/node/class.rb +0 -8
- data/lib/mutant/mutator/node/define.rb +0 -12
- data/lib/mutant/mutator/node/generic.rb +30 -44
- data/lib/mutant/mutator/node/index.rb +4 -4
- data/lib/mutant/mutator/node/literal/regex.rb +0 -39
- data/lib/mutant/mutator/node/send.rb +13 -12
- data/lib/mutant/parallel.rb +61 -40
- data/lib/mutant/parallel/driver.rb +59 -0
- data/lib/mutant/parallel/source.rb +6 -2
- data/lib/mutant/parallel/worker.rb +63 -45
- data/lib/mutant/range.rb +15 -0
- data/lib/mutant/reporter/cli.rb +5 -11
- data/lib/mutant/reporter/cli/format.rb +3 -46
- data/lib/mutant/reporter/cli/printer/config.rb +5 -6
- data/lib/mutant/reporter/cli/printer/env.rb +40 -0
- data/lib/mutant/reporter/cli/printer/env_progress.rb +13 -17
- data/lib/mutant/reporter/cli/printer/isolation_result.rb +17 -3
- data/lib/mutant/reporter/cli/printer/mutation_result.rb +2 -3
- data/lib/mutant/reporter/cli/printer/status_progressive.rb +19 -10
- data/lib/mutant/repository.rb +0 -65
- data/lib/mutant/repository/diff.rb +104 -0
- data/lib/mutant/repository/diff/ranges.rb +52 -0
- data/lib/mutant/result.rb +16 -7
- data/lib/mutant/runner.rb +38 -47
- data/lib/mutant/runner/sink.rb +1 -1
- data/lib/mutant/selector/null.rb +19 -0
- data/lib/mutant/subject.rb +3 -1
- data/lib/mutant/subject/method/instance.rb +3 -1
- data/lib/mutant/transform.rb +511 -0
- data/lib/mutant/variable.rb +282 -0
- data/lib/mutant/version.rb +1 -1
- data/lib/mutant/warnings.rb +113 -0
- data/meta/case.rb +1 -0
- data/meta/class.rb +0 -9
- data/meta/def.rb +1 -26
- data/meta/regexp.rb +10 -20
- data/meta/send.rb +14 -46
- data/mutant-minitest.gemspec +1 -1
- data/mutant-rspec.gemspec +2 -2
- data/mutant.gemspec +15 -16
- data/mutant.yml +6 -0
- data/spec/integration/mutant/isolation/fork_spec.rb +22 -5
- data/spec/integration/mutant/minitest_spec.rb +3 -2
- data/spec/integration/mutant/rspec_spec.rb +4 -3
- data/spec/integrations.yml +16 -13
- data/spec/shared/base_behavior.rb +45 -0
- data/spec/shared/framework_integration_behavior.rb +43 -14
- data/spec/spec_helper.rb +21 -17
- data/spec/support/corpus.rb +56 -95
- data/spec/support/shared_context.rb +37 -14
- data/spec/support/xspec.rb +7 -3
- data/spec/unit/mutant/bootstrap_spec.rb +216 -0
- data/spec/unit/mutant/cli_spec.rb +173 -117
- data/spec/unit/mutant/config_spec.rb +126 -0
- data/spec/unit/mutant/either_spec.rb +247 -0
- data/spec/unit/mutant/env_spec.rb +162 -40
- data/spec/unit/mutant/expression/method_spec.rb +16 -0
- data/spec/unit/mutant/expression/parser_spec.rb +29 -33
- data/spec/unit/mutant/expression_spec.rb +5 -7
- data/spec/unit/mutant/integration_spec.rb +100 -9
- data/spec/unit/mutant/isolation/fork_spec.rb +125 -67
- data/spec/unit/mutant/isolation/result_spec.rb +33 -1
- data/spec/unit/mutant/license_spec.rb +257 -0
- data/spec/unit/mutant/loader_spec.rb +50 -11
- data/spec/unit/mutant/matcher/compiler_spec.rb +0 -78
- data/spec/unit/mutant/matcher/method/instance_spec.rb +55 -11
- data/spec/unit/mutant/matcher/method/singleton_spec.rb +12 -2
- data/spec/unit/mutant/matcher_spec.rb +102 -0
- data/spec/unit/mutant/maybe_spec.rb +60 -0
- data/spec/unit/mutant/meta/example/dsl_spec.rb +1 -17
- data/spec/unit/mutant/mutation_spec.rb +13 -6
- data/spec/unit/mutant/parallel/driver_spec.rb +112 -14
- data/spec/unit/mutant/parallel/source/array_spec.rb +25 -17
- data/spec/unit/mutant/parallel/worker_spec.rb +182 -44
- data/spec/unit/mutant/parallel_spec.rb +105 -8
- data/spec/unit/mutant/range_spec.rb +141 -0
- data/spec/unit/mutant/reporter/cli/printer/config_spec.rb +7 -21
- data/spec/unit/mutant/reporter/cli/printer/env_progress_spec.rb +15 -6
- data/spec/unit/mutant/reporter/cli/printer/env_result_spec.rb +10 -2
- data/spec/unit/mutant/reporter/cli/printer/isolation_result_spec.rb +12 -4
- data/spec/unit/mutant/reporter/cli/printer/mutation_result_spec.rb +31 -2
- data/spec/unit/mutant/reporter/cli/printer/status_progressive_spec.rb +4 -4
- data/spec/unit/mutant/reporter/cli/printer/subject_result_spec.rb +5 -0
- data/spec/unit/mutant/reporter/cli_spec.rb +46 -123
- data/spec/unit/mutant/repository/diff/ranges_spec.rb +180 -0
- data/spec/unit/mutant/repository/diff_spec.rb +84 -71
- data/spec/unit/mutant/require_highjack_spec.rb +1 -1
- data/spec/unit/mutant/result/env_spec.rb +39 -9
- data/spec/unit/mutant/result/test_spec.rb +14 -0
- data/spec/unit/mutant/runner_spec.rb +88 -41
- data/spec/unit/mutant/selector/expression_spec.rb +11 -10
- data/spec/unit/mutant/selector/null_spec.rb +17 -0
- data/spec/unit/mutant/subject/method/instance_spec.rb +44 -5
- data/spec/unit/mutant/subject/method/singleton_spec.rb +9 -2
- data/spec/unit/mutant/subject_spec.rb +9 -1
- data/spec/unit/mutant/transform/array_spec.rb +92 -0
- data/spec/unit/mutant/transform/bool_spec.rb +63 -0
- data/spec/unit/mutant/transform/error_spec.rb +132 -0
- data/spec/unit/mutant/transform/exception_spec.rb +44 -0
- data/spec/unit/mutant/transform/hash_spec.rb +236 -0
- data/spec/unit/mutant/transform/index_spec.rb +92 -0
- data/spec/unit/mutant/transform/named_spec.rb +49 -0
- data/spec/unit/mutant/transform/primitive_spec.rb +56 -0
- data/spec/unit/mutant/transform/sequence_spec.rb +98 -0
- data/spec/unit/mutant/variable_spec.rb +618 -0
- data/spec/unit/mutant/warnings_spec.rb +89 -0
- data/spec/unit/mutant/world_spec.rb +63 -0
- data/test_app/Gemfile.minitest +0 -2
- metadata +79 -113
- data/.gitattributes +0 -1
- data/.ruby-gemset +0 -1
- data/config/triage.yml +0 -2
- data/lib/mutant/actor.rb +0 -57
- data/lib/mutant/actor/env.rb +0 -31
- data/lib/mutant/actor/mailbox.rb +0 -34
- data/lib/mutant/actor/receiver.rb +0 -42
- data/lib/mutant/actor/sender.rb +0 -26
- data/lib/mutant/ast/meta/restarg.rb +0 -19
- data/lib/mutant/ast/regexp.rb +0 -42
- data/lib/mutant/ast/regexp/transformer.rb +0 -187
- data/lib/mutant/ast/regexp/transformer/direct.rb +0 -123
- data/lib/mutant/ast/regexp/transformer/named_group.rb +0 -59
- data/lib/mutant/ast/regexp/transformer/options_group.rb +0 -83
- data/lib/mutant/ast/regexp/transformer/quantifier.rb +0 -114
- data/lib/mutant/ast/regexp/transformer/recursive.rb +0 -58
- data/lib/mutant/ast/regexp/transformer/root.rb +0 -31
- data/lib/mutant/ast/regexp/transformer/text.rb +0 -60
- data/lib/mutant/env/bootstrap.rb +0 -160
- data/lib/mutant/matcher/compiler.rb +0 -60
- data/lib/mutant/mutator/node/regexp.rb +0 -35
- data/lib/mutant/mutator/node/regexp/alternation_meta.rb +0 -23
- data/lib/mutant/mutator/node/regexp/capture_group.rb +0 -28
- data/lib/mutant/mutator/node/regexp/character_type.rb +0 -32
- data/lib/mutant/mutator/node/regexp/end_of_line_anchor.rb +0 -23
- data/lib/mutant/mutator/node/regexp/end_of_string_or_before_end_of_line_anchor.rb +0 -23
- data/lib/mutant/mutator/node/regexp/greedy_zero_or_more.rb +0 -27
- data/lib/mutant/parallel/master.rb +0 -181
- data/lib/mutant/reporter/cli/printer/status.rb +0 -53
- data/lib/mutant/reporter/cli/tput.rb +0 -46
- data/lib/mutant/warning_filter.rb +0 -61
- data/meta/regexp/character_types.rb +0 -23
- data/meta/regexp/regexp_alternation_meta.rb +0 -13
- data/meta/regexp/regexp_bol_anchor.rb +0 -10
- data/meta/regexp/regexp_bos_anchor.rb +0 -18
- data/meta/regexp/regexp_capture_group.rb +0 -19
- data/meta/regexp/regexp_eol_anchor.rb +0 -10
- data/meta/regexp/regexp_eos_anchor.rb +0 -8
- data/meta/regexp/regexp_eos_ob_eol_anchor.rb +0 -10
- data/meta/regexp/regexp_greedy_zero_or_more.rb +0 -12
- data/meta/regexp/regexp_root_expression.rb +0 -10
- data/meta/restarg.rb +0 -10
- data/spec/support/fake_actor.rb +0 -111
- data/spec/support/warning.rb +0 -66
- data/spec/unit/mutant/actor/binding_spec.rb +0 -34
- data/spec/unit/mutant/actor/env_spec.rb +0 -31
- data/spec/unit/mutant/actor/mailbox_spec.rb +0 -28
- data/spec/unit/mutant/actor/message_spec.rb +0 -25
- data/spec/unit/mutant/actor/receiver_spec.rb +0 -58
- data/spec/unit/mutant/actor/sender_spec.rb +0 -24
- data/spec/unit/mutant/ast/regexp/parse_spec.rb +0 -19
- data/spec/unit/mutant/ast/regexp/transformer/lookup_table/table_spec.rb +0 -21
- data/spec/unit/mutant/ast/regexp/transformer/lookup_table_spec.rb +0 -35
- data/spec/unit/mutant/ast/regexp/transformer_spec.rb +0 -21
- data/spec/unit/mutant/ast/regexp_spec.rb +0 -704
- data/spec/unit/mutant/env/bootstrap_spec.rb +0 -188
- data/spec/unit/mutant/matcher/compiler/subject_prefix_spec.rb +0 -26
- data/spec/unit/mutant/parallel/master_spec.rb +0 -338
- data/spec/unit/mutant/reporter/cli/printer/status_spec.rb +0 -121
- data/spec/unit/mutant/reporter/cli/tput_spec.rb +0 -50
- data/spec/unit/mutant/warning_filter_spec.rb +0 -106
- data/spec/unit/mutant_spec.rb +0 -17
- data/test_app/Gemfile.rspec3.7 +0 -7
data/.gitattributes
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
Changelog.md merge=union
|
data/.ruby-gemset
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
mutant
|
data/config/triage.yml
DELETED
data/lib/mutant/actor.rb
DELETED
@@ -1,57 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Mutant
|
4
|
-
# A minimal actor implementation
|
5
|
-
module Actor
|
6
|
-
|
7
|
-
# Error raised when actor signalling protocol is violated
|
8
|
-
class ProtocolError < RuntimeError
|
9
|
-
end # ProtocolError
|
10
|
-
|
11
|
-
# Undefined message payload
|
12
|
-
Undefined = Class.new do
|
13
|
-
INSPECT = 'Mutant::Actor::Undefined'
|
14
|
-
|
15
|
-
# Object inspection
|
16
|
-
#
|
17
|
-
# @return [String]
|
18
|
-
def inspect
|
19
|
-
INSPECT
|
20
|
-
end
|
21
|
-
end.new
|
22
|
-
|
23
|
-
# Message being exchanged between actors
|
24
|
-
class Message
|
25
|
-
include Concord::Public.new(:type, :payload)
|
26
|
-
|
27
|
-
# New message
|
28
|
-
#
|
29
|
-
# @param [Symbol] type
|
30
|
-
# @param [Object] payload
|
31
|
-
#
|
32
|
-
# @return [Message]
|
33
|
-
def self.new(_type, _payload = Undefined)
|
34
|
-
super
|
35
|
-
end
|
36
|
-
|
37
|
-
end # Message
|
38
|
-
|
39
|
-
# Binding to other actors sender for simple RPC
|
40
|
-
class Binding
|
41
|
-
include Concord.new(:mailbox, :other)
|
42
|
-
|
43
|
-
# Send message and wait for reply
|
44
|
-
#
|
45
|
-
# @param [Symbol] type
|
46
|
-
#
|
47
|
-
# @return [Object]
|
48
|
-
def call(type)
|
49
|
-
other.call(Message.new(type, mailbox.sender))
|
50
|
-
message = mailbox.receiver.call
|
51
|
-
fail ProtocolError, "Expected #{type} but got #{message.type}" unless type.equal?(message.type)
|
52
|
-
message.payload
|
53
|
-
end
|
54
|
-
|
55
|
-
end # Binding
|
56
|
-
end # Actor
|
57
|
-
end # Mutant
|
data/lib/mutant/actor/env.rb
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Mutant
|
4
|
-
module Actor
|
5
|
-
# Actor root environment
|
6
|
-
class Env
|
7
|
-
include Concord.new(:thread_root)
|
8
|
-
|
9
|
-
# Spawn a new actor executing block
|
10
|
-
#
|
11
|
-
# @return [Actor::Sender]
|
12
|
-
def spawn
|
13
|
-
mailbox = new_mailbox
|
14
|
-
|
15
|
-
thread_root.new do
|
16
|
-
yield mailbox
|
17
|
-
end
|
18
|
-
|
19
|
-
mailbox.sender
|
20
|
-
end
|
21
|
-
|
22
|
-
# New unbound mailbox
|
23
|
-
#
|
24
|
-
# @return [Mailbox]
|
25
|
-
def new_mailbox
|
26
|
-
Mailbox.new
|
27
|
-
end
|
28
|
-
|
29
|
-
end # Env
|
30
|
-
end # Actor
|
31
|
-
end # Mutant
|
data/lib/mutant/actor/mailbox.rb
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Mutant
|
4
|
-
module Actor
|
5
|
-
# Unbound mailbox
|
6
|
-
class Mailbox
|
7
|
-
include Adamantium::Flat, Concord::Public.new(:receiver, :sender)
|
8
|
-
|
9
|
-
# New mailbox
|
10
|
-
#
|
11
|
-
# @return [Mailbox]
|
12
|
-
def self.new
|
13
|
-
mutex = Mutex.new
|
14
|
-
condition_variable = ConditionVariable.new
|
15
|
-
messages = []
|
16
|
-
|
17
|
-
super(
|
18
|
-
Receiver.new(condition_variable, mutex, messages),
|
19
|
-
Sender.new(condition_variable, mutex, messages)
|
20
|
-
)
|
21
|
-
end
|
22
|
-
|
23
|
-
# Binding for RPC to other actors
|
24
|
-
#
|
25
|
-
# @param [Actor::Sender] other
|
26
|
-
#
|
27
|
-
# @return [Binding]
|
28
|
-
def bind(other)
|
29
|
-
Binding.new(self, other)
|
30
|
-
end
|
31
|
-
|
32
|
-
end # Mailbox
|
33
|
-
end # Actor
|
34
|
-
end # Mutant
|
@@ -1,42 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Mutant
|
4
|
-
module Actor
|
5
|
-
# Receiver side of an actor
|
6
|
-
class Receiver
|
7
|
-
include Adamantium::Flat, Concord.new(:condition_variable, :mutex, :messages)
|
8
|
-
|
9
|
-
# Receives a message, blocking
|
10
|
-
#
|
11
|
-
# @return [Object]
|
12
|
-
def call
|
13
|
-
2.times do
|
14
|
-
message = try_blocking_receive
|
15
|
-
return message unless message.equal?(Undefined)
|
16
|
-
end
|
17
|
-
fail ProtocolError
|
18
|
-
end
|
19
|
-
|
20
|
-
private
|
21
|
-
|
22
|
-
# Try a blocking receive
|
23
|
-
#
|
24
|
-
# @return [Undefined]
|
25
|
-
# if there is no message yet
|
26
|
-
#
|
27
|
-
# @return [Object]
|
28
|
-
# if there is a message
|
29
|
-
def try_blocking_receive
|
30
|
-
mutex.synchronize do
|
31
|
-
if messages.empty?
|
32
|
-
condition_variable.wait(mutex)
|
33
|
-
Undefined
|
34
|
-
else
|
35
|
-
messages.shift
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
end # Receiver
|
41
|
-
end # Actor
|
42
|
-
end # Mutant
|
data/lib/mutant/actor/sender.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Mutant
|
4
|
-
module Actor
|
5
|
-
|
6
|
-
# Sender for messages to acting thread
|
7
|
-
class Sender
|
8
|
-
include Adamantium::Flat, Concord.new(:condition_variable, :mutex, :messages)
|
9
|
-
|
10
|
-
# Send a message to actor
|
11
|
-
#
|
12
|
-
# @param [Object] message
|
13
|
-
#
|
14
|
-
# @return [self]
|
15
|
-
def call(message)
|
16
|
-
mutex.synchronize do
|
17
|
-
messages << message
|
18
|
-
condition_variable.signal
|
19
|
-
end
|
20
|
-
|
21
|
-
self
|
22
|
-
end
|
23
|
-
|
24
|
-
end # Sender
|
25
|
-
end # Actor
|
26
|
-
end # Mutant
|
@@ -1,19 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Mutant
|
4
|
-
module AST
|
5
|
-
# Node meta information mixin
|
6
|
-
module Meta
|
7
|
-
|
8
|
-
# Metadata for restarg nodes
|
9
|
-
class Restarg
|
10
|
-
include NamedChildren, Concord.new(:node)
|
11
|
-
|
12
|
-
children :name
|
13
|
-
|
14
|
-
public :name
|
15
|
-
end # Restarg
|
16
|
-
|
17
|
-
end # Meta
|
18
|
-
end # AST
|
19
|
-
end # Mutant
|
data/lib/mutant/ast/regexp.rb
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Mutant
|
4
|
-
module AST
|
5
|
-
# Regexp source mapper
|
6
|
-
module Regexp
|
7
|
-
# Parse regex string into expression
|
8
|
-
#
|
9
|
-
# @param regexp [String]
|
10
|
-
#
|
11
|
-
# @return [Regexp::Expression, nil]
|
12
|
-
#
|
13
|
-
# rubocop:disable Lint/HandleExceptions
|
14
|
-
def self.parse(regexp)
|
15
|
-
::Regexp::Parser.parse(regexp)
|
16
|
-
# regexp_parser is more strict than MRI
|
17
|
-
rescue ::Regexp::Scanner::PrematureEndError
|
18
|
-
end
|
19
|
-
# rubocop:enable Lint/HandleExceptions
|
20
|
-
|
21
|
-
# Convert expression into ast node
|
22
|
-
#
|
23
|
-
# @param expression [Regexp::Expression]
|
24
|
-
#
|
25
|
-
# @return [Parser::AST::Node]
|
26
|
-
def self.to_ast(expression)
|
27
|
-
ast_type = :"regexp_#{expression.token}_#{expression.type}"
|
28
|
-
|
29
|
-
Transformer.lookup(ast_type).to_ast(expression)
|
30
|
-
end
|
31
|
-
|
32
|
-
# Convert node into expression
|
33
|
-
#
|
34
|
-
# @param node [Parser::AST::Node]
|
35
|
-
#
|
36
|
-
# @return [Regexp::Expression]
|
37
|
-
def self.to_expression(node)
|
38
|
-
Transformer.lookup(node.type).to_expression(node)
|
39
|
-
end
|
40
|
-
end # Regexp
|
41
|
-
end # AST
|
42
|
-
end # Mutant
|
@@ -1,187 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Mutant
|
4
|
-
module AST
|
5
|
-
module Regexp
|
6
|
-
# Regexp bijective mapper
|
7
|
-
#
|
8
|
-
# Transforms parsed regular expression representation from
|
9
|
-
# `Regexp::Expression` instances (provided by `regexp_parser`) into
|
10
|
-
# equivalent representations using `Parser::AST::Node`
|
11
|
-
class Transformer
|
12
|
-
include AbstractType
|
13
|
-
|
14
|
-
REGISTRY = Registry.new
|
15
|
-
|
16
|
-
# Lookup transformer class for regular expression node type
|
17
|
-
#
|
18
|
-
# @param type [Symbol]
|
19
|
-
#
|
20
|
-
# @return [Class<Transformer>]
|
21
|
-
def self.lookup(type)
|
22
|
-
REGISTRY.lookup(type)
|
23
|
-
end
|
24
|
-
|
25
|
-
# Register transformer class as responsible for handling node type
|
26
|
-
#
|
27
|
-
# @param type [Symbol]
|
28
|
-
#
|
29
|
-
# @return [undefined]
|
30
|
-
def self.register(type)
|
31
|
-
REGISTRY.register(type, self)
|
32
|
-
end
|
33
|
-
private_class_method :register
|
34
|
-
|
35
|
-
# Transform expression
|
36
|
-
#
|
37
|
-
# @param expression [Regexp::Expression]
|
38
|
-
#
|
39
|
-
# @return [Parser::AST::Node]
|
40
|
-
def self.to_ast(expression)
|
41
|
-
self::ExpressionToAST.call(expression)
|
42
|
-
end
|
43
|
-
|
44
|
-
# Transform node
|
45
|
-
#
|
46
|
-
# @param node [Parser::AST::Node]
|
47
|
-
#
|
48
|
-
# @return [Regexp::Expression]
|
49
|
-
def self.to_expression(node)
|
50
|
-
self::ASTToExpression.call(node)
|
51
|
-
end
|
52
|
-
|
53
|
-
# Abstract expression transformer
|
54
|
-
class ExpressionToAST
|
55
|
-
PREFIX = :regexp
|
56
|
-
|
57
|
-
include Concord.new(:expression), Procto.call, AST::Sexp, AbstractType, Adamantium
|
58
|
-
|
59
|
-
private
|
60
|
-
|
61
|
-
# Node with provided children using node type constructed in `type`
|
62
|
-
#
|
63
|
-
# @param [Object,Parser::AST::Node] child of node
|
64
|
-
#
|
65
|
-
# @return [Parser::AST::Node]
|
66
|
-
def ast(*children)
|
67
|
-
s(type, *children)
|
68
|
-
end
|
69
|
-
|
70
|
-
# Wrap provided node in a quantifier
|
71
|
-
#
|
72
|
-
# @param node [Parser::AST::Node]
|
73
|
-
#
|
74
|
-
# @return [Parser::AST::Node]
|
75
|
-
# quantifier node wrapping provided node if expression is quantified
|
76
|
-
#
|
77
|
-
# @return [Parser::AST::Node]
|
78
|
-
# original node otherwise
|
79
|
-
def quantify(node)
|
80
|
-
return node unless expression.quantified?
|
81
|
-
|
82
|
-
Quantifier.to_ast(expression.quantifier).append(node)
|
83
|
-
end
|
84
|
-
|
85
|
-
# Transformed children of expression
|
86
|
-
#
|
87
|
-
# @return [Array<Parser::AST::Node>]
|
88
|
-
def children
|
89
|
-
expression.expressions.map(&Regexp.method(:to_ast))
|
90
|
-
end
|
91
|
-
|
92
|
-
# Node type constructed from token and type of `Regexp::Expression`
|
93
|
-
#
|
94
|
-
# @return [Symbol]
|
95
|
-
def type
|
96
|
-
:"#{PREFIX}_#{expression.token}_#{expression.type}"
|
97
|
-
end
|
98
|
-
end # ExpressionToAST
|
99
|
-
|
100
|
-
# Abstract node transformer
|
101
|
-
class ASTToExpression
|
102
|
-
include Concord.new(:node), Procto.call, AbstractType, Adamantium
|
103
|
-
|
104
|
-
# Call generic transform method and freeze result
|
105
|
-
#
|
106
|
-
# @return [Regexp::Expression]
|
107
|
-
def call
|
108
|
-
transform.freeze
|
109
|
-
end
|
110
|
-
|
111
|
-
private
|
112
|
-
|
113
|
-
# Transformation of ast into expression
|
114
|
-
#
|
115
|
-
# @return [Regexp::Expression]
|
116
|
-
abstract_method :transform
|
117
|
-
|
118
|
-
# Transformed children of node
|
119
|
-
#
|
120
|
-
# @return [Array<Regexp::Expression>]
|
121
|
-
def subexpressions
|
122
|
-
node.children.map(&Regexp.public_method(:to_expression))
|
123
|
-
end
|
124
|
-
end # ASTToExpression
|
125
|
-
|
126
|
-
# Mixin for node transformers
|
127
|
-
#
|
128
|
-
# Helps construct a mapping from Parser::AST::Node domain to
|
129
|
-
# Regexp::Expression domain
|
130
|
-
module LookupTable
|
131
|
-
Mapping = Class.new.include(Concord::Public.new(:token, :regexp_class))
|
132
|
-
|
133
|
-
# Table mapping ast types to object information for regexp domain
|
134
|
-
class Table
|
135
|
-
|
136
|
-
# Coerce array of mapping information into structured table
|
137
|
-
#
|
138
|
-
# @param [Array(Symbol, Array, Class<Regexp::Expression>)]
|
139
|
-
#
|
140
|
-
# @return [Table]
|
141
|
-
def self.create(*rows)
|
142
|
-
table = rows.map do |ast_type, token, klass|
|
143
|
-
[ast_type, Mapping.new(::Regexp::Token.new(*token), klass)]
|
144
|
-
end.to_h
|
145
|
-
|
146
|
-
new(table)
|
147
|
-
end
|
148
|
-
|
149
|
-
include Concord.new(:table), Adamantium
|
150
|
-
|
151
|
-
# Types defined by the table
|
152
|
-
#
|
153
|
-
# @return [Array<Symbol>]
|
154
|
-
def types
|
155
|
-
table.keys
|
156
|
-
end
|
157
|
-
|
158
|
-
# Lookup mapping information given an ast node type
|
159
|
-
#
|
160
|
-
# @param type [Symbol]
|
161
|
-
#
|
162
|
-
# @return [Mapping]
|
163
|
-
def lookup(type)
|
164
|
-
table.fetch(type)
|
165
|
-
end
|
166
|
-
end # Table
|
167
|
-
|
168
|
-
private
|
169
|
-
|
170
|
-
# Lookup expression token given node type
|
171
|
-
#
|
172
|
-
# @return [Regexp::Token]
|
173
|
-
def expression_token
|
174
|
-
self.class::TABLE.lookup(node.type).token
|
175
|
-
end
|
176
|
-
|
177
|
-
# Lookup regexp class given node type
|
178
|
-
#
|
179
|
-
# @return [Class<Regexp::Expression>]
|
180
|
-
def expression_class
|
181
|
-
self.class::TABLE.lookup(node.type).regexp_class
|
182
|
-
end
|
183
|
-
end # LookupTable
|
184
|
-
end # Transformer
|
185
|
-
end # Regexp
|
186
|
-
end # AST
|
187
|
-
end # Mutant
|