From 0137d4eb18726201ebe1523b7eb3d15d0edd1f32 Mon Sep 17 00:00:00 2001
From: Anders Nilsson <anders.nilsson@cs.lth.se>
Date: Thu, 11 Jan 2007 13:15:21 +0100
Subject: [PATCH] Can parse and build AST from x3d-3.0.xsd

---
 examples/simple.xsd  |  73 +++++++++++++++++++++-
 examples/x3d-3.0.xsd |  70 +++++++++++++++++++++
 xmlschema.ast        |  11 +++-
 xmlschema.jjt        | 143 ++++++++++++++++++++++++++++++++++++-------
 4 files changed, 270 insertions(+), 27 deletions(-)

diff --git a/examples/simple.xsd b/examples/simple.xsd
index f17fc75..af92cff 100644
--- a/examples/simple.xsd
+++ b/examples/simple.xsd
@@ -8,8 +8,75 @@
 	<!--======================================================================-->
 	<xs:annotation>
 		<xs:appinfo>XML Schema for the Extensible 3D (X3D) Graphics Specification tagset</xs:appinfo>
+		<xs:documentation><![CDATA[
+=============================================================================
+
+X3D Specification Schema:  	http://www.web3d.org/specifications/x3d-3.0.xsd
+
+Schema extensions:		http://www.web3d.org/specifications/x3d-3.0-Web3dExtensionsPublic.xsd
+				http://www.web3d.org/specifications/x3d-3.0-Web3dExtensionsPrivate.xsd
+
+Web3D extensions update URL:	http://www.web3d.org/x3d/content/x3d-3.0-Web3dExtensionsPublic.xsd
+
+Revised:  10 January 2006
+
+Author:   Don Brutzman
+
+Acknowledgements:
+- Thanks for excellent insights and assistance from Len Bullard,
+  Justin Couch, Leonard Daly, Paul Diefenbach, Rick Goldberg, Bryan Housel,
+  Alan Hudson, Chris Lilley, Braden McDaniel, Tony Parisi, Nick Polys,
+  Dick Puk, Jeff Sonstein, C. Michael Sperberg-McQueen, Henry Thompson
+  and Joe Williams.
+- Rick Goldberg of the Sun Java3D team wrote the first two versions of the
+  SAI API using IDL, and provided invaluable help on the schema-based SAI.
+- Joe Williams of HyperMultiMedia documented and further developed
+  the scene graph interface hierarchy, making this schema possible.
+- Alan Hudson added many default values and performed consistency checks.
+
+- XML Schema validator:		http://www.w3.org/2001/03/webdata/xsv
+
+Design summary:
+- Schemas define XML tagsets in depth using a native-XML format.
+- Schemas are an alternative to Document Type Definitions (DTDs).
+- This X3D Schema matches the functionality of the X3D DTD and
+  provides significant further capabilities, including strong type
+  checking of both node and attribute values.
+- An XSLT stylesheet can be applied to this schema to autogenerate
+  source code for the Scene Authoring Interface (SAI), documentation
+  of node lists, etc.
+
+Design patterns:
+- X3D nodes implement X3D node types and are represented by XML elements.
+- X3D non-node fields are represented by XML attributes using field types.
+- X3D field types are represented by XML Schema (xsd) simpleTypes.
+- XML Schema list types are used to augment XML Schema simple types, to
+  create arrays of X3D base types (integerList etc).
+- X3D node types (X3DGroupingNode etc.) are represented by Schema complexTypes.
+- X3D node types are captured as XML Schema complexType definitions.  These match 
+  the X3D interface hierarchy, capture strong typing of node relationships, and collect 
+  common attributes shared among node types.  These features also provide functional 
+  consistency between scene graph content and the X3D Scene Authoring Interface (SAI) 
+  application programming interface (API).
+- XML Schema xs:group element definitions provide child-node content models,
+  since complexTypes can't be used as references to unnamed element children.
+
+X3D Schema version numbering:
+- 0.4  Nodes for Core and Base profiles (20 November 2000)
+- 0.5  Nodes for DIS, GeoSpatial, HumanoidAnimation, Nurbs, any others in Full profile
+- 0.6  Autogenerated Scene Authoring Interface (SAI).  First show Java interfaces,
+       then show multiple formats including Interface Description Language (IDL)
+       and matching Java classes that implement the Java interfaces
+- 0.7  Resolution of compromise/compact representations (wrapper tags), and use of
+       DTD internal parameter-entity names updated to match interface hierarchy
+       naming scheme
+- 0.8  Exemplar content and authoring tools that correspond to X3D Schema tagset
+- 2.9  Public review and Xj3D open-source implementation version
+- 3.0  X3D Task Group consensus for inclusion in X3D Specification
+- 3.1  X3D Specification Amendment 1
+- 3.2  X3D Specification Amendment 2
+
+=============================================================================
+]]></xs:documentation>
 	</xs:annotation>
