Class: VCDOM::XPath::Internal::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/vcdom/xpath/internal/parser.rb

Overview

:nodoc: all

Defined Under Namespace

Classes: XPathStringScanner

Constant Summary collapse

XML_Q_NAME =
/#{xml_q_name}/u
XPATH_TOKEN_REGEXP =
/#{xpath_token}/u
XPATH_WHITE_SPACE_REGEXP =
/[\x20\x0A\x0D\x09]+/u
XPATH_PATH_EXPR_STARTER_REGEXP =
/#{xpath_number}|#{xpath_literal}|#{xpath_anyname}|\/\/?|\.\.?|@|\(/u
XPATH_VARIABLE_REFERENCE_REGEXP =
/\$#{xml_q_name}/u
XPATH_NUMBER_REGEXP =
/#{xpath_number}/u
XPATH_LITERAL_REGEXP =
/#{xpath_literal}/u
XPATH_NAME_TEST =
/(?:#{xml_nc_name}:)?\*|#{xml_q_name}/u
XPATH_STEP_STARTER_REGEXP =
/\*|\.\.?|@|#{xml_q_name}/u
AXIS_NAME_LIST =
[ :'ancestor', :'ancestor-or-self', :'attribute', :'child', :'descendant', :'descendant-or-self', 
:'following', :'following-sibling', :'namespace', :'parent', :'preceding', :'preceding-sibling', :'self' ]
NODE_TYPE_LIST =
[ :'comment', :'text', :'processing-instruction', :'node' ]

Instance Method Summary collapse

Constructor Details

#initialize(xpath_str, ns_resolver) ⇒ Parser

Returns a new instance of Parser.



81
82
83
84
# File 'lib/vcdom/xpath/internal/parser.rb', line 81

def initialize( xpath_str, ns_resolver )
  @scanner = XPathStringScanner.new( xpath_str )
  @ns_resolver = ns_resolver
end

Instance Method Details

#parseObject



86
87
88
89
90
# File 'lib/vcdom/xpath/internal/parser.rb', line 86

def parse()
  expr = parse_expr()
  raise "invalid xpath [#{@scanner.rest}]" unless @scanner.eos?
  expr
end

#parse_additive_expr(expr) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/vcdom/xpath/internal/parser.rb', line 140

def parse_additive_expr( expr )
  parse_multiplicative_expr( expr )
  while token = @scanner.scan( /\+|\-/u ) do
    parse_multiplicative_expr( expr )
    expr << VCDOM::XPath::Internal.get_operation_command( token.intern )
  end
end

#parse_and_exprObject



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/vcdom/xpath/internal/parser.rb', line 109

def parse_and_expr()
  expr = parse_equality_expr()
  if token = @scanner.check( /and/u ) then
    tmp = expr
    expr = AndExpr.new()
    expr << tmp
    while @scanner.scan( /and/u ) do
      expr << parse_equality_expr()
    end
  end
  expr
end

#parse_equality_exprObject



122
123
124
125
126
127
128
129
130
# File 'lib/vcdom/xpath/internal/parser.rb', line 122

def parse_equality_expr()
  expr = VCDOM::XPath::Internal::EqualityExpr.new()
  parse_relational_expr( expr )
  while token = @scanner.scan( /=|\!=/u ) do
    parse_relational_expr( expr )
    expr << VCDOM::XPath::Internal.get_operation_command( token.intern )
  end
  expr
end

#parse_exprObject



92
93
94
# File 'lib/vcdom/xpath/internal/parser.rb', line 92

def parse_expr()
  parse_or_expr()
end

#parse_expr_listObject



314
315
316
317
318
319
320
321
# File 'lib/vcdom/xpath/internal/parser.rb', line 314

def parse_expr_list()
  exprs = Array.new()
  exprs << parse_expr()
  while @scanner.scan( /,/u ) do
    exprs << parse_expr()
  end
  exprs
end

#parse_multiplicative_expr(expr) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/vcdom/xpath/internal/parser.rb', line 148

def parse_multiplicative_expr( expr )
  parse_unary_expr( expr )
  while token = @scanner.scan( /\*|div|mod/u ) do
    parse_unary_expr( expr )
    expr << VCDOM::XPath::Internal.get_operation_command( token.intern )
  end
end

#parse_or_exprObject



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/vcdom/xpath/internal/parser.rb', line 96

def parse_or_expr()
  expr = parse_and_expr()
  if @scanner.check( /or/u ) then
    tmp = expr
    expr = OrExpr.new()
    expr << tmp
    while @scanner.scan( /or/u ) do
      expr << parse_and_expr()
    end
  end
  expr
end

#parse_path_expr(expr) ⇒ Object

VariableReference ‘(’ Expr ‘)’ Literal Number FunctionCall



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/vcdom/xpath/internal/parser.rb', line 186

def parse_path_expr( expr )
  is_path = false
  if token = @scanner.scan( XPATH_NUMBER_REGEXP ) then
    # Number の場合
    expr << VCDOM::XPath::Internal::NumberValue.new( token.to_f() )
  elsif token = @scanner.scan( XPATH_LITERAL_REGEXP) then
    # Literal の場合
    token[0,1]  = ""
    token[-1,1] = ""
    expr << VCDOM::XPath::Internal::StringValue.new( token )
  elsif token = @scanner.scan( XPATH_VARIABLE_REFERENCE_REGEXP ) then
    # VariableReference の場合
    raise "VariableReference is not supported"
  elsif @scanner.scan( /\(/u ) then
    # "(" Expr ")" の場合
    expr << VCDOM::XPath::Internal::ExprEvalCommand.new( parse_expr() )
    raise "invalid xpath expression" unless @scanner.scan( /\)/u )
  else
    # Path or FunctionCall
    pos = @scanner.pos
    is_path = true
    if token = @scanner.scan( XML_Q_NAME ) then
      # FunctionName or AxisName or NameTest or NodeType
      if @scanner.scan( /\(/u ) then
        # FunctionName or NodeType
        case token
        when "comment", "text", "processing-instruction", "node"
          # do nothing
        else
          # FunctionName
          is_path = false
        end
      end
    end
    if is_path then
      # Path
      @scanner.pos = pos
      if token = @scanner.scan( /\/\/?/u ) then
        # "/" or "//"
        expr << VCDOM::XPath::Internal::RootNodeCommand.get_instance()
        if token == "/" then
          # 続く要素がなければここで終了
          return unless @scanner.check( XPATH_STEP_STARTER_REGEXP )
        else # "//"
          expr << VCDOM::XPath::Internal::NodeSelectionCommand.new( :"descendant-or-self", :node, nil, nil, nil )
        end
      else
        expr << VCDOM::XPath::Internal::ContextNodeCommand.get_instance()
      end
      expr << parse_step()
    else
      # FunctionCall
      function_name = token.intern
      arg_exprs = @scanner.check( /\)/u ) ? nil : parse_expr_list()
      raise "invalid xpath [#{@scanner.rest}]" unless @scanner.scan( /\)/u )
      expr << FunctionCallCommand.new( function_name, arg_exprs )
    end
  end
  if not is_path then
    if @scanner.check( /\[/u ) then
      pred_exprs = parse_predicates()
      expr << PredsEvalCommand.new( pred_exprs )
    end
  end
  while token = @scanner.scan( /\/\/?/ ) do
    if token == "//" then
      expr << VCDOM::XPath::Internal::NodeSelectionCommand.new( :"descendant-or-self", :node, nil, nil, nil )
    end
    expr << parse_step()
  end
end

#parse_predicatesObject



323
324
325
326
327
328
329
330
# File 'lib/vcdom/xpath/internal/parser.rb', line 323

def parse_predicates()
  exprs = Array.new()
  while @scanner.scan( /\[/u ) do
    exprs << parse_expr()
    raise "invalid xpath expression" unless @scanner.scan( /\]/u )
  end
  exprs
end

#parse_relational_expr(expr) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/vcdom/xpath/internal/parser.rb', line 132

def parse_relational_expr( expr )
  parse_additive_expr( expr )
  while token = @scanner.scan( /\<=?|\>=?/u ) do
    parse_additive_expr( expr )
    expr << VCDOM::XPath::Internal.get_operation_command( token.intern )
  end
end

#parse_stepObject



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/vcdom/xpath/internal/parser.rb', line 261

def parse_step()
  # "." or ".."
  if @scanner.scan( /\.\./u ) then
    return VCDOM::XPath::Internal::NodeSelectionCommand.new( :parent, :node, nil, nil, nil )
  elsif token = @scanner.scan( /\./u ) then
    return VCDOM::XPath::Internal::NodeSelectionCommand.new( :self, :node, nil, nil, nil )
  end
  # AxisName
  pos = @scanner.pos
  if @scanner.scan( /@/u ) then
    axis_name = :attribute
  elsif token = @scanner.scan( XML_Q_NAME ) then
    if @scanner.scan( /::/u ) then
      axis_name = token.intern
      raise "invalid AxisName [#{axis_name}]" unless AXIS_NAME_LIST.include? axis_name
    else
      axis_name = :child
      @scanner.pos = pos
    end
  else
    axis_name = :child
  end
  # NameTest or NodeType
  if token = @scanner.scan( XPATH_NAME_TEST ) then
    if @scanner.scan( /\(/u ) then
      # NodeType
      node_type = token.intern
      raise "invalid NodeType [#{node_type}]" unless NODE_TYPE_LIST.include? node_type
      node_name = ( node_type == :"processing-instruction" and token = @scanner.scan( XPATH_LITERAL ) ) ? token.intern : nil
      node_ns_uri = nil
      raise "invalid xpath expression" unless @scanner.scan( /\)/u )
    else
      # NameTest
      node_type = :named_node
      name_pair = token.split( /:/u )
      if name_pair.length == 2 then
        node_name = ( name_pair[1] == "*" ) ? nil : name_pair[1].intern
        node_ns_uri = @ns_resolver.lookup_namespace_uri( name_pair[0] )
        node_ns_uri.nil? or node_ns_uri = node_ns_uri.intern
      else
        # length == 1
        node_name = ( name_pair[0] == "*" ) ? nil : name_pair[0].intern
        node_ns_uri = nil
      end
    end
  else
    raise "invalid xpath expression [#{@scanner.rest}]"
  end
  # Predicate
  pred_exprs = @scanner.check( /\[/u ) ? parse_predicates() : nil
  VCDOM::XPath::Internal::NodeSelectionCommand.new( axis_name, node_type, node_ns_uri, node_name, pred_exprs )
end

#parse_unary_expr(expr) ⇒ Object



156
157
158
159
160
161
162
163
164
165
# File 'lib/vcdom/xpath/internal/parser.rb', line 156

def parse_unary_expr( expr )
  num = 0
  while @scanner.scan( /\-/u ) do
    num += 1
  end
  parse_union_expr( expr )
  num.times do
    expr << VCDOM::XPath::Internal.get_operation_command( :"-@" )
  end
end

#parse_union_expr(expr) ⇒ Object



167
168
169
170
171
172
173
# File 'lib/vcdom/xpath/internal/parser.rb', line 167

def parse_union_expr( expr )
  parse_path_expr( expr )
  while @scanner.scan( /\|/u ) do
    parse_path_expr( expr )
    expr << VCDOM::XPath::Internal.get_operation_command( :"|" )
  end
end