Method: Rubyang::Xpath::Parser#parse

Defined in:
lib/rubyang/xpath/parser.rb

#parse(xpath_str) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rubyang/xpath/parser.rb', line 20

def parse( xpath_str )
  @tokens = Array.new

  s = StringScanner.new( xpath_str )

  scanre_list = [
    ["(",                      /^\(/],
    [")",                      /^\)/],
    ["[",                      /^\[/],
    ["]",                      /^\]/],
    ["..",                     /^\.\./],
    [".",                      /^\./],
    ["@",                      /^\@/],
    [",",                      /^\,/],
    ["::",                     /^\:\:/],
    ["Literal",                /^(?:"[^"]*"|'[^']*')/],
    ["Digits",                 /^[0-9]+/],
    ["//",                     /^\/\//],
    ["/",                      /^\//],
    ["|",                      /^\|/],
    ["+",                      /^\+/],
    ["-",                      /^\-/],
    ["!=",                     /^\!\=/],
    ["<=",                     /^\<\=/],
    [">=",                     /^\>\=/],
    ["=",                      /^\=/],
    ["<",                      /^\</],
    [">",                      /^\>/],
    ["and",                    /^and/],
    ["or",                     /^or/],
    ["mod",                    /^mod/],
    ["div",                    /^div/],
    ["$",                      /^\$/],
    ["*",                      /^\*/],
    [":",                      /^\:/],
    ["S",                      /^[ \t]+/],
    # NodeType
    ["comment",                /^comment/],
    ["text",                   /^text/],
    ["node",                   /^node/],
    # ProcessingInstruction
    ["processing-instruction", /^processing-instruction/],
    # Axis
    ["ancestor",               /^ancestor/],
    ["ancestor-or-self",       /^ancestor-or-self/],
    ["attribute",              /^attribute/],
    ["child",                  /^child/],
    ["descendant",             /^descendant/],
    ["descendant-or-self",     /^descendant-or-self/],
    ["following",              /^following/],
    ["following-sibling",      /^following-sibling/],
    ["namespace",              /^namespace/],
    ["parent",                 /^parent/],
    ["preceding",              /^preceding/],
    ["preceding-sibling",      /^preceding-sibling/],
    ["self",                   /^self/],
    # FunctionName
    ["last(",                  /^last[ \t]*\(/],
    ["position(",              /^position[ \t]*\(/],
    ["count(",                 /^count[ \t]*\(/],
    ["id(",                    /^id[ \t]*\(/],
    ["local-name(",            /^local-name[ \t]*\(/],
    ["namespace-uri(",         /^namespace-uri[ \t]*\(/],
    ["name(",                  /^name[ \t]*\(/],
    ["string(",                /^string[ \t]*\(/],
    ["concat(",                /^concat[ \t]*\(/],
    ["starts-with(",           /^starts-with[ \t]*\(/],
    ["contains(",              /^contains[ \t]*\(/],
    ["substring-before(",      /^substring-before[ \t]*\(/],
    ["substring-after(",       /^substring-after[ \t]*\(/],
    ["substring(",             /^substring[ \t]*\(/],
    ["string-length(",         /^string-length[ \t]*\(/],
    ["normalize-space(",       /^normalize-space[ \t]*\(/],
    ["translate(",             /^translate[ \t]*\(/],
    ["boolean(",               /^boolean[ \t]*\(/],
    ["not(",                   /^not[ \t]*\(/],
    ["true(",                  /^true[ \t]*\(/],
    ["false(",                 /^false[ \t]*\(/],
    ["lang(",                  /^lang[ \t]*\(/],
    ["number(",                /^number[ \t]*\(/],
    ["sum(",                   /^sum[ \t]*\(/],
    ["floor(",                 /^floor[ \t]*\(/],
    ["ceiling(",               /^ceiling[ \t]*\(/],
    ["round(",                 /^round[ \t]*\(/],
    ["current(",               /^current[ \t]*\(/],
    # NCName
    ["NCName",                 /^(?:[A-Z]|\_|[a-z])(?:[A-Z]|\_|[a-z]|\-|\.|[0-9])*/],
  ]

  scanre = Regexp.union( scanre_list.map{ |scanre| scanre[1] } )

  until s.eos?
    token = s.scan( scanre )
    @logger.debug { token }
    next if "S" == scanre_list.find{ |s| s[1] =~ token }[0]
    @tokens.push [scanre_list.find{ |s| s[1] =~ token }[0], token]
    @logger.debug { @tokens.last }
  end

  @logger.debug { 'run do_parse' }
  result = self.do_parse
  result
end