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
21
# 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
  @quantifier         = nil
  @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)


120
121
122
# File 'lib/regexp_parser/expression.rb', line 120

def ascii_classes?
  (@options and @options[:a]) ? true : false
end

#case_insensitive?Boolean Also known as: i?, ignore_case?

Returns:

  • (Boolean)


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

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

#cloneObject



23
24
25
26
27
28
29
30
31
# File 'lib/regexp_parser/expression.rb', line 23

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



49
50
51
# File 'lib/regexp_parser/expression.rb', line 49

def coded_offset
  '@%d+%d' % offset
end

#default_classes?Boolean Also known as: d?

Returns:

  • (Boolean)


115
116
117
# File 'lib/regexp_parser/expression.rb', line 115

def default_classes?
  (@options and @options[:d]) ? true : false
end

#free_spacing?Boolean Also known as: x?, extended?

Returns:

  • (Boolean)


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

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

#full_lengthObject



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

def full_length
  to_s.length
end

#greedy?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/regexp_parser/expression.rb', line 84

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: =~



135
136
137
# File 'lib/regexp_parser/expression.rb', line 135

def match(string, offset)
  Regexp.new(to_s).match(string, offset)
end

#matches?(string) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/regexp_parser/expression.rb', line 131

def matches?(string)
  Regexp.new(to_s) =~ string ? true : false
end

#multiline?Boolean Also known as: m?

Returns:

  • (Boolean)


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

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

#offsetObject



45
46
47
# File 'lib/regexp_parser/expression.rb', line 45

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)


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

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

#quantified?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/regexp_parser/expression.rb', line 75

def quantified?
  not @quantifier.nil?
end

#quantify(token, text, min = nil, max = nil, mode = :greedy) ⇒ Object



71
72
73
# File 'lib/regexp_parser/expression.rb', line 71

def quantify(token, text, min = nil, max = nil, mode = :greedy)
  @quantifier = Quantifier.new(token, text, min, max, mode)
end

#quantityObject



79
80
81
82
# File 'lib/regexp_parser/expression.rb', line 79

def quantity
  return [nil,nil] unless quantified?
  [@quantifier.min, @quantifier.max]
end

#reluctant?Boolean Also known as: lazy?

Returns:

  • (Boolean)


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

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

#starts_atObject



37
38
39
# File 'lib/regexp_parser/expression.rb', line 37

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)


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

def terminal?
  !respond_to?(:expressions)
end

#to_hObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/regexp_parser/expression.rb', line 140

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



33
34
35
# File 'lib/regexp_parser/expression.rb', line 33

def to_re(format = :full)
  ::Regexp.new(to_s(format))
end

#to_s(format = :full) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/regexp_parser/expression.rb', line 53

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)


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

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