(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / Mono.Xml.Ext / Mono.Xml.XPath2 / XQueryParser.jay
blobf11635bfda2e99103bcaa880353055b52b22dd4b
1 %{
2 //
3 // XQueryParser.jay
4 //
5 // Author:
6 //      Atsushi Enomoto <atsushi@ximian.com>
7 //
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 // FIXME:
32 //      attribute value template
33 //      handle double literal
36 #if NET_2_0
38 using System;
39 using System.Collections;
40 using System.IO;
41 using System.Xml;
42 using System.Xml.Query;
43 using System.Xml.Schema;
44 using System.Xml.XPath;
45 using System.Security.Policy;
46 using Mono.Xml.XPath2;
47 using Mono.Xml.XQuery;
48 using Mono.Xml;
50 namespace Mono.Xml.XQuery.Parser
52         internal class XQueryParser
53         {
54                 // See also FunctionCall production rule.
55                 static Hashtable reservedFunctionNames;
57                 static XQueryParser ()
58                 {
59                         reservedFunctionNames = new Hashtable ();
60                         reservedFunctionNames.Add ("attribute", "attribute");
61                         reservedFunctionNames.Add ("comment", "comment");
62                         reservedFunctionNames.Add ("document", "document");
63                         reservedFunctionNames.Add ("element", "element");
64                         reservedFunctionNames.Add ("empty", "empty");
65                         reservedFunctionNames.Add ("if", "if");
66                         reservedFunctionNames.Add ("item", "item");
67                         reservedFunctionNames.Add ("node", "node");
68                         reservedFunctionNames.Add ("processing-instruction", "processing-instruction");
69                         reservedFunctionNames.Add ("text", "text");
70                         reservedFunctionNames.Add ("type", "type");
71                         reservedFunctionNames.Add ("typeswitch", "typeswitch");
72                 }
74                 public static XQueryModule Parse (TextReader reader)
75                 {
76                         return new XQueryParser ().RunParse (reader);
77                 }
79                 private XQueryTokenizer tokenizer;
81                 private XQueryParser ()
82                 {
83                 }
85                 // FIXME: we don't need Evidence here at all. It is used only
86                 // to generate runnable IL (on loading resulting Assembly).
87                 public XQueryModule RunParse (TextReader source)
88                 {
89                         tokenizer = null;
90                         try {
91 //                              debug = new yydebug.yyDebugSimple ();
92                                 tokenizer = new XQueryTokenizer (source);
93                                 XQueryModule mod = (XQueryModule) yyparse (tokenizer);
94                                 mod.NSResolver = tokenizer.NSResolver;
95                                 return mod;
96                         } catch (yyParser.yyException ex) {
97                                 throw new XmlQueryCompileException (String.Format ("Tokenizer error at line {0}, column {1}: {2}", tokenizer.LineNumber, tokenizer.LinePosition, ex.Message), ex);
98                         }
99                 }
101                 public XmlTypeCode GetAtomicTypeCode (XmlQualifiedName name)
102                 {
103                         if (name.Namespace == InternalPool.XdtNamespace) {
104                                 switch (name.Name) {
105                                 case "anyAtomicType":
106                                         return XmlTypeCode.AnyAtomicType;
107                                 case "dayTimeDuration":
108                                         return XmlTypeCode.DayTimeDuration;
109                                 case "item":
110                                         return XmlTypeCode.Item;
111                                 case "untypedAtomic":
112                                         return XmlTypeCode.UntypedAtomic;
113                                 case "yearMonthDuration":
114                                         return XmlTypeCode.YearMonthDuration;
115                                 }
116                         } else if (name.Namespace == XmlSchema.Namespace) {
117                                 switch (name.Name) {
118                                 case "boolean":
119                                         return XmlTypeCode.Boolean;
120                                 case "byte":
121                                         return XmlTypeCode.Byte;
122                                 case "date":
123                                         return XmlTypeCode.Date;
124                                 case "dateTime":
125                                         return XmlTypeCode.DateTime;
126                                 case "decimal":
127                                         return XmlTypeCode.Decimal;
128                                 case "double":
129                                         return XmlTypeCode.Double;
130                                 case "duration":
131                                         return XmlTypeCode.Duration;
132                                 case "entity":
133                                         return XmlTypeCode.Entity;
134                                 case "float":
135                                         return XmlTypeCode.Float;
136                                 case "gDay":
137                                         return XmlTypeCode.GDay;
138                                 case "gMonth":
139                                         return XmlTypeCode.GMonth;
140                                 case "gMonthDay":
141                                         return XmlTypeCode.GMonthDay;
142                                 case "gYear":
143                                         return XmlTypeCode.GYear;
144                                 case "gYearMonth":
145                                         return XmlTypeCode.GYearMonth;
146                                 case "hexBinary":
147                                         return XmlTypeCode.HexBinary;
148                                 case "id":
149                                         return XmlTypeCode.Id;
150                                 case "idref":
151                                         return XmlTypeCode.Idref;
152                                 case "int":
153                                         return XmlTypeCode.Int;
154                                 case "integer":
155                                         return XmlTypeCode.Integer;
156                                 case "language":
157                                         return XmlTypeCode.Language;
158                                 case "long":
159                                         return XmlTypeCode.Long;
160                                 case "Name":
161                                         return XmlTypeCode.Name;
162                                 case "NCName":
163                                         return XmlTypeCode.NCName;
164                                 case "negativeInteger":
165                                         return XmlTypeCode.NegativeInteger;
166                                 case "NMTOKEN":
167                                         return XmlTypeCode.NmToken;
168                                 case "nonNegativeInteger":
169                                         return XmlTypeCode.NonNegativeInteger;
170                                 case "nonPositiveInteger":
171                                         return XmlTypeCode.NonPositiveInteger;
172                                 case "normalizedString":
173                                         return XmlTypeCode.NormalizedString;
174                                 case "NOTATION":
175                                         return XmlTypeCode.Notation;
176                                 case "positiveInteger":
177                                         return XmlTypeCode.PositiveInteger;
178                                 case "QName":
179                                         return XmlTypeCode.QName;
180                                 case "short":
181                                         return XmlTypeCode.Short;
182                                 case "string":
183                                         return XmlTypeCode.String;
184                                 case "time":
185                                         return XmlTypeCode.Time;
186                                 case "token":
187                                         return XmlTypeCode.Token;
188                                 case "unsignedByte":
189                                         return XmlTypeCode.UnsignedByte;
190                                 case "unsignedInt":
191                                         return XmlTypeCode.UnsignedInt;
192                                 case "unsignedLong":
193                                         return XmlTypeCode.UnsignedLong;
194                                 case "unsignedShort":
195                                         return XmlTypeCode.UnsignedShort;
196                                 }
197                         }
198                         throw new XmlQueryCompileException (String.Format ("Unexpected type name was specified as atomic type: {0}", name));
199                 }
203 /* --------------------------------------------------------
204         Tokens
205 -------------------------------------------------------- */
207 /* These are for numbers */
208 //%token SMALL_E //"e"
209 //%token LARGE_E //"E"
211 %token DOT                      "."
212 %token DOT2                     ".."
213 %token SEMICOLON                ";"
214 %token OPEN_PAREN               "("
215 %token OPEN_PAREN_COLON         "(:"
216 %token PRAGMA_OPEN              "(::"
217 %token CLOSE_PAREN              ")"
218 %token COLON                    ":"
219 %token COLON2                   "::"
220 %token PRAGMA_CLOSE             "::)"
221 %token CLOSE_PAREN_COLON        ":)"
222 %token COLON_EQUAL              ":="
223 %token OPEN_BRACKET             "["
224 %token CLOSE_BRACKET            "]"
225 %token OPEN_CURLY               "{"
226 %token CLOSE_CURLY              "}"
227 %token COMMA                    ","
228 %token DOLLAR                   "$"
229 %token EQUAL                    "="
230 %token NOT_EQUAL                "!="
231 %token LESSER                   "<"
232 %token LESSER2                  "<<"
233 %token LESSER_EQUAL             "<="
234 %token GREATER                  ">"
235 %token GREATER2                 ">>"
236 %token GREATER_EQUAL            ">="
237 %token BAR                      "|"
238 %token ASTERISK                 "*"
239 %token PLUS                     "+"
240 %token MINUS                    "-"
241 %token SLASH                    "/"
242 %token SLASH2                   "//"
243 %token QUESTION                 "?"
246 %token XQUERY           //"xquery"
247 %token VERSION          //"version"
248 %token PRAGMA           //"pragma"
249 %token EXTENSION        //"extension"
250 %token MODULE           //"module"
251 %token NAMESPACE        //"namespace"
252 %token DECLARE          //"declare"
253 %token XMLSPACE         //"xmlspace"
254 %token PRESERVE         //"preserve"
255 %token STRIP            //"strip"
256 %token DEFAULT          //"default"
257 %token DOCUMENT_NODE    //"document-node"
258 %token DOCUMENT         //"document"
259 %token ELEMENT          //"element"
260 %token ATTRIBUTE        //"attribute"
261 %token PROCESSING_INSTRUCTION //"processing-instruction"
262 %token COMMENT          //"comment"
263 %token TEXT             //"text"
264 %token NODE             //"node"
265 %token FUNCTION         //"function"
266 %token COLLATION        //"collation"
267 %token CONSTRUCTION     //"construction"
268 %token ORDERING         //"ordering"
269 %token ORDERED          //"ordered"
270 %token UNORDERED        //"unordered"
271 %token BASEURI          //"base-uri"
272 %token IMPORT           //"import"
273 %token SCHEMA           //"schema"
274 %token AT               //"at"
275 %token VARIABLE         //"variable"
276 %token AS               //"as"
277 %token EXTERNAL         //"external"
278 %token VALIDATION       //"validation"
279 %token LAX              //"lax"
280 %token STRICT           //"strict"
281 %token SKIP             //"skip"
282 %token RETURN           //"return"
283 %token FOR              //"for"
284 %token LET              //"let"
285 %token IN               //"in"
286 %token WHERE            //"where"
287 %token ORDER            //"order"
288 %token BY               //"by"
289 %token STABLE           //"stable"
290 %token ASCENDING        //"ascending"
291 %token DESCENDING       //"descending"
292 %token EMPTY            //"empty"
293 %token GREATEST         //"greatest"
294 %token LEAST            //"least"
295 %token SOME             //"some"
296 %token EVERY            //"every"
297 %token SATISFIES        //"satisfies"
298 %token IS               //"is"
299 %token TO               //"to"
300 %token EQ               //"eq"
301 %token NE               //"ne"
302 %token LT               //"lt"
303 %token LE               //"le"
304 %token GT               //"gt"
305 %token GE               //"ge"
306 %token AND              //"and"
307 %token OR               //"or"
308 %token INSTANCE         //"instance"
309 %token OF               //"of"
310 %token IF               //"if"
311 %token THEN             //"then"
312 %token ELSE             //"else"
313 %token TYPESWITCH       //"typeswitch"
314 %token CASE             //"case"
315 %token TREAT            //"treat"
316 %token CASTABLE         //"castable"
317 %token CAST             //"as"
318 %token DIV              //"div"
319 %token IDIV             //"idiv"
320 %token MOD              //"mod"
321 %token UNION            //"union"
322 %token INTERSECT        //"intersect"
323 %token EXCEPT           //"except"
324 %token VALIDATE         //"validate"
325 %token CONTEXT          //"context"
326 %token NILLABLE         //"nillable"
327 %token ITEM             //"item"
330 %token GLOBAL           //"global"
331 %token TYPE             //"type"
333 %token CHILD                    //"child"
334 %token DESCENDANT               //"descendant"
335 %token ATTRIBUTE                //"attribute"
336 %token SELF                     //"self"
337 %token DESCENDANT_OR_SELF       //"descendant-or-self"
338 %token FOLLOWING_SIBLING        //"following-sibling"
339 %token FOLLOWING                //"following"
340 %token PARENT                   //"parent"
341 %token ANCESTOR                 //"ancestor"
342 %token PRECEDING_SIBLING        //"preceding-sibling"
343 %token PRECEDING                //"preceding"
344 %token ANCESTOR_OR_SELF         //"ancestor-or-self"
348 %token QNAME
349 %token NCNAME
350 %token WILD_LOCALNAME
351 %token WILD_PREFIX
353 %token STRING_LITERAL
354 %token DECIMAL_LITERAL
355 %token DOUBLE_LITERAL
357 %token PRAGMA_CONTENTS // characters until "::)"
360 %token PREDEFINED_ENTITY_REF
361 %token CHAR_REF
363 // Used only inside Constructor
364 %token XML_COMMENT_START // "<!--"
365 %token XML_COMMENT_TO_END // XML comment content immediate before "-->"
366 %token XML_PI_START // "<?"
367 %token XML_PI_TO_END // PI content immediate before "?>"
368 %token XML_CDATA_START // <![CDATA[
369 %token XML_CDATA_TO_END // CDATA section content immediate before "]]>"
370 %token EMPTY_TAG_CLOSE // "/>"
371 %token END_TAG_START // "</". Its appearance depends on the context
372 %token ATT_VALUE_LITERAL
373 %token ELEM_CONTENT_LITERAL
374 %token EXT_CONTENT
375 %token APOS                     "'"
376 //%token APOS2                  "''"
377 %token QUOT                     //""""
378 //%token QUOT2                  //""""""
380 %start Module
384 /* --------------------------------------------------------
385          Modules and Prologs
386 -------------------------------------------------------- */
388 Module // returns Module
389         : VersionDecl MainModule
390         {
391                 string version = (string) $1;
392                 XQueryMainModule module = (XQueryMainModule) $2;
393                 module.Version = version;
394                 $$ = module;
395         }
396         | VersionDecl LibraryModule
397         {
398                 string version = (string) $1;
399                 XQueryLibraryModule module = (XQueryLibraryModule) $2;
400                 $$ = module;
401         }
402         ;
404 VersionDecl // returns string
405         : // empty
406         {
407                 $$ = null;
408         }
409         | XQUERY VERSION STRING_LITERAL {
410                 tokenizer.State = ParseState.Operator;
411           } SEMICOLON {
412                 tokenizer.State = ParseState.Default;
413           }
414         {
415                 $$ = (string) $3;
416         }
417         ;
419 MainModule // returns MainModule 
420         : Prolog QueryBody
421         {
422                 Prolog prolog = (Prolog) $1;
423                 ExprSequence body = (ExprSequence) $2;
425                 XQueryMainModule mod = new XQueryMainModule ();
426                 mod.Prolog = prolog;
427                 mod.QueryBody = body;
428                 $$ = mod;
429         }
430         ;
432 LibraryModule // returns LibraryModule 
433         : ModuleDecl Prolog
434         {
435                 XQueryLibraryModule mod = new XQueryLibraryModule ();
436                 mod.ModuleDecl = (ModuleDecl) $1;
437                 mod.Prolog = (Prolog) $2;
438                 $$ = mod;
439         }
440         ;
442 ModuleDecl // returns ModuleDecl 
443         : MODULE NAMESPACE {
444                 tokenizer.State = ParseState.NamespaceDecl;
445           } NCName EQUAL STRING_LITERAL {
446                 tokenizer.State = ParseState.Default;
447           } SEMICOLON
448         {
449                 ModuleDecl decl = new ModuleDecl ();
450                 decl.Prefix = (string) $4;
451                 decl.Namespace = (string) $6;
452                 tokenizer.AddNamespace (decl.Prefix, decl.Namespace);
453                 $$ = decl;
454         }
455         ;
457 Prolog
458         : // empty
459         {
460                 $$ = new Prolog ();
461         }
462         | PrologContent SEMICOLON Prolog
463         {
464                 Prolog p = (Prolog) $3;
465                 p.Add ($1);
466                 $$ = p;
467         }
468         ;
470 PrologContent
471         : Setter
472         | DeclarationOrImport
473         ;
475 // FIXME: this production rule is the right one, but it brings 
476 // major shift/reduce conflicts.
478 Prolog // returns Prolog 
479         : Setters DeclarationsAndImports
480         {
481                 Prolog p = (Prolog) $1;
482                 ArrayList al = (ArrayList) $2;
483                 if (al != null) {
484                         foreach (object o in al)
485                                 p.Add (o);
486                 }
487                 $$ = p;
488         }
489         ;
491 Setters // returns Prolog 
492         : // empty
493         {
494                 $$ = new Prolog ();
495         }
496         | Setter SEMICOLON Setters
497         {
498                 Prolog p = (Prolog) $3;
499                 p.Add ($1);
500                 $$ = p;
501         }
502         ;
504 DeclarationsAndImports // returns ArrayList 
505         : // empty
506         {
507                 $$ = null;
508         }
509         | DeclarationOrImport SEMICOLON DeclarationsAndImports
510         {
511                 ArrayList al = (ArrayList) $3;
512                 if (al == null)
513                         al = new ArrayList ();
514                 al.Add ($1);
515                 $$ = al;
516         }
517         ;
521 Setter // returns object 
522         : XmlSpaceDecl // XmlSpaceDecl
523         | DefaultCollationDecl // SimplePrologContent
524         | BaseURIDecl // SimplePrologContent
525         | ConstructionDecl // ConstuctionDecl
526         | DefaultNamespaceDecl // SimplePrologContent
527         | DefaultOrderingDecl // bool
528         ;
530 DeclarationOrImport // returns object 
531         : SchemaImport
532         | ModuleImport
533         | NamespaceDecl
534         | VarDecl
535         | FunctionDecl
536         ;
538 NamespaceDecl // returns XmlQualifiedName 
539         : DECLARE NAMESPACE {
540                 tokenizer.State = ParseState.NamespaceDecl;
541           } NCName EQUAL STRING_LITERAL {
542                 tokenizer.State = ParseState.Default;
543           }
544         {
545                 XmlQualifiedName name = new XmlQualifiedName ((string) $4, (string) $6);
546                 tokenizer.AddNamespace (name.Name, name.Namespace);
547                 $$ = name;
548         }
549         ;
551 XmlSpaceDecl // returns XmlSpaceDecl 
552         : DECLARE XMLSPACE { 
553                 tokenizer.State = ParseState.XmlSpaceDecl;
554           } PRESERVE {
555                 tokenizer.State = ParseState.Default;
556           }
557         {
558                 $$ = new XmlSpaceDecl (XmlSpace.Preserve);
559         }
560         | DECLARE XMLSPACE { 
561                 tokenizer.State = ParseState.XmlSpaceDecl;
562           } STRIP {
563                 tokenizer.State = ParseState.Default;
564           }
565         {
566                 $$ = new XmlSpaceDecl (XmlSpace.Default);
567         }
568         ;
570 ConstructionDecl // returns ConstructionDecl 
571         : DECLARE CONSTRUCTION { 
572                 tokenizer.State = ParseState.Operator;
573           } PRESERVE {
574                 tokenizer.State = ParseState.Default;
575           }
576         {
577                 $$ = new ConstructionDecl (XmlSpace.Preserve);
578         }
579         | DECLARE CONSTRUCTION { 
580                 tokenizer.State = ParseState.Operator;
581           } STRIP {
582                 tokenizer.State = ParseState.Default;
583           }
584         {
585                 $$ = new ConstructionDecl (XmlSpace.Default);
586         }
587         ;
589 DefaultNamespaceDecl // returns SimplePrologContent 
590         : DECLARE DEFAULT ELEMENT {
591                 tokenizer.State = ParseState.NamespaceKeyword;
592           } NAMESPACE {
593                 tokenizer.State = ParseState.NamespaceDecl;
594           } STRING_LITERAL {
595                 tokenizer.State = ParseState.Default;
596           }
597         {
598                 tokenizer.AddNamespace (String.Empty, (string) $7);
599                 $$ = new SimplePrologContent (PrologContentType.DefaultElementNamespace, (string) $7);
600         }
601         | DECLARE DEFAULT FUNCTION {
602                 tokenizer.State = ParseState.NamespaceKeyword;
603           } NAMESPACE {
604                 tokenizer.State = ParseState.NamespaceDecl;
605           } STRING_LITERAL {
606                 tokenizer.State = ParseState.Default;
607           }
608         {
609                 tokenizer.DefaultFunctionNamespace = (string) $5;
610                 $$ = new SimplePrologContent (PrologContentType.DefaultFunctionNamespace, (string) $5);
611         }
612         ;
614 DefaultCollationDecl // returns SimplePrologContent 
615         : DECLARE DEFAULT COLLATION {
616                 tokenizer.State = ParseState.NamespaceDecl;
617           } STRING_LITERAL {
618                 tokenizer.State = ParseState.Default;
619           }
620         {
621                 $$ = new SimplePrologContent (PrologContentType.DefaultCollation, (string) $4);
622         }
623         ;
625 DefaultOrderingDecl // returns bool 
626         : DECLARE DEFAULT ORDERING {
627                 tokenizer.State = ParseState.Operator;
628           } ORDERED {
629                 tokenizer.State = ParseState.Default;
630           }
631         {
632                 $$ = true;
633         }
634         | DECLARE DEFAULT ORDERING {
635                 tokenizer.State = ParseState.Operator;
636           } UNORDERED {
637                 tokenizer.State = ParseState.Default;
638           }
639         {
640                 $$ = false;
641         }
642         ;
644 BaseURIDecl // returns SimplePrologContent 
645         : DECLARE BASEURI {
646                 tokenizer.State = ParseState.NamespaceDecl;
647           } STRING_LITERAL {
648                 tokenizer.State = ParseState.Default;
649           }
650         {
651                 $$ = new SimplePrologContent (PrologContentType.BaseUri, (string) $3);
652         }
653         ;
655 SchemaImport // returns SchemaImport 
656         : IMPORT SCHEMA {
657                 tokenizer.State = ParseState.NamespaceKeyword;
658           } OptionalSchemaPrefix STRING_LITERAL {
659                 tokenizer.State = ParseState.Default;
660           } OptionalLocations
661         {
662                 if ($4 != null)
663                         tokenizer.AddNamespace ((string) $4, (string) $5);
664                 $$ = new SchemaImport ((string) $4, (string) $5, (ICollection) $7);
665         }
666         ;
668 OptionalSchemaPrefix // returns string or null 
669         : // empty
670         {
671                 $$ = null;
672         }
673         | SchemaPrefix
674         ;
676 SchemaPrefix // returns string 
677         : NAMESPACE {
678                 tokenizer.State = ParseState.NamespaceDecl;
679           } NCName EQUAL {
680           }
681         {
682                 $$ = (string) $3;
683         }
684         | DEFAULT ELEMENT NAMESPACE {
685                 tokenizer.State = ParseState.NamespaceDecl;
686           }
687         {
688                 $$ = "";
689         }
690         ;
692 ModuleImport // returns ModuleImport 
693         : IMPORT MODULE {
694                 tokenizer.State = ParseState.NamespaceKeyword;
695           } OptionalModuleNamespace STRING_LITERAL {
696                 tokenizer.State = ParseState.Default;
697           } OptionalLocations
698         {
699                 $$ = new ModuleImport ((string) $4, (string) $5, (ICollection) $7);
700                 tokenizer.AddNamespace ((string) $4, (string) $5);
701         }
702         ;
704 OptionalModuleNamespace // returns string 
705         : // empty
706         {
707                 $$ = String.Empty;
708         }
709         | NAMESPACE {
710                 tokenizer.State = ParseState.NamespaceDecl;
711           } NCName EQUAL
712         {
713                 $$ = (string) $3;
714         }
715         ;
717 OptionalLocations // returns ArrayList or null 
718         : // empty
719         {
720                 $$ = null;
721         }
722         | AT STRING_LITERAL AdditionalLocations
723         {
724                 ArrayList al = (ArrayList) $3;
725                 if (al != null)
726                         al = new ArrayList ();
727                 al.Add ((string) $2);
728                 $$ = al;
729         }
730         ;
732 AdditionalLocations // returns ArrayList or null 
733         : // empty
734         {
735                 $$ = null;
736         }
737         | COMMA STRING_LITERAL AdditionalLocations
738         {
739                 ArrayList al = (ArrayList) $3;
740                 if (al == null)
741                         al = new ArrayList ();
742                 al.Add ((string) $2);
743                 $$ = al;
744         }
745         ;
747 VarDecl // returns VariableDeclaration 
748         : DECLARE VARIABLE DOLLAR {
749                 tokenizer.PushState (ParseState.Default);
750                 tokenizer.State = ParseState.VarName;
751           } VarName {
752                 tokenizer.State = ParseState.Operator;
753           } OptionalTypeDeclaration VarDeclBody
754         {
755                 $$ = new XQueryVariable ((XmlQualifiedName) $4, (SequenceType) $5, (ExprSequence) $6);
756         }
757         ;
759 VarDeclBody // returns ExprSequence or null 
760         : OPEN_CURLY {
761                 tokenizer.State = ParseState.Default;
762           } Expr CloseCurly
763         {
764                 $$ = $2;
765         }
766         | EXTERNAL {
767                 // LAMESPEC: This state transition is not described in XQuery 1.0 spec
768                 tokenizer.PopState ();
769           }
770         {
771                 $$ = null;
772         }
773         ;
775 VarName // returns XmlQualifiedName 
776         : QName {
777                 tokenizer.State = ParseState.Operator;
778           }
779         {
780                 $$ = $1;
781         }
782         ;
784 OptionalTypeDeclaration // returns SequenceType or null 
785         : // empty
786         {
787                 $$ = null;
788         }
789         | TypeDeclaration
790         ;
792 TypeDeclaration // returns SequenceType 
793         : AS { // Note that this transition applies only at Operator state.
794                 tokenizer.State = ParseState.ItemType;
795           } SequenceType
796         {
797                 $$ = $2;
798         }
799         ;
801 FunctionDecl // returns FunctionDeclaration 
802         : DECLARE FUNCTION {
803                 tokenizer.PushState (ParseState.Default);
804                 tokenizer.State = ParseState.Default;
805           } QName OPEN_PAREN OptionalParamList CLOSE_PAREN {
806                 tokenizer.State = ParseState.Operator;
807           }  OptionalTypeDeclaration FunctionBody
808         {
809                 $$ = new FunctionDeclaration (
810                         (XmlQualifiedName) $4,
811                         (XQueryFunctionArgumentList) $6,
812                         (SequenceType) $9,
813                         (EnclosedExpr) $10);
814         }
815         ;
817 FunctionBody // returns EnclosedExpr or null 
818         : EnclosedExpr
819         | EXTERNAL
820         {
821                 $$ = null;
822         }
823         ;
825 SequenceType // returns SequenceType 
826         : ItemType OptionalOccurenceIndicator
827         {
828                 $$ = new SequenceType ((ItemType) $1, (Occurence) $2);
829         }
830         | EMPTY OPEN_PAREN CLOSE_PAREN {
831                 tokenizer.State = ParseState.Operator;
832           }
833         {
834                 $$ = SequenceType.Create (XmlTypeCode.None, Occurence.One);
835         }
836         ;
838 OptionalOccurenceIndicator // returns Occurence 
839         : // empty
840         {
841                 $$ = Occurence.One;
842         }
843         | OccurenceIndicator
844         ;
846 OccurenceIndicator // returns Occurence 
847         : QUESTION
848         {
849                 $$ = Occurence.Optional;
850         }
851         | ASTERISK
852         {
853                 $$ = Occurence.ZeroOrMore;
854         }
855         | PLUS
856         {
857                 $$ = Occurence.OneOrMore;
858         }
859         ;
861 OptionalParamList // returns XQueryFunctionArgumentList
862         : // empty
863         {
864                 $$ = new XQueryFunctionArgumentList ();
865         }
866         | ParamList
867         ;
869 ParamList // returns XQueryFunctionArgumentList 
870         : Param
871         {
872                 XQueryFunctionArgumentList pl = new XQueryFunctionArgumentList ();
873                 pl.Add ((XQueryFunctionArgument) $1);
874                 $$ = pl;
875         }
876         | Param COMMA ParamList
877         {
878                 XQueryFunctionArgumentList pl = (XQueryFunctionArgumentList) $3;
879                 pl.Insert (0, (XQueryFunctionArgument) $1);
880                 $$ = pl;
881         }
882         ;
884 Param // returns XQueryFunctionArgument 
885         : DOLLAR {
886                 tokenizer.State = ParseState.VarName;
887           } VarName {
888                 tokenizer.State = ParseState.Operator;
889           } OptionalTypeDeclaration
890         {
891                 $$ = new XQueryFunctionArgument ((XmlQualifiedName) $3, (SequenceType) $5);
892         }
893         ;
895 QueryBody
896         : Expr
897         ;
900 /* --------------------------------------------------------
901         Expressions
902 -------------------------------------------------------- */
904 Expr // returns ExprSequence 
905         : ExprSequence
906         {
907                 ExprSequence seq = (ExprSequence) $1;
908                 $$ = seq;
909         }
910         ;
912 ExprSequence // returns ExprSequence 
913         : ExprSingle
914         {
915                 ExprSequence seq = new ExprSequence ();
916                 seq.Add ((ExprSingle) $1);
917                 $$ = seq;
918         }
919         | ExprSingle COMMA ExprSequence
920         {
921                 ExprSequence seq = (ExprSequence) $3;
922                 seq.Insert (0, (ExprSingle) $1);
923                 $$ = seq;
924         }
925         ;
927 ExprSingle // returns ExprSingle 
928         : FLWORExpr
929         | QuantifiedExpr
930         | TypeswitchExpr
931         | IfExpr
932         | OrExpr
933         ;
935 /* -------------------------------------
936         FLWORExpr
937 ------------------------------------- */
939 FLWORExpr // returns FLWORExpr 
940         : RepeatedForLetClause OptionalWhereClause OptionalOrderByClause RETURN {
941                 tokenizer.State = ParseState.Default;
942           } ExprSingle
943         {
944                 ForLetClauseCollection col = (ForLetClauseCollection) $1;
945                 $$ = new FLWORExpr (col, (ExprSequence) $2, (OrderSpecList) $3, (ExprSingle) $6);
946         }
947         ;
949 RepeatedForLetClause // returns ForLetClauseCollection 
950         : ForLetClause
951         {
952                 ForLetClauseCollection col = new ForLetClauseCollection ();
953                 col.Add ((ForLetClause) $1);
954                 $$ = col;
955         }
956         | ForLetClause RepeatedForLetClause
957         {
958                 ForLetClauseCollection col = (ForLetClauseCollection) $2;
959                 col.Insert (0, (ForLetClause) $1);
960                 $$ = col;
961         }
962         ;
964 ForLetClause // returns ForLetClause 
965         : ForClause
966         | LetClause
967         ;
969 OptionalWhereClause // returns ExprSequence or null 
970         : // empty
971         {
972                 $$ = null;
973         }
974         | WhereClause
975         ;
977 OptionalOrderByClause
978         : // empty
979         {
980                 $$ = null;
981         }
982         | OrderByClause
983         ;
985 ForClause // returns ForClause 
986         : FOR ForBody
987         {
988                 ForClause fc = (ForClause) $2;
989                 $$ = fc;
990         }
991         ;
993 ForBody
994         : ForSingleBody
995         {
996                 ForClause fc = new ForClause ();
997                 fc.Add ((ForSingleBody) $1);
998                 $$ = fc;
999         }
1000         | ForSingleBody COMMA ForBody
1001         {
1002                 ForClause fc = (ForClause) $3;
1003                 fc.Insert (0, (ForSingleBody) $1);
1004                 $$ = fc;
1005         }
1006         ;
1008 ForSingleBody // returns ForSingleBody 
1009         : DOLLAR {
1010                 tokenizer.State = ParseState.VarName;
1011           } VarName {
1012                 tokenizer.State = ParseState.Operator;
1013           } OptionalTypeDeclaration OptionalPositionalVar IN {
1014                 tokenizer.State = ParseState.Default;
1015           } ExprSingle
1016         {
1017                 $$ = new ForSingleBody ((XmlQualifiedName) $3, (SequenceType) $5, (XmlQualifiedName) $6, (ExprSingle) $9);
1018         }
1019         ;
1021 LetClause
1022         : LET LetBody
1023         {
1024                 LetClause let = (LetClause) $2;
1025                 $$ = let;
1026         }
1027         ;
1029 LetBody
1030         : LetSingleBody
1031         {
1032                 LetClause lc = new LetClause ();
1033                 lc.Add ((LetSingleBody) $1);
1034                 $$ = lc;
1035         }
1036         | LetSingleBody COMMA LetBody
1037         {
1038                 LetClause let = (LetClause) $3;
1039                 let.Insert (0, (LetSingleBody) $1);
1040                 $$ = let;
1041         }
1043 LetSingleBody
1044         : DOLLAR {
1045                 tokenizer.State = ParseState.VarName;
1046           } VarName {
1047                 tokenizer.State = ParseState.Operator;
1048           } OptionalTypeDeclaration COLON_EQUAL {
1049                 tokenizer.State = ParseState.Default;
1050           } ExprSingle
1051         {
1052                 $$ = new LetSingleBody ((XmlQualifiedName) $3, (SequenceType) $5, (ExprSingle) $8);
1053         }
1054         ;
1056 OptionalPositionalVar // returns XmlQualifiedName 
1057         : // empty
1058         {
1059                 $$ = XmlQualifiedName.Empty;
1060         }
1061         | PositionalVar
1062         ;
1064 PositionalVar
1065         : AT {
1066                 tokenizer.State = ParseState.Default;
1067           } DOLLAR {
1068                 tokenizer.State = ParseState.VarName;
1069           } VarName {
1070                 tokenizer.State = ParseState.Operator;
1071           }
1072         {
1073                 $$ = $5;
1074         }
1075         ;
1077 WhereClause // returns ExprSequence 
1078         : WHERE {
1079                 tokenizer.State = ParseState.Default;
1080           } Expr
1081         {
1082                 $$ = $3;
1083         }
1084         ;
1086 OrderByClause // returns OrderSpecList 
1087         : ORDER BY {
1088                 tokenizer.State = ParseState.Default;
1089           } OrderSpecList
1090         {
1091                 OrderSpecList l = (OrderSpecList) $4;
1092                 $$ = l;
1093         }
1094         | STABLE ORDER BY {
1095                 tokenizer.State = ParseState.Default;
1096           } OrderSpecList
1097         {
1098                 OrderSpecList l = (OrderSpecList) $5;
1099                 l.IsStable = true;
1100                 $$ = l;
1101         }
1102         ;
1104 OrderSpecList // returns OrderSpecList 
1105         : OrderSpec
1106         {
1107                 OrderSpecList osl = new OrderSpecList ();
1108                 osl.Add ((OrderSpec) $1);
1109                 $$ = osl;
1110         }
1111         | OrderSpec COMMA OrderSpecList
1112         {
1113                 OrderSpecList l = (OrderSpecList) $3;
1114                 l.Insert (0, (OrderSpec) $1);
1115                 $$ = l;
1116         }
1117         ;
1119 OrderSpec // returns OrderSpec 
1120         : ExprSingle OrderModifier
1121         {
1122                 $$ = new OrderSpec ((ExprSingle) $1, (OrderModifier) $2);
1123         }
1124         ;
1126 OrderModifier
1127         : OrderSpecBase OrderEmptyHandling OptionalCollation
1128         {
1129                 $$ = new OrderModifier ((XmlSortOrder) $1, (XmlSortOrder) $2, (string) $3);
1130         }
1131         ;
1133 OrderSpecBase // returns XmlSortOrder 
1134         : // empty
1135         {
1136                 $$ = XmlSortOrder.Ascending;
1137         }
1138         | ASCENDING
1139         {
1140                 $$ = XmlSortOrder.Ascending;
1141         }
1142         | DESCENDING
1143         {
1144                 $$ = XmlSortOrder.Descending;
1145         }
1146         ;
1148 /* FIXME: check if it is correct (especially empty case) */
1149 OrderEmptyHandling // returns XmlSortOrder 
1150         : // empty
1151         {
1152                 $$ = XmlSortOrder.Ascending;
1153         }
1154         | EMPTY GREATEST
1155         {
1156                 $$ = XmlSortOrder.Ascending;
1157         }
1158         | EMPTY LEAST
1159         {
1160                 $$ = XmlSortOrder.Descending;
1161         }
1162         ;
1164 OptionalCollation // returns string 
1165         : // empty
1166         {
1167                 $$ = null;
1168         }
1169         | COLLATION STRING_LITERAL
1170         {
1171                 $$ = $2;
1172         }
1173         ;
1175 /* -------------------------------------
1176         QuantifiedExpr
1177 ------------------------------------- */
1179 QuantifiedExpr
1180         : SOME QuantifiedExprBody SATISFIES ExprSingle
1181         {
1182                 QuantifiedExprBodyList l = (QuantifiedExprBodyList) $2;
1183                 $$ = new QuantifiedExpr (false, l, (ExprSingle) $4);
1184         }
1185         | EVERY QuantifiedExprBody SATISFIES ExprSingle
1186         {
1187                 QuantifiedExprBodyList l = (QuantifiedExprBodyList) $2;
1188                 $$ = new QuantifiedExpr (true, l, (ExprSingle) $4);
1189         }
1190         ;
1192 QuantifiedExprBody
1193         : SingleQuantifiedExprBody
1194         {
1195                 QuantifiedExprBodyList l = new QuantifiedExprBodyList ();
1196                 l.Add ((QuantifiedExprBody) $1);
1197                 $$ = l;
1198         }
1199         | SingleQuantifiedExprBody COMMA QuantifiedExprBody
1200         {
1201                 QuantifiedExprBodyList l = (QuantifiedExprBodyList) $3;
1202                 l.Insert (0, (QuantifiedExprBody) $1);
1203                 $$ = l;
1204         }
1205         ;
1207 SingleQuantifiedExprBody // returns QuantifiedExprBody 
1208         : DOLLAR {
1209                 tokenizer.State = ParseState.VarName;
1210           } VarName {
1211                 tokenizer.State = ParseState.Operator;
1212           } OptionalTypeDeclaration IN {
1213                 tokenizer.State = ParseState.Default;
1214           } ExprSingle
1215         {
1216                 $$ = new QuantifiedExprBody ((XmlQualifiedName) $3, (SequenceType) $5, (ExprSingle) $8);
1217         }
1218         ;
1220 /* -------------------------------------
1221         TypeswitchExpr
1222 ------------------------------------- */
1224 TypeswitchExpr // returns TypeswitchExpr 
1225         : TYPESWITCH OPEN_PAREN {
1226                 tokenizer.PushState (ParseState.Operator);
1227                 tokenizer.State = ParseState.Default;
1228           } Expr CLOSE_PAREN {
1229                 tokenizer.State = ParseState.Operator;
1230           } RepeatedCaseClause DEFAULT OptionalVarSpec RETURN {
1231                 tokenizer.State = ParseState.Default;
1232           } ExprSingle
1233         {
1234                 $$ = new TypeswitchExpr ((ExprSequence) $4, (CaseClauseList) $7, (XmlQualifiedName) $9, (ExprSingle) $12);
1235         }
1236         ;
1238 RepeatedCaseClause // returns CaseClauseList 
1239         : CaseClause
1240         {
1241                 CaseClauseList ccl = new CaseClauseList ();
1242                 ccl.Add ((CaseClause) $1);
1243                 $$ = ccl;
1244         }
1245         | CaseClause RepeatedCaseClause
1246         {
1247                 CaseClauseList l = (CaseClauseList) $2;
1248                 l.Add ((CaseClause) $1);
1249                 $$ = l;
1250         }
1251         ;
1253 CaseClause // returns CaseClause 
1254         : CASE {
1255                 tokenizer.State = ParseState.ItemType;
1256           } SequenceType RETURN {
1257                 tokenizer.State = ParseState.Default;
1258           } ExprSingle
1259         {
1260                 $$ = new CaseClause ((SequenceType) $3, (ExprSingle) $6, XmlQualifiedName.Empty);
1261         }
1262         | CASE {
1263                 tokenizer.State = ParseState.ItemType;
1264           } DOLLAR {
1265                 tokenizer.State = ParseState.VarName;
1266           } VarName {
1267                 tokenizer.State = ParseState.Operator;
1268           } AS {
1269                 tokenizer.State = ParseState.ItemType;
1270           } SequenceType RETURN {
1271                 tokenizer.State = ParseState.Default;
1272           } ExprSingle
1273         {
1274                 $$ = new CaseClause ((SequenceType) $9, (ExprSingle) $12, (XmlQualifiedName) $5);
1275         }
1276         ;
1278 OptionalVarSpec // returns XmlQualifiedName 
1279         : // empty
1280         {
1281                 $$ = XmlQualifiedName.Empty;
1282         }
1283         | DOLLAR VarName
1284         {
1285                 $$ = (XmlQualifiedName) $2;
1286         }
1287         ;
1289 /* -------------------------------------
1290         IfExpr
1291 ------------------------------------- */
1293 IfExpr
1294         : IF OPEN_PAREN Expr CLOSE_PAREN {
1295                 tokenizer.State = ParseState.Operator;
1296           } THEN {
1297                 tokenizer.State = ParseState.Default;
1298           } ExprSingle ELSE {
1299                 tokenizer.State = ParseState.Default;
1300           } ExprSingle
1301         {
1302                 $$ = new IfExpr ((ExprSequence) $3, (ExprSingle) $8, (ExprSingle) $11);
1303         }
1304         ;
1306 /* -------------------------------------
1307         Logical Expressions
1308 ------------------------------------- */
1310 OrExpr
1311         : AndExpr
1312         | AndExpr OR {
1313                 tokenizer.State = ParseState.Default;
1314           } OrExpr
1315         {
1316                 $$ = new OrExpr ((ExprSingle) $1, (ExprSingle) $4);
1317         }
1318         ;
1320 AndExpr
1321         : InstanceOfExpr
1322         | InstanceOfExpr AND {
1323                 tokenizer.State = ParseState.Default;
1324           } AndExpr
1325         {
1326                 $$ = new AndExpr ((ExprSingle) $1, (ExprSingle) $4);
1327         }
1328         ;
1330 /* -------------------------------------
1331         Typed Expressions
1332 ------------------------------------- */
1334 InstanceOfExpr
1335         : TreatExpr
1336         | TreatExpr INSTANCE OF {
1337                 tokenizer.State = ParseState.ItemType;
1338           } SequenceType
1339         {
1340                 $$ = new InstanceOfExpr ((ExprSingle) $1, (SequenceType) $5);
1341         }
1342         ;
1344 TreatExpr
1345         : CastableExpr
1346         | CastableExpr TREAT AS {
1347                 tokenizer.State = ParseState.ItemType;
1348           } SequenceType
1349         {
1350                 $$ = new TreatExpr ((ExprSingle) $1, (SequenceType) $5);
1351         }
1352         ;
1354 CastableExpr
1355         : CastExpr
1356         | CastExpr CASTABLE AS {
1357                 tokenizer.State = ParseState.ItemType;
1358           } AtomicType OptionalQuestion
1359         {
1360                 $$ = new CastableExpr ((ExprSingle) $1, (XmlTypeCode) $5, (bool)$6);
1361         }
1362         ;
1364 OptionalQuestion
1365         : // empty
1366         {
1367                 $$ = false;
1368         }
1369         | QUESTION
1370         {
1371                 $$ = true;
1372         }
1373         ;
1375 CastExpr
1376         : ComparisonExpr
1377         | ComparisonExpr CAST AS {
1378                 tokenizer.State = ParseState.ItemType;
1379           } AtomicType OptionalQuestion
1380         {
1381                 $$ = new CastExpr ((ExprSingle) $1, (XmlTypeCode) $5, (bool) $6);
1382         }
1383         ;
1385 /* -------------------------------------
1386         Comparison Expressions
1387 ------------------------------------- */
1389 ComparisonExpr
1390         : RangeExpr
1391         | RangeExpr Comp {
1392                 tokenizer.State = ParseState.Default;
1393           } RangeExpr
1394         {
1395                 $$ = new ComparisonExpr ((ExprSingle) $1, (ExprSingle) $4, (ComparisonOperator) $2);
1396         }
1397         ;
1399 Comp // returns ComparisonOperator 
1400         : ValueComp
1401         | GeneralComp
1402         | NodeComp
1403         ;
1405 ValueComp
1406         : EQ
1407         {
1408                 $$ = ComparisonOperator.ValueEQ;
1409         }
1410         | NE
1411         {
1412                 $$ = ComparisonOperator.ValueNE;
1413         }
1414         | LT
1415         {
1416                 $$ = ComparisonOperator.ValueLT;
1417         }
1418         | LE
1419         {
1420                 $$ = ComparisonOperator.ValueLE;
1421         }
1422         | GT
1423         {
1424                 $$ = ComparisonOperator.ValueGT;
1425         }
1426         | GE
1427         {
1428                 $$ = ComparisonOperator.ValueGE;
1429         }
1430         ;
1432 GeneralComp
1433         : EQUAL
1434         {
1435                 $$ = ComparisonOperator.GeneralEQ;
1436         }
1437         | NOT_EQUAL
1438         {
1439                 $$ = ComparisonOperator.GeneralNE;
1440         }
1441         | LESSER
1442         {
1443                 $$ = ComparisonOperator.GeneralLT;
1444         }
1445         | LESSER_EQUAL
1446         {
1447                 $$ = ComparisonOperator.GeneralLE;
1448         }
1449         | GREATER
1450         {
1451                 $$ = ComparisonOperator.GeneralGT;
1452         }
1453         | GREATER_EQUAL
1454         {
1455                 $$ = ComparisonOperator.GeneralGE;
1456         }
1457         ;
1459 NodeComp
1460         : IS
1461         {
1462                 $$ = ComparisonOperator.NodeIs;
1463         }
1464         | LESSER2
1465         {
1466                 $$ = ComparisonOperator.NodeFWD;
1467         }
1468         | GREATER2
1469         {
1470                 $$ = ComparisonOperator.NodeBWD;
1471         }
1472         ;
1474 RangeExpr
1475         : AdditiveExpr
1476         | AdditiveExpr TO {
1477                 tokenizer.State = ParseState.Default;
1478           } AdditiveExpr
1479         {
1480                 $$ = new RangeExpr ((ExprSingle) $1, (ExprSingle)$4);
1481         }
1482         ;
1484 /* -------------------------------------
1485         Arithmetic Expressions
1486 ------------------------------------- */
1488 AdditiveExpr
1489         : MultiplicativeExpr
1490         | MultiplicativeExpr PLUS {
1491                 tokenizer.State = ParseState.Default;
1492           } AdditiveExpr
1493         {
1494                 $$ = new ArithmeticOperationExpr ((ExprSingle) $1, (ExprSingle) $4, ArithmeticOperator.Add);
1495         }
1496         | MultiplicativeExpr MINUS {
1497                 tokenizer.State = ParseState.Default;
1498           } AdditiveExpr
1499         {
1500                 $$ = new ArithmeticOperationExpr ((ExprSingle) $1, (ExprSingle) $4, ArithmeticOperator.Sub);
1501         }
1502         ;
1504 MultiplicativeExpr 
1505         : UnaryExpr
1506         | UnaryExpr ASTERISK {
1507                 tokenizer.State = ParseState.Default;
1508           } MultiplicativeExpr
1509         {
1510                 $$ = new ArithmeticOperationExpr ((ExprSingle) $1, (ExprSingle) $4, ArithmeticOperator.Mul);
1511         }
1512         | UnaryExpr DIV {
1513                 tokenizer.State = ParseState.Default;
1514           } MultiplicativeExpr
1515         {
1516                 $$ = new ArithmeticOperationExpr ((ExprSingle) $1, (ExprSingle) $4, ArithmeticOperator.Div);
1517         }
1518         | UnaryExpr IDIV {
1519                 tokenizer.State = ParseState.Default;
1520           } MultiplicativeExpr
1521         {
1522                 $$ = new ArithmeticOperationExpr ((ExprSingle) $1, (ExprSingle) $4, ArithmeticOperator.IDiv);
1523         }
1524         | UnaryExpr MOD {
1525                 tokenizer.State = ParseState.Default;
1526           } MultiplicativeExpr
1527         {
1528                 $$ = new ArithmeticOperationExpr ((ExprSingle) $1, (ExprSingle) $4, ArithmeticOperator.IMod);
1529         }
1530         ;
1532 UnaryExpr
1533         : UnionExpr
1534         | MINUS UnionExpr
1535         {
1536                 $$ = new MinusExpr ((ExprSingle) $2);
1537         }
1538         | PLUS UnionExpr
1539         {
1540                 $$ = $2;
1541         }
1542         ;
1544 UnionExpr
1545         : IntersectExceptExpr
1546         | IntersectExceptExpr UNION {
1547                 tokenizer.State = ParseState.Default;
1548           } UnionExpr
1549         {
1550                 $$ = new GroupExpr ((ExprSingle) $1, (ExprSingle) $4, AggregationType.Union);
1551         }
1552         | IntersectExceptExpr BAR {
1553                 tokenizer.State = ParseState.Default;
1554           } UnionExpr
1555         {
1556                 $$ = new GroupExpr ((ExprSingle) $1, (ExprSingle) $4, AggregationType.Union);
1557         }
1558         ;
1560 IntersectExceptExpr
1561         : ValueExpr
1562         | ValueExpr INTERSECT {
1563                 tokenizer.State = ParseState.Default;
1564           } IntersectExceptExpr
1565         {
1566                 $$ = new GroupExpr ((ExprSingle) $1, (ExprSingle) $4, AggregationType.Intersect);
1567         }
1568         | ValueExpr EXCEPT {
1569                 tokenizer.State = ParseState.Default;
1570           } IntersectExceptExpr
1571         {
1572                 $$ = new GroupExpr ((ExprSingle) $1, (ExprSingle) $4, AggregationType.Except);
1573         }
1574         ;
1576 ValueExpr
1577         : ValidateExpr
1578         | PathExpr
1579         ;
1581 /* -----------------
1582  Validation Expressions
1583 ----------------- */
1585 // FIXME: Here state transition is not well-tracked.
1587 ValidateExpr // returns ValidateExpr 
1588         : VALIDATE OptionalValidationMode OPEN_CURLY {
1589                 tokenizer.State = ParseState.Default;
1590                 tokenizer.PushState (ParseState.Operator);
1591           } Expr CloseCurly
1592         {
1593                 $$ = new ValidateExpr ((XmlSchemaContentProcessing) $2, (ExprSequence) $6);
1594         }
1595         ;
1597 OptionalValidationMode // returns XmlSchemaContentProcessing 
1598         : // empty
1599         {
1600                 $$ = XmlSchemaContentProcessing.Strict; // FIXME: confirm
1601         }
1602         | LAX
1603         {
1604                 $$ = XmlSchemaContentProcessing.Lax;
1605         }
1606         | STRICT
1607         {
1608                 $$ = XmlSchemaContentProcessing.Strict;
1609         }
1610         ;
1612 /* -----------------
1613  Path Expressions
1614 ----------------- */
1616 PathExpr // returns PathExpr 
1617         : Slash
1618         {
1619                 $$ = new PathRootExpr ();
1620         }
1621         | Slash RelativePathExpr
1622         {
1623                 $$ = new PathSlashExpr (new PathRootExpr (), (ExprSingle) $2);
1624         }
1625         | Slash2 RelativePathExpr
1626         {
1627                 $$ = new PathSlash2Expr (new PathRootExpr (), (ExprSingle) $2);
1628         }
1629         | RelativePathExpr
1630         ;
1632 RelativePathExpr // returns PathExpr 
1633         : StepExpr
1634         | StepExpr Slash RelativePathExpr
1635         {
1636                 $$ = new PathSlashExpr ((ExprSingle) $1, (ExprSingle) $3);
1637         }
1638         | StepExpr Slash2 RelativePathExpr
1639         {
1640                 $$ = new PathSlash2Expr ((ExprSingle) $1, (ExprSingle) $3);
1641         }
1642         ;
1644 StepExpr // returns ExprSingle 
1645         : AxisStep
1646         | FilterStep
1647         ;
1649 AxisStep // returns PathExpr 
1650         : ForwardOrReverseStep
1651         | AxisStep Predicate
1652         {
1653                 $$ = new FilterStepExpr ((ExprSingle) $1, (ExprSequence) $2);
1654         }
1655         ;
1657 ForwardOrReverseStep // returns ExprSingle 
1658         : ForwardStep
1659         | ReverseStep
1660         ;
1662 Predicate
1663         : OPEN_BRACKET Expr CLOSE_BRACKET {
1664                 tokenizer.State = ParseState.Operator;
1665           }
1666         {
1667                 $$ = $2;
1668         }
1669         ;
1671 FilterStep // returns ExprSingle 
1672         : PrimaryExpr
1673         | FilterStep Predicate
1674         {
1675                 $$ = new FilterStepExpr ((ExprSingle) $1, (ExprSequence) $2);
1676         }
1677         ;
1679 ForwardStep // returns AxisStepExpr
1680         : ForwardAxis NodeTest
1681         {
1682                 $$ = new AxisStepExpr ((XPathAxis) $1, (XPath2NodeTest) $2);
1683         }
1684         | AbbrevForwardStep
1685         ;
1687 ReverseStep // returns AxisStepExpr
1688         : ReverseAxis NodeTest
1689         {
1690                 $$ = new AxisStepExpr ((XPathAxis) $1, (XPath2NodeTest) $2);
1691         }
1692         | AbbrevReverseStep
1693         ;
1695 ForwardAxis // returns XPathAxis 
1696         : CHILD COLON2
1697         {
1698                 $$ = XPathAxis.Child;
1699         }
1700         | DESCENDANT COLON2
1701         {
1702                 $$ = XPathAxis.Descendant;
1703         }
1704         | ATTRIBUTE COLON2
1705         {
1706                 $$ = XPathAxis.Attribute;
1707         }
1708         | SELF COLON2
1709         {
1710                 $$ = XPathAxis.Self;
1711         }
1712         | DESCENDANT_OR_SELF COLON2
1713         {
1714                 $$ = XPathAxis.DescendantOrSelf;
1715         }
1716         | FOLLOWING_SIBLING COLON2
1717         {
1718                 $$ = XPathAxis.FollowingSibling;
1719         }
1720         | FOLLOWING COLON2
1721         {
1722                 $$ = XPathAxis.Following;
1723         }
1724         ;
1726 ReverseAxis // returns XPathAxis 
1727         : PARENT COLON2
1728         {
1729                 $$ = XPathAxis.Parent;
1730         }
1731         | ANCESTOR COLON2
1732         {
1733                 $$ = XPathAxis.Ancestor;
1734         }
1735         | PRECEDING_SIBLING COLON2
1736         {
1737                 $$ = XPathAxis.PrecedingSibling;
1738         }
1739         | PRECEDING COLON2
1740         {
1741                 $$ = XPathAxis.Preceding;
1742         }
1743         | ANCESTOR_OR_SELF COLON2
1744         {
1745                 $$ = XPathAxis.AncestorOrSelf;
1746         }
1747         ;
1749 // LAMESPEC: in the XQuery spec, it is "@? NodeTest", but NodeKindTest 
1750 // should not appear after AT. (imagine @processing-instruction::(name)).
1751 AbbrevForwardStep // returns NodeTestExpr 
1752         : NodeTest
1753         {
1754                 $$ = new AxisStepExpr (XPathAxis.Child, (XPath2NodeTest) $1);
1755         }
1756         | AT NameTest
1757         {
1758                 $$ = new AxisStepExpr (XPathAxis.Attribute, new XPath2NodeTest ((XmlQualifiedName) $2));
1759         }
1760         ;
1762 AbbrevReverseStep // returns AxisStepExpr
1763         : DOT2
1764         {
1765                 $$ = new AxisStepExpr (XPathAxis.Parent, null);
1766         }
1767         ;
1769 NodeTest // returns NodeTest
1770         : KindTest
1771         {
1772                 $$ = new XPath2NodeTest ((KindTest) $1);
1773         }
1774         | NameTest
1775         {
1776                 $$ = new XPath2NodeTest ((XmlQualifiedName) $1);
1777         }
1778         ;
1780 NameTest // returns XmlQualifiedName 
1781         : QName
1782         | Wildcard {
1783                 tokenizer.State = ParseState.Operator;
1784           }
1785         ;
1787 Wildcard // returns XmlQualifiedName  /*ws:explicit*/
1788         : ASTERISK
1789         {
1790                 $$ = new XmlQualifiedName ("*", "*");
1791         }
1792         | WILD_LOCALNAME
1793         {
1794                 $$ = (XmlQualifiedName) $1;
1795         }
1796         | WILD_PREFIX
1797         {
1798                 $$ = (XmlQualifiedName) $1;
1799         }
1800         ;
1802 AtomicType // returns XmlTypeCode 
1803         : QName
1804         {
1805                 $$ = GetAtomicTypeCode ((XmlQualifiedName) $1);
1806         }
1807         ;
1809 ItemType // returns ItemType
1810         : AtomicType {
1811                 tokenizer.State = ParseState.OccurenceIndicator;
1812           }
1813         {
1814                 $$ = new ItemType ((XmlTypeCode) $1);
1815         }
1816         | KindTest
1817         | ITEM OPEN_PAREN CLOSE_PAREN {
1818                 tokenizer.State = ParseState.OccurenceIndicator;
1819           }
1820         {
1821                 $$ = new ItemType (XmlTypeCode.Item);
1822         }
1823         ;
1825 KindTest // returns KindTest
1826         : DocumentTest
1827         | ElementTest
1828         | AttributeTest
1829         | PITest
1830         | CommentTest
1831         | TextTest
1832         | AnyKindTest
1833         ;
1835 PITest
1836         : PROCESSING_INSTRUCTION OPEN_PAREN {
1837                 // LAMESPEC: push state is described as Operator, but should 
1838                 // be OccurenceIndicator (as any KindTest could be followed by
1839                 // '?' '+' or '*')
1840                 tokenizer.PushState (ParseState.OccurenceIndicator);
1841                 tokenizer.State = ParseState.KindTestForPI;
1842           } PITestContent CLOSE_PAREN {
1843                 tokenizer.PopState ();
1844           }
1845         {
1846                 $$ = $4;
1847         }
1848         ;
1850 PITestContent // returns KindTest
1851         : // empty
1852         {
1853                 $$ = new KindTest (XmlTypeCode.ProcessingInstruction);
1854         }
1855         | NCName
1856         {
1857                 $$ = new XmlPITest ((string) $1);
1858         }
1859         | STRING_LITERAL
1860         {
1861                 $$ = new XmlPITest ((string) $1);
1862         }
1863         ;
1865 CommentTest
1866         : COMMENT OPEN_PAREN {
1867                 tokenizer.PushState (ParseState.OccurenceIndicator);
1868                 tokenizer.State = ParseState.KindTest;
1869           } CLOSE_PAREN {
1870                 tokenizer.PopState ();
1871           }
1872         {
1873                 $$ = new KindTest (XmlTypeCode.Comment);
1874         }
1875         ;
1877 TextTest
1878         : TEXT OPEN_PAREN {
1879                 tokenizer.PushState (ParseState.OccurenceIndicator);
1880                 tokenizer.State = ParseState.KindTest;
1881           } CLOSE_PAREN {
1882                 tokenizer.PopState ();
1883           }
1884         {
1885                 $$ = new KindTest (XmlTypeCode.Text);
1886         }
1887         ;
1889 AnyKindTest
1890         : NODE OPEN_PAREN {
1891                 tokenizer.PushState (ParseState.OccurenceIndicator);
1892                 tokenizer.State = ParseState.KindTest;
1893           } CLOSE_PAREN {
1894                 tokenizer.PopState ();
1895           }
1896         {
1897                 $$ = new KindTest (XmlTypeCode.Node);
1898         }
1899         ;
1901 DocumentTest // returns DocumentTest
1902         : DOCUMENT_NODE OPEN_PAREN {
1903                 tokenizer.PushState (ParseState.OccurenceIndicator);
1904                 tokenizer.State = ParseState.KindTest;
1905           } DocumentTestContent CLOSE_PAREN {
1906                 tokenizer.PopState ();
1907           }
1908         {
1909                 $$ = $4;
1910         }
1911         ;
1913 DocumentTestContent // returns DocumentTest
1914         : // empty
1915         {
1916                 $$ = new KindTest (XmlTypeCode.Document);
1917         }
1918         | ElementTest
1919         {
1920                 $$ = new DocumentTest ((ElementTest) $1);
1921         }
1922         ;
1924 ElementTest // returns ElementTest
1925         : ELEMENT OPEN_PAREN {
1926                 tokenizer.PushState (ParseState.OccurenceIndicator);
1927                 tokenizer.State = ParseState.KindTest;
1928           } ElementTestContent CLOSE_PAREN {
1929                 tokenizer.PopState ();
1930           }
1931         {
1932                 $$ = $4;
1933         }
1934         ;
1936 ElementTestContent // returns ElementTest
1937         : // empty
1938         {
1939                 $$ = new KindTest (XmlTypeCode.Element);
1940         }
1941         | ElementNameOrWildcard
1942         {
1943                 $$ = new ElementTest ((XmlQualifiedName) $1);
1944         }
1945         | ElementNameOrWildcard COMMA TypeName OptionalQuestion
1946         {
1947                 $$ = new ElementTest ((XmlQualifiedName) $1, (XmlQualifiedName) $3, (bool) $4);
1948         }
1949         ;
1951 OptionalQuestion // returns bool 
1952         : // empty
1953         {
1954                 $$ = false;
1955         }
1956         | QUESTION
1957         {
1958                 $$ = true;
1959         }
1960         ;
1962 AttributeTest // returns AttributeTest
1963         : ATTRIBUTE OPEN_PAREN {
1964                 tokenizer.PushState (ParseState.OccurenceIndicator);
1965                 tokenizer.State = ParseState.KindTest;
1966           } AttributeTestContent CLOSE_PAREN {
1967                 tokenizer.PopState ();
1968           }
1969         {
1970                 $$ = $4;
1971         }
1972         ;
1974 AttributeTestContent // returns AttributeTest
1975         : // empty
1976         {
1977                 $$ = AttributeTest.AnyAttribute;
1978         }
1979         | AttributeNameOrWildcard
1980         {
1981                 $$ = new AttributeTest ((XmlQualifiedName) $1);
1982         }
1983         | AttributeNameOrWildcard COMMA TypeNameOrWildcard
1984         {
1985                 $$ = new AttributeTest ((XmlQualifiedName) $1, (XmlQualifiedName) $3);
1986         }
1987         ;
1989 ElementName
1990         : QName
1991         ;
1993 AttributeName
1994         : QName
1995         ;
1997 TypeName
1998         : QName
1999         ;
2001 ElementNameOrWildcard // returns XmlQualifiedName 
2002         : ElementName
2003         | ASTERISK
2004         {
2005                 $$ = new XmlQualifiedName ("*", "*");
2006         }
2007         ;
2009 AttributeNameOrWildcard // returns XmlQualifiedName 
2010         : AttributeName
2011         | ASTERISK
2012         {
2013                 $$ = new XmlQualifiedName ("*", "*");
2014         }
2015         ;
2017 TypeNameOrWildcard // returns XmlQualifiedName 
2018         : TypeName
2019         | ASTERISK
2020         {
2021                 $$ = new XmlQualifiedName ("*", "*");
2022         }
2023         ;
2025 /* ----------------
2026  Primary Expressions
2027 ---------------- */
2029 PrimaryExpr // returns ExprSingle 
2030         : Literal {
2031                 tokenizer.State = ParseState.Operator;
2032           }
2033         | VarRef
2034         | ParenthesizedExpr
2035         | ContextItemExpr
2036         | FunctionCall
2037         | Constructor
2038         | OrderedExpr
2039         | UnorderedExpr
2040         ;
2042 Literal
2043         : DECIMAL_LITERAL
2044         {
2045                 $$ = new DecimalLiteralExpr ((decimal) $1);
2046         }
2047         | DOUBLE_LITERAL
2048         {
2049                 $$ = new DoubleLiteralExpr ((double) $1);
2050         }
2051         | STRING_LITERAL
2052         {
2053                 $$ = new StringLiteralExpr ((string) $1);
2054         }
2055         ;
2058 NUMERIC_LITERAL
2059         : IntegerLiteral
2060         | DecimalLiteral
2061         | DoubleLiteral
2062         ;
2064 IntegerLiteral
2065         : Digits
2066         ;
2068 DecimalLiteral
2069         : DOT Digits
2070         | Digits DOT OptDigits
2071         ;
2073 DoubleLiteral
2074         : DoubleDecimalPartSpec ExponentSpec
2075         ;
2077 DoubleDecimalPartSpec
2078         : DOT Digits
2079         | Digits
2080         | Digits DOT OptDigits
2082 ExponentSpec
2083         : // empty/*
2084         | ExponentAlpha Digits
2085         | ExponentAlpha PLUS Digits
2086         | ExponentAlpha MINUS Digits
2087         ;
2089 ExponentAlpha
2090         : LARGE_E
2091         | SMALL_E
2092         ;
2094 OptDigits
2095         : // empty/*
2096         | Digits
2097         ;
2099 Digits
2100         : ZERO_TO_NINE_SEQ
2101         ;
2103 STRING_LITERAL
2104         : QUOT STRING_LITERALContentQuot
2105         | APOS STRING_LITERALContentApos
2106         ;
2108 STRING_LITERALContentQuot
2109         : PREDEFINED_ENTITY_REF
2110         | CHAR_REF
2111         | QUOT2
2112         | REMAINING_LITERAL
2113         ;
2115 STRING_LITERALContentApos
2116         : PREDEFINED_ENTITY_REF
2117         | CHAR_REF
2118         | APOS2
2119         | REMAINING_LITERAL
2120         ;
2123 VarRef // returns VariableReferenceExpr 
2124         : DOLLAR {
2125                 tokenizer.State = ParseState.VarName;
2126           } VarName
2127         {
2128                 $$ = new VariableReferenceExpr ((XmlQualifiedName) $3);
2129         }
2130         ;
2132 VarName // returns XmlQualifiedName 
2133         : QName {
2134                 tokenizer.State = ParseState.Operator;
2135           }
2136         {
2137                 $$ = $1;
2138         }
2139         ;
2141 ParenthesizedExpr // returns ParenthesizedExpr 
2142         : OPEN_PAREN OptionalExpr CLOSE_PAREN {
2143                 tokenizer.State = ParseState.Operator;
2144           }
2145         {
2146                 $$ = new ParenthesizedExpr ((ExprSequence) $2);
2147         }
2148         ;
2150 OptionalExpr
2151         : // empty
2152         {
2153                 $$ = new ExprSequence ();
2154         }
2155         | Expr
2156         ;
2158 ContextItemExpr // returns ContextItemExpr 
2159         : DOT {
2160                 tokenizer.State = ParseState.Operator;
2161           }
2162         {
2163                 $$ = new ContextItemExpr ();
2164         }
2165         ;
2167 FunctionCall
2168         : QName OPEN_PAREN {
2169                 tokenizer.State = ParseState.Default;
2170           } OptionalExpr CLOSE_PAREN {
2171                 tokenizer.State = ParseState.Operator;
2172           }
2173         {
2174                 XmlQualifiedName name = (XmlQualifiedName) $1;
2175                 if (name.Namespace == "")
2176                         name = new XmlQualifiedName (name.Name, tokenizer.DefaultFunctionNamespace);
2177                 if (name.Namespace != ""
2178                                 && name.Namespace != InternalPool.XdtNamespace
2179                                 && reservedFunctionNames [name.Name] != null)
2180                         throw new XmlQueryCompileException (String.Format ("'{0}' is reserved and cannot be used as a function name.", name.Name));
2182                 $$ = new FunctionCallExpr (name, (ExprSequence) $4);
2183         }
2184         ;
2186 OrderedExpr
2187         : ORDERED OPEN_BRACKET Expr CLOSE_BRACKET
2188         {
2189                 $$ = new OrderSpecifiedExpr ((ExprSequence) $3, true);
2190         }
2191         ;
2193 UnorderedExpr
2194         : UNORDERED OPEN_BRACKET {
2195                 tokenizer.PushState (ParseState.Operator);
2196                 tokenizer.State = ParseState.Default;
2197           } Expr CLOSE_BRACKET {
2198                 tokenizer.PopState ();
2199           }
2200         {
2201                 $$ = new OrderSpecifiedExpr ((ExprSequence) $4, false);
2202         }
2203         ;
2206 /* -----------------
2207  Constructors
2208 ----------------- */
2211 Constructor // returns ExprSingle 
2212         : DirElemConstructor
2213         | ComputedConstructor
2214         | XmlComment
2215         | XmlPI
2216         | XmlCData
2217         ;
2219 DirElemConstructor // returns XmlElemConstructor 
2220         : LESSER {
2221                 if (tokenizer.State == ParseState.ElementContent)
2222                         tokenizer.PushState (tokenizer.State);
2223 //              if (tokenizer.State == ParseState.Default)
2224                 else // considering <foo></foo><bar></bar> there after </foo> state is Operator.
2225                         tokenizer.PushState (ParseState.Operator);
2226                 tokenizer.State = ParseState.StartTag;
2227                 // FIXME: tokenizer.Space = WhitespaceHandling.Significant;
2228           } QName AttributeList FollowDirElemConstructor {
2229                 tokenizer.PopState ();
2230           }
2231         {
2232                 ExprSequence expr = new ExprSequence ();
2233                 expr.AddRange ((ICollection) $4);
2234                 expr.AddRange ((ICollection) $5);
2235                 $$ = new XmlElemConstructor ((XmlQualifiedName) $3, expr);
2236         }
2237         ;
2239 FollowDirElemConstructor // returns ExprSequence 
2240         : EMPTY_TAG_CLOSE
2241         {
2242                 $$ = new ExprSequence ();
2243         }
2244         | GREATER {
2245                 tokenizer.State = ParseState.ElementContent;
2246           } ElementContentList END_TAG_START {
2247                 tokenizer.State = ParseState.EndTag;
2248           } QName {
2249 //              tokenizer.Space = WhitespaceHandling.Arbitrary;
2250           } GREATER
2251         {
2252                 $$ = $3;
2253         }
2254         ;
2256 ElementContentList // returns ExprSequence 
2257         : // empty
2258         {
2259                 $$ = new ExprSequence ();
2260         }
2261         | ElementContent ElementContentList
2262         {
2263                 ExprSequence el = (ExprSequence) $2;
2264                 el.Insert (0, (ExprSingle) $1);
2265                 $$ = el;
2266         }
2267         ;
2269 AttributeList // returns XmlAttrConstructorList 
2270         : // empty
2271         {
2272                 $$ = new XmlAttrConstructorList ();
2273         }
2274         | /* space */ Attribute AttributeList
2275         {
2276                 XmlAttrConstructorList al = (XmlAttrConstructorList) $2;
2277                 al.Insert (0, (XmlAttrConstructor) $1);
2278                 $$ = al;
2279         }
2280         ;
2282 Attribute // returns XmlAttrConstructor
2283         : QName /* opt-space */ EQUAL /* opt-space */ AttributeValue
2284         {
2285                 $$ = new XmlAttrConstructor ((XmlQualifiedName) $1, (ExprSequence) $3);
2286         }
2287         ;
2290 // FIXME: it should be more complex
2291 AttributeValue // returns ExprSequence
2292         : STRING_LITERAL
2293         {
2294                 ExprSequence es = new ExprSequence ();
2295                 es.Insert (0, new StringLiteralExpr ((string) $1));
2296                 $$ = es;
2297         }
2298         ;
2301 AttributeValue // returns ExprSequence
2302         : QUOT {
2303                 tokenizer.State = ParseState.QuotAttributeContent;
2304           } AttributeValueContentSequence QUOT {
2305                 tokenizer.State = ParseState.StartTag;
2306           }
2307         {
2308                 $$ = $3;
2309         }
2310         | APOS {
2311                 tokenizer.State = ParseState.AposAttributeContent;
2312           } AttributeValueContentSequence APOS {
2313                 tokenizer.State = ParseState.StartTag;
2314           }
2315         {
2316                 $$ = $3;
2317         }
2318         ;
2321 AttributeValueContentSequence // returns ExprSequence
2322         : // empty
2323         {
2324                 $$ = new ExprSequence ();
2325         }
2326         | AttributeValueContent AttributeValueContentSequence
2327         {
2328                 ExprSequence es = (ExprSequence) $2;
2329                 es.Insert (0, (ExprSingle) $1);
2330                 $$ = es;
2331         }
2332         ;
2334 AttributeValueContent // returns ExprSingle
2335         : ATT_VALUE_LITERAL // including "{{", "}}" and char/predefined entities
2336         {
2337                 $$ = new StringLiteralExpr ((string) $1);
2338         }
2339         | EnclosedExpr
2340         ;
2342 EnclosedExpr // returns EnclosedExpr 
2343         // FIXME: check if this state transition is valid for ElementContent and AttributeValueContent
2344         : OPEN_CURLY {
2345                 switch (tokenizer.State) {
2346                 case ParseState.ElementContent:
2347                 case ParseState.QuotAttributeContent:
2348                 case ParseState.AposAttributeContent:
2349                         tokenizer.PushState (tokenizer.State);
2350                         break;
2351                 }
2352                 tokenizer.State = ParseState.Default;
2353           } Expr CloseCurly
2354         {
2355                 $$ = new EnclosedExpr ((ExprSequence) $3);
2356         }
2357         ;
2359 ElementContent // returns ExprSingle 
2360         : ELEM_CONTENT_LITERAL // including "{{", "}}" and char/predefined entities
2361         {
2362                 $$ = new XmlTextConstructor ((string) $1);
2363         }
2364         | DirElemConstructor
2365         | EnclosedExpr
2366         | XmlCData
2367         | XmlComment
2368         | XmlPI
2369         ;
2371 XmlCData
2372         : XML_CDATA_START XML_CDATA_TO_END
2373         {
2374                 $$ = new XmlTextConstructor ((string) $2);
2375         }
2376         ;
2378 XmlComment // returns XmlCommentConstructor 
2379         : XML_COMMENT_START XML_COMMENT_TO_END
2380         {
2381                 $$ = new XmlCommentConstructor ((string) $2);
2382         }
2383         ;
2385 XmlPI // returns XmlPIConstructor 
2386         : XML_PI_START {
2387                 tokenizer.PushState (tokenizer.State);
2388                 tokenizer.State = ParseState.XmlPI;
2389           } PITarget {
2390                 tokenizer.State = ParseState.XmlPIContent;
2391           } XML_PI_TO_END {
2392                 tokenizer.PopState ();
2393           }
2394         {
2395                 string name = (string) $3;
2396                 $$ = new XmlPIConstructor (name, (string) $5);
2397         }
2398         ;
2400 PITarget
2401         : NCName
2402         ;
2404 ComputedConstructor // returns ExprSingle 
2405         : CompElemConstructor
2406         | CompAttrConstructor
2407         | CompDocConstructor
2408         | CompTextConstructor
2409         | CompXmlPI
2410         | CompXmlComment
2411         | CompNSConstructor
2412         ;
2414 CompElemConstructor
2415         : ELEMENT QName OPEN_CURLY Expr CloseCurly
2416         {
2417                 $$ = new XmlElemConstructor ((XmlQualifiedName) $2, (ExprSequence) $4);
2418         }
2419         | ELEMENT OPEN_CURLY Expr CloseCurly OPEN_CURLY Expr CloseCurly
2420         {
2421                 $$ = new XmlElemConstructor ((ExprSequence) $3, (ExprSequence) $6);
2422         }
2423         ;
2425 CompAttrConstructor
2426         : ATTRIBUTE QName OPEN_CURLY Expr CloseCurly
2427         {
2428                 $$ = new XmlAttrConstructor ((XmlQualifiedName) $2, (ExprSequence) $4);
2429         }
2430         | ATTRIBUTE OPEN_CURLY Expr CloseCurly OPEN_CURLY Expr CloseCurly
2431         {
2432                 $$ = new XmlAttrConstructor ((ExprSequence) $3, (ExprSequence) $6);
2433         }
2434         ;
2436 CompNSConstructor
2437         : NAMESPACE NCName OPEN_CURLY Expr CloseCurly
2438         {
2439                 $$ = new XmlNSConstructor ((string) $2, (ExprSequence) $4);
2440         }
2441         ;
2443 CompDocConstructor
2444         : DOCUMENT OPEN_CURLY Expr CloseCurly
2445         {
2446                 $$ = new XmlDocConstructor ((ExprSequence) $3);
2447         }
2448         ;
2450 CompTextConstructor
2451         : TEXT OPEN_CURLY Expr CloseCurly
2452         {
2453                 $$ = new XmlTextConstructor ((ExprSequence) $3);
2454         }
2455         ;
2457 CompXmlComment
2458         : COMMENT OPEN_CURLY Expr CloseCurly
2459         {
2460                 $$ = new XmlCommentConstructor ((ExprSequence) $3);
2461         }
2462         ;
2464 CompXmlPI
2465         : PROCESSING_INSTRUCTION NCName OPEN_CURLY Expr CloseCurly
2466         {
2467                 $$ = new XmlPIConstructor ((string) $2, (ExprSequence) $4);
2468         }
2469         | PROCESSING_INSTRUCTION OPEN_CURLY Expr CloseCurly OPEN_CURLY Expr CloseCurly
2470         {
2471                 $$ = new XmlPIConstructor ((ExprSequence) $3, (ExprSequence) $6);
2472         }
2473         ;
2475 /* -----------------
2476  Terminal Wrappers
2477 ----------------- */
2480 NCName // returns string 
2481         : NCNAME
2482         ;
2484 QName // returns XmlQualifiedName. Note that this state is just a wrapper for state transition. 
2485         : QNAME {
2486                 switch (tokenizer.State) {
2487                 case ParseState.Default:
2488                         tokenizer.State = ParseState.Operator;
2489                         break;
2490                 case ParseState.ItemType:
2491                         tokenizer.State = ParseState.OccurenceIndicator;
2492                         break;
2493                 case ParseState.KindTest:
2494                 case ParseState.SchemaContextStep:
2495                         tokenizer.State = ParseState.CloseKindTest;
2496                         break;
2497                 case ParseState.ExtKey:
2498                         tokenizer.State = ParseState.ExprComment;
2499                         break;
2500                 }
2501           }
2502         ;
2504 Slash
2505         : SLASH {
2506                 switch (tokenizer.State) {
2507                 case ParseState.Operator:
2508                         tokenizer.State = ParseState.Default;
2509                         break;
2510                 case ParseState.KindTest:
2511                         tokenizer.State = ParseState.SchemaContextStep;
2512                         break;
2513                 }
2514           }
2515         ;
2517 Slash2
2518         : SLASH2 {
2519                 if (tokenizer.State == ParseState.Operator)
2520                         tokenizer.State = ParseState.Default;
2521           }
2522         ;
2524 CloseCurly
2525         : CLOSE_CURLY {
2526                 if (tokenizer.State == ParseState.Operator)
2527                         tokenizer.PopState ();
2528           }
2529         ;
2535 #endif