-		<xs:appinfo> </xs:appinfo>
-	<!--======================================================================-->
-	<!--Several pattern possibilities for urls/URNs are conceivable, perhaps a type definition or regex pattern is preferable.-->
 </xs:schema>
diff --git a/examples/x3d-3.0.xsd b/examples/x3d-3.0.xsd
index b2654f2..09b9cb8 100644
--- a/examples/x3d-3.0.xsd
+++ b/examples/x3d-3.0.xsd
@@ -8,6 +8,76 @@
 	<!--======================================================================-->
 	<xs:annotation>
 		<xs:appinfo>XML Schema for the Extensible 3D (X3D) Graphics Specification tagset</xs:appinfo>
+		<xs:documentation><![CDATA[
+=============================================================================
+
+X3D Specification Schema:  	http://www.web3d.org/specifications/x3d-3.0.xsd
+
+Schema extensions:		http://www.web3d.org/specifications/x3d-3.0-Web3dExtensionsPublic.xsd
+				http://www.web3d.org/specifications/x3d-3.0-Web3dExtensionsPrivate.xsd
+
+Web3D extensions update URL:	http://www.web3d.org/x3d/content/x3d-3.0-Web3dExtensionsPublic.xsd
+
+Revised:  10 January 2006
+
+Author:   Don Brutzman
+
+Acknowledgements:
+- Thanks for excellent insights and assistance from Len Bullard,
+  Justin Couch, Leonard Daly, Paul Diefenbach, Rick Goldberg, Bryan Housel,
+  Alan Hudson, Chris Lilley, Braden McDaniel, Tony Parisi, Nick Polys,
+  Dick Puk, Jeff Sonstein, C. Michael Sperberg-McQueen, Henry Thompson
+  and Joe Williams.
+- Rick Goldberg of the Sun Java3D team wrote the first two versions of the
+  SAI API using IDL, and provided invaluable help on the schema-based SAI.
+- Joe Williams of HyperMultiMedia documented and further developed
+  the scene graph interface hierarchy, making this schema possible.
+- Alan Hudson added many default values and performed consistency checks.
+
+- XML Schema validator:		http://www.w3.org/2001/03/webdata/xsv
+
+Design summary:
+- Schemas define XML tagsets in depth using a native-XML format.
+- Schemas are an alternative to Document Type Definitions (DTDs).
+- This X3D Schema matches the functionality of the X3D DTD and
+  provides significant further capabilities, including strong type
+  checking of both node and attribute values.
+- An XSLT stylesheet can be applied to this schema to autogenerate
+  source code for the Scene Authoring Interface (SAI), documentation
+  of node lists, etc.
+
+Design patterns:
+- X3D nodes implement X3D node types and are represented by XML elements.
+- X3D non-node fields are represented by XML attributes using field types.
+- X3D field types are represented by XML Schema (xsd) simpleTypes.
+- XML Schema list types are used to augment XML Schema simple types, to
+  create arrays of X3D base types (integerList etc).
+- X3D node types (X3DGroupingNode etc.) are represented by Schema complexTypes.
+- X3D node types are captured as XML Schema complexType definitions.  These match 
+  the X3D interface hierarchy, capture strong typing of node relationships, and collect 
+  common attributes shared among node types.  These features also provide functional 
+  consistency between scene graph content and the X3D Scene Authoring Interface (SAI) 
+  application programming interface (API).
+- XML Schema xs:group element definitions provide child-node content models,
+  since complexTypes can't be used as references to unnamed element children.
+
+X3D Schema version numbering:
+- 0.4  Nodes for Core and Base profiles (20 November 2000)
+- 0.5  Nodes for DIS, GeoSpatial, HumanoidAnimation, Nurbs, any others in Full profile
+- 0.6  Autogenerated Scene Authoring Interface (SAI).  First show Java interfaces,
+       then show multiple formats including Interface Description Language (IDL)
+       and matching Java classes that implement the Java interfaces
+- 0.7  Resolution of compromise/compact representations (wrapper tags), and use of
+       DTD internal parameter-entity names updated to match interface hierarchy
+       naming scheme
+- 0.8  Exemplar content and authoring tools that correspond to X3D Schema tagset
+- 2.9  Public review and Xj3D open-source implementation version
+- 3.0  X3D Task Group consensus for inclusion in X3D Specification
+- 3.1  X3D Specification Amendment 1
+- 3.2  X3D Specification Amendment 2
+
+=============================================================================
+]]></xs:documentation>
 	</xs:annotation>
 	<!--======================================================================-->
 	<!--Several pattern possibilities for urls/URNs are conceivable, perhaps a type definition or regex pattern is preferable.-->
diff --git a/xmlschema.ast b/xmlschema.ast
index 8080d6e..161bc08 100644
--- a/xmlschema.ast
+++ b/xmlschema.ast
@@ -19,24 +19,31 @@ XsAnnotation     : ComplexElement;
 XsAppinfo        : ComplexElement;
 XsAttribute      : ComplexElement;
 XsAttributeGroup : ComplexElement;
+XsChoice         : ComplexElement;
 XsComplexContent : ComplexElement;
 XsComplexType    : ComplexElement;
 XsDocumentation  : ComplexElement;
+XsElement        : ComplexElement;
 XsEnumeration    : ComplexElement;
 XsExtension      : ComplexElement;
 XsGroup          : ComplexElement;
 XsInclude        : ComplexElement;
 XsList           : ComplexElement;
+XsMaxInclusive   : ComplexElement;
+XsMinInclusive   : ComplexElement;
+XsMinExclusive   : ComplexElement;
 XsMinLength      : ComplexElement;
 XsPattern        : ComplexElement;
 XsRestriction    : ComplexElement;
+XsSequence       : ComplexElement;
 XsSimpleType     : ComplexElement;
 XsSchema         : ComplexElement;
 XsWhiteSpace     : ComplexElement;
 
 
 abstract SimpleElement : Element;
-StringElement : SimpleElement ::= <IDENTIFIER>;
+StringElement : SimpleElement ::= <STRING>;
+CDataElement : SimpleElement ::= <STRING>;
 IntElement : SimpleElement ::= <INTEGER_LITERAL>;
 FloatElement : SimpleElement ::= <FLOAT_LITERAL>;
 
@@ -47,6 +54,7 @@ Base                 : Attribute;
 Default              : Attribute;
 ElementFormDefault   : Attribute;
 Encoding             : Attribute;
+Fixed                : Attribute;
 ItemType             : Attribute;
 MinOccurs            : Attribute;
 MaxOccurs            : Attribute;
@@ -56,6 +64,7 @@ Ref                  : Attribute;
 Source               : Attribute;
 SchemaLocation       : Attribute;
 Type                 : Attribute;
+Use                  : Attribute;
 Value                : Attribute;
 Version              : Attribute;
 XmlnsXs              : Attribute;
diff --git a/xmlschema.jjt b/xmlschema.jjt
index 883dce9..3c50768 100644
--- a/xmlschema.jjt
+++ b/xmlschema.jjt
@@ -14,9 +14,9 @@ options {
     NODE_PREFIX                 = "";
     JAVA_UNICODE_ESCAPE         = true;
     STATIC                      = false;
-   	DEBUG_PARSER                = true;
-//   	DEBUG_LOOKAHEAD             = true;
+//    	DEBUG_PARSER                = true;
 //    	DEBUG_TOKEN_MANAGER         = true;
+//   	DEBUG_LOOKAHEAD             = true;
 } // options
 
 
@@ -71,17 +71,23 @@ SPECIAL_TOKEN : /* COMMENTS */
   | < XS_APPINFO               : "xs:appinfo" >
   | < XS_ATTRIBUTE             : "xs:attribute" >
   | < XS_ATTRIBUTEGROUP        : "xs:attributeGroup" >
+  | < XS_CHOICE                : "xs:choice" >
   | < XS_COMPLEXCONTENT        : "xs:complexContent" >
   | < XS_COMPLEXTYPE           : "xs:complexType" >
   | < XS_DOCUMENTATION         : "xs:documentation" >
+  | < XS_ELEMENT               : "xs:element" >
   | < XS_ENUMERATION           : "xs:enumeration" >
   | < XS_EXTENSION             : "xs:extension" >
   | < XS_GROUP                 : "xs:group" >
   | < XS_INCLUDE               : "xs:include" >
   | < XS_LIST                  : "xs:list" >
+  | < XS_MAXINCLUSIVE          : "xs:maxInclusive" >
+  | < XS_MININCLUSIVE          : "xs:minInclusive" >
+  | < XS_MINEXCLUSIVE          : "xs:minExclusive" >
   | < XS_MINLENGTH             : "xs:minLength" >
   | < XS_PATTERN               : "xs:pattern" >
   | < XS_RESTRICTION           : "xs:restriction" >
+  | < XS_SEQUENCE              : "xs:sequence" >
   | < XS_SCHEMA                : "xs:schema" >
   | < XS_SIMPLETYPE            : "xs:simpleType" >
   | < XS_WHITESPACE            : "xs:whiteSpace" >
@@ -101,6 +107,7 @@ SPECIAL_TOKEN : /* COMMENTS */
   | < SCHEMALOCATION           : "schemaLocation" >
   | < SOURCE                   : "source" >
   | < TYPE                     : "type" >
+  | < USE                      : "use" >
   | < VALUE                    : "value" >
   | < VERSION                  : "version" >
   | < XML                      : "xml"          >
@@ -134,28 +141,33 @@ SPECIAL_TOKEN : /* COMMENTS */
     >
     |
     < #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
-    |
-    < CHARACTER_LITERAL:
-        "'"
-        (   (~["'","\\","\n","\r"])
-            |
-            ("\\"
-                (   ["n","t","b","r","f","\\","'","\""]
-                    |
-                    ["0"-"7"] ( ["0"-"7"] )?
-                    |
-                    ["0"-"3"] ["0"-"7"] ["0"-"7"]
-                )
-            )
-        )
-        "'"
-    >
+//     |
+//     < CHARACTER_LITERAL:
+//         "'"
+//         (   (~["'","\\","\n","\r"])
+//             |
+//             ("\\"
+//                 (   ["n","t","b","r","f","\\","'","\""]
+//                     |
+//                     ["0"-"7"] ( ["0"-"7"] )?
+//                     |
+//                     ["0"-"3"] ["0"-"7"] ["0"-"7"]
+//                 )
+//             )
+//         )
+//         "'"
+//     >
     |
     < STRING_LITERAL:
         "\""
         (   (~["\""])
         )*
         "\""
+		|
+        "\'"
+        (   (~["\'","\n","\r"])
+        )*
+        "\'"
     >
 //     < STRING_LITERAL:
 //         "\""
@@ -222,6 +234,8 @@ SPECIAL_TOKEN : /* COMMENTS */
 {
       < LPAREN               : "("    >
     | < RPAREN               : ")"    >
+     | < CDATA_START         : "<![CDATA["    >
+     | < CDATA_END           : "]]>"    >
     | < LBRACE               : "{"    >
     | < RBRACE               : "}"    >
     | < LBRACKET             : "["    >
@@ -229,6 +243,7 @@ SPECIAL_TOKEN : /* COMMENTS */
     | < SEMICOLON            : ";"    >
     | < COMMA                : ","    >
     | < DOT                  : "."    >
+    | < SQUOTE               : "'"    >
 }// SEPARATORS
 
 
@@ -242,6 +257,7 @@ SPECIAL_TOKEN : /* COMMENTS */
 //     | < TILDE                : "~"    >
 //     | < HOOK                 : "?"    >
      | < COLON                : ":"    >
+     | < HASH                 : "#"    >
 //     | < EQ                   : "=="   >
 //     | < LE                   : "<="   >
 //     | < GE                   : ">="   >
@@ -321,17 +337,23 @@ void Element() : {}
 		| XsAppinfo()
 		| XsAttribute()
 		| XsAttributeGroup()
+		| XsChoice()
 		| XsComplexContent()
 		| XsComplexType()
         | XsDocumentation()
+        | XsElement()
         | XsEnumeration()
         | XsExtension()
         | XsGroup()
         | XsInclude()
         | XsList()
+        | XsMaxInclusive()
+        | XsMinInclusive()
+        | XsMinExclusive()
         | XsMinLength()
         | XsPattern()
         | XsRestriction()
+        | XsSequence()
         | XsSimpleType()
         | XsWhiteSpace()
 	    ))
@@ -362,6 +384,12 @@ void XsAttributeGroup() #XsAttributeGroup : {}
 		 (">" ElementList() "</" <XS_ATTRIBUTEGROUP> ">" | ElementList() "/>")
 }
 
+void XsChoice() #XsChoice : {}
+{
+    <XS_CHOICE> AttributeList() 
+		 (">" ElementList() "</" <XS_CHOICE> ">" | ElementList() "/>")
+}
+
 void XsComplexContent() #XsComplexContent : {}
 {
     <XS_COMPLEXCONTENT> AttributeList() 
@@ -380,6 +408,12 @@ void XsDocumentation() #XsDocumentation : {}
 		 (">" ElementList() "</" <XS_DOCUMENTATION> ">" | ElementList() "/>")
 }
 
+void XsElement() #XsElement : {}
+{
+    <XS_ELEMENT> AttributeList() 
+		 (">" ElementList() "</" <XS_ELEMENT> ">" | ElementList() "/>")
+}
+
 void XsEnumeration() #XsEnumeration : {}
 {
     <XS_ENUMERATION> AttributeList() 
@@ -404,6 +438,24 @@ void XsInclude() #XsInclude : {}
 		 (">" ElementList() "</" <XS_INCLUDE> ">" | ElementList() "/>")
 }
 
+void XsMaxInclusive() #XsMaxInclusive : {}
+{
+    <XS_MAXINCLUSIVE> AttributeList() 
+		 (">" ElementList() "</" <XS_MAXINCLUSIVE> ">" | ElementList() "/>")
+}
+
+void XsMinInclusive() #XsMinInclusive : {}
+{
+    <XS_MININCLUSIVE> AttributeList() 
+		 (">" ElementList() "</" <XS_MININCLUSIVE> ">" | ElementList() "/>")
+}
+
+void XsMinExclusive() #XsMinExclusive : {}
+{
+    <XS_MINEXCLUSIVE> AttributeList() 
+		 (">" ElementList() "</" <XS_MINEXCLUSIVE> ">" | ElementList() "/>")
+}
+
 void XsList() #XsList : {}
 {
     <XS_LIST> AttributeList() 
@@ -428,6 +480,12 @@ void XsRestriction() #XsRestriction : {}
 		 (">" ElementList() "</" <XS_RESTRICTION> ">" | ElementList() "/>")
 }
 
+void XsSequence() #XsSequence : {}
+{
+    <XS_SEQUENCE> AttributeList() 
+		 (">" ElementList() "</" <XS_SEQUENCE> ">" | ElementList() "/>")
+}
+
 void XsSimpleType() #XsSimpleType : {}
 {
     <XS_SIMPLETYPE> AttributeList() 
@@ -443,8 +501,9 @@ void XsWhiteSpace() #XsWhiteSpace : {}
 void SimpleElement() : {}
 {
     IntElement()
-        | FloatElement()
-        | StringElement()
+  | FloatElement()
+  | StringElement()
+  | CDataElement()
 }
 
 void IntElement() #IntElement : { Token t; }
@@ -461,8 +520,6 @@ void FloatElement() #FloatElement : { Token t; }
 
 void StringElement() #StringElement : { Token t;String s; }
 {
-//     t = <IDENTIFIER> | t = <COMMA> | t = <MINUS> | t = <DOT> | t = <PLUS> | t = <SLASH>
-//                 | t = <COLON>
 	t = <IDENTIFIER>
 	{
 		s = t.image;
@@ -470,7 +527,7 @@ void StringElement() #StringElement : { Token t;String s; }
 		s += getData();
 	}
 	}
-        {jjtThis.setIDENTIFIER(s);}
+        {jjtThis.setSTRING(s);}
 }
 
 JAVACODE
@@ -494,6 +551,40 @@ String getData() {
 	return s.toString();
 }
 
+void CDataElement() #CDataElement : {String s; }
+{
+	<CDATA_START>
+	{
+		s = new String();
+		if (getToken(1).kind != CDATA_END ) {
+		s += getCData();
+	}
+	}
+	<CDATA_END>
+        {jjtThis.setSTRING(s);}
+}
+
+JAVACODE
+String getCData() {
+	StringBuffer s = new StringBuffer();
+//     jjtThis.someData = true;
+    while ((getToken(1)).kind != CDATA_END) {
+		Token t = getNextToken();
+		if (t.specialToken != null) {
+			Token tmp_t = t.specialToken;
+			while (tmp_t.specialToken != null) {
+				tmp_t = tmp_t.specialToken;
+			}
+			while (tmp_t != null) {
+				s.append(tmp_t.image);
+				tmp_t = tmp_t.next;
+			}
+		}
+		s.append(t.image);
+	}
+	return s.toString();
+}
+
 void AttributeList() #List : {}
 {
     (Attribute())*
@@ -518,6 +609,7 @@ void Attribute() : {}
   |	Source()
   |	SchemaLocation()
   |	Type()
+  |	Use()
   |	Value()
   |	XmlnsXs()
 }
@@ -607,6 +699,11 @@ void Type() #Type : {}
     <TYPE> <ASSIGN> AttValue()
 }
 
+void Use() #Use : {}
+{
+    <USE> <ASSIGN> AttValue()
+}
+
 void Value() #Value : {}
 {
     <VALUE> <ASSIGN> AttValue()
-- 
GitLab