Class: Regexp::Expression::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/regexp_parser/expression.rb,
lib/regexp_parser/expression/methods/tests.rb,
lib/regexp_parser/expression/methods/strfregexp.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_levelObject

Returns the value of attribute conditional_level.



6
7
8
# File 'lib/regexp_parser/expression.rb', line 6

def conditional_level
  @conditional_level
end

#levelObject

Returns the value of attribute level.



6
7
8
# File 'lib/regexp_parser/expression.rb', line 6

def level
  @level
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/regexp_parser/expression.rb', line 9

def options
  @options
end

#quantifierObject

Returns the value of attribute quantifier.



8
9
10
# File 'lib/regexp_parser/expression.rb', line 8

def quantifier
  @quantifier
end

#set_levelObject

Returns the value of attribute set_level.



6
7
8
# File 'lib/regexp_parser/expression.rb', line 6

def set_level
  @set_level
end

#textObject

Returns the value of attribute text.



5
6
7
# File 'lib/regexp_parser/expression.rb', line 5

def text
  @text
end

#tokenObject

Returns the value of attribute token.



4
5
6
# File 'lib/regexp_parser/expression.rb', line 4

def token
  @token
end

#tsObject

Returns the value of attribute ts.



5
6
7
# File 'lib/regexp_parser/expression.rb', line 5

def ts
  @ts
end

#typeObject

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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


101
102
103
# File 'lib/regexp_parser/expression.rb', line 101

def case_insensitive?
  (@options and @options[:i]) ? true : false
end

#cloneObject



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_offsetObject



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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


107
108
109
# File 'lib/regexp_parser/expression.rb', line 107

def free_spacing?
  (@options and @options[:x]) ? true : false
end

#full_lengthObject



40
41
42
# File 'lib/regexp_parser/expression.rb', line 40

def full_length
  to_s.length
end

#greedy?Boolean

Returns:

  • (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.

# Any expressions
exp.is? :*  # always returns true

# is it a :capture
exp.is? :capture

# is it a :character and a :set
exp.is? :character, :set

# is it a :meta :dot
exp.is? :dot, :meta

# is it a :meta or :escape :dot
exp.is? :dot, [:meta, :escape]

Returns:

  • (Boolean)


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

Returns:

  • (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?

Returns:

  • (Boolean)


96
97
98
# File 'lib/regexp_parser/expression.rb', line 96

def multiline?
  (@options and @options[:m]) ? true : false
end

#offsetObject



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: :*})

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


92
93
94
# File 'lib/regexp_parser/expression.rb', line 92

def possessive?
  quantified? and @quantifier.mode == :possessive
end

#quantified?Boolean

Returns:

  • (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

#quantityObject



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?

Returns:

  • (Boolean)


87
88
89
# File 'lib/regexp_parser/expression.rb', line 87

def reluctant?
  quantified? and @quantifier.mode == :reluctant
end

#starts_atObject



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 = {}

  # Order is important! Fields that use other fields in their
  # definition must appear before the fields they use.
  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

Returns:

  • (Boolean)


66
67
68
# File 'lib/regexp_parser/expression.rb', line 66

def terminal?
  !respond_to?(:expressions)
end

#to_hObject



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.

# is it a :group expression
exp.type? :group

# is it a :set, :subset, or :meta
exp.type? [:set, :subset, :meta]

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


124
125
126
# File 'lib/regexp_parser/expression.rb', line 124

def unicode_classes?
  (@options and @options[:u]) ? true : false
end