Class: TorqueBox::VFS::GlobTranslator

Inherits:
Object
  • Object
show all
Defined in:
lib/torquebox/vfs/glob_translator.rb

Constant Summary collapse

ESCAPED_CHARS =
[ '.', '?', '(', ')', '[', ']' ]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ GlobTranslator

Returns a new instance of GlobTranslator.



24
25
26
27
28
# File 'lib/torquebox/vfs/glob_translator.rb', line 24

def initialize(input)
  @input      = input
  @cur        = 0
  @alternation_depth = 0
end

Class Method Details

.to_regexp(glob_str) ⇒ Object



38
39
40
41
42
# File 'lib/torquebox/vfs/glob_translator.rb', line 38

def self.to_regexp(glob_str)
  regexp_str = translate( glob_str )
  ##puts "regexp_str: #{regexp_str}"
  Regexp.new( regexp_str )
end

.translate(glob_str) ⇒ Object



31
32
33
34
35
36
# File 'lib/torquebox/vfs/glob_translator.rb', line 31

def self.translate(glob_str)
  translator = GlobTranslator.new( glob_str )
  regexp_str = translator.glob()
  # puts "#{glob_str} ==> #{regexp_str}"
  regexp_str
end

Instance Method Details

#alternationObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/torquebox/vfs/glob_translator.rb', line 108

def alternation()
  #puts "enter alternation()"
  @alternation_depth += 1
  
  result = '('
  
  match_empty = false
  
  consume('{')
  if ( la() == ',' )
    consume(',')
    match_empty = true
  end
  
  result += '(' + glob() +')' 
  
  while ( la() == ',' )
    consume(',')
    if ( la() == '}' )
      match_empty = true
      break
    end
    result += '|(' + glob() + ')'
  end
  
  if ( match_empty )
    result += '|'
  end
  result += ')'
  consume( '}' )
  @alternation_depth -= 1
  #puts "exit alternation()"
  result
end

#charObject



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/torquebox/vfs/glob_translator.rb', line 156

def char()
  #puts "enter char()"
  c = consume() 
  if ( c == '\\' )
    c = consume()
  end
  if ( ESCAPED_CHARS.include?( c  ) )
    c = '\\' + c
  end
  #puts "exit char() #{c}"
  c
end

#char_classObject



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/torquebox/vfs/glob_translator.rb', line 143

def char_class()
  consume( '[' )
  result = '['

  while( la() != ']' )
    result += consume();
  end

  consume( ']' )
  result += ']'
  result
end

#double_splatObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/torquebox/vfs/glob_translator.rb', line 86

def double_splat()
  #puts "enter double_splat()"
  if la(2) == '/'
  	# result = '([^.][^/]+/){0,}'
  	result = '([^/.][^/]*/)*'
  else
  	result = '([^/.][^/]*)'
  end
  consume('*')
  consume('*')
  consume('/') if ( la() == '/' )
  #puts "exit double_splat()"
  result
end

#globObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/torquebox/vfs/glob_translator.rb', line 44

def glob()
  result = ''
  while ( ! complete? )
    c = la()
    case( c )
    when '*'
      result += splat()
    when '?'
      result += question()
    when '{'
      result += alternation()
    when '['
      result += char_class()
    else
      if ( la_is_char? )
        result += char();
      else
        break
      end
    end
  end
  result
end

#la_is_char?Boolean

Returns:

  • (Boolean)


169
170
171
172
173
174
175
176
177
178
179
# File 'lib/torquebox/vfs/glob_translator.rb', line 169

def la_is_char?()
  ( return true ) if ( @alternation_depth == 0 )
  case ( la() )
  when '}'
    return false
  when ','
    return false
  else
    return true
  end
end

#questionObject



101
102
103
104
105
106
# File 'lib/torquebox/vfs/glob_translator.rb', line 101

def question()
  #puts "enter question()"
  consume('?')
  #puts "exit question()"
  '[^/]'
end

#splatObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/torquebox/vfs/glob_translator.rb', line 68

def splat()
  #puts "enter splat()"
  if ( la(1) == '*' )
    result = double_splat()
  else
    if ( cur() == 0 || lb() == '/' )
      result = '((?<=/|^)[^.][^/]*)*'
      # result = '[^/.][^/]*'
    else
      result = '[^/]*'
    end
    
    consume('*')
  end
  #puts "exit splat()"
  result
end