cql_ruby 0.0.7 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/cql_ruby +139 -116
- data/lib/cql_ruby.rb +15 -15
- data/lib/cql_ruby/abstract_printer.rb +12 -12
- data/lib/cql_ruby/console_printer.rb +97 -97
- data/lib/cql_ruby/crumb_collector.rb +18 -18
- data/lib/cql_ruby/executor.rb +194 -148
- data/lib/cql_ruby/filter_evaluator.rb +178 -54
- data/lib/cql_ruby/filter_reader.rb +130 -81
- data/lib/cql_ruby/pattern_matcher.rb +57 -53
- metadata +2 -2
@@ -1,81 +1,130 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
#
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
def
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# TODO: Have convenience filters for type:_ such as: isclass, ismodule, isdef ...
|
4
|
+
|
5
|
+
module CqlRuby
|
6
|
+
# TODO Move this under filter reader namespace.
|
7
|
+
class HierarchyPattern
|
8
|
+
SELF_MARKER = 'X'
|
9
|
+
|
10
|
+
def self.from(raw_value)
|
11
|
+
parts = raw_value.split('-')
|
12
|
+
self_marker_idx = parts.index(SELF_MARKER)
|
13
|
+
raise "Missing self marker '#{SELF_MARKER}' in hierarchy pattern." if self_marker_idx.nil?
|
14
|
+
|
15
|
+
ancestors = parts[0...self_marker_idx]
|
16
|
+
descendants = parts[self_marker_idx + 1..]
|
17
|
+
|
18
|
+
new(ancestors, descendants)
|
19
|
+
end
|
20
|
+
|
21
|
+
attr_reader :ancestors
|
22
|
+
attr_reader :descendants
|
23
|
+
|
24
|
+
def initialize(ancestors, descendants)
|
25
|
+
@ancestors = ancestors
|
26
|
+
@descendants = descendants
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class NodeSpec < Struct.new(:type, :name)
|
31
|
+
# Make this non duplicated.
|
32
|
+
NAME_ANY = '*'
|
33
|
+
|
34
|
+
class << self
|
35
|
+
#
|
36
|
+
# @param [String] raw_value
|
37
|
+
# Format: TYPE(=NAME|=*)
|
38
|
+
# Accepted types: class, module, def, block
|
39
|
+
#
|
40
|
+
def from(raw_value)
|
41
|
+
type, name = raw_value.split('=')
|
42
|
+
name ||= NAME_ANY
|
43
|
+
|
44
|
+
raise "Type '#{type}' is not recognized. See 'cql_ruby --help' for allowed types." unless Parser::Meta::NODE_TYPES.member?(type.to_sym)
|
45
|
+
|
46
|
+
new(type, name)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def restrict_name?
|
51
|
+
name != NAME_ANY
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
#
|
56
|
+
# Reads and provides filters.
|
57
|
+
#
|
58
|
+
# Accepted filters and syntax:
|
59
|
+
#
|
60
|
+
# Type:
|
61
|
+
#
|
62
|
+
# type:[name](,[name])*
|
63
|
+
# example: type:def,send
|
64
|
+
#
|
65
|
+
class FilterReader
|
66
|
+
NESTING_ALLOWED_TYPES = %w[class module def block].freeze
|
67
|
+
|
68
|
+
# @attribute [Array<Symbol>] allowed_types
|
69
|
+
attr_reader :allowed_types
|
70
|
+
# @attribute [Array<CqlRuby::NodeSpec>] nest_under
|
71
|
+
attr_reader :nest_under
|
72
|
+
# @attribute [Array<CqlRuby::NodeSpec>] has_leaves
|
73
|
+
attr_reader :has_leaves
|
74
|
+
# @attribute [Array<CqlRuby::HierarchyPattern>] patterns
|
75
|
+
attr_reader :patterns
|
76
|
+
|
77
|
+
def initialize(raw_filters)
|
78
|
+
super()
|
79
|
+
|
80
|
+
@allowed_types = []
|
81
|
+
@nest_under = []
|
82
|
+
@has_leaves = []
|
83
|
+
@patterns = []
|
84
|
+
|
85
|
+
parse_raw_filters(raw_filters)
|
86
|
+
end
|
87
|
+
|
88
|
+
def restrict_types?
|
89
|
+
!@allowed_types.empty?
|
90
|
+
end
|
91
|
+
|
92
|
+
def restrict_nesting?
|
93
|
+
!@nest_under.empty?
|
94
|
+
end
|
95
|
+
|
96
|
+
def restrict_children?
|
97
|
+
!@has_leaves.empty?
|
98
|
+
end
|
99
|
+
|
100
|
+
def restrict_pattern?
|
101
|
+
!@patterns.empty?
|
102
|
+
end
|
103
|
+
|
104
|
+
private
|
105
|
+
|
106
|
+
# @param [Array<String>] raw_filters
|
107
|
+
def parse_raw_filters(raw_filters)
|
108
|
+
raw_filters.each do |raw_filter|
|
109
|
+
name, value = raw_filter.split(':')
|
110
|
+
raise "Unrecognized filter: #{raw_filter}" if name.nil? || value.nil?
|
111
|
+
|
112
|
+
if %w[type t].include?(name)
|
113
|
+
@allowed_types += value.split(',').map(&:to_sym)
|
114
|
+
elsif %w[nest n].include?(name)
|
115
|
+
spec = NodeSpec.from(value)
|
116
|
+
raise "Unknown type for nesting: '#{spec.type}' from '#{raw_filter}'. Allowed: #{NESTING_ALLOWED_TYPES}" unless NESTING_ALLOWED_TYPES.include?(spec.type)
|
117
|
+
raise "Type #{spec.type} cannot have a name." if %w[block].include?(spec.type) && spec.restrict_name?
|
118
|
+
|
119
|
+
@nest_under << spec
|
120
|
+
elsif %w[has h].include?(name)
|
121
|
+
@has_leaves << NodeSpec.from(value)
|
122
|
+
elsif %w[pattern p].include?(name)
|
123
|
+
@patterns << HierarchyPattern.from(value)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
nil
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
@@ -1,53 +1,57 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
module CqlRuby
|
3
|
-
module PatternMatcher
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
if
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
fops
|
38
|
-
fops |= Regexp::
|
39
|
-
Regexp
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module CqlRuby
|
3
|
+
module PatternMatcher
|
4
|
+
MATCH_ANY = '*'
|
5
|
+
|
6
|
+
def self.match?(pattern, subject)
|
7
|
+
pattern = pattern.to_s
|
8
|
+
return true if pattern == MATCH_ANY
|
9
|
+
|
10
|
+
subject = subject.to_s
|
11
|
+
|
12
|
+
if regex?(pattern)
|
13
|
+
regex_match?(pattern, subject)
|
14
|
+
elsif partial_string?(pattern)
|
15
|
+
partial_string_match?(pattern, subject)
|
16
|
+
else
|
17
|
+
full_string_match?(pattern, subject)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.regex?(pattern)
|
22
|
+
pattern[0..1] == 'r/'
|
23
|
+
end
|
24
|
+
private_class_method :regex?
|
25
|
+
|
26
|
+
def self.partial_string?(pattern)
|
27
|
+
pattern[0] == '%'
|
28
|
+
end
|
29
|
+
private_class_method :partial_string?
|
30
|
+
|
31
|
+
def self.regex_match?(pattern, subject)
|
32
|
+
pattern = pattern[2..]
|
33
|
+
delim_idx = pattern.rindex('/')
|
34
|
+
mods = pattern[delim_idx + 1..].chars
|
35
|
+
pattern = pattern[0..delim_idx - 1]
|
36
|
+
|
37
|
+
fops = 0
|
38
|
+
fops |= Regexp::IGNORECASE if mods.include?('i')
|
39
|
+
fops |= Regexp::MULTILINE if mods.include?('m')
|
40
|
+
fops |= Regexp::EXTENDED if mods.include?('x')
|
41
|
+
fops |= Regexp::FIXEDENCODING if mods.include?('f')
|
42
|
+
fops |= Regexp::NOENCODING if mods.include?('n')
|
43
|
+
Regexp.new(pattern, fops).match?(subject)
|
44
|
+
end
|
45
|
+
private_class_method :regex_match?
|
46
|
+
|
47
|
+
def self.full_string_match?(pattern, subject)
|
48
|
+
pattern == subject
|
49
|
+
end
|
50
|
+
private_class_method :full_string_match?
|
51
|
+
|
52
|
+
def self.partial_string_match?(pattern, subject)
|
53
|
+
!subject.index(pattern[1..]).nil?
|
54
|
+
end
|
55
|
+
private_class_method :partial_string_match?
|
56
|
+
end
|
57
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cql_ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- itarato
|
@@ -65,7 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
65
|
- !ruby/object:Gem::Version
|
66
66
|
version: '0'
|
67
67
|
requirements: []
|
68
|
-
rubygems_version: 3.0.
|
68
|
+
rubygems_version: 3.0.3
|
69
69
|
signing_key:
|
70
70
|
specification_version: 4
|
71
71
|
summary: Code Query Language for Ruby
|