Debian Codegraph @

parse.go

raw
1 package xpath
2
3 import (
4 "bytes"
5 "errors"
6 "fmt"
7 "strconv"
8 "unicode"
9 "unicode/utf8"
10 )
11
12 // A XPath expression token type.
13 type itemType int
14
15 const (
16 itemComma itemType = iota // ','
17 itemSlash // '/'
18 itemAt // '@'
19 itemDot // '.'
20 itemLParens // '('
21 itemRParens // ')'
22 itemLBracket // '['
23 itemRBracket // ']'
24 itemStar // '*'
25 itemPlus // '+'
26 itemMinus // '-'
27 itemEq // '='
28 itemLt // '<'
29 itemGt // '>'
30 itemBang // '!'
31 itemDollar // '$'
32 itemApos // '\''
33 itemQuote // '"'
34 itemUnion // '|'
35 itemNe // '!='
36 itemLe // '<='
37 itemGe // '>='
38 itemAnd // '&&'
39 itemOr // '||'
40 itemDotDot // '..'
41 itemSlashSlash // '//'
42 itemName // XML Name
43 itemString // Quoted string constant
44 itemNumber // Number constant
45 itemAxe // Axe (like child::)
46 itemEOF // END
47 )
48
49 // A node is an XPath node in the parse tree.
50 type node interface {
51 Type() nodeType
52 }
53
54 // nodeType identifies the type of a parse tree node.
55 type nodeType int
56
57 func (t nodeType) Type() nodeType {
58 return t
59 }
60
61 const (
62 nodeRoot nodeType = iota
63 nodeAxis
64 nodeFilter
65 nodeFunction
66 nodeOperator
67 nodeVariable
68 nodeConstantOperand
69 nodeGroup
70 )
71
72 type parser struct {
73 r *scanner
74 d int
75 namespaces map[string]string
76 }
77
78 // newOperatorNode returns new operator node OperatorNode.
79 func newOperatorNode(op string, left, right node) node {
80 return &operatorNode{nodeType: nodeOperator, Op: op, Left: left, Right: right}
81 }
82
83 // newOperand returns new constant operand node OperandNode.
84 func newOperandNode(v interface{}) node {
85 return &operandNode{nodeType: nodeConstantOperand, Val: v}
86 }
87
88 // newAxisNode returns new axis node AxisNode.
89 func newAxisNode(axisType string, typeTest NodeType, localName, prefix, prop string, n node, opts ...func(p *axisNode)) node {
90 a := axisNode{
91 nodeType: nodeAxis,
92 typeTest: typeTest,
93 LocalName: localName,
94 Prefix: prefix,
95 AxisType: axisType,
96 Prop: prop,
97 Input: n,
98 }
99 for _, o := range opts {
100 o(&a)
101 }
102 return &a
103 }
104
105 // newVariableNode returns new variable node VariableNode.
106 func newVariableNode(prefix, name string) node {
107 return &variableNode{nodeType: nodeVariable, Name: name, Prefix: prefix}
108 }
109
110 // newFilterNode returns a new filter node FilterNode.
111 func newFilterNode(n, m node) node {
112 return &filterNode{nodeType: nodeFilter, Input: n, Condition: m}
113 }
114
115 func newGroupNode(n node) node {
116 return &groupNode{nodeType: nodeGroup, Input: n}
117 }
118
119 // newRootNode returns a root node.
120 func newRootNode(s string) node {
121 return &rootNode{nodeType: nodeRoot, slash: s}
122 }
123
124 // newFunctionNode returns function call node.
125 func newFunctionNode(name, prefix string, args []node) node {
126 return &functionNode{nodeType: nodeFunction, Prefix: prefix, FuncName: name, Args: args}
127 }
128
129 // testOp reports whether current item name is an operand op.
130 func testOp(r *scanner, op string) bool {
131 return r.typ == itemName && r.prefix == "" && r.name == op
132 }
133
134 func isPrimaryExpr(r *scanner) bool {
135 switch r.typ {
136 case itemString, itemNumber, itemDollar, itemLParens:
137 return true
138 case itemName:
139 return r.canBeFunc && !isNodeType(r)
140 }
141 return false
142 }
143
144 func isNodeType(r *scanner) bool {
145 switch r.name {
146 case "node", "text", "processing-instruction", "comment":
147 return r.prefix == ""
148 }
149 return false
150 }
151
152 func isStep(item itemType) bool {
153 switch item {
154 case itemDot, itemDotDot, itemAt, itemAxe, itemStar, itemName:
155 return true
156 }
157 return false
158 }
159
160 func checkItem(r *scanner, typ itemType) {
161 if r.typ != typ {
162 panic(fmt.Sprintf("%s has an invalid token", r.text))
163 }
164 }
165
166 // parseExpression parsing the expression with input node n.
167 func (p *parser) parseExpression(n node) node {
168 if p.d = p.d + 1; p.d > 200 {
169 panic("the xpath query is too complex(depth > 200)")
170 }
171 n = p.parseOrExpr(n)
172 p.d--
173 return n
174 }
175
176 // next scanning next item on forward.
177 func (p *parser) next() bool {
178 return p.r.nextItem()
179 }
180
181 func (p *parser) skipItem(typ itemType) {
182 checkItem(p.r, typ)
183 p.next()
184 }
185
186 // OrExpr ::= AndExpr | OrExpr 'or' AndExpr
187 func (p *parser) parseOrExpr(n node) node {
188 opnd := p.parseAndExpr(n)
189 for {
190 if !testOp(p.r, "or") {
191 break
192 }
193 p.next()
194 opnd = newOperatorNode("or", opnd, p.parseAndExpr(n))
195 }
196 return opnd
197 }
198
199 // AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
200 func (p *parser) parseAndExpr(n node) node {
201 opnd := p.parseEqualityExpr(n)
202 for {
203 if !testOp(p.r, "and") {
204 break
205 }
206 p.next()
207 opnd = newOperatorNode("and", opnd, p.parseEqualityExpr(n))
208 }
209 return opnd
210 }
211
212 // EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr | EqualityExpr '!=' RelationalExpr
213 func (p *parser) parseEqualityExpr(n node) node {
214 opnd := p.parseRelationalExpr(n)
215 Loop:
216 for {
217 var op string
218 switch p.r.typ {
219 case itemEq:
220 op = "="
221 case itemNe:
222 op = "!="
223 default:
224 break Loop
225 }
226 p.next()
227 opnd = newOperatorNode(op, opnd, p.parseRelationalExpr(n))
228 }
229 return opnd
230 }
231
232 // RelationalExpr ::= AdditiveExpr | RelationalExpr '<' AdditiveExpr | RelationalExpr '>' AdditiveExpr
233 //
234 // | RelationalExpr '<=' AdditiveExpr
235 // | RelationalExpr '>=' AdditiveExpr
236 func (p *parser) parseRelationalExpr(n node) node {
237 opnd := p.parseAdditiveExpr(n)
238 Loop:
239 for {
240 var op string
241 switch p.r.typ {
242 case itemLt:
243 op = "<"
244 case itemGt:
245 op = ">"
246 case itemLe:
247 op = "<="
248 case itemGe:
249 op = ">="
250 default:
251 break Loop
252 }
253 p.next()
254 opnd = newOperatorNode(op, opnd, p.parseAdditiveExpr(n))
255 }
256 return opnd
257 }
258
259 // AdditiveExpr ::= MultiplicativeExpr | AdditiveExpr '+' MultiplicativeExpr | AdditiveExpr '-' MultiplicativeExpr
260 func (p *parser) parseAdditiveExpr(n node) node {
261 opnd := p.parseMultiplicativeExpr(n)
262 Loop:
263 for {
264 var op string
265 switch p.r.typ {
266 case itemPlus:
267 op = "+"
268 case itemMinus:
269 op = "-"
270 default:
271 break Loop
272 }
273 p.next()
274 opnd = newOperatorNode(op, opnd, p.parseMultiplicativeExpr(n))
275 }
276 return opnd
277 }
278
279 // MultiplicativeExpr ::= UnaryExpr | MultiplicativeExpr MultiplyOperator(*) UnaryExpr
280 //
281 // | MultiplicativeExpr 'div' UnaryExpr | MultiplicativeExpr 'mod' UnaryExpr
282 func (p *parser) parseMultiplicativeExpr(n node) node {
283 opnd := p.parseUnaryExpr(n)
284 Loop:
285 for {
286 var op string
287 if p.r.typ == itemStar {
288 op = "*"
289 } else if testOp(p.r, "div") || testOp(p.r, "mod") {
290 op = p.r.name
291 } else {
292 break Loop
293 }
294 p.next()
295 opnd = newOperatorNode(op, opnd, p.parseUnaryExpr(n))
296 }
297 return opnd
298 }
299
300 // UnaryExpr ::= UnionExpr | '-' UnaryExpr
301 func (p *parser) parseUnaryExpr(n node) node {
302 minus := false
303 // ignore '-' sequence
304 for p.r.typ == itemMinus {
305 p.next()
306 minus = !minus
307 }
308 opnd := p.parseUnionExpr(n)
309 if minus {
310 opnd = newOperatorNode("*", opnd, newOperandNode(float64(-1)))
311 }
312 return opnd
313 }
314
315 // UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
316 func (p *parser) parseUnionExpr(n node) node {
317 opnd := p.parsePathExpr(n)
318 Loop:
319 for {
320 if p.r.typ != itemUnion {
321 break Loop
322 }
323 p.next()
324 opnd2 := p.parsePathExpr(n)
325 // Checking the node type that must be is node set type?
326 opnd = newOperatorNode("|", opnd, opnd2)
327 }
328 return opnd
329 }
330
331 // PathExpr ::= LocationPath | FilterExpr | FilterExpr '/' RelativeLocationPath | FilterExpr '//' RelativeLocationPath
332 func (p *parser) parsePathExpr(n node) node {
333 var opnd node
334 if isPrimaryExpr(p.r) {
335 opnd = p.parseFilterExpr(n)
336 switch p.r.typ {
337 case itemSlash:
338 p.next()
339 opnd = p.parseRelativeLocationPath(opnd)
340 case itemSlashSlash:
341 p.next()
342 opnd = p.parseRelativeLocationPath(newAxisNode("descendant-or-self", allNode, "", "", "", opnd))
343 }
344 } else {
345 opnd = p.parseLocationPath(nil)
346 }
347 return opnd
348 }
349
350 // FilterExpr ::= PrimaryExpr | FilterExpr Predicate
351 func (p *parser) parseFilterExpr(n node) node {
352 opnd := p.parsePrimaryExpr(n)
353 if p.r.typ == itemLBracket {
354 opnd = newFilterNode(opnd, p.parsePredicate(opnd))
355 }
356 return opnd
357 }
358
359 // Predicate ::= '[' PredicateExpr ']'
360 func (p *parser) parsePredicate(n node) node {
361 p.skipItem(itemLBracket)
362 opnd := p.parseExpression(n)
363 p.skipItem(itemRBracket)
364 return opnd
365 }
366
367 // LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
368 func (p *parser) parseLocationPath(n node) (opnd node) {
369 switch p.r.typ {
370 case itemSlash:
371 p.next()
372 opnd = newRootNode("/")
373 if isStep(p.r.typ) {
374 opnd = p.parseRelativeLocationPath(opnd) // ?? child:: or self ??
375 }
376 case itemSlashSlash:
377 p.next()
378 opnd = newRootNode("//")
379 opnd = p.parseRelativeLocationPath(newAxisNode("descendant-or-self", allNode, "", "", "", opnd))
380 default:
381 opnd = p.parseRelativeLocationPath(n)
382 }
383 return opnd
384 }
385
386 // RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | AbbreviatedRelativeLocationPath
387 func (p *parser) parseRelativeLocationPath(n node) node {
388 opnd := n
389 Loop:
390 for {
391 opnd = p.parseStep(opnd)
392 switch p.r.typ {
393 case itemSlashSlash:
394 p.next()
395 opnd = newAxisNode("descendant-or-self", allNode, "", "", "", opnd)
396 case itemSlash:
397 p.next()
398 default:
399 break Loop
400 }
401 }
402 return opnd
403 }
404
405 // Step ::= AxisSpecifier NodeTest Predicate* | AbbreviatedStep
406 func (p *parser) parseStep(n node) (opnd node) {
407 if p.r.typ == itemDot || p.r.typ == itemDotDot {
408 if p.r.typ == itemDot {
409 opnd = newAxisNode("self", allNode, "", "", "", n)
410 } else {
411 opnd = newAxisNode("parent", allNode, "", "", "", n)
412 }
413 p.next()
414 if p.r.typ != itemLBracket {
415 return opnd
416 }
417 } else {
418 axisType := "child" // default axes value.
419 switch p.r.typ {
420 case itemAt:
421 axisType = "attribute"
422 p.next()
423 case itemAxe:
424 axisType = p.r.name
425 p.next()
426 case itemLParens:
427 return p.parseSequence(n)
428 }
429 matchType := ElementNode
430 if axisType == "attribute" {
431 matchType = AttributeNode
432 }
433 opnd = p.parseNodeTest(n, axisType, matchType)
434 }
435 for p.r.typ == itemLBracket {
436 opnd = newFilterNode(opnd, p.parsePredicate(opnd))
437 }
438 return opnd
439 }
440
441 // Expr ::= '(' Step ("," Step)* ')'
442 func (p *parser) parseSequence(n node) (opnd node) {
443 p.skipItem(itemLParens)
444 opnd = p.parseStep(n)
445 for {
446 if p.r.typ != itemComma {
447 break
448 }
449 p.next()
450 opnd2 := p.parseStep(n)
451 opnd = newOperatorNode("|", opnd, opnd2)
452 }
453 p.skipItem(itemRParens)
454 return opnd
455 }
456
457 // NodeTest ::= NameTest | nodeType '(' ')' | 'processing-instruction' '(' Literal ')'
458 func (p *parser) parseNodeTest(n node, axeTyp string, matchType NodeType) (opnd node) {
459 switch p.r.typ {
460 case itemName:
461 if p.r.canBeFunc && isNodeType(p.r) {
462 var prop string
463 switch p.r.name {
464 case "comment", "text", "processing-instruction", "node":
465 prop = p.r.name
466 }
467 var name string
468 p.next()
469 p.skipItem(itemLParens)
470 if prop == "processing-instruction" && p.r.typ != itemRParens {
471 checkItem(p.r, itemString)
472 name = p.r.strval
473 p.next()
474 }
475 p.skipItem(itemRParens)
476 switch prop {
477 case "comment":
478 matchType = CommentNode
479 case "text":
480 matchType = TextNode
481 case "processing-instruction":
482 case "node":
483 matchType = allNode
484 default:
485 matchType = RootNode
486 }
487
488 opnd = newAxisNode(axeTyp, matchType, name, "", prop, n)
489 } else {
490 prefix := p.r.prefix
491 name := p.r.name
492 p.next()
493 if p.r.name == "*" {
494 name = ""
495 }
496 opnd = newAxisNode(axeTyp, matchType, name, prefix, "", n, func(a *axisNode) {
497 if prefix != "" && p.namespaces != nil {
498 if ns, ok := p.namespaces[prefix]; ok {
499 a.hasNamespaceURI = true
500 a.namespaceURI = ns
501 } else {
502 panic(fmt.Sprintf("prefix %s not defined.", prefix))
503 }
504 }
505 })
506 }
507 case itemStar:
508 opnd = newAxisNode(axeTyp, matchType, "", "", "", n)
509 p.next()
510 default:
511 panic("expression must evaluate to a node-set")
512 }
513 return opnd
514 }
515
516 // PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
517 func (p *parser) parsePrimaryExpr(n node) (opnd node) {
518 switch p.r.typ {
519 case itemString:
520 opnd = newOperandNode(p.r.strval)
521 p.next()
522 case itemNumber:
523 opnd = newOperandNode(p.r.numval)
524 p.next()
525 case itemDollar:
526 p.next()
527 checkItem(p.r, itemName)
528 opnd = newVariableNode(p.r.prefix, p.r.name)
529 p.next()
530 case itemLParens:
531 p.next()
532 opnd = p.parseExpression(n)
533 if opnd.Type() != nodeConstantOperand {
534 opnd = newGroupNode(opnd)
535 }
536 p.skipItem(itemRParens)
537 case itemName:
538 if p.r.canBeFunc && !isNodeType(p.r) {
539 opnd = p.parseMethod(nil)
540 }
541 }
542 return opnd
543 }
544
545 // FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument )* )? ')'
546 func (p *parser) parseMethod(n node) node {
547 var args []node
548 name := p.r.name
549 prefix := p.r.prefix
550
551 p.skipItem(itemName)
552 p.skipItem(itemLParens)
553 if p.r.typ != itemRParens {
554 for {
555 args = append(args, p.parseExpression(n))
556 if p.r.typ == itemRParens {
557 break
558 }
559 p.skipItem(itemComma)
560 }
561 }
562 p.skipItem(itemRParens)
563 return newFunctionNode(name, prefix, args)
564 }
565
566 // Parse parsing the XPath express string expr and returns a tree node.
567 func parse(expr string, namespaces map[string]string) node {
568 r := &scanner{text: expr}
569 r.nextChar()
570 r.nextItem()
571 p := &parser{r: r, namespaces: namespaces}
572 return p.parseExpression(nil)
573 }
574
575 // rootNode holds a top-level node of tree.
576 type rootNode struct {
577 nodeType
578 slash string
579 }
580
581 func (r *rootNode) String() string {
582 return r.slash
583 }
584
585 // operatorNode holds two Nodes operator.
586 type operatorNode struct {
587 nodeType
588 Op string
589 Left, Right node
590 }
591
592 func (o *operatorNode) String() string {
593 return fmt.Sprintf("%v%s%v", o.Left, o.Op, o.Right)
594 }
595
596 // axisNode holds a location step.
597 type axisNode struct {
598 nodeType
599 Input node
600 Prop string // node-test name.[comment|text|processing-instruction|node]
601 AxisType string // name of the axis.[attribute|ancestor|child|....]
602 LocalName string // local part name of node.
603 Prefix string // prefix name of node.
604 namespaceURI string // namespace URI of node
605 hasNamespaceURI bool // if namespace URI is set (can be "")
606 typeTest NodeType
607 }
608
609 func (a *axisNode) String() string {
610 var b bytes.Buffer
611 if a.AxisType != "" {
612 b.Write([]byte(a.AxisType + "::"))
613 }
614 if a.Prefix != "" {
615 b.Write([]byte(a.Prefix + ":"))
616 }
617 b.Write([]byte(a.LocalName))
618 if a.Prop != "" {
619 b.Write([]byte("/" + a.Prop + "()"))
620 }
621 return b.String()
622 }
623
624 // operandNode holds a constant operand.
625 type operandNode struct {
626 nodeType
627 Val interface{}
628 }
629
630 func (o *operandNode) String() string {
631 return fmt.Sprintf("%v", o.Val)
632 }
633
634 // groupNode holds a set of node expression
635 type groupNode struct {
636 nodeType
637 Input node
638 }
639
640 func (g *groupNode) String() string {
641 return fmt.Sprintf("%s", g.Input)
642 }
643
644 // filterNode holds a condition filter.
645 type filterNode struct {
646 nodeType
647 Input, Condition node
648 }
649
650 func (f *filterNode) String() string {
651 return fmt.Sprintf("%s[%s]", f.Input, f.Condition)
652 }
653
654 // variableNode holds a variable.
655 type variableNode struct {
656 nodeType
657 Name, Prefix string
658 }
659
660 func (v *variableNode) String() string {
661 if v.Prefix == "" {
662 return v.Name
663 }
664 return fmt.Sprintf("%s:%s", v.Prefix, v.Name)
665 }
666
667 // functionNode holds a function call.
668 type functionNode struct {
669 nodeType
670 Args []node
671 Prefix string
672 FuncName string // function name
673 }
674
675 func (f *functionNode) String() string {
676 var b bytes.Buffer
677 // fun(arg1, ..., argn)
678 b.Write([]byte(f.FuncName))
679 b.Write([]byte("("))
680 for i, arg := range f.Args {
681 if i > 0 {
682 b.Write([]byte(","))
683 }
684 b.Write([]byte(fmt.Sprintf("%s", arg)))
685 }
686 b.Write([]byte(")"))
687 return b.String()
688 }
689
690 type scanner struct {
691 text, name, prefix string
692
693 pos int
694 curr rune
695 currSize int
696 typ itemType
697 strval string // text value at current pos
698 numval float64 // number value at current pos
699 canBeFunc bool
700 }
701
702 func (s *scanner) nextChar() bool {
703 if s.pos >= len(s.text) {
704 s.curr = rune(0)
705 s.currSize = 1
706 return false
707 }
708
709 r, size := rune(s.text[s.pos]), 1
710 if r >= 0x80 { // handle multi-byte runes
711 r, size = utf8.DecodeRuneInString(s.text[s.pos:])
712 }
713
714 s.curr = r
715 s.currSize = size
716 s.pos += size
717 return true
718 }
719
720 func (s *scanner) nextItem() bool {
721 s.skipSpace()
722 switch s.curr {
723 case 0:
724 s.typ = itemEOF
725 return false
726 case ',', '@', '(', ')', '|', '*', '[', ']', '+', '-', '=', '#', '$':
727 s.typ = asItemType(s.curr)
728 s.nextChar()
729 case '<':
730 s.typ = itemLt
731 s.nextChar()
732 if s.curr == '=' {
733 s.typ = itemLe
734 s.nextChar()
735 }
736 case '>':
737 s.typ = itemGt
738 s.nextChar()
739 if s.curr == '=' {
740 s.typ = itemGe
741 s.nextChar()
742 }
743 case '!':
744 s.typ = itemBang
745 s.nextChar()
746 if s.curr == '=' {
747 s.typ = itemNe
748 s.nextChar()
749 }
750 case '.':
751 s.typ = itemDot
752 s.nextChar()
753 if s.curr == '.' {
754 s.typ = itemDotDot
755 s.nextChar()
756 } else if isDigit(s.curr) {
757 s.typ = itemNumber
758 s.numval = s.scanFraction()
759 }
760 case '/':
761 s.typ = itemSlash
762 s.nextChar()
763 if s.curr == '/' {
764 s.typ = itemSlashSlash
765 s.nextChar()
766 }
767 case '"', '\'':
768 s.typ = itemString
769 s.strval = s.scanString()
770 default:
771 if isDigit(s.curr) {
772 s.typ = itemNumber
773 s.numval = s.scanNumber()
774 } else if isName(s.curr) {
775 s.typ = itemName
776 s.name = s.scanName()
777 s.prefix = ""
778 // "foo:bar" is one itemem not three because it doesn't allow spaces in between
779 // We should distinct it from "foo::" and need process "foo ::" as well
780 if s.curr == ':' {
781 s.nextChar()
782 // can be "foo:bar" or "foo::"
783 if s.curr == ':' {
784 // "foo::"
785 s.nextChar()
786 s.typ = itemAxe
787 } else { // "foo:*", "foo:bar" or "foo: "
788 s.prefix = s.name
789 if s.curr == '*' {
790 s.nextChar()
791 s.name = "*"
792 } else if isName(s.curr) {
793 s.name = s.scanName()
794 } else {
795 panic(fmt.Sprintf("%s has an invalid qualified name.", s.text))
796 }
797 }
798 } else {
799 s.skipSpace()
800 if s.curr == ':' {
801 s.nextChar()
802 // it can be "foo ::" or just "foo :"
803 if s.curr == ':' {
804 s.nextChar()
805 s.typ = itemAxe
806 } else {
807 panic(fmt.Sprintf("%s has an invalid qualified name.", s.text))
808 }
809 }
810 }
811 s.skipSpace()
812 s.canBeFunc = s.curr == '('
813 } else {
814 panic(fmt.Sprintf("%s has an invalid token.", s.text))
815 }
816 }
817 return true
818 }
819
820 func (s *scanner) skipSpace() {
821 Loop:
822 for {
823 if !unicode.IsSpace(s.curr) || !s.nextChar() {
824 break Loop
825 }
826 }
827 }
828
829 func (s *scanner) scanFraction() float64 {
830 var (
831 i = s.pos - 2
832 c = 1 // '.'
833 )
834 for isDigit(s.curr) {
835 s.nextChar()
836 c++
837 }
838 v, err := strconv.ParseFloat(s.text[i:i+c], 64)
839 if err != nil {
840 panic(fmt.Errorf("xpath: scanFraction parse float got error: %v", err))
841 }
842 return v
843 }
844
845 func (s *scanner) scanNumber() float64 {
846 var (
847 c int
848 i = s.pos - 1
849 )
850 for isDigit(s.curr) {
851 s.nextChar()
852 c++
853 }
854 if s.curr == '.' {
855 s.nextChar()
856 c++
857 for isDigit(s.curr) {
858 s.nextChar()
859 c++
860 }
861 }
862 v, err := strconv.ParseFloat(s.text[i:i+c], 64)
863 if err != nil {
864 panic(fmt.Errorf("xpath: scanNumber parse float got error: %v", err))
865 }
866 return v
867 }
868
869 func (s *scanner) scanString() string {
870 var (
871 end = s.curr
872 )
873 s.nextChar()
874 i := s.pos - s.currSize
875 c := s.currSize
876 for s.curr != end {
877 if !s.nextChar() {
878 panic(errors.New("xpath: scanString got unclosed string"))
879 }
880 c += s.currSize
881 }
882 c -= 1
883 s.nextChar()
884 return s.text[i : i+c]
885 }
886
887 func (s *scanner) scanName() string {
888 var (
889 c = s.currSize - 1
890 i = s.pos - s.currSize
891 )
892
893 // Detect current rune size
894
895 for isName(s.curr) {
896 if !s.nextChar() {
897 c += s.currSize
898 break
899 }
900 c += s.currSize
901 }
902 return s.text[i : i+c]
903 }
904
905 func isName(r rune) bool {
906 // '*' is not a name character (XPath 1.0 §3.7): a NCName immediately followed by '*'
907 // (e.g. `price*0.01`) must be scanned as name · MultiplyOperator · number, not as a single
908 // name "price*0.01". The '*' of a namespace wildcard (`prefix:*`) is handled explicitly in the
909 // scanner (the ':' branch, `if s.curr == '*'`), so dropping it here does not affect that case.
910 return string(r) != ":" && string(r) != "/" &&
911 (unicode.Is(first, r) || unicode.Is(second, r))
912 }
913
914 func isDigit(r rune) bool {
915 return unicode.IsDigit(r)
916 }
917
918 func asItemType(r rune) itemType {
919 switch r {
920 case ',':
921 return itemComma
922 case '@':
923 return itemAt
924 case '(':
925 return itemLParens
926 case ')':
927 return itemRParens
928 case '|':
929 return itemUnion
930 case '*':
931 return itemStar
932 case '[':
933 return itemLBracket
934 case ']':
935 return itemRBracket
936 case '+':
937 return itemPlus
938 case '-':
939 return itemMinus
940 case '=':
941 return itemEq
942 case '$':
943 return itemDollar
944 }
945 panic(fmt.Errorf("unknown item: %v", r))
946 }
947
948 var first = &unicode.RangeTable{
949 R16: []unicode.Range16{
950 {0x003A, 0x003A, 1},
951 {0x0041, 0x005A, 1},
952 {0x005F, 0x005F, 1},
953 {0x0061, 0x007A, 1},
954 {0x00C0, 0x00D6, 1},
955 {0x00D8, 0x00F6, 1},
956 {0x00F8, 0x00FF, 1},
957 {0x0100, 0x0131, 1},
958 {0x0134, 0x013E, 1},
959 {0x0141, 0x0148, 1},
960 {0x014A, 0x017E, 1},
961 {0x0180, 0x01C3, 1},
962 {0x01CD, 0x01F0, 1},
963 {0x01F4, 0x01F5, 1},
964 {0x01FA, 0x0217, 1},
965 {0x0250, 0x02A8, 1},
966 {0x02BB, 0x02C1, 1},
967 {0x0386, 0x0386, 1},
968 {0x0388, 0x038A, 1},
969 {0x038C, 0x038C, 1},
970 {0x038E, 0x03A1, 1},
971 {0x03A3, 0x03CE, 1},
972 {0x03D0, 0x03D6, 1},
973 {0x03DA, 0x03E0, 2},
974 {0x03E2, 0x03F3, 1},
975 {0x0401, 0x040C, 1},
976 {0x040E, 0x044F, 1},
977 {0x0451, 0x045C, 1},
978 {0x045E, 0x0481, 1},
979 {0x0490, 0x04C4, 1},
980 {0x04C7, 0x04C8, 1},
981 {0x04CB, 0x04CC, 1},
982 {0x04D0, 0x04EB, 1},
983 {0x04EE, 0x04F5, 1},
984 {0x04F8, 0x04F9, 1},
985 {0x0531, 0x0556, 1},
986 {0x0559, 0x0559, 1},
987 {0x0561, 0x0586, 1},
988 {0x05D0, 0x05EA, 1},
989 {0x05F0, 0x05F2, 1},
990 {0x0621, 0x063A, 1},
991 {0x0641, 0x064A, 1},
992 {0x0671, 0x06B7, 1},
993 {0x06BA, 0x06BE, 1},
994 {0x06C0, 0x06CE, 1},
995 {0x06D0, 0x06D3, 1},
996 {0x06D5, 0x06D5, 1},
997 {0x06E5, 0x06E6, 1},
998 {0x0905, 0x0939, 1},
999 {0x093D, 0x093D, 1},
1000 {0x0958, 0x0961, 1},
1001 {0x0985, 0x098C, 1},
1002 {0x098F, 0x0990, 1},
1003 {0x0993, 0x09A8, 1},
1004 {0x09AA, 0x09B0, 1},
1005 {0x09B2, 0x09B2, 1},
1006 {0x09B6, 0x09B9, 1},
1007 {0x09DC, 0x09DD, 1},
1008 {0x09DF, 0x09E1, 1},
1009 {0x09F0, 0x09F1, 1},
1010 {0x0A05, 0x0A0A, 1},
1011 {0x0A0F, 0x0A10, 1},
1012 {0x0A13, 0x0A28, 1},
1013 {0x0A2A, 0x0A30, 1},
1014 {0x0A32, 0x0A33, 1},
1015 {0x0A35, 0x0A36, 1},
1016 {0x0A38, 0x0A39, 1},
1017 {0x0A59, 0x0A5C, 1},
1018 {0x0A5E, 0x0A5E, 1},
1019 {0x0A72, 0x0A74, 1},
1020 {0x0A85, 0x0A8B, 1},
1021 {0x0A8D, 0x0A8D, 1},
1022 {0x0A8F, 0x0A91, 1},
1023 {0x0A93, 0x0AA8, 1},
1024 {0x0AAA, 0x0AB0, 1},
1025 {0x0AB2, 0x0AB3, 1},
1026 {0x0AB5, 0x0AB9, 1},
1027 {0x0ABD, 0x0AE0, 0x23},
1028 {0x0B05, 0x0B0C, 1},
1029 {0x0B0F, 0x0B10, 1},
1030 {0x0B13, 0x0B28, 1},
1031 {0x0B2A, 0x0B30, 1},
1032 {0x0B32, 0x0B33, 1},
1033 {0x0B36, 0x0B39, 1},
1034 {0x0B3D, 0x0B3D, 1},
1035 {0x0B5C, 0x0B5D, 1},
1036 {0x0B5F, 0x0B61, 1},
1037 {0x0B85, 0x0B8A, 1},
1038 {0x0B8E, 0x0B90, 1},
1039 {0x0B92, 0x0B95, 1},
1040 {0x0B99, 0x0B9A, 1},
1041 {0x0B9C, 0x0B9C, 1},
1042 {0x0B9E, 0x0B9F, 1},
1043 {0x0BA3, 0x0BA4, 1},
1044 {0x0BA8, 0x0BAA, 1},
1045 {0x0BAE, 0x0BB5, 1},
1046 {0x0BB7, 0x0BB9, 1},
1047 {0x0C05, 0x0C0C, 1},
1048 {0x0C0E, 0x0C10, 1},
1049 {0x0C12, 0x0C28, 1},
1050 {0x0C2A, 0x0C33, 1},
1051 {0x0C35, 0x0C39, 1},
1052 {0x0C60, 0x0C61, 1},
1053 {0x0C85, 0x0C8C, 1},
1054 {0x0C8E, 0x0C90, 1},
1055 {0x0C92, 0x0CA8, 1},
1056 {0x0CAA, 0x0CB3, 1},
1057 {0x0CB5, 0x0CB9, 1},
1058 {0x0CDE, 0x0CDE, 1},
1059 {0x0CE0, 0x0CE1, 1},
1060 {0x0D05, 0x0D0C, 1},
1061 {0x0D0E, 0x0D10, 1},
1062 {0x0D12, 0x0D28, 1},
1063 {0x0D2A, 0x0D39, 1},
1064 {0x0D60, 0x0D61, 1},
1065 {0x0E01, 0x0E2E, 1},
1066 {0x0E30, 0x0E30, 1},
1067 {0x0E32, 0x0E33, 1},
1068 {0x0E40, 0x0E45, 1},
1069 {0x0E81, 0x0E82, 1},
1070 {0x0E84, 0x0E84, 1},
1071 {0x0E87, 0x0E88, 1},
1072 {0x0E8A, 0x0E8D, 3},
1073 {0x0E94, 0x0E97, 1},
1074 {0x0E99, 0x0E9F, 1},
1075 {0x0EA1, 0x0EA3, 1},
1076 {0x0EA5, 0x0EA7, 2},
1077 {0x0EAA, 0x0EAB, 1},
1078 {0x0EAD, 0x0EAE, 1},
1079 {0x0EB0, 0x0EB0, 1},
1080 {0x0EB2, 0x0EB3, 1},
1081 {0x0EBD, 0x0EBD, 1},
1082 {0x0EC0, 0x0EC4, 1},
1083 {0x0F40, 0x0F47, 1},
1084 {0x0F49, 0x0F69, 1},
1085 {0x10A0, 0x10C5, 1},
1086 {0x10D0, 0x10F6, 1},
1087 {0x1100, 0x1100, 1},
1088 {0x1102, 0x1103, 1},
1089 {0x1105, 0x1107, 1},
1090 {0x1109, 0x1109, 1},
1091 {0x110B, 0x110C, 1},
1092 {0x110E, 0x1112, 1},
1093 {0x113C, 0x1140, 2},
1094 {0x114C, 0x1150, 2},
1095 {0x1154, 0x1155, 1},
1096 {0x1159, 0x1159, 1},
1097 {0x115F, 0x1161, 1},
1098 {0x1163, 0x1169, 2},
1099 {0x116D, 0x116E, 1},
1100 {0x1172, 0x1173, 1},
1101 {0x1175, 0x119E, 0x119E - 0x1175},
1102 {0x11A8, 0x11AB, 0x11AB - 0x11A8},
1103 {0x11AE, 0x11AF, 1},
1104 {0x11B7, 0x11B8, 1},
1105 {0x11BA, 0x11BA, 1},
1106 {0x11BC, 0x11C2, 1},
1107 {0x11EB, 0x11F0, 0x11F0 - 0x11EB},
1108 {0x11F9, 0x11F9, 1},
1109 {0x1E00, 0x1E9B, 1},
1110 {0x1EA0, 0x1EF9, 1},
1111 {0x1F00, 0x1F15, 1},
1112 {0x1F18, 0x1F1D, 1},
1113 {0x1F20, 0x1F45, 1},
1114 {0x1F48, 0x1F4D, 1},
1115 {0x1F50, 0x1F57, 1},
1116 {0x1F59, 0x1F5B, 0x1F5B - 0x1F59},
1117 {0x1F5D, 0x1F5D, 1},
1118 {0x1F5F, 0x1F7D, 1},
1119 {0x1F80, 0x1FB4, 1},
1120 {0x1FB6, 0x1FBC, 1},
1121 {0x1FBE, 0x1FBE, 1},
1122 {0x1FC2, 0x1FC4, 1},
1123 {0x1FC6, 0x1FCC, 1},
1124 {0x1FD0, 0x1FD3, 1},
1125 {0x1FD6, 0x1FDB, 1},
1126 {0x1FE0, 0x1FEC, 1},
1127 {0x1FF2, 0x1FF4, 1},
1128 {0x1FF6, 0x1FFC, 1},
1129 {0x2126, 0x2126, 1},
1130 {0x212A, 0x212B, 1},
1131 {0x212E, 0x212E, 1},
1132 {0x2180, 0x2182, 1},
1133 {0x3007, 0x3007, 1},
1134 {0x3021, 0x3029, 1},
1135 {0x3041, 0x3094, 1},
1136 {0x30A1, 0x30FA, 1},
1137 {0x3105, 0x312C, 1},
1138 {0x4E00, 0x9FA5, 1},
1139 {0xAC00, 0xD7A3, 1},
1140 },
1141 }
1142
1143 var second = &unicode.RangeTable{
1144 R16: []unicode.Range16{
1145 {0x002D, 0x002E, 1},
1146 {0x0030, 0x0039, 1},
1147 {0x00B7, 0x00B7, 1},
1148 {0x02D0, 0x02D1, 1},
1149 {0x0300, 0x0345, 1},
1150 {0x0360, 0x0361, 1},
1151 {0x0387, 0x0387, 1},
1152 {0x0483, 0x0486, 1},
1153 {0x0591, 0x05A1, 1},
1154 {0x05A3, 0x05B9, 1},
1155 {0x05BB, 0x05BD, 1},
1156 {0x05BF, 0x05BF, 1},
1157 {0x05C1, 0x05C2, 1},
1158 {0x05C4, 0x0640, 0x0640 - 0x05C4},
1159 {0x064B, 0x0652, 1},
1160 {0x0660, 0x0669, 1},
1161 {0x0670, 0x0670, 1},
1162 {0x06D6, 0x06DC, 1},
1163 {0x06DD, 0x06DF, 1},
1164 {0x06E0, 0x06E4, 1},
1165 {0x06E7, 0x06E8, 1},
1166 {0x06EA, 0x06ED, 1},
1167 {0x06F0, 0x06F9, 1},
1168 {0x0901, 0x0903, 1},
1169 {0x093C, 0x093C, 1},
1170 {0x093E, 0x094C, 1},
1171 {0x094D, 0x094D, 1},
1172 {0x0951, 0x0954, 1},
1173 {0x0962, 0x0963, 1},
1174 {0x0966, 0x096F, 1},
1175 {0x0981, 0x0983, 1},
1176 {0x09BC, 0x09BC, 1},
1177 {0x09BE, 0x09BF, 1},
1178 {0x09C0, 0x09C4, 1},
1179 {0x09C7, 0x09C8, 1},
1180 {0x09CB, 0x09CD, 1},
1181 {0x09D7, 0x09D7, 1},
1182 {0x09E2, 0x09E3, 1},
1183 {0x09E6, 0x09EF, 1},
1184 {0x0A02, 0x0A3C, 0x3A},
1185 {0x0A3E, 0x0A3F, 1},
1186 {0x0A40, 0x0A42, 1},
1187 {0x0A47, 0x0A48, 1},
1188 {0x0A4B, 0x0A4D, 1},
1189 {0x0A66, 0x0A6F, 1},
1190 {0x0A70, 0x0A71, 1},
1191 {0x0A81, 0x0A83, 1},
1192 {0x0ABC, 0x0ABC, 1},
1193 {0x0ABE, 0x0AC5, 1},
1194 {0x0AC7, 0x0AC9, 1},
1195 {0x0ACB, 0x0ACD, 1},
1196 {0x0AE6, 0x0AEF, 1},
1197 {0x0B01, 0x0B03, 1},
1198 {0x0B3C, 0x0B3C, 1},
1199 {0x0B3E, 0x0B43, 1},
1200 {0x0B47, 0x0B48, 1},
1201 {0x0B4B, 0x0B4D, 1},
1202 {0x0B56, 0x0B57, 1},
1203 {0x0B66, 0x0B6F, 1},
1204 {0x0B82, 0x0B83, 1},
1205 {0x0BBE, 0x0BC2, 1},
1206 {0x0BC6, 0x0BC8, 1},
1207 {0x0BCA, 0x0BCD, 1},
1208 {0x0BD7, 0x0BD7, 1},
1209 {0x0BE7, 0x0BEF, 1},
1210 {0x0C01, 0x0C03, 1},
1211 {0x0C3E, 0x0C44, 1},
1212 {0x0C46, 0x0C48, 1},
1213 {0x0C4A, 0x0C4D, 1},
1214 {0x0C55, 0x0C56, 1},
1215 {0x0C66, 0x0C6F, 1},
1216 {0x0C82, 0x0C83, 1},
1217 {0x0CBE, 0x0CC4, 1},
1218 {0x0CC6, 0x0CC8, 1},
1219 {0x0CCA, 0x0CCD, 1},
1220 {0x0CD5, 0x0CD6, 1},
1221 {0x0CE6, 0x0CEF, 1},
1222 {0x0D02, 0x0D03, 1},
1223 {0x0D3E, 0x0D43, 1},
1224 {0x0D46, 0x0D48, 1},
1225 {0x0D4A, 0x0D4D, 1},
1226 {0x0D57, 0x0D57, 1},
1227 {0x0D66, 0x0D6F, 1},
1228 {0x0E31, 0x0E31, 1},
1229 {0x0E34, 0x0E3A, 1},
1230 {0x0E46, 0x0E46, 1},
1231 {0x0E47, 0x0E4E, 1},
1232 {0x0E50, 0x0E59, 1},
1233 {0x0EB1, 0x0EB1, 1},
1234 {0x0EB4, 0x0EB9, 1},
1235 {0x0EBB, 0x0EBC, 1},
1236 {0x0EC6, 0x0EC6, 1},
1237 {0x0EC8, 0x0ECD, 1},
1238 {0x0ED0, 0x0ED9, 1},
1239 {0x0F18, 0x0F19, 1},
1240 {0x0F20, 0x0F29, 1},
1241 {0x0F35, 0x0F39, 2},
1242 {0x0F3E, 0x0F3F, 1},
1243 {0x0F71, 0x0F84, 1},
1244 {0x0F86, 0x0F8B, 1},
1245 {0x0F90, 0x0F95, 1},
1246 {0x0F97, 0x0F97, 1},
1247 {0x0F99, 0x0FAD, 1},
1248 {0x0FB1, 0x0FB7, 1},
1249 {0x0FB9, 0x0FB9, 1},
1250 {0x20D0, 0x20DC, 1},
1251 {0x20E1, 0x3005, 0x3005 - 0x20E1},
1252 {0x302A, 0x302F, 1},
1253 {0x3031, 0x3035, 1},
1254 {0x3099, 0x309A, 1},
1255 {0x309D, 0x309E, 1},
1256 {0x30FC, 0x30FE, 1},
1257 },
1258 }