Class: Regexp::Expression::Base
- Inherits:
-
Object
- Object
- Regexp::Expression::Base
show all
- Defined in:
- lib/regexp_parser/expression.rb,
lib/regexp_parser/expression/methods/tests.rb,
lib/regexp_parser/expression/methods/strfregexp.rb
Direct Known Subclasses
Anchor::Base, Regexp::Expression::Backreference::Base, CharacterSet, CharacterType::Base, Conditional::Condition, EscapeSequence::Base, FreeSpace, Keep::Mark, Literal, Subexpression, UnicodeProperty::Base
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#ascii_classes? ⇒ Boolean
(also: #a?)
-
#case_insensitive? ⇒ Boolean
(also: #i?, #ignore_case?)
-
#clone ⇒ Object
-
#coded_offset ⇒ Object
-
#default_classes? ⇒ Boolean
(also: #d?)
-
#free_spacing? ⇒ Boolean
(also: #x?, #extended?)
-
#full_length ⇒ Object
-
#greedy? ⇒ Boolean
-
#initialize(token) ⇒ Base
constructor
-
#is?(test_token, test_type = nil) ⇒ Boolean
Test if this expression has the given test_token, and optionally a given test_type.
-
#match(string, offset) ⇒ Object
(also: #=~)
-
#matches?(string) ⇒ Boolean
-
#multiline? ⇒ Boolean
(also: #m?)
-
#offset ⇒ Object
-
#one_of?(scope, top = true) ⇒ Boolean
Test if this expression matches an entry in the given scope spec.
-
#possessive? ⇒ Boolean
-
#quantified? ⇒ Boolean
-
#quantify(token, text, min = nil, max = nil, mode = :greedy) ⇒ Object
-
#quantity ⇒ Object
-
#reluctant? ⇒ Boolean
(also: #lazy?)
-
#starts_at ⇒ Object
-
#strfregexp(format = '%a', indent_offset = 0, index = nil) ⇒ Object
(also: #strfre)
%l Level (depth) of the expression.
-
#terminal? ⇒ Boolean
-
#to_h ⇒ Object
-
#to_re(format = :full) ⇒ Object
-
#to_s(format = :full) ⇒ Object
-
#type?(test_type) ⇒ Boolean
Test if this expression has the given test_type, which can be either a symbol or an array of symbols to check against the expression’s type.
-
#unicode_classes? ⇒ Boolean
(also: #u?)
Constructor Details
#initialize(token) ⇒ Base
Returns a new instance of Base.
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/regexp_parser/expression.rb', line 11
def initialize(token)
@type = token.type
@token = token.token
@text = token.text
@ts = token.ts
@level = token.level
@set_level = token.set_level
@conditional_level = token.conditional_level
@options = nil
end
|
Instance Attribute Details
#conditional_level ⇒ Object
Returns the value of attribute conditional_level.
6
7
8
|
# File 'lib/regexp_parser/expression.rb', line 6
def conditional_level
@conditional_level
end
|
#level ⇒ Object
Returns the value of attribute level.
6
7
8
|
# File 'lib/regexp_parser/expression.rb', line 6
def level
@level
end
|
#options ⇒ Object
Returns the value of attribute options.
9
10
11
|
# File 'lib/regexp_parser/expression.rb', line 9
def options
@options
end
|
#quantifier ⇒ Object
Returns the value of attribute quantifier.
8
9
10
|
# File 'lib/regexp_parser/expression.rb', line 8
def quantifier
@quantifier
end
|
#set_level ⇒ Object
Returns the value of attribute set_level.
6
7
8
|
# File 'lib/regexp_parser/expression.rb', line 6
def set_level
@set_level
end
|
#text ⇒ Object
Returns the value of attribute text.
5
6
7
|
# File 'lib/regexp_parser/expression.rb', line 5
def text
@text
end
|
#token ⇒ Object
Returns the value of attribute token.
4
5
6
|
# File 'lib/regexp_parser/expression.rb', line 4
def token
@token
end
|
#ts ⇒ Object
Returns the value of attribute ts.
5
6
7
|
# File 'lib/regexp_parser/expression.rb', line 5
def ts
@ts
end
|
#type ⇒ Object
Returns the value of attribute type.
4
5
6
|
# File 'lib/regexp_parser/expression.rb', line 4
def type
@type
end
|
Instance Method Details
#ascii_classes? ⇒ Boolean
Also known as:
a?
119
120
121
|
# File 'lib/regexp_parser/expression.rb', line 119
def ascii_classes?
(@options and @options[:a]) ? true : false
end
|
#case_insensitive? ⇒ Boolean
Also known as:
i?, ignore_case?
101
102
103
|
# File 'lib/regexp_parser/expression.rb', line 101
def case_insensitive?
(@options and @options[:i]) ? true : false
end
|
#clone ⇒ Object
22
23
24
25
26
27
28
29
30
|
# File 'lib/regexp_parser/expression.rb', line 22
def clone
copy = self.dup
copy.text = (self.text ? self.text.dup : nil)
copy.options = (self.options ? self.options.dup : nil)
copy.quantifier = (self.quantifier ? self.quantifier.clone : nil)
copy
end
|
#coded_offset ⇒ Object
48
49
50
|
# File 'lib/regexp_parser/expression.rb', line 48
def coded_offset
'@%d+%d' % offset
end
|
#default_classes? ⇒ Boolean
Also known as:
d?
114
115
116
|
# File 'lib/regexp_parser/expression.rb', line 114
def default_classes?
(@options and @options[:d]) ? true : false
end
|
#free_spacing? ⇒ Boolean
Also known as:
x?, extended?
107
108
109
|
# File 'lib/regexp_parser/expression.rb', line 107
def free_spacing?
(@options and @options[:x]) ? true : false
end
|
#full_length ⇒ Object
40
41
42
|
# File 'lib/regexp_parser/expression.rb', line 40
def full_length
to_s.length
end
|
#greedy? ⇒ Boolean
83
84
85
|
# File 'lib/regexp_parser/expression.rb', line 83
def greedy?
quantified? and @quantifier.mode == :greedy
end
|
#is?(test_token, test_type = nil) ⇒ Boolean
Test if this expression has the given test_token, and optionally a given test_type.
exp.is? :*
exp.is? :capture
exp.is? :character, :set
exp.is? :dot, :meta
exp.is? :dot, [:meta, :escape]
46
47
48
49
|
# File 'lib/regexp_parser/expression/methods/tests.rb', line 46
def is?(test_token, test_type = nil)
return true if test_token === :*
token == test_token and (test_type ? type?(test_type) : true)
end
|
#match(string, offset) ⇒ Object
Also known as:
=~
134
135
136
|
# File 'lib/regexp_parser/expression.rb', line 134
def match(string, offset)
Regexp.new(to_s).match(string, offset)
end
|
#matches?(string) ⇒ Boolean
130
131
132
|
# File 'lib/regexp_parser/expression.rb', line 130
def matches?(string)
Regexp.new(to_s) =~ string ? true : false
end
|
#multiline? ⇒ Boolean
Also known as:
m?
96
97
98
|
# File 'lib/regexp_parser/expression.rb', line 96
def multiline?
(@options and @options[:m]) ? true : false
end
|
#offset ⇒ Object
44
45
46
|
# File 'lib/regexp_parser/expression.rb', line 44
def offset
[starts_at, full_length]
end
|
#one_of?(scope, top = true) ⇒ Boolean
Test if this expression matches an entry in the given scope spec.
A scope spec can be one of:
. An array: Interpreted as a set of tokens, tested for inclusion
of the expression's token.
. A hash: Where the key is interpreted as the expression type
and the value is either a symbol or an array. In this
case, when the scope is a hash, one_of? calls itself to
evaluate the key's value.
. A symbol: matches the expression's token or type, depending on
the level of the call. If one_of? is called directly with
a symbol then it will always be checked against the
type of the expression. If it's being called for a value
from a hash, it will be checked against the token of the
expression.
# any expression
exp.one_of?(:*) # always true
# like exp.type?(:group)
exp.one_of?(:group)
# any expression of type meta
exp.one_of?(:meta => :*)
# meta dots and alternations
exp.one_of?(:meta => [:dot, :alternation])
# meta dots and any set tokens
exp.one_of?({meta: [:dot], set: :*})
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/regexp_parser/expression/methods/tests.rb', line 85
def one_of?(scope, top = true)
case scope
when Array
if scope.include?(:*)
return (scope.include?(token) or scope.include?(:*))
else
return scope.include?(token)
end
when Hash
if scope.has_key?(:*)
test_type = scope.has_key?(type) ? type : :*
return one_of?(scope[test_type], false)
else
return (scope.has_key?(type) and one_of?(scope[type], false))
end
when Symbol
return true if scope == :*
return is?(scope) unless top
return type?(scope) if top
else
raise "Array, Hash, or Symbol expected, #{scope.class.name} given"
end
false
end
|
#possessive? ⇒ Boolean
92
93
94
|
# File 'lib/regexp_parser/expression.rb', line 92
def possessive?
quantified? and @quantifier.mode == :possessive
end
|
#quantified? ⇒ Boolean
74
75
76
|
# File 'lib/regexp_parser/expression.rb', line 74
def quantified?
not @quantifier.nil?
end
|
#quantify(token, text, min = nil, max = nil, mode = :greedy) ⇒ Object
70
71
72
|
# File 'lib/regexp_parser/expression.rb', line 70
def quantify(token, text, min = nil, max = nil, mode = :greedy)
@quantifier = Quantifier.new(token, text, min, max, mode)
end
|
#quantity ⇒ Object
78
79
80
81
|
# File 'lib/regexp_parser/expression.rb', line 78
def quantity
return [nil,nil] unless quantified?
[@quantifier.min, @quantifier.max]
end
|
#reluctant? ⇒ Boolean
Also known as:
lazy?
87
88
89
|
# File 'lib/regexp_parser/expression.rb', line 87
def reluctant?
quantified? and @quantifier.mode == :reluctant
end
|
#starts_at ⇒ Object
36
37
38
|
# File 'lib/regexp_parser/expression.rb', line 36
def starts_at
@ts
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'
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
|
# File 'lib/regexp_parser/expression/methods/strfregexp.rb', line 38
def strfregexp(format = '%a', indent_offset = 0, index = nil)
have_index = index ? true : false
part = {}
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['>'] = level ? (' ' * (level + indent_offset)) : ''
part['l'] = level ? "#{'%d' % 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
|
#terminal? ⇒ Boolean
66
67
68
|
# File 'lib/regexp_parser/expression.rb', line 66
def terminal?
!respond_to?(:expressions)
end
|
#to_h ⇒ Object
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/regexp_parser/expression.rb', line 139
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
32
33
34
|
# File 'lib/regexp_parser/expression.rb', line 32
def to_re(format = :full)
::Regexp.new(to_s(format))
end
|
#to_s(format = :full) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/regexp_parser/expression.rb', line 52
def to_s(format = :full)
s = ''
case format
when :base
s << @text.dup
else
s << @text.dup
s << @quantifier if quantified?
end
s
end
|
#type?(test_type) ⇒ Boolean
Test if this expression has the given test_type, which can be either a symbol or an array of symbols to check against the expression’s type.
exp.type? :group
exp.type? [:set, :subset, :meta]
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/regexp_parser/expression/methods/tests.rb', line 13
def type?(test_type)
case test_type
when Array
if test_type.include?(:*)
return (test_type.include?(type) or test_type.include?(:*))
else
return test_type.include?(type)
end
when Symbol
return (type == test_type or test_type == :*)
else
raise "Array or Symbol expected, #{test_type.class.name} given"
end
end
|
#unicode_classes? ⇒ Boolean
Also known as:
u?
124
125
126
|
# File 'lib/regexp_parser/expression.rb', line 124
def unicode_classes?
(@options and @options[:u]) ? true : false
end
|