Fix a bug in the handling of processing-instruction() test.
[tatoo.git] / src / xpath / xpath_internal_parser.mly
1 %{
2 (***********************************************************************)
3 (*                                                                     *)
4 (*                               TAToo                                 *)
5 (*                                                                     *)
6 (*                     Kim Nguyen, LRI UMR8623                         *)
7 (*                   Université Paris-Sud & CNRS                       *)
8 (*                                                                     *)
9 (*  Copyright 2010-2012 Université Paris-Sud and Centre National de la *)
10 (*  Recherche Scientifique. All rights reserved.  This file is         *)
11 (*  distributed under the terms of the GNU Lesser General Public       *)
12 (*  License, with the special exception on linking described in file   *)
13 (*  ../LICENSE.                                                        *)
14 (*                                                                     *)
15 (***********************************************************************)
16
17 (*
18   Time-stamp: <Last modified on 2013-03-13 12:38:54 CET by Kim Nguyen>
19 *)
20
21   open Ast
22   open Tree.Common
23 %}
24
25 %token <string> TAG
26 %token <string> PI
27 %token <string> ATTNAME
28 %token <string> STRING
29 %token <int>  INT
30 %token <float> FLOAT
31 %token <Ast.axis> AXIS
32 %token RB LB LP RP
33 %token SLASH SLASHSLASH COLONCOLON STAR PIPE
34 %token EQ NEQ LT GT LTE GTE OR AND ADD SUB DIV MOD
35 %token NODE TEXT COMMENT
36 %token COMMA
37 %token EOF
38
39 %left OR
40 %left AND
41 %left EQ NEQ
42 %left LT GT LTE GTE
43 %left ADD SUB
44 %left MOD DIV STAR
45 %nonassoc uminus
46
47 %start xpath_query
48 %type <Ast.path> xpath_query
49
50
51 %%
52 xpath_query:
53 path EOF          { $1 }
54 ;
55
56 path:
57   path_rev { List.rev $1 }
58 ;
59
60 path_rev:
61   simple_path     { [ $1 ] }
62 | path_rev PIPE simple_path { $3 :: $1 }
63 ;
64
65
66 simple_path:
67    absolute_path  { Absolute  (List.rev $1) }
68 |  relative_path  { Relative  (List.rev $1) }
69 ;
70
71 absolute_path:
72   SLASH relative_path { $2 }
73 | SLASHSLASH relative_path { $2 @
74                                [(Descendant true,
75                                  (node, NodeKind.Node),
76                                  [])] }
77 ;
78
79 /*
80   step is always a small list, of size 1-3 so @ is
81   cheap
82 */
83 relative_path:
84   step { $1 }
85 | relative_path SLASH step { $3 @ $1 }
86 | relative_path SLASHSLASH step { $3 @
87                                     ((Descendant true,
88                                       (node, NodeKind.Node),
89                                       [])
90                                      :: $1) }
91 ;
92
93 step:
94   axis_test pred_list    {
95     match $1 with
96       (a,b) :: r -> (a,b,$2) :: (List.map (fun (a,b) -> (a,b,[])) r)
97     | [] -> assert false
98   }
99 ;
100
101 axis_test:
102   AXIS COLONCOLON test  { let a, (t,k) = $1, $3 in
103                           match a with
104                             Attribute when Utils.QNameSet.is_finite t ->
105                               [ a, ((Utils.QNameSet.fold
106                                        (fun t a ->
107                                          Utils.QNameSet.add
108                                            (Utils.QName.attribute t) a)
109                                        t Utils.QNameSet.empty), k) ]
110                           | Preceding|Following ->
111                               [ (Descendant true, (t,k));
112                                 if a == Preceding then
113                                   (PrecedingSibling, (node, NodeKind.Node))
114                                 else
115                                   (FollowingSibling, (node, NodeKind.Node));
116                                 (Ancestor true, (node, NodeKind.Node)) ]
117
118                           | _ -> [ a, (t,k) ]
119                         }
120 | test                  { [ Child, $1 ] }
121 | AXIS            {
122   let _ = Format.flush_str_formatter () in
123   let () = Format.fprintf Format.str_formatter "%a" Ast.print_axis $1 in
124   let a = Format.flush_str_formatter () in
125   [Child, (Utils.QNameSet.singleton (Utils.QName.of_string a),NodeKind.Element)]
126 }
127 | ATTNAME             {  [(Attribute,
128                            (Utils.QNameSet.singleton (Utils.QName.of_string $1),
129                             NodeKind.Attribute))] }
130 ;
131
132 test:
133   NODE                { node, NodeKind.Node }
134 | TEXT                { text, NodeKind.Text }
135 | STAR                { star, NodeKind.Element }
136 | COMMENT             { Utils.QNameSet.singleton(Utils.QName.comment),
137                         NodeKind.Comment
138                       }
139 | PI                  { (if $1 = "" then star
140                          else Utils.QNameSet.singleton(
141                            Utils.QName.processing_instruction (
142                              Utils.QName.of_string $1)
143                          )), NodeKind.ProcessingInstruction
144                       }
145 | TAG                 { Utils.QNameSet.singleton(Utils.QName.of_string $1),
146                         NodeKind.Element
147                       }
148 ;
149
150 pred_list:
151   pred_list_rev             { List.rev $1 }
152 ;
153
154 pred_list_rev:
155              { [] }
156 | pred_list LB expr RB   { $3 :: $1 }
157 ;
158
159 expr:
160   INT                       { Number(`Int($1)) }
161 | FLOAT                     { Number(`Float($1)) }
162 | STRING                    { String $1 }
163 | SUB expr     %prec uminus { Unop(Neg, $2) }
164 | expr AND expr             { Binop($1, And, $3) }
165 | expr OR expr              { Binop($1, Or, $3) }
166 | expr ADD expr             { Binop($1, Add, $3) }
167 | expr SUB expr             { Binop($1, Sub, $3) }
168 | expr STAR expr            { Binop($1, Mult, $3) }
169 | expr DIV expr             { Binop($1, Div, $3) }
170 | expr MOD expr             { Binop($1, Mod, $3) }
171 | expr EQ expr              { Binop($1, Eq, $3) }
172 | expr NEQ expr             { Binop($1, Neq, $3) }
173 | expr LT expr              { Binop($1, Lt, $3) }
174 | expr LTE expr             { Binop($1, Lte, $3) }
175 | expr GT expr              { Binop($1, Gt, $3) }
176 | expr GTE expr             { Binop($1, Gte, $3) }
177 | TAG LP arg_list RP        { Fun_call(Utils.QName.of_string $1, $3) }
178 | LP expr RP                { $2 }
179 | path                      { Path $1 }
180 ;
181
182 arg_list:
183                             { [] }
184 | arg_list1                 { List.rev $1 }
185 ;
186
187 arg_list1:
188   expr                     { [ $1 ] }
189 | arg_list1 COMMA expr     { $3 :: $1 }
190 ;