**** Merged from MCS ****
[mono-project.git] / mcs / class / Commons.Xml.Relaxng / Commons.Xml.Relaxng.Rnc / RncParser.jay
blobb9442a8b25aefdb9e1896729599758b7fc12a399
1 %{
2 //
3 // RELAX NG Compact Syntax parser
4 //
5 // Author:
6 //      Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
7 //
8 // (C)2003 Atsushi Enomoto
9 //
10 // Copyright (c) 2004 Novell Inc.
11 // All rights reserved
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System;
36 using System.Collections;
37 using System.Collections.Specialized;
38 using System.IO;
39 using System.Xml;
40 using Commons.Xml.Relaxng;
42 namespace Commons.Xml.Relaxng.Rnc
45         public class RncParser
46         {
47                 public static RelaxngPattern ParseRnc (TextReader reader)
48                 {
49                         return ParseRnc (reader, new NameTable ());
50                 }
52                 public static RelaxngPattern ParseRnc (TextReader reader, XmlNameTable nameTable)
53                 {
54                         return new RncParser (nameTable).Parse (reader);
55                 }
57                 XmlNamespaceManager nsmgr;
58                 XmlNamespaceManager dtnsmgr;
59                 string defaultNamespace = String.Empty;
61                 RncTokenizer tokenizer;
63                 public RncParser (XmlNameTable nameTable)
64                 {
65                         nsmgr = new XmlNamespaceManager (nameTable);
66                         dtnsmgr = new XmlNamespaceManager (nameTable);
67                         ErrorOutput = System.IO.TextWriter.Null;
68                 }
70                 public int Line {
71                         get { return tokenizer.Line; }
72                 }
74                 public int Column {
75                         get { return tokenizer.Column; }
76                 }
78                 // note that this is different notion than that of xmlns.
79                 public string DefaultNamespace {
80                         get { return defaultNamespace; }
81                 }
83                 public RelaxngPattern Parse (TextReader source)
84                 {
85                         try {
86 //                              debug = new yydebug.yyDebugSimple ();
87                                 tokenizer = new RncTokenizer (source);
88                                 return (RelaxngPattern) yyparse (tokenizer);
89                         } catch (Exception ex) {\r
90                                 throw new RelaxngException (String.Format ("Tokenizer error at line {0}, column {1}: {2}", Line, Column, ex.Message), ex);\r
91                         }\r
92                 }
95                 private void FillGrammarContent (IList source, IList starts, IList defines, IList divs, IList includes)
96                 {
97                         foreach (RelaxngElementBase elem in source) {
98                                 if (elem is RelaxngStart)
99                                         starts.Add (elem);
100                                 else if (elem is RelaxngDefine)
101                                         defines.Add (elem);
102                                 else if (elem is RelaxngDiv)
103                                         divs.Add (elem);
104                                 else if (elem is RelaxngInclude)
105                                         includes.Add (elem);
106                                 else
107                                         throw new InvalidOperationException ();
108                         }
109                 }
111                 private XmlQualifiedName SplitQName (XmlNamespaceManager nsmgr, string name)
112                 {
113                         int colon = name.IndexOf (':');
114                         if (colon < 0)
115                                 return new XmlQualifiedName (name, String.Empty);
116                         string local = name.Substring (colon + 1);
117                         string prefix = name.Substring (0, colon);
118                         return new XmlQualifiedName (local, nsmgr.LookupNamespace (nsmgr.NameTable.Get (prefix)));
119                 }
121                 private void FillElementDefaultNS (RelaxngNameClass nc)
122                 {
123                         RelaxngName name = nc as RelaxngName;
124                         if (name != null) {
125                                 if (name.Namespace == null)
126                                         name.Namespace = this.DefaultNamespace;
127                                 return;
128                         }
129                         RelaxngNameChoice choice = nc as RelaxngNameChoice;
130                         if (choice != null) {
131                                 foreach (RelaxngNameClass c in choice.Children)
132                                         FillElementDefaultNS (c);
133                         }
134                 }
136                 private void FillAttributeDefaultNS (RelaxngNameClass nc)
137                 {
138                         RelaxngName name = nc as RelaxngName;
139                         if (name != null) {
140                                 if (name.Namespace == null)
141                                         name.Namespace = String.Empty;
142                                 return;
143                         }
144                         RelaxngNameChoice choice = nc as RelaxngNameChoice;
145                         if (choice != null) {
146                                 foreach (RelaxngNameClass c in choice.Children)
147                                         FillAttributeDefaultNS (c);
148                         }
149                 }
153 %token ERROR
154 %token EOF
156 %token KeywordAttribute "attribute"
157 %token KeywordDefault //"default"
158 %token KeywordDatatypes "datatypes"
159 %token KeywordDiv "div"
160 %token KeywordElement "element"
161 %token KeywordEmpty "empty"
162 %token KeywordExternal "external"
163 %token KeywordGrammar "grammar"
164 %token KeywordInclude "include"
165 %token KeywordInherit "inherit"
166 %token KeywordList "list"
167 %token KeywordMixed "mixed"
168 %token KeywordNamespace //"namespace"
169 %token KeywordNotAllowed "notAllowed"
170 %token KeywordParent "parent"
171 %token KeywordStart "start"
172 %token KeywordString //"string"
173 %token KeywordText "text"
174 %token KeywordToken "left"
176 %token Equal "="
177 %token Comma ","
178 %token Tilde "~"
179 %token OpenCurly "{"
180 %token CloseCurly "}"
181 %token OpenParen "("
182 %token CloseParen ")"
183 %token OpenBracket "["
184 %token CloseBracket "]"
185 %token Amp "&"
186 %token Bar "|"
187 %token Question "?"
188 %token Asterisk "*"
189 %token BackSlash "\\"
190 %token Plus "+"
191 %token Minus "-"
192 %token OrEquals "|="
193 %token AndEquals "&="
194 %token TwoGreaters ">>"
196 %token LiteralSegment
197 %token NCNameButKeyword
199 %token Documentation
201 /* These tokens are parsed by RncTokenizer, since whitespaces between 
202    the particles are not allowed. */
203 %token NCName
204 %token CName
205 %token NsName
207 %start TopLevel
212 TopLevel /* returns RelaxngPattern */
213         /* TODO: here Annotations is not specified in the spec!! */
214         : Annotations TopLevelAfterAnnotations 
215         {
216                 $$ = (RelaxngPattern) $2;
217         }
218         ;
220 TopLevelAfterAnnotations /* returns RelaxngPattern */
221         /* TODO: here Annotations is not specified in the spec!! */
222         : Preamble TopLevelBody
223         {
224                 $$ = (RelaxngPattern) $2;
225         }
226         ;
228 Preamble /* returns null */
229         : /* empty */
230         {
231                 $$ = null;
232         }
233         | Decl Preamble
234         {
235                 $$ = null;
236         }
237         ;
239 Decl /* returns null */
240         : KeywordNamespace NamespacePrefix Equal NamespaceURILiteral
241         {
242                 // TODO: constraints
243                 string prefix = (string) $2;
244                 string ns = (string) $4;
245                 if (prefix == "local")
246                         nsmgr.AddNamespace (String.Empty, ns);
247                 else
248                         nsmgr.AddNamespace (prefix, ns);
249                 $$ = null;
250         }
251         | KeywordDefault KeywordNamespace Equal NamespaceURILiteral
252         {
253                 // TODO: constraints
254                 string ns = (string) $4;
255                 defaultNamespace = ns;
256                 $$ = null;
257         }
258         | KeywordDefault KeywordNamespace NamespacePrefix Equal NamespaceURILiteral
259         {
260                 // TODO: constraints
261                 string prefix = (string) $3;
262                 string ns = (string) $5;
263                 defaultNamespace = ns;
264                 nsmgr.AddNamespace (prefix, ns);
265                 $$ = null;
266         }
267         | KeywordDatatypes DatatypePrefix Equal Literal
268         {
269                 // TODO: constraints
270                 string prefix = (string) $2;
271                 string ns = (string) $4;
272                 dtnsmgr.AddNamespace (prefix, ns);
273                 $$ = null;
274         }
275         ;
277 NamespacePrefix /* returns string */
278         : IdentifierOrKeyword
279         {
280                 $$ = (string) $1;
281         }
282         ;
284 DatatypePrefix /* returns string */
285         : IdentifierOrKeyword
286         {
287                 $$ = (string) $1;
288         }
289         ;
291 NamespaceURILiteral /* returns string */
292         : Literal
293         {
294                 $$ = (string) $1;
295         }
296         | KeywordInherit
297         {
298                 $$ = (string) $1;
299         }
300         ;
302 TopLevelBody /* returns RelaxngPattern */
303         : Pattern
304         {
305                 // TODO: Constraint: single element
306 //              IList pl = (IList) $1;
307 //              if (pl.Count != 1)
308 //                      throw new RelaxngException ("The number of the top level pattern must be exactly one.");
309 //              $$ = pl [0];
310                 $$ = (RelaxngPattern) $1;
311         }
312         | Grammar
313         {
314                 RelaxngGrammar g = new RelaxngGrammar ();
315                 RelaxngGrammarContentList list = (RelaxngGrammarContentList) $1;
316                 FillGrammarContent (list, g.Starts, g.Defines, g.Divs, g.Includes);
317                 $$ = g;
318         }
319         ;
321 Grammar /* returns RelaxngGrammarContentList */
322         : /* empty */
323         {
324                 $$ = new RelaxngGrammarContentList ();
325         }
326         | Member Grammar
327         {
328                 RelaxngGrammarContentList al = (RelaxngGrammarContentList) $2;
329                 if ($1 != null)
330                         al.Insert (0, (IGrammarContent) $1);
331                 $$ = al;
332         }
333         ;
335 Member /* returns nullable IGrammarContent (RelaxngDiv, RelaxngInclude, RelaxngStart, RelaxngDiv) */
336         : AnnotatedComponent
337         {
338                 $$ = (IGrammarContent) $1;
339         }
340         | AnnotationElementNotKeyword
341         {
342                 $$ = null;
343         }
344         ;
346 AnnotatedComponent /* returns IGrammarContent */
347         : Annotations Component
348         {
349 //              $$ = ApplyAnnotations ((string) $1, (RelaxngElementBase) $2);
350                 $$ = (IGrammarContent) $2;
351         }
352         ;
354 Component /* returns IGrammarContent */
355         : Start
356         {
357                 $$ = (RelaxngStart) $1;
358         }
359         | Define
360         {
361                 $$ = (RelaxngDefine) $1;
362         }
363         | Include
364         {
365                 $$ = (RelaxngInclude) $1;
366         }
367         | Div
368         {
369                 $$ = (RelaxngDiv) $1;
370         }
371         ;
373 Start /* returns RelaxngStart */
374         : KeywordStart AssignOp Pattern
375         {
376                 RelaxngStart start = new RelaxngStart ();
377                 start.Combine = (string) $2;
378                 start.Pattern = (RelaxngPattern) $3;
379                 $$ = start;
380         }
381         ;
383 Define /* returns RelaxngDefine */
384         : Identifier AssignOp Pattern
385         {
386                 RelaxngDefine def = new RelaxngDefine ();
387                 def.Name = (string) $1;
388                 def.Combine = (string) $2;
389                 def.Patterns.Add ((RelaxngPattern) $3);
390                 $$ = def;
391         }
392         ;
394 AssignOp /* returns string */
395         : Equal
396         {
397                 $$ = null;
398         }
399         | OrEquals
400         {
401                 $$ = "choice";
402         }
403         | AndEquals
404         {
405                 $$ = "interleave";
406         }
407         ;
409 Include /* returns RelaxngInclude */
410         : KeywordInclude AnyURILiteral OptInherit OptIncludeBody
411         {
412                 // FIXME: OptInherit is not handled properly.
413                 RelaxngInclude include = new RelaxngInclude ();
414                 include.Href = (string) $2;
415                 include.NSContext = (string) $3;
416                 FillGrammarContent ((IList) $4, include.Starts, include.Defines, include.Divs, null);
417                 $$ = include;
418         }
419         ;
421 AnyURILiteral /* returns string */
422         : Literal
423         {
424                 // Constraints: any URI
425                 $$ = (string) $1;
426         }
427         ;
429 OptInherit /* returns string */
430         /* FIXME: It is not handled properly. */
431         : /* empty */
432         {
433                 // MakeNsAttribute (LookupDefault (environment));
434                 $$ = nsmgr.DefaultNamespace;
435         }
436         | KeywordInherit Equal IdentifierOrKeyword
437         {
438                 // MakeNsAttribute (LookupPrefix (environment, $3));
439                 $$ = nsmgr.LookupPrefix ((string) $3);
440         }
441         ;
443 OptIncludeBody /* returns IList */
444         : /* empty */
445         {
446                 $$ = new ArrayList ();
447         }
448         | OpenCurly IncludeBody CloseCurly
449         {
450                 $$ = (IList) $2;
451         }
452         ;
454 IncludeBody /* returns IList */
455         : /* empty */
456         {
457                 $$ = new ArrayList ();
458         }
459         | IncludeMember IncludeBody
460         {
461                 ArrayList al = (ArrayList) $2;
462                 al.Insert (0, $1);
463                 $$ = al;
464         }
465         ;
467 IncludeMember /* returns RelaxngElementBase */
468         : AnnotatedIncludeComponent
469         {
470                 $$ = (RelaxngElementBase) $1;
471         }
472         | AnnotationElementNotKeyword
473         {
474                 $$ = (RelaxngElementBase) $1;
475         }
476         ;
478 AnnotatedIncludeComponent /* returns IGrammarContent */
479         : Annotations IncludeComponent
480         {
481 //              $$ = ApplyAnnotations ((string) $1, (RelaxngElementBase) $2);
482                 $$ = (IGrammarContent) $2;
483         }
484         ;
486 IncludeComponent /* returns IGrammarContent */
487         : Start
488         {
489                 $$ = (RelaxngStart) $1;
490         }
491         | Define
492         {
493                 $$ = (RelaxngDefine) $1;
494         }
495         | IncludeDiv
496         {
497                 $$ = (RelaxngDiv) $1;
498         }
499         ;
501 Div /* returns RelaxngDiv */
502         : KeywordDiv OpenCurly Grammar CloseCurly
503         {
504                 RelaxngDiv div = new RelaxngDiv ();
505                 FillGrammarContent ((IList) $3, div.Starts, div.Defines, div.Divs, div.Includes);
506                 $$ = div;
507         }
508         ;
510 IncludeDiv /* returns RelaxngDiv */
511         : KeywordDiv OpenCurly IncludeBody CloseCurly
512         {
513                 RelaxngDiv div = new RelaxngDiv ();
514                 FillGrammarContent ((IList) $3, div.Starts, div.Defines, div.Divs, div.Includes);
515                 $$ = div;
516         }
517         ;
519 Pattern /* returns RelaxngPattern */
520         : InnerPattern
521         ;
523 InnerPattern /* returns RelaxngPattern */
524         /* TODO: applyAnnotations() are omitted */
525         : InnerParticle
526         {
527                 $$ = (RelaxngPattern) $1;
528         }
529         | ParticleChoice
530         {
531                 RelaxngPatternList list = (RelaxngPatternList) $1;
532                 RelaxngChoice choice = new RelaxngChoice ();
533                 for (int i = 0; i < list.Count; i++)
534                         choice.Patterns.Add (list [i]);
535                 // This is said as to return Elements, while ApplyAnnotations() is said to return Element
536                 $$ = choice;
537         }
538         | ParticleGroup
539         {
540                 RelaxngPatternList list = (RelaxngPatternList) $1;
541                 RelaxngGroup group = new RelaxngGroup ();
542                 for (int i = 0; i < list.Count; i++)
543                         group.Patterns.Add (list [i]);
544                 // This is said as to return Elements, while ApplyAnnotations() is said to return Element
545                 $$ = group;
546         }
547         | ParticleInterleave
548         {
549                 RelaxngPatternList list = (RelaxngPatternList) $1;
550                 RelaxngInterleave interleave = new RelaxngInterleave ();
551                 for (int i = 0; i < list.Count; i++)
552                         interleave.Patterns.Add (list [i]);
553                 // This is said as to return Elements, while ApplyAnnotations() is said to return Element
554                 $$ = interleave;
555         }
556         | AnnotatedDataExcept
557         {
558                 $$ = (RelaxngData) $1;
559         }
560         ;
562 ParticleChoice /* returns RelaxngPatternList */
563         : Particle Bar Particle
564         {
565                 RelaxngPatternList list = new RelaxngPatternList ();
566                 list.Add ((RelaxngPattern) $1);
567                 list.Add ((RelaxngPattern) $3);
568                 $$ = list;
569         }
570         | Particle Bar ParticleChoice
571         {
572                 RelaxngPatternList list = (RelaxngPatternList) $3;
573                 list.Insert (0, (RelaxngPattern) $1);
574                 $$ = list;
575         }
576         ;
578 ParticleGroup /* returns RelaxngPatternList */
579         : Particle Comma Particle
580         {
581                 RelaxngPatternList list = new RelaxngPatternList ();
582                 list.Add ((RelaxngPattern) $1);
583                 list.Add ((RelaxngPattern) $3);
584                 $$ = list;
585         }
586         | Particle Comma ParticleGroup
587         {
588                 RelaxngPatternList list = (RelaxngPatternList) $3;
589                 list.Insert (0, (RelaxngPattern) $1);
590                 $$ = list;
591         }
592         ;
594 ParticleInterleave /* returns RelaxngPatternList */
595         : Particle Amp Particle
596         {
597                 RelaxngPatternList list = new RelaxngPatternList ();
598                 list.Add ((RelaxngPattern) $1);
599                 list.Add ((RelaxngPattern) $3);
600                 $$ = list;
601         }
602         | Particle Amp ParticleInterleave
603         {
604                 RelaxngPatternList list = (RelaxngPatternList) $3;
605                 list.Insert (0, (RelaxngPattern) $1);
606                 $$ = list;
607         }
608         ;
610 Particle /* returns RelaxngPattern */
611         : InnerParticle
612         ;
614 InnerParticle /* returns RelaxngPattern */
615         : AnnotatedPrimary
616         {
617 //              $$ = ApplyAnnotationsGroup (null, (RelaxngPatternList) $1);
618                 $$ = $1;
619         }
620         | RepeatedPrimary FollowAnnotations
621         {
622                 // FIXME: annotations are not handled
623                 RelaxngPattern p = (RelaxngPattern) $1;
624 //              RelaxngPatternList l = new RelaxngPatternList ();
625 //              l.Add (p);
626 //              $$ = l;
627                 $$ = p;
628         }
629         ;
631 RepeatedPrimary /* returns RelaxngPattern */
632         : AnnotatedPrimary Asterisk
633         {
634                 RelaxngZeroOrMore zom = new RelaxngZeroOrMore ();
635 //              foreach (RelaxngPattern p in (ICollection) $1)
636 //                      zom.Patterns.Add (p);
637                 zom.Patterns.Add ((RelaxngPattern) $1);
638                 $$ = zom;
639         }
640         | AnnotatedPrimary Plus
641         {
642                 RelaxngOneOrMore oom = new RelaxngOneOrMore ();
643 //              foreach (RelaxngPattern p in (ICollection) $1)
644 //                      oom.Patterns.Add (p);
645                 oom.Patterns.Add ((RelaxngPattern) $1);
646                 $$ = oom;
647         }
648         | AnnotatedPrimary Question
649         {
650                 RelaxngOptional opt = new RelaxngOptional ();
651 //              foreach (RelaxngPattern p in (ICollection) $1)
652 //                      opt.Patterns.Add (p);
653                 opt.Patterns.Add ((RelaxngPattern) $1);
654                 $$ = opt;
655         }
656         ;
658 AnnotatedPrimary /* returns RelaxngPattern */
659         : LeadAnnotatedPrimary FollowAnnotations
660         {
661                 // FIXME: handle followAnnotations
662                 $$ = $1;
663         }
664         ;
666 AnnotatedDataExcept /* returns RelaxngPatternList */
667         : LeadAnnotatedDataExcept FollowAnnotations
668         {
669                 // FIXME: handle followAnnotations
670                 RelaxngData p = (RelaxngData) $1;
671                 RelaxngPatternList l = new RelaxngPatternList ();
672                 l.Add (p);
673                 $$ = l;
674         }
675         ;
677 LeadAnnotatedDataExcept /* returns RelaxngData */
678         : Annotations DataExcept
679         {
680                 $$ = $2;
681         }
682         ;
684 LeadAnnotatedPrimary /* returns RelaxngPattern */
685         : Annotations Primary
686         {
687                 // LAMESPEC: This should return Elements, while ApplyAnnotations() returns Element
688 //              RelaxngPatternList list = new RelaxngPatternList ();
689 //              list.Add ((RelaxngPattern) ApplyAnnotations ((string) $1, (RelaxngPattern) $2));
690 //              $$ = list;
691                 $$ = (RelaxngPattern) $2;
692         }
693         | Annotations OpenParen InnerPattern CloseParen
694         {
695 //              $$ = (RelaxngPatternList) $3;
696                 $$ = (RelaxngPattern) $3;
697         }
698         ;
700 Primary /* returns RelaxngPattern */
701         : KeywordElement NameClass OpenCurly Pattern CloseCurly
702         {
703                 RelaxngNameClass nc = (RelaxngNameClass) $2;
704                 RelaxngElement el = new RelaxngElement ();
705                 el.NameClass = nc;
706                 FillElementDefaultNS (el.NameClass);
708 //              foreach (RelaxngPattern p in (RelaxngPatternList) $4)
709 //                      el.Patterns.Add (p);
710                 el.Patterns.Add ((RelaxngPattern) $4);
711                 $$ = el;
712         }
713         | KeywordAttribute NameClass OpenCurly Pattern CloseCurly
714         {
715                 RelaxngNameClass nc = (RelaxngNameClass) $2;
717                 RelaxngAttribute attr = new RelaxngAttribute ();
718                 attr.NameClass = nc;
719                 FillAttributeDefaultNS (attr.NameClass);
720                 attr.Pattern = (RelaxngPattern) $4;
721                 $$ = attr;
722         }
723         | KeywordMixed OpenCurly Pattern CloseCurly
724         {
725                 RelaxngMixed mixed = new RelaxngMixed ();
726                 foreach (RelaxngPattern p in (RelaxngPatternList) $3)
727                         mixed.Patterns.Add (p);
728                 $$ = mixed;
729         }
730         | KeywordList OpenCurly Pattern CloseCurly
731         {
732                 RelaxngList list = new RelaxngList ();
733                 foreach (RelaxngPattern p in (RelaxngPatternList) $3)
734                         list.Patterns.Add (p);
735                 $$ = list;
736         }
737         | DatatypeName OptParams
738         {
739                 RelaxngData data = new RelaxngData ();
740                 XmlQualifiedName dtName = (XmlQualifiedName) $1;
741                 data.DatatypeLibrary = dtName.Namespace;
742                 data.Type = dtName.Name;
743                 foreach (RelaxngParam p in (ICollection) $2)
744                         data.ParamList.Add (p);
746                 $$ = data;
747         }
748         | DatatypeName DatatypeValue
749         {
750                 RelaxngValue value = new RelaxngValue ();
751                 XmlQualifiedName dtName = (XmlQualifiedName) $1;
752                 if (dtName.Namespace != RelaxngGrammar.NamespaceURI)
753                         value.DatatypeLibrary = dtName.Namespace;
754                 value.Type = dtName.Name;
755                 value.Value = (string) $2;
757                 $$ = value;
758         }
759         | DatatypeValue
760         {
761                 RelaxngValue value = new RelaxngValue ();
762                 value.Value = (string) $1;
764                 // RELAX NG default type
765                 value.Type = "string";
766                 value.DatatypeLibrary = String.Empty;
768                 $$ = value;
769         }
770         | KeywordEmpty
771         {
772                 $$ = new RelaxngEmpty ();
773         }
774         | KeywordNotAllowed
775         {
776                 $$ = new RelaxngNotAllowed ();
777         }
778         | KeywordText
779         {
780                 $$ = new RelaxngText ();
781         }
782         | Ref
783         {
784                 RelaxngRef r = new RelaxngRef ();
785                 r.Name = (string) $1;
786                 $$ = r;
787         }
788         | KeywordParent Ref
789         {
790                 RelaxngParentRef pref = new RelaxngParentRef ();
791                 pref.Name = (string) $2;
792                 $$ = pref;
793         }
794         | KeywordGrammar OpenCurly Grammar CloseCurly
795         {
796                 RelaxngGrammar g = new RelaxngGrammar ();
797                 FillGrammarContent ((IList) $3, g.Starts, g.Defines, g.Divs, g.Includes);
798                 $$ = g;
799         }
800         | KeywordExternal AnyURILiteral OptInherit
801         {
802                 RelaxngExternalRef extref = new RelaxngExternalRef ();
803                 extref.Href = (string) $2;
804                 extref.NSContext = (string) $3;
805                 $$ = extref;
806         }
807         ;
809 DataExcept /* returns RelaxngData */
810         : DatatypeName OptParams Minus LeadAnnotatedPrimary
811         {
812                 RelaxngData data = new RelaxngData ();
813                 foreach (RelaxngParam p in (IList) $2)
814                         data.ParamList.Add (p);
815                 data.Except = new RelaxngExcept ();
816                 foreach (RelaxngPattern p in (RelaxngPatternList) $4)
817                         data.Except.Patterns.Add (p);
818                 $$ = data;
819         }
820         ;
822 Ref /* returns string */
823         : Identifier
824         ;
826 DatatypeName
827         : CName
828         {
829                 string cname = (string) $1;
830         /*
831                 int colonAt = cname.IndexOf (':');
832                 string local = cname.Substring (colonAt + 1);
833                 string prefix = cname.Substring (0, colonAt);
834                 string ns = dtnsmgr.LookupNamespace (prefix);
835                 $$ = new XmlQualifiedName (local, ns);
836         */
837                 $$ = SplitQName (dtnsmgr, cname);
838         }
839         | KeywordString
840         {
841                 $$ = new XmlQualifiedName ("string", String.Empty);
842         }
843         | KeywordToken
844         {
845                 $$ = new XmlQualifiedName ("token", String.Empty);
846         }
847         ;
849 DatatypeValue
850         : Literal
851         ;
853 OptParams
854         : /* empty */
855         {
856                 $$ = new RelaxngParamList ();
857         }
858         | OpenCurly Params CloseCurly
859         {
860                 $$ = $2;
861         }
862         ;
864 Params
865         : /* empty */
866         {
867                 $$ = new RelaxngParamList ();
868         }
869         | Param Params
870         {
871                 RelaxngParamList al = (RelaxngParamList) $2;
872                 al.Insert (0, (RelaxngParam) $1);
873                 $$ = al;
874         }
875         ;
877 Param /* returns RelaxngParam */
878         : Annotations IdentifierOrKeyword Equal Literal
879         {
880                 RelaxngParam prm = new RelaxngParam ();
881                 prm.Name = (string) $2;
882                 prm.Value = (string) $4;
884 //              $$ = ApplyAnnotations ((string) $1, prm);
885                 $$ = prm;
886         }
887         ;
889 NameClass /* returns RelaxngNameClass */
890         : InnerNameClass
891         {
892                 $$ = $1;
893         }
894         ;
896 InnerNameClass /* returns RelaxngNameClass */
897         : AnnotatedSimpleNameClass
898         {
899                 $$ = (RelaxngNameClass) $1;
900         }
901         | NameClassChoice
902         {
903                 RelaxngNameChoice cho = new RelaxngNameChoice ();
904                 RelaxngNameClassList list = (RelaxngNameClassList) $1;
905                 for (int i = 0; i < list.Count; i++)
906                         cho.Children.Add ((RelaxngNameClass) list [i]);
907                 $$ = cho;
908         }
909         | AnnotatedExceptNameClass
910         {
911                 $$ = (RelaxngNameClass) $1;
912         }
913         ;
915 NameClassChoice /* returns RelaxngNameClassList */
916         : AnnotatedSimpleNameClass Bar AnnotatedSimpleNameClass
917         {
918                 RelaxngNameClassList list = new RelaxngNameClassList ();
919                 list.Add ((RelaxngNameClass) $1);
920                 list.Add ((RelaxngNameClass) $3);
921                 $$ = list;
922         }
923         | AnnotatedSimpleNameClass Bar NameClassChoice
924         {
925                 RelaxngNameClassList list = (RelaxngNameClassList) $3;
926                 list.Insert (0, (RelaxngNameClass) $1);
927                 $$ = list;
928         }
929         ;
931 AnnotatedExceptNameClass /* returns RelaxngNameClass */
932         : LeadAnnotatedExceptNameClass FollowAnnotations
933         {
934                 $$ = (RelaxngNameClass) $1;
935         }
936         ;
938 LeadAnnotatedExceptNameClass /* returns RelaxngNameClass */
939         : Annotations ExceptNameClass
940         {
941                 $$ = (RelaxngNameClass) $2;
942         }
943         ;
945 AnnotatedSimpleNameClass /* returns RelaxngNameClass */
946         : LeadAnnotatedSimpleNameClass FollowAnnotations
947         {
948                 // FIXME: annotations
949                 $$ = $1;
950         }
951         ;
953 LeadAnnotatedSimpleNameClass /* returns RelaxngNameClass */
954         : Annotations SimpleNameClass
955         {
956                 // FIXME: applyAnnotations
957                 $$ = (RelaxngNameClass) $2;
958         }
959         | Annotations OpenParen InnerNameClass CloseParen
960         {
961                 $$ = $3;
962         }
963         ;
965 ExceptNameClass
966         : NsName Minus LeadAnnotatedSimpleNameClass
967         {
968                 RelaxngNsName nsName = new RelaxngNsName ();
969                 nsName.Namespace = nsmgr.LookupNamespace ((string) $1);
970                 nsName.Except = new RelaxngExceptNameClass ();
971                 nsName.Except.Names.Add ((RelaxngNameClass) $3);
972                 $$ = nsName;
973         }
974         | Asterisk Minus LeadAnnotatedSimpleNameClass
975         {
976                 RelaxngAnyName anyName = new RelaxngAnyName ();
977                 anyName.Except = new RelaxngExceptNameClass ();
978                 anyName.Except.Names.Add ((RelaxngNameClass) $3);
979                 $$ = anyName;
980         }
981         ;
983 SimpleNameClass /* returns RelaxngNameClass */
984         : IdentifierOrKeyword
985         {
986                 RelaxngName name = new RelaxngName ();
987                 name.LocalName = (string) $1;
988                 name.Namespace = null;
989                 $$ = name;
990         }
991         | CName
992         {
993                 string cname = (string) $1;
994 //              int colonAt = cname.IndexOf (':');
995 //              XmlQualifiedName qname = new XmlQualifiedName (cname.Substring (colonAt + 1), nsmgr.LookupNamespace (cname.Substring (0, colonAt - 1)));
996                 XmlQualifiedName qname = SplitQName (nsmgr, cname);
997                 RelaxngName name = new RelaxngName ();
998                 name.LocalName = qname.Name;
999                 name.Namespace = qname.Namespace;
1000                 $$ = name;
1001         }
1002         | NsName
1003         {
1004                 RelaxngNsName nsName = new RelaxngNsName ();
1005                 nsName.Namespace = nsmgr.LookupNamespace ((string) $1);
1006                 $$ = nsName;
1007         }
1008         | Asterisk
1009         {
1010                 $$ = new RelaxngAnyName ();
1011         }
1012         ;
1014 FollowAnnotations
1015         : /* empty */
1016         {
1017                 $$ = null;
1018         }
1019         | TwoGreaters AnnotationElement FollowAnnotations
1020         {
1021                 // FIXME: handle them
1022                 $$ = null;
1023         }
1024         ;
1026 Annotations /* returns null */
1027         /* FIXME: needed to handle them? */
1028         : Documentations
1029         {
1030                 $$ = null;
1031         }
1032         | Documentations OpenCurly AnnotationAttributes AnnotationElements CloseCurly
1033         {
1034                 $$ = null;
1035         }
1036         ;
1038 AnnotationAttributes /* returns null */
1039         : /* empty */
1040         {
1041                 $$ = null;
1042         }
1043         | ForeignAttributeName Equal Literal AnnotationAttributes
1044         {
1045                 // Constraint: duplicate attributes
1047                 // FIXME: do something
1048                 $$ = null;
1049         }
1050         ;
1052 ForeignAttributeName /* returns XmlQualifiedName */
1053         : PrefixedName
1054         {
1055                 // Constraint: xmlns namespace URI
1056                 // Constraint: unqualified name
1057                 // Constraint: RELAX NG namespace URI
1059                 // do nothing
1060 //              $$ = $1;
1061         }
1062         ;
1064 AnnotationElements
1065         : /* empty */
1066         | AnnotationElement AnnotationElements
1067         ;
1069 AnnotationElement
1070         : ForeignElementName AnnotationAttributesContent
1071         {
1072                 // do nothing
1073 //              $$ = Element ($1, $2);
1074                 $$ = null;
1075         }
1076         ;
1078 ForeignElementName
1079         : /*IdentifierOrKeyword
1080         {
1081                 $$ = new XmlQualifiedName ((string) $1, String.Empty);
1082         }
1083         |*/ PrefixedName
1084         {
1085                 // Constraint: RELAX NG namespace URI
1086                 $$ = $1;
1087         }
1088         ;
1090 AnnotationElementNotKeyword /* returns null */
1091         : ForeignElementNameNotKeyword AnnotationAttributesContent
1092         {
1093                 // do nothing
1094 //              $$ = Element ($1, $2);
1095         }
1096         ;
1098 ForeignElementNameNotKeyword  /* returns XmlQualifiedName */
1099 /* LAMESPEC: unprefixed Identifier causes conflict */
1100         : /*Identifier
1101         {
1102                 $$ = new XmlQualifiedName ((string) $1, String.Empty);
1103         }
1104         |*/ PrefixedName
1105         {
1106                 // Constraint: RELAX NG namespace URI
1107                 $$ = (XmlQualifiedName) $1;
1108         }
1109         ;
1111 AnnotationAttributesContent /* returns null */
1112         : OpenBracket NestedAnnotationAttributes AnnotationContent CloseBracket
1113         {
1114                 $$ = null;
1115         }
1116         ;
1118 NestedAnnotationAttributes /* returns null */
1119         : /* empty */
1120         {
1121                 $$ = null;
1122         }
1123         | AnyAttributeName Equal Literal NestedAnnotationAttributes
1124         {
1125                 // Constraint: duplicate attributes
1127                 // do nothing
1128 //              $$ = Attribute ($1, $2);
1129                 $$ = null;
1130         }
1132 AnyAttributeName /* returns XmlQualifiedName */
1133         : IdentifierOrKeyword
1134         {
1135                 $$ = new XmlQualifiedName ((string) $1);
1136         }
1137         | PrefixedName
1138         {
1139                 // Constraint: xmlns namespace URI
1140                 $$ = (XmlQualifiedName) $1;
1141         }
1142         ;
1144 AnnotationContent /* returns null */
1145         : /* empty */
1146         {
1147                 $$ = null;
1148         }
1149         | NestedAnnotationElement AnnotationContent
1150         {
1151                 $$ = null;
1152         }
1153         | Literal AnnotationContent
1154         {
1155                 $$ = null;
1156         }
1157         ;
1159 NestedAnnotationElement /* returns null */
1160         : AnyElementName AnnotationAttributesContent
1161         {
1162                 // do nothing
1163 //              $$ = Element ($1, $2);
1164                 $$ = null;
1165         }
1166         ;
1168 AnyElementName /* returns XmlQualifiedName */
1169         : IdentifierOrKeyword
1170         {
1171                 $$ = new XmlQualifiedName ((string) $1, String.Empty);
1172         }
1173         | PrefixedName
1174         {
1175                 $$ = (XmlQualifiedName) $1;
1176         }
1177         ;
1179 PrefixedName /* returns XmlQualifiedName */
1180         : CName
1181         {
1182                 // Constraint: annotation inherit
1183                 $$ = SplitQName (nsmgr, (string) $1);
1184         }
1185         ;
1187 Documentations /* returns null */
1188         : /* empty */
1189         {
1190                 $$ = null;
1191         }
1192         | Documentation Documentations
1193         {
1194                 // do nothing
1195 //              $$ = Element (DocumentationElementName (), Text ((string) $1), $2);
1196                 $$ = null;
1197         }
1198         ;
1200 IdentifierOrKeyword /* returns string */
1201         : Identifier
1202         {
1203                 $$ = (string) $1;
1204         }
1205         | Keyword
1206         {
1207                 $$ = (string) $1;
1208         }
1209         ;
1211 Keyword /* returns string */
1212         : KeywordAttribute
1213         | KeywordDefault
1214         | KeywordDatatypes
1215         | KeywordDiv
1216         | KeywordElement
1217         | KeywordEmpty
1218         | KeywordExternal
1219         | KeywordGrammar
1220         | KeywordInclude
1221         | KeywordInherit
1222         | KeywordList
1223         | KeywordMixed
1224         | KeywordNamespace
1225         | KeywordNotAllowed
1226         | KeywordParent
1227         | KeywordStart
1228         | KeywordString
1229         | KeywordText
1230         | KeywordToken
1231         ;
1233 Literal /* returns string */
1234         : LiteralSegment
1235         {
1236                 $$ = (string) $1;
1237         }
1238         | LiteralSegment Tilde Literal
1239         {
1240                 $$ = (string) $1 + (string) $3;
1241         }
1242         ;
1244 Identifier /* returns string */
1245         : NCNameButKeyword
1246         {
1247                 $$ = (string) $1;
1248         }
1249         | BackSlash NCNameButKeyword
1250         {
1251                 $$ = (string) $2;
1252         }
1253         /* extended */
1254         | BackSlash Keyword
1255         {
1256                 $$ = (string) $2;
1257         }
1258         ;