Class: Regexp::Expression::Base
- Inherits:
-
Object
- Object
- Regexp::Expression::Base
show all
- Includes:
- Shared
- Defined in:
- lib/regexp_parser/expression/base.rb,
lib/regexp_parser/expression/methods/match.rb,
lib/regexp_parser/expression/methods/options.rb,
lib/regexp_parser/expression/methods/strfregexp.rb
Direct Known Subclasses
Anchor::Base, Regexp::Expression::Backreference::Base, CharacterType::Base, Conditional::Condition, EscapeSequence::Base, FreeSpace, Keep::Mark, Literal, PosixClass, Subexpression, UnicodeProperty::Base
Instance Method Summary
collapse
Methods included from Shared
#==, #base_length, #coded_offset, #full_length, #human_name, included, #is?, #nesting_level=, #offset, #one_of?, #optional?, #parts, #quantified?, #quantifier=, #quantifier_affix, #referential?, #starts_at, #terminal?, #to_s, #token_class, #type?
Constructor Details
#initialize(token, options = {}) ⇒ Base
Returns a new instance of Base.
5
6
7
|
# File 'lib/regexp_parser/expression/base.rb', line 5
def initialize(token, options = {})
init_from_token_and_options(token, options)
end
|
Instance Method Details
#ascii_classes? ⇒ Boolean
Also known as:
a?
25
26
27
|
# File 'lib/regexp_parser/expression/methods/options.rb', line 25
def ascii_classes?
options[:a] == true
end
|
#case_insensitive? ⇒ Boolean
Also known as:
i?, ignore_case?
8
9
10
|
# File 'lib/regexp_parser/expression/methods/options.rb', line 8
def case_insensitive?
options[:i] == true
end
|
#default_classes? ⇒ Boolean
Also known as:
d?
20
21
22
|
# File 'lib/regexp_parser/expression/methods/options.rb', line 20
def default_classes?
options[:d] == true
end
|
#free_spacing? ⇒ Boolean
Also known as:
x?, extended?
14
15
16
|
# File 'lib/regexp_parser/expression/methods/options.rb', line 14
def free_spacing?
options[:x] == true
end
|
#greedy? ⇒ Boolean
54
55
56
|
# File 'lib/regexp_parser/expression/base.rb', line 54
def greedy?
quantified? and quantifier.greedy?
end
|
#initialize_copy(orig) ⇒ Object
9
10
11
12
13
14
|
# File 'lib/regexp_parser/expression/base.rb', line 9
def initialize_copy(orig)
self.text = orig.text.dup if orig.text
self.options = orig.options.dup if orig.options
self.quantifier = orig.quantifier.clone if orig.quantifier
super
end
|
#match(string, offset = 0) ⇒ Object
Also known as:
=~
8
9
10
|
# File 'lib/regexp_parser/expression/methods/match.rb', line 8
def match(string, offset = 0)
Regexp.new(to_s).match(string, offset)
end
|
#match?(string) ⇒ Boolean
Also known as:
matches?
3
4
5
|
# File 'lib/regexp_parser/expression/methods/match.rb', line 3
def match?(string)
!!match(string)
end
|
#multiline? ⇒ Boolean
Also known as:
m?
3
4
5
|
# File 'lib/regexp_parser/expression/methods/options.rb', line 3
def multiline?
options[:m] == true
end
|
#possessive? ⇒ Boolean
63
64
65
|
# File 'lib/regexp_parser/expression/base.rb', line 63
def possessive?
quantified? and quantifier.possessive?
end
|
#quantify(*args) ⇒ Object
24
25
26
|
# File 'lib/regexp_parser/expression/base.rb', line 24
def quantify(*args)
self.quantifier = Quantifier.new(*args)
end
|
#quantity ⇒ Object
Deprecated. Prefer ‘#repetitions` which has a more uniform interface.
33
34
35
36
|
# File 'lib/regexp_parser/expression/base.rb', line 33
def quantity
return [nil,nil] unless quantified?
[quantifier.min, quantifier.max]
end
|
#reluctant? ⇒ Boolean
Also known as:
lazy?
58
59
60
|
# File 'lib/regexp_parser/expression/base.rb', line 58
def reluctant?
quantified? and quantifier.reluctant?
end
|
#repetitions ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/regexp_parser/expression/base.rb', line 38
def repetitions
@repetitions ||=
if quantified?
min = quantifier.min
max = quantifier.max < 0 ? Float::INFINITY : quantifier.max
range = min..max
if RUBY_VERSION.to_f < 2.7
range.define_singleton_method(:minmax) { [min, max] }
end
range
else
1..1
end
end
|
#strfregexp(format = '%a', indent_offset = 0, index = nil) ⇒ Object
Also known as:
strfre
%l Level (depth) of the expression. Returns ‘root’ for the root
expression, returns zero or higher for all others.
%> Indentation at expression's level.
%x Index of the expression at its depth. Available when using
the sprintf_tree method only.
%s Start offset within the whole expression.
%e End offset within the whole expression.
%S Length of expression.
%o Coded offset and length, same as '@%s+%S'
%y Type of expression.
%k Token of expression.
%i ID, same as '%y:%k'
%c Class name
%q Quantifier info, as {m[,M]}
%Q Quantifier text
%z Quantifier min
%Z Quantifier max
%t Base text of the expression (excludes quantifier, if any)
%~t Full text if the expression is terminal, otherwise %i
%T Full text of the expression (includes quantifier, if any)
%b Basic info, same as '%o %i'
%m Most info, same as '%b %q'
%a All info, same as '%m %t'
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/regexp_parser/expression/methods/strfregexp.rb', line 37
def strfregexp(format = '%a', indent_offset = 0, index = nil)
have_index = index ? true : false
part = {}
print_level = nesting_level > 0 ? nesting_level - 1 : nil
part_keys = %w[a m b o i l x s e S y k c q Q z Z t ~t T >]
part.keys.each {|k| part[k] = "<?#{k}?>"}
part['>'] = print_level ? (' ' * (print_level + indent_offset)) : ''
part['l'] = print_level ? "#{'%d' % print_level}" : 'root'
part['x'] = "#{'%d' % index}" if have_index
part['s'] = starts_at
part['S'] = full_length
part['e'] = starts_at + full_length
part['o'] = coded_offset
part['k'] = token
part['y'] = type
part['i'] = '%y:%k'
part['c'] = self.class.name
if quantified?
if quantifier.max == -1
part['q'] = "{#{quantifier.min}, or-more}"
else
part['q'] = "{#{quantifier.min}, #{quantifier.max}}"
end
part['Q'] = quantifier.text
part['z'] = quantifier.min
part['Z'] = quantifier.max
else
part['q'] = '{1}'
part['Q'] = ''
part['z'] = '1'
part['Z'] = '1'
end
part['t'] = to_s(:base)
part['~t'] = terminal? ? to_s : "#{type}:#{token}"
part['T'] = to_s(:full)
part['b'] = '%o %i'
part['m'] = '%b %q'
part['a'] = '%m %t'
out = format.dup
part_keys.each do |k|
out.gsub!(/%#{k}/, part[k].to_s)
end
out
end
|
#to_h ⇒ Object
Also known as:
attributes
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/regexp_parser/expression/base.rb', line 67
def to_h
{
type: type,
token: token,
text: to_s(:base),
starts_at: ts,
length: full_length,
level: level,
set_level: set_level,
conditional_level: conditional_level,
options: options,
quantifier: quantified? ? quantifier.to_h : nil,
}
end
|
#to_re(format = :full) ⇒ Object
16
17
18
19
20
21
22
|
# File 'lib/regexp_parser/expression/base.rb', line 16
def to_re(format = :full)
if set_level > 0
warn "Calling #to_re on character set members is deprecated - "\
"their behavior might not be equivalent outside of the set."
end
::Regexp.new(to_s(format))
end
|
#unicode_classes? ⇒ Boolean
Also known as:
u?
30
31
32
|
# File 'lib/regexp_parser/expression/methods/options.rb', line 30
def unicode_classes?
options[:u] == true
end
|
#unquantified_clone ⇒ Object
28
29
30
|
# File 'lib/regexp_parser/expression/base.rb', line 28
def unquantified_clone
clone.tap { |exp| exp.quantifier = nil }
end
|