Gitlab service upgraded. Please report any problems to dl_gitadmin@cs.lth.se

CompilerGeneration.jrag 35.9 KB
Newer Older
Anders Nilsson's avatar
Anders Nilsson committed
1 2 3
/* -*-Java-*- */

/* 
4
 * Copyright (C) 2007-2011  Anders Nilsson <anders.nilsson@control.lth.se>
Anders Nilsson's avatar
Anders Nilsson committed
5
 *
Anders Nilsson's avatar
Anders Nilsson committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * This file is part of XMLSchemaCompiler.
 *
 * XMLSchemaCompiler is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * XMLSchemaCompiler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XMLSchemaCompiler.  If not, see <http://www.gnu.org/licenses/>.
*/
Anders Nilsson's avatar
Anders Nilsson committed
21 22 23 24

import java.io.*;
import java.util.*;

25 26 27 28 29 30 31 32 33 34
aspect Misc {
    private static String ASTNode.grammar = new String();
    public static void ASTNode.setGrammar(String s) {
	grammar = s;
    }
    public static String ASTNode.getGrammar() {
	return grammar;
    }
}

Anders Nilsson's avatar
Anders Nilsson committed
35 36 37 38 39 40 41 42 43
aspect Subclasses {
	static int Element.superClassNum = 0;
	inh String Element.superClass();
	// eq ComplexElement.getElement().superClass();
	eq Start.getSpecification().superClass() = "ComplexElement";
	// eq XsChoice.getElement(int i).superClass() = "Choice_"+superClassNum;
	// eq XsSequence.getElement(int i).superClass() = "Choice_"+superClassNum;
}

Anders Nilsson's avatar
Anders Nilsson committed
44 45 46 47 48 49 50 51
aspect AbsGrammarGeneration {
    void ASTNode.genAbsGrammar(PrintStream pStream) {
		for (int i=0; i<getNumChild(); i++) {
			getChild(i).genAbsGrammar(pStream);
		}
    }

    public void Start.genAbsGrammar(PrintStream pStream) {
52 53 54
        pStream.println("// -*-Java-*- ");
        pStream.println();
        pStream.println("// Automatically generated JastAdd abstract grammar file ");
Anders Nilsson's avatar
Anders Nilsson committed
55
		pStream.println("// Predefined types");
56
		pStream.println("Start ::= Specification;");
57
        pStream.println("Specification ::= XmlHeader [DocType] Element*;");
58
        pStream.println("XmlHeader ::= Attribute*;");
59
        pStream.println("DocType ::= <STRING>;");
Anders Nilsson's avatar
Anders Nilsson committed
60 61
		pStream.println("abstract Thing : ComplexElement ::=;");
		pStream.println("abstract Element;");
62
  		pStream.println("ComplexElement : Element ::=  Attribute* Element*;");
Anders Nilsson's avatar
Anders Nilsson committed
63
		pStream.println("ValueElement : ComplexElement;");
64
		pStream.println("__DefaultElement__ : ComplexElement ::= <TAG>;");
Anders Nilsson's avatar
Anders Nilsson committed
65
		pStream.println("abstract SimpleElement : Element;");
66 67 68
		pStream.println("StringElement : SimpleElement ::= <LITERAL>;");
		pStream.println("IntElement : SimpleElement ::= <LITERAL>;");
		pStream.println("FloatElement : SimpleElement ::= <LITERAL>;");
69
		pStream.println("Attribute ::= AttrValue;");
70
		pStream.println("__DefaultAttribute__ : Attribute ::= <NAME>;");
71
		pStream.println("AttrValue ::= <LITERAL>;");
72
        pStream.println("Encoding : Attribute ::=;");
73 74 75
        pStream.println("Version : Attribute ::=;");
        pStream.println("XmlnsXsi : Attribute ::=;");
        pStream.println("XsiNoNamespaceSchemaLocation : Attribute ::=;");
Anders Nilsson's avatar
Anders Nilsson committed
76 77
		pStream.println();
		pStream.println("// Generated types");
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

        // This is somewhat of a hack, but seems to work work as an
        // easy way to eliminate duplicate attribute
        // declarations. Anders Nilsson 070809.
        Hashtable h = elementTypes();
        h.putAll(attributeTypes());
        Iterator iter = h.values().iterator();
        while (iter.hasNext()) {
            ((ASTNode) iter.next()).genAbsGrammar(pStream);
        }
// 		super.genAbsGrammar(pStream);
    }

    void XsElement.genAbsGrammar(PrintStream pStream) {
        if (hasName()) { // If not, it's just a reference to an element declaration
93
            String name = fixName(name());
Anders Nilsson's avatar
Anders Nilsson committed
94
            pStream.print(name + " : "+superClass()+" ::= ");
95 96 97 98
			// for (Element e : getElements()) {
			// 	e.genAbsGrammarNTA(pStream);
			// }
			pStream.println(";");
99 100 101 102 103 104 105 106
        }
    }

    void XsAttribute.genAbsGrammar(PrintStream pStream) {
        if (hasName()) { // If not, it's just a reference to an attribute declaration
            String name = fixName(name());
            pStream.println(name + " : Attribute ::= ;");
        }
Anders Nilsson's avatar
Anders Nilsson committed
107 108 109 110 111 112 113 114 115 116 117 118 119
    }


}

aspect GenJavaCC {
	void ASTNode.genJavaCC(PrintStream pStream) {
		for (int i=0; i<getNumChild(); i++) {
			getChild(i).genJavaCC(pStream);
		}
	}

	public void Start.genJavaCC(PrintStream pStream) {
120
        genJavaCCHeader(pStream);
121
        genJavaCCTokens(pStream);
122 123 124 125 126 127 128
        genJavaCCLiterals(pStream);
        genJavaCCIdentifiers(pStream);
        genJavaCCSeparators(pStream);
        genJavaCCOperators(pStream);
        pStream.println("Start Start() #Start : {}");
        pStream.println("{");
        pStream.println("  Specification()");
129
        pStream.println("  { return jjtThis; }");
130 131 132 133 134
        pStream.println("}");
        pStream.println();
        pStream.println("void Specification() #Specification : {}");
        pStream.println("{");
        pStream.println("  XmlHeader()");
135
        pStream.println("  ( [DocType()] ) #Opt(true)");
136 137 138
        pStream.println("  ElementList()");
        pStream.println("}");
        pStream.println();
139
        pStream.println("void DocType() #DocType : {String s;}");
140 141 142 143 144 145 146 147
        pStream.println("{");
        pStream.println("  <LTBANG>");
        pStream.println("  {");
        pStream.println("		s = new String();");
        pStream.println("		if (getToken(1).kind != GT ) {");
        pStream.println("		   s += getData();");
        pStream.println("        } ");
        pStream.println("  }       ");
148
        pStream.println("     {jjtThis.setSTRING(s);}");
149 150 151 152 153 154
        pStream.println("}");
        pStream.println();
        pStream.println("JAVACODE");
        pStream.println("String getData() {");
        pStream.println("	StringBuffer s = new StringBuffer();");
        pStream.println("//     jjtThis.someData = true;");
155
        pStream.println("    while ((getToken(1)).kind != LT && (getToken(1)).kind != LTSLASH) {");
156 157 158 159 160 161 162 163 164 165 166 167
        pStream.println("		Token t = getNextToken();");
        pStream.println("		if (t.specialToken != null) {");
        pStream.println("			Token tmp_t = t.specialToken;");
        pStream.println("			while (tmp_t.specialToken != null) {");
        pStream.println("				tmp_t = tmp_t.specialToken;");
        pStream.println("			}");
        pStream.println("			while (tmp_t != null) {");
        pStream.println("				s.append(tmp_t.image);");
        pStream.println("				tmp_t = tmp_t.next;");
        pStream.println("			}");
        pStream.println("		}");
        pStream.println("		s.append(t.image);");
168
        pStream.println("       s.append(\" \");");
169 170 171 172
        pStream.println("	}");
        pStream.println("	return s.toString();");
        pStream.println("}");
        pStream.println();
173 174
        pStream.println("void XmlHeader() #XmlHeader : {}");
        pStream.println("{");
175
        pStream.println("  <LTQ> <XML> AttributeList() <QGT>");
176 177 178 179 180 181 182 183 184
        pStream.println("}");
        pStream.println();
        pStream.println("void ElementList() #List : {}");
        pStream.println("{");
        pStream.println("  (Element())*");
        pStream.println("}");
        pStream.println();
        pStream.println("void AttributeList() #List : {}");
        pStream.println("{");
185
        pStream.println("  (LOOKAHEAD(2) Attribute())*");
186 187
        pStream.println("}");
        pStream.println();
188
        pStream.println("void AttrValue() #AttrValue : { Token t; }");
189 190
        pStream.println("{");
        pStream.println("    t = <STRING_LITERAL>");
191
        pStream.println("        {jjtThis.setLITERAL(t.image);}");
192
        pStream.println("}");
193 194 195 196 197 198
        pStream.println();
        pStream.println("void Encoding() #Encoding : {}");
        pStream.println("{");
        pStream.println("  <encoding> \"=\" AttrValue()");
        pStream.println("}");
        pStream.println();
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
        pStream.println("void Version() #Version : {}");
        pStream.println("{");
        pStream.println("  <version> \"=\" AttrValue()");
        pStream.println("}");
        pStream.println();
        pStream.println("void XsiNoNamespaceSchemaLocation() #XsiNoNamespaceSchemaLocation : {}");
        pStream.println("{");
        pStream.println("  <XsiNoNamespaceSchemaLocation> \"=\" AttrValue()");
        pStream.println("}");
        pStream.println();
        pStream.println("void XmlnsXsi() #XmlnsXsi : {}");
        pStream.println("{");
        pStream.println("  <XmlnsXsi> \"=\" AttrValue()");
        pStream.println("}");
        pStream.println();
214 215
        pStream.println("void Element()  : {}");
        pStream.println("{");
216
        pStream.println(" ( <LT> (");
217 218 219 220 221 222
        Iterator iter = elementTypes().values().iterator();
        if (iter.hasNext()) {
            pStream.println("  "+fixName(((XsElement) iter.next()).name())+"()");
        }
        while (iter.hasNext()) {
            pStream.println("  | "+fixName(((XsElement) iter.next()).name())+"()");
Anders Nilsson's avatar
Anders Nilsson committed
223
        }
224
        pStream.println("  | __DefaultElement__() ");
225 226 227 228 229 230 231 232 233
        pStream.println(" )) ");
        pStream.println(" | SimpleElement()");
        pStream.println("}");
        pStream.println();
        pStream.println("void SimpleElement() : {}");
        pStream.println("{");
        pStream.println("    IntElement()");
        pStream.println("	| FloatElement()");
        pStream.println("  	| StringElement()");
234 235
        pStream.println("}");
        pStream.println();
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
        pStream.println("void IntElement() #IntElement : { Token t; }");
        pStream.println("{");
        pStream.println("    t = <INTEGER_LITERAL>");
        pStream.println("	{jjtThis.setLITERAL(t.image);}");
        pStream.println("}");
        pStream.println();
        pStream.println("void FloatElement() #FloatElement : { Token t; }");
        pStream.println("{");
        pStream.println("    t = <FLOAT_LITERAL>");
        pStream.println("	{jjtThis.setLITERAL(t.image);}");
        pStream.println("}");
        pStream.println();
        pStream.println("void StringElement() #StringElement : { Token t;String s; }");
        pStream.println("{");
        pStream.println("	t = <IDENTIFIER>");
        pStream.println("	{");
        pStream.println("		s = t.image;");
        pStream.println("		if (getToken(1).kind != GT ) {");
        pStream.println("		s += getData();");
        pStream.println("	}");
        pStream.println("	}");
        pStream.println("        {jjtThis.setLITERAL(s);}");
        pStream.println("}");
259
        pStream.println("void __DefaultElement__() #__DefaultElement__ : { Token t; }");
260 261 262
        pStream.println("{");
        pStream.println("	t = <IDENTIFIER> AttributeList()");
		pStream.println(" 	(\">\" ElementList() \"</\" <IDENTIFIER> \">\" | ElementList() \"/>\")");
263
        pStream.println("        {jjtThis.setTAG(t.image);}");
264
        pStream.println("}");
265 266
        pStream.println("void Attribute()  : {}");
        pStream.println("{");
267
        pStream.println("  Encoding()");
268 269 270
        pStream.println(" | Version()");
        pStream.println(" | XsiNoNamespaceSchemaLocation()");
        pStream.println(" | XmlnsXsi()");
271 272 273 274
        iter = attributeTypes().values().iterator();
        while (iter.hasNext()) {
            pStream.println("  | "+fixName(((XsAttribute) iter.next()).name())+"()");
        }
275
        pStream.println("  | __DefaultAttribute__() ");
276 277
        pStream.println("  ");
        pStream.println("}");
278 279
        pStream.println();

280 281 282 283 284 285
		pStream.println("void __DefaultAttribute__() # __DefaultAttribute__ : {Token t;}");
		pStream.println("{");
		pStream.println("  t=<IDENTIFIER> \"=\" AttrValue()");
        pStream.println("        {jjtThis.setNAME(t.image);}");
		pStream.println("}");

286 287 288 289 290 291 292 293 294
        // This is somewhat of a hack, but seems to work work as an
        // easy way to eliminate duplicate attribute
        // declarations. Anders Nilsson 070808.
        Hashtable h = elementTypes();
        h.putAll(attributeTypes());
        iter = h.values().iterator();
        while (iter.hasNext()) {
            ((ASTNode) iter.next()).genJavaCC(pStream);
        }
Anders Nilsson's avatar
Anders Nilsson committed
295
	}
296 297

    void XsElement.genJavaCC(PrintStream pStream) {
298
        if (hasName()) { // If not, it's just a reference to an element declaration
299
            String name = fixName(name());
300 301 302
            pStream.println("void "+name+"() #"+name+" : {}");
            pStream.println("{");

303 304 305
            pStream.println("   <"+name+"> AttributeList()");
            pStream.print("  ( <GT> ElementList()");
            pStream.println("  \"</\" <"+name+">  \">\" | ElementList() \"/>\" )");
306 307 308 309 310 311 312 313 314
            pStream.println("}");
        }
    }

    void XsAttribute.genJavaCC(PrintStream pStream) {
        if (hasName()) { // If not, it's just a reference to an attribute declaration
            String name = fixName(name());
            pStream.println("void "+name+"() #"+name+" : {}");
            pStream.println("{");
315
            pStream.println("  <"+name+"> \"=\" AttrValue()");
316 317
            pStream.println("}");
        }
318 319
    }

320
    void Start.genJavaCCTokens(PrintStream pStream) {
321
		pStream.println("<DEFAULT> TOKEN : /* Reserved Words */");
322 323 324 325
		pStream.println("{");
        Hashtable h = elementTypes();
        h.putAll(attributeTypes());
        Iterator iter = h.values().iterator();
326 327 328 329
        pStream.println("   <XML\t\t: \"xml\">");
        pStream.println(" | <DOCTYPE : \"DOCTYPE\">");
        pStream.println(" | <PUBLIC : \"PUBLIC\">");
        pStream.println(" | <encoding\t\t: \"encoding\">");
330 331 332
        pStream.println(" | <version\t\t: \"version\">");
        pStream.println(" | <XsiNoNamespaceSchemaLocation\t\t: \"xsi:noNamespaceSchemaLocation\">");
        pStream.println(" | <XmlnsXsi\t\t: \"xmlns:xsi\">");
333 334 335 336 337 338 339 340
        while (iter.hasNext()) {
            String name = ((ComplexElement) iter.next()).name();
            pStream.println(" | <"+fixName(name)+
                            "\t\t: \""+name+"\" >");
        }
		pStream.println("}// Reserved Words");
        pStream.println();
    }
Anders Nilsson's avatar
Anders Nilsson committed
341 342
}

Anders Nilsson's avatar
Anders Nilsson committed
343
aspect Types {
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
    Hashtable Start.elementTypes() {
        Hashtable h = new Hashtable();
        return elementTypes(h);
    }

    syn Hashtable ASTNode.elementTypes(Hashtable h) {
        for (int i=0; i<getNumChild(); i++) {
            getChild(i).elementTypes(h);
        }
        return h;
    }

    eq XsElement.elementTypes(Hashtable h) {
        if (hasName()) {
            h.put(name(),this);
        }
        super.elementTypes(h);
        return h;
    }
    
    Hashtable Start.attributeTypes() {
        Hashtable h = new Hashtable();
        return attributeTypes(h);
    }

    syn Hashtable ASTNode.attributeTypes(Hashtable h) {
        for (int i=0; i<getNumChild(); i++) {
            getChild(i).attributeTypes(h);
        }
        return h;
    }

    eq XsAttribute.attributeTypes(Hashtable h) {
377
        if (hasName() && !defaultAttr()) {
378 379 380 381
            h.put(name(),this);
        }
        super.attributeTypes(h);
        return h;
Anders Nilsson's avatar
Anders Nilsson committed
382 383 384 385
    }
    
}

386
aspect Misc {
387 388 389 390 391 392 393 394 395 396 397 398 399
    static String XsAttribute.defAttrs[] = {"version"};

    syn boolean XsAttribute.defaultAttr() {
        String name = name();
        for (int i=0; i<defAttrs.length; i++) {
            if (name.equals(defAttrs[i])) {
                return true;
            }
        }
        return false;
    }

	syn lazy String ComplexElement.name() {
400 401 402 403 404 405
		for (int i=0; i<getNumAttribute(); i++) {
			if (getAttribute(i) instanceof Name) {
				return getAttribute(i).getAttValue().getSTRING_LITERAL().
                    replace("\""," ").trim();
			}
		}
406 407 408 409 410 411 412
		// Ok, let's check for "ref" instead
		for (int i=0; i<getNumAttribute(); i++) {
			if (getAttribute(i) instanceof Ref) {
				return getAttribute(i).getAttValue().getSTRING_LITERAL().
                    replace("\""," ").trim();
			}
		}
413 414 415 416
		// OK, no name found so let's report that
		return "NoNameFound";
	}

417
	syn lazy boolean ComplexElement.hasName() {
418 419 420 421 422 423 424 425 426
		for (int i=0; i<getNumAttribute(); i++) {
			if (getAttribute(i) instanceof Name) {
				return true;
			}
		}
		// OK, no name found so let's report that
		return false;
	}

Anders Nilsson's avatar
Anders Nilsson committed
427 428 429 430 431 432 433 434 435
	static final  String[] ASTNode.forbiddenNames = {"Attribute",
													 "else",
													 "final",
													 "if",
													 "to"};
	static ArrayList<String> ASTNode.problemNames = new ArrayList();
	static HashSet<String> ASTNode.registeredNames = new HashSet();

	static String ASTNode.fixName(String s) {
436 437 438 439
		// if (s.equals("class")) {
		// 	return "clazz";
		// }
		// if (problemNames.contains(s)) {
440
			s = "_" + s;
441
		// }
442
		s = s.replace("-","_").replace(":","");
Anders Nilsson's avatar
Anders Nilsson committed
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
		return s;
	}

	public void ASTNode.registerProblematicNames() {
		for (ASTNode child : this) {
			child.registerProblematicNames();
		}
	}

	public void Start.registerProblematicNames() {
		for (int i=0; i<forbiddenNames.length; i++) {
			problemNames.add(forbiddenNames[i]);
		}
		super.registerProblematicNames();
	}

	public void XsElement.registerProblematicNames() {
		if (!registeredNames.add(name().toLowerCase())) {
			problemNames.add(name());
		}
		super.registerProblematicNames();
	}

	public void XsAttribute.registerProblematicNames() {
		if (!registeredNames.add(name().toLowerCase())) {
			problemNames.add(name());
		}
	}
471 472
}

473 474
aspect GenAspects {
	public void Start.genAspects(PrintStream pStream) {
475 476 477
        pStream.println("/* -*-Java-*- */");
		pStream.println();
        pStream.println("// Automatically generated JastAdd aspects file ");
478 479 480
		pStream.println();
		pStream.println("import java.io.PrintStream;");
		pStream.println();
Anders Nilsson's avatar
Anders Nilsson committed
481 482
// 		genRewrites(pStream);
// 		genMisc(pStream);
483
		genNTA(pStream);
484 485 486
		genPrettyPrinter(pStream);
	}
}
Anders Nilsson's avatar
Anders Nilsson committed
487

488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
aspect NTA {
	void ASTNode.genNTA(PrintStream pStream) {}
	void Start.genNTA(PrintStream pStream) {
		pStream.println("aspect NTA {");
		getSpecification().getXsSchema().genNTA(pStream);
		pStream.println("}");
	}
	void ComplexElement.genNTA(PrintStream pStream) {
		for (Element e : getElements()) {
			e.genNTA(pStream);
		}
	}
	void XsElement.genNTA(PrintStream pStream) {
		for (Element e : getElements()) {
			e.genNTAAttribute(pStream,this);
		}		
	}

	void Element.genNTAAttribute(PrintStream pStream,ComplexElement elem) {}
	void ComplexElement.genNTAAttribute(PrintStream pStream,ComplexElement elem) {
		for (Element e : getElements()) {
			e.genNTAAttribute(pStream,elem);
		}
	}
    void XsAttribute.genNTAAttribute(PrintStream pStream,ComplexElement elem) {
		pStream.println("    syn nta "+fixName(name())+" "+
					  fixName(elem.name())+".get"+name()+"() {");
		pStream.println("        for (Attribute a : getAttributes()) {");
		pStream.println("            if (a instanceof "+fixName(name())+") {");
		pStream.println("                return ("+fixName(name())+") a;");
		pStream.println("            }");
		pStream.println("        }");
		pStream.println("        return null;");
		pStream.println("    }");
	}
    void XsElement.genNTAAttribute(PrintStream pStream,ComplexElement elem) {
		pStream.println("    syn nta List<"+fixName(name())+"> "+
					  fixName(elem.name())+".get"+name()+"() {");
		pStream.println("        List<"+fixName(name())+"> l = new List<"+
						fixName(name())+">();"); 
		pStream.println("        for (Element e : getElements()) {");
		pStream.println("            if (e instanceof "+fixName(name())+") {");
		pStream.println("                l.add(("+fixName(name())+")e);");
		pStream.println("            }");
		pStream.println("        }");
		pStream.println("        return l;");
		pStream.println("    }");
	}
}



540 541 542 543 544 545
aspect GenPrettyPrinter {
	public void ASTNode.genPrettyPrinter(PrintStream pStream) {
		for (int i=0; i<getNumChild(); i++) {
			getChild(i).genPrettyPrinter(pStream);
		}
	}
Anders Nilsson's avatar
Anders Nilsson committed
546

547 548 549 550
	public void Start.genPrettyPrinter(PrintStream pStream) {
		pStream.println();
		pStream.println("aspect PrettyPrinter {");
		pStream.println("  public void ASTNode.prettyPrint(String indent, PrintStream pStream) {");
551
// 		pStream.println("    pStream.print(getNumChild());");
552
		pStream.println("    for (int i=0; i<getNumChild(); i++) {");
553
// 		pStream.println("      pStream.print(getChild(i).getClass());");
554 555 556 557 558 559
		pStream.println("      getChild(i).prettyPrint(indent,pStream);");
		pStream.println("    }");
		pStream.println("  }\n");

// 		pStream.println("  public void Specification.prettyPrint(String indent, PrintStream pStream) {");
//         pStream.println("    getXmlHeader().prettyPrint(indent,pStream);");
Anders Nilsson's avatar
Anders Nilsson committed
560 561 562
// 		pStream.println("    for (int i=0; i<getNumElement(); i++) {");
// 		pStream.println("      getElement(i).prettyPrint(indent,pStream);");
// 		pStream.println("    }");
563 564 565 566 567 568 569 570 571 572
// 		pStream.println("  }\n");        

		pStream.println("  public void XmlHeader.prettyPrint(String indent, PrintStream pStream) {");
		pStream.println("    pStream.print(\"<?xml \");");
		pStream.println("    for (int i=0; i<getNumAttribute(); i++) {");
		pStream.println("      getAttribute(i).prettyPrint(indent,pStream);");
		pStream.println("    }");
		pStream.println("    pStream.println(\"?> \");");
		pStream.println("  }\n");

573 574 575
		pStream.println("  public void DocType.prettyPrint(String indent, PrintStream pStream) {");
		pStream.println("    pStream.print(\"<!\");");
        pStream.println("    pStream.print(getSTRING());");
576 577 578
		pStream.println("    pStream.println();");
        // Ugly as h*ll, but I don't have the time to fix it right now. AndersN 070914
// 		pStream.println("    pStream.println(\"> \");");
579 580
		pStream.println("  }\n");

581
		pStream.println("  public void AttrValue.prettyPrint(String indent, PrintStream pStream) {");
582
		pStream.println("    pStream.print(getLITERAL());");
583 584 585 586 587 588 589
		pStream.println("  }\n");

		pStream.println("  public void Encoding.prettyPrint(String indent, PrintStream pStream) {");
 		pStream.println("    pStream.print(\" encoding=\");");
        pStream.println("    getAttrValue().prettyPrint(indent,pStream);");
		pStream.println("  }\n");        

590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
		pStream.println("  public void Version.prettyPrint(String indent, PrintStream pStream) {");
 		pStream.println("    pStream.print(\" version=\");");
        pStream.println("    getAttrValue().prettyPrint(indent,pStream);");
		pStream.println("  }\n");        

		pStream.println("  public void XmlnsXsi.prettyPrint(String indent, PrintStream pStream) {");
 		pStream.println("    pStream.print(\" xmlns:xsi=\");");
        pStream.println("    getAttrValue().prettyPrint(indent,pStream);");
		pStream.println("  }\n");        

		pStream.println("  public void XsiNoNamespaceSchemaLocation.prettyPrint(String indent, PrintStream pStream) {");
 		pStream.println("    pStream.print(\" xsi:NoNamespaceSchemaLocation=\");");
        pStream.println("    getAttrValue().prettyPrint(indent,pStream);");
		pStream.println("  }\n");        

605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
		pStream.println("  public void __DefaultElement__.prettyPrint(String ind, PrintStream pStream) {");
		pStream.println("    pStream.print(ind+\"<\"+getTAG()+\" \");");
		pStream.println("    for (int i=0; i<getNumAttribute(); i++) {");
		pStream.println("      getAttribute(i).prettyPrint(ind,pStream);");
		pStream.println("    }");
        pStream.println("    if (getNumElement() == 0) {");
        pStream.println("       pStream.println(\"/> \");");
        pStream.println("    } else {");
        pStream.println("       pStream.println(\"> \");");
        pStream.println("       String newInd = ind+\"  \";");
        pStream.println("       for (int i=0; i<getNumElement(); i++) {");
        pStream.println("          getElement(i).prettyPrint(newInd,pStream);");
        pStream.println("       }");
		pStream.println("       pStream.println(ind+\"</\"+getTAG()+\"> \");");
        pStream.println("    }");
		pStream.println("  }\n");        

		pStream.println("  public void __DefaultAttribute__.prettyPrint(String indent, PrintStream pStream) {");
 		pStream.println("    pStream.print(\" \"+getNAME()+\"=\");");
        pStream.println("       getAttrValue().prettyPrint(indent,pStream);");
		pStream.println("  }\n");        

627 628 629 630 631 632
        Hashtable h = elementTypes();
        h.putAll(attributeTypes());
        Iterator iter = h.values().iterator();
        while (iter.hasNext()) {
            ((ASTNode) iter.next()).genPrettyPrinter(pStream);
        }
Anders Nilsson's avatar
Anders Nilsson committed
633
// 		super.genPrettyPrinter(pStream);
634 635 636 637
		pStream.println("}\n");
	}

    public void XsAttribute.genPrettyPrinter(PrintStream pStream) {
638 639 640
//         String name = fixName(name());
        String name = name();
		pStream.println("  public void "+fixName(name)+".prettyPrint(String indent, PrintStream pStream) {");
641 642 643 644 645 646
 		pStream.println("    pStream.print(\" "+name()+"=\");");
        pStream.println("       getAttrValue().prettyPrint(indent,pStream);");
		pStream.println("  }\n");        
    }

    public void XsElement.genPrettyPrinter(PrintStream pStream) {
647 648 649
//         String name = fixName(name());
        String name = name();
		pStream.println("  public void "+fixName(name)+".prettyPrint(String ind, PrintStream pStream) {");
650
		pStream.println("    pStream.print(ind+\"<"+name+" \");");
651
		pStream.println("    for (int i=0; i<getNumAttribute(); i++) {");
652
		pStream.println("      getAttribute(i).prettyPrint(ind,pStream);");
653
		pStream.println("    }");
654 655 656 657 658 659 660 661 662 663
        pStream.println("    if (getNumElement() == 0) {");
        pStream.println("       pStream.println(\"/> \");");
        pStream.println("    } else {");
        pStream.println("       pStream.println(\"> \");");
        pStream.println("       String newInd = ind+\"  \";");
        pStream.println("       for (int i=0; i<getNumElement(); i++) {");
        pStream.println("          getElement(i).prettyPrint(newInd,pStream);");
        pStream.println("       }");
		pStream.println("       pStream.println(ind+\"</"+name+"> \");");
        pStream.println("    }");
664 665 666
		pStream.println("  }\n");        
    }
}
Anders Nilsson's avatar
Anders Nilsson committed
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857

// 	public void OwlClassDecl.genPrettyPrinter(PrintStream pStream) {
// 		// Generate prettyprinter for class declaration
// 		pStream.print(ind(1)+"public void "+name());
// 		pStream.println(".prettyPrint(String indent, PrintStream pStream) {");
// 		pStream.println(ind(2)+"System.out.print(indent+\"<"+name()+"\");");
// 		pStream.println(ind(2)+"for (int i=0; i<getNumAttribute(); i++) {");
// 		pStream.println(ind(3)+"getAttribute(i).prettyPrint(indent,pStream);");
// 		pStream.println(ind(2)+"}");
// 		pStream.println("System.out.println(\">\");");
// 		pStream.println(ind(2)+"String childIndent = indent + \"  \";");
// // 		Restrictions restr = getRestrictions();
// // 		for (int i=0; i<restr.getNumOwlRestriction(); i++) {
// // 			OwlRestriction r = restr.getOwlRestriction(i);
// // 			pStream.println(ind(2)+"System.out.println(childIndent+\"<has"+
// // 							r.name()+">\");");
// // 			pStream.println(ind(2)+"for (int i=0; i<getNum"+r.name()+"(); i++) {");
// // 			pStream.println(ind(3)+"get"+r.name()+
// // 							"(i).prettyPrint(childIndent+\"  \",pStream);");
// // 			pStream.println(ind(2)+"}");
// // 			pStream.println(ind(2)+"System.out.println(childIndent+\"</has"+
// // 							r.name()+">\");");
// // 		}
// 		pStream.println(ind(2)+"for (int i=0; i<getNumElement(); i++) {");
// 		pStream.println(ind(3)+"getElement(i).prettyPrint(childIndent,pStream);");
// 		pStream.println(ind(2)+"}");
// 		pStream.println(ind(1)+"System.out.println(indent+\"</"+name()+">\");");
// 		pStream.println(ind(1)+"}\n");		
// 	}
// }

// aspect GenRewrites {

//     public void ASTNode.genRewrites(PrintStream pStream) {
// 		for (int i=0; i<getNumChild(); i++) {
// 			getChild(i).genRewrites(pStream);
// 		}
//     }

// 	public void Start.genRewrites(PrintStream pStream) {
// 		pStream.println();
// 		pStream.println("aspect Rewrites {");
// 		pStream.println(ind(1)+"rewrite ComplexElement {");
// 		pStream.println(ind(2)+"when (!(this instanceof ClassUse) && getOwlIdentifier().getIDENTIFIER().equals(\"value\") && !(this instanceof ValueElement))");
// 		pStream.println(ind(3)+"to ValueElement {");
// 		pStream.println(ind(3)+"ValueElement node = new ValueElement();");
// 		pStream.println(ind(3)+"node.setOwlIdentifier(getOwlIdentifier());");
// 		pStream.println(ind(3)+"node.setAttributeList(getAttributeList());");
// 		pStream.println(ind(3)+"node.setElementList(getElementList());");
// 		pStream.println(ind(3)+"return node;");
// 		pStream.println(ind(2)+"}");
// 		pStream.println(ind(1)+"}");

// 		// TEMPORARY!!!!!
// 		// Rewrite isSkillOf/hasSkill/hasProperty/isPropertyOf to ClassUse
// 		pStream.println(ind(1)+"rewrite ComplexElement {");
// 		pStream.println(ind(2)+"when ((name().equals(\"hasSkill\")");
// 		pStream.println(ind(2)+"|| name().equals(\"isSkillOf\"))");
// 		pStream.println("&& getNumAttribute() > 0 && getAttribute(0) instanceof RdfResource)");
// 		pStream.println(ind(2)+"to ClassUse {");
// 		pStream.println(ind(3)+"ClassUse use = new ClassUse();");
// 		pStream.println(ind(3)+"String att = getAttribute(0).name();");
// 		pStream.println(ind(3)+"use.setOwlIdentifier(new OwlIdentifier(att.substring(att.indexOf('#')+1,att.indexOf('_'))));");
// 		pStream.println(ind(3)+"use.setAttributeList(getAttributeList());");
// 		pStream.println(ind(3)+"use.setElementList(new List());");
// 		pStream.println(ind(3)+"return use;");
// 		pStream.println(ind(2)+"}");
// 		pStream.println(ind(1)+"}");
	  

// 		super.genRewrites(pStream);
// 		pStream.println("}");
// 	}

// 	public void OwlClassDecl.genRewrites(PrintStream pStream) {
// 		String ind = "  ";
// 		int ix = 0;
// 		// Specialize ComplexElements to the actual class in the ontology
// 		pStream.println(ind(1)+"rewrite ComplexElement {");
// 		pStream.println(ind(2)+"when (!(this instanceof ClassUse) && getOwlIdentifier().getIDENTIFIER().equals(\""+
// 						name()+"\") && !(this instanceof Thing))");
// 		pStream.println(ind(3)+"to "+name()+" {");

// 		pStream.println(ind(3)+name()+" node = new "+name()+"();");
// 		pStream.println(ind(3)+"node.setOwlIdentifier(getOwlIdentifier());");
// 		pStream.println(ind(3)+"node.setAttributeList(getAttributeList());");
//  		pStream.println(ind(3)+"node.setElementList(getElementList());");
// // 		Restrictions restr = getRestrictions();
// // 		pStream.println(ind(3)+"List l, remain;");
// // 		pStream.println(ind(3)+"remain = new List();");
// // 		for (int i=0; i<restr.getNumOwlRestriction(); i++) {
// // 			OwlRestriction r = restr.getOwlRestriction(i);
// // 			pStream.println(ind(3)+"l = new List();");
// // 			pStream.println(ind(3)+"for (int i=0; i<getNumElement(); i++) {");
// // 			pStream.println(ind(4)+"ComplexElement e = (ComplexElement) getElement(i);");
// // 			pStream.println(ind(4)+"if (e.getNumElement() > 0 && e.getElement(0) instanceof "+r.name()+" ) {");
// // 			pStream.println(ind(5)+"l.add(e.getElement(0));");
// // 			pStream.println(ind(4)+"}");
// // 			pStream.println(ind(3)+"}");
// // 			if (r.allValuesFrom()) {
// // 				pStream.println(ind(3)+"node.set"+r.name()+"List(l);");
// // 			} else {
// // 				pStream.println(ind(3)+r.name()+" c"+ix+" = new "+r.name()+"();");
// // 				pStream.println(ind(3)+"node.set"+r.name()+"(c"+(ix++)+");");
// // 			}
// // 		}
// // 		pStream.println(ind(3)+"for (int i=0; i<getNumElement(); i++) {");
// // 		pStream.println(ind(4)+"if (getElement(i) instanceof ValueElement) {");
// // 		pStream.println(ind(5)+"remain.add(getElement(i));");
// // 		pStream.println(ind(4)+"}");
// // 		pStream.println(ind(3)+"}");
// //   		pStream.println(ind(3)+"node.setElementList(remain);");
// 		pStream.println(ind(3)+"return node;");
// 		pStream.println(ind(2)+"}");
// 		pStream.println(ind(1)+"}");
// 		pStream.println();
// 		// Rearrange device nodes on the AST to get away from the
// 		// current diagonal style from Protege
// 		pStream.println(ind(1)+"rewrite "+name()+" {");
// 		pStream.println(ind(2)+"when (!isTopElement())");
// 		pStream.println(ind(2)+"to ClassUse {");
// 		pStream.println(ind(3)+"ClassUse use = new ClassUse();");
// 		pStream.println(ind(3)+"use.setOwlIdentifier(new OwlIdentifier(\""+name()+"\"));");
// 		pStream.println(ind(3)+"use.setAttributeList(new List().add(new RdfResource(getAttribute(0).getValue())));");
// 		pStream.println(ind(3)+"use.setElementList(new List());");
// 		pStream.println(ind(3)+"getStart().addElement(this);");
// 		pStream.println(ind(3)+"return use;");
// 		pStream.println(ind(2)+"}");
// 		pStream.println(ind(1)+"}");
// 	}

// }

// aspect GenMisc {

//     public void ASTNode.genMisc(PrintStream pStream) {
// 		for (int i=0; i<getNumChild(); i++) {
// 			getChild(i).genMisc(pStream);
// 		}
//     }

// 	public void Start.genMisc(PrintStream pStream) {
// 		pStream.println();
// 		pStream.println("aspect Misc {");
// 		pStream.println();
// 		pStream.println(ind(1)+"String ValueElement.value() {");
// 		pStream.println(ind(2)+"StringBuffer b = new StringBuffer();");
// 		pStream.println(ind(2)+"for (int i=0; i<getNumElement(); i++) {");
// 		pStream.println(ind(3)+"b.append(((SimpleElement) getElement(i)).value());");
// 		pStream.println(ind(3)+"b.append(\" \");");
// 		pStream.println(ind(2)+"}");
// 		pStream.println(ind(2)+"return b.toString().trim();");
// 		pStream.println(ind(1)+"}");
// 		pStream.println();
// 		pStream.println(ind(1)+"syn String SimpleElement.value() = \"\";");
// 		pStream.println(ind(1)+"eq StringElement.value() = getIDENTIFIER();");
// 		pStream.println(ind(1)+"eq IntElement.value() = getINTEGER_LITERAL();");
// 		pStream.println(ind(1)+"eq FloatElement.value() = getFLOAT_LITERAL();");
		
// 		pStream.println(ind(1)+"boolean Thing.isTopElement() {");
// 		pStream.println(ind(2)+"return getParent().getParent() instanceof Start;");
// 		pStream.println(ind(1)+"}");

// 		pStream.println(ind(1)+"Start ASTNode.getStart() {");
// 		pStream.println(ind(2)+"return getParent().getStart();");
// 		pStream.println(ind(1)+"}");
// 		pStream.println(ind(1)+"Start Start.getStart() {");
// 		pStream.println(ind(2)+"return this;");
// 		pStream.println(ind(1)+"}");
		
// 		pStream.println(ind(1)+"String ComplexElement.name() {");
// 		pStream.println(ind(2)+"return getOwlIdentifier().getIDENTIFIER();");
// 		pStream.println(ind(1)+"}");
// 		pStream.println(ind(1)+"String Attribute.name() {");
// 		pStream.println(ind(2)+"return getValue().getSTRING_LITERAL();");
// 		pStream.println(ind(1)+"}");


// 		super.genMisc(pStream);
// 		pStream.println("}");
// 	}

// }

aspect ParserGen {
	public void Start.genParser(PrintStream pStream) {
		pStream.println("import java.io.FileNotFoundException;");
		pStream.println("import java.io.FileReader;");
		pStream.println("import java.io.InputStreamReader;");
		pStream.println("import java.io.Reader;");
		pStream.println("");
858 859 860
		pStream.println("import "+getGrammar()+"AST.XmlParser;");
		pStream.println("import "+getGrammar()+"AST.ParseException;");
		pStream.println("import "+getGrammar()+"AST.Start;");
Anders Nilsson's avatar
Anders Nilsson committed
861 862 863 864 865 866 867
		pStream.println("");
		pStream.println("public class Parser {");
		pStream.println();
		pStream.println("	protected static Start parse(String args[]) {");
		pStream.println("		Reader r = getReader(args);");
		pStream.println("		Start ast = null;");
		pStream.println("		try {");
868
		pStream.println("			XmlParser parser = new XmlParser(r);");
Anders Nilsson's avatar
Anders Nilsson committed
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
		pStream.println();
		pStream.println("			ast = parser.Start();");
		pStream.println("		} catch (ParseException e) {");
		pStream.println("			System.out.println(e.getMessage());");
		pStream.println("		}");
		pStream.println("		return ast;");
		pStream.println("	}");
		pStream.println();
		pStream.println("	private static Reader getReader(String[] args) {");
		pStream.println("		Reader r = null;");
		pStream.println("		if (args.length != 1) {");
		pStream.println("			r = new InputStreamReader(System.in);");
		pStream.println("		} else {");
		pStream.println("			try {");
		pStream.println("				r = new FileReader(args[0]);");
		pStream.println("			} catch (FileNotFoundException e1) {");
		pStream.println("				System.err.println(\"Dumper: file \" + args[0] + \" not found\");");
		pStream.println("			}");
		pStream.println("		}");
		pStream.println("		return r;");
		pStream.println("	}");
		pStream.println("}");
	}
892 893

	public void Start.genPrettyPrint(PrintStream pStream) {
894
        pStream.println("import "+getGrammar()+"AST.Start;");
895 896 897 898 899 900 901
		pStream.println();
        pStream.println("public class PrettyPrint extends Parser {");
        pStream.println("        public static void main(String args[]) {");
        pStream.println("                Start ast = parse(args);");
		pStream.println();
        pStream.println("                // Dump the AST");
        pStream.println("                ast.prettyPrint(\"  \", System.out);");
902
//         pStream.println("                ast.dumpTree(\"  \", System.out);");
903 904 905 906
        pStream.println("        }");
        pStream.println("}");

    }
Anders Nilsson's avatar
Anders Nilsson committed
907
}