Commit 719e248a authored by Anders Nilsson's avatar Anders Nilsson
Browse files

Cleaned up somewhat and implemented an empty, but automatically generated, PrettyPrinter

parent c5dc13be
......@@ -7,3 +7,4 @@ plugins
testontologies/ontologyV06_Jambalaya.properties
siaras/Parser.java
siaras/Siaras.ast
siaras/PrettyPrinter.jrag
......@@ -50,14 +50,17 @@ aspect AbsGrammarGeneration {
pStream.print(getId());
pStream.print(" : "+getSuperClass().getId());
pStream.print(" ::=");
Restrictions restrs = getOwnRestrictions();
for (int i=0; i<restrs.getNumOwlRestriction(); i++) {
pStream.print(" ");
restrs.getOwlRestriction(i).genAbsGrammar(pStream);
}
getOwnRestrictions().genAbsGrammar(pStream);
pStream.println(";");
}
void Restrictions.genAbsGrammar(PrintStream pStream) {
for (int i=0; i<getNumOwlRestriction(); i++) {
pStream.print(" ");
getOwlRestriction(i).genAbsGrammar(pStream);
}
}
void OwlRestriction.genAbsGrammar(PrintStream pStream) {
// pStream.print(getRestrictionPropertyId());
pStream.print(getRestrictionClassId().substring(0,1).toLowerCase()+
......@@ -101,107 +104,129 @@ aspect AbsGrammarGeneration {
}
aspect GenPrettyPrinter {
public void ASTNode.genPrettyPrinter(PrintStream pStream) {
for (int i=0; i<getNumChild(); i++) {
getChild(i).genPrettyPrinter(pStream);
}
}
public void Start.genPrettyPrinter(PrintStream pStream) {
pStream.println("");
pStream.println("import java.io.PrintStream;");
pStream.println("");
pStream.println("aspect PrettyPrinter {");
pStream.println(" public void ASTNode.prettyPrint(String indent, PrintStream pStream) {");
pStream.println(" String childIndent = indent + \" \";");
pStream.println(" for (int i=0; i<getNumChild(); i++) {");
pStream.println(" getChild(i).prettyPrint(childIndent,pStream);");
pStream.println(" }");
pStream.println(" }");
pStream.println("}");
}
}
aspect JavaCCGen {
// JavaCC grammar is not generated, but hand-written as general as is possible
public void ASTNode.genJavaCC(PrintStream pStream) {
for (int i=0; i<getNumChild(); i++) {
getChild(i).genAbsGrammar(pStream);
}
}
// aspect JavaCCGen {
public void Start.genJavaCC(PrintStream pStream) {
genJavaCCHeader(pStream);
pStream.println();
genTokenDefs(pStream);
pStream.println();
genJavaCCLiterals(pStream);
pStream.println();
genJavaCCIdentifiers(pStream);
pStream.println();
genJavaCCSeparators(pStream);
pStream.println();
genJavaCCOperators(pStream);
pStream.println();
// public void ASTNode.genJavaCC(PrintStream pStream) {
// for (int i=0; i<getNumChild(); i++) {
// getChild(i).genAbsGrammar(pStream);
// }
// }
// OK, so now it's time to start generating some real grammar
// productions.
genJavaCCGrammar(pStream);
}
// public void Start.genJavaCC(PrintStream pStream) {
// genJavaCCHeader(pStream);
// pStream.println();
// genTokenDefs(pStream);
// pStream.println();
// genJavaCCLiterals(pStream);
// pStream.println();
// genJavaCCIdentifiers(pStream);
// pStream.println();
// genJavaCCSeparators(pStream);
// pStream.println();
// genJavaCCOperators(pStream);
// pStream.println();
void ASTNode.genTokenDefs(PrintStream pStream) {
for (int i=0; i<getNumChild(); i++) {
getChild(i).genTokenDefs(pStream);
}
}
// // OK, so now it's time to start generating some real grammar
// // productions.
// genJavaCCGrammar(pStream);
// }
void Start.genTokenDefs(PrintStream pStream) {
pStream.println("<DEFAULT, BOUNDS> TOKEN : /* RESERVED WORDS */");
pStream.println("{");
pStream.println(" <XML \t : \"xml\" >");
super.genTokenDefs(pStream);
pStream.println("}");
pStream.println();
}
// void ASTNode.genTokenDefs(PrintStream pStream) {
// for (int i=0; i<getNumChild(); i++) {
// getChild(i).genTokenDefs(pStream);
// }
// }
void OwlClassDecl.genTokenDefs(PrintStream pStream) {
pStream.println(" |<"+getId().toUpperCase()+" \t : \""+getId()+"\">");
}
// void Start.genTokenDefs(PrintStream pStream) {
// pStream.println("<DEFAULT, BOUNDS> TOKEN : /* RESERVED WORDS */");
// pStream.println("{");
// pStream.println(" <XML \t : \"xml\" >");
// super.genTokenDefs(pStream);
// pStream.println("}");
// pStream.println();
// }
void ASTNode.genJavaCCGrammar(PrintStream pStream) {
for (int i=0; i<getNumChild(); i++) {
getChild(i).genJavaCCGrammar(pStream);
}
}
// void OwlClassDecl.genTokenDefs(PrintStream pStream) {
// pStream.println(" |<"+getId().toUpperCase()+" \t : \""+getId()+"\">");
// }
void Start.genJavaCCGrammar(PrintStream pStream) {
pStream.println("Start Start() #Start : {}");
pStream.println("{");
pStream.println(" (Device())*");
pStream.println(" { return jjtThis; }");
pStream.println("}");
pStream.println();
super.genJavaCCGrammar(pStream);
}
// void ASTNode.genJavaCCGrammar(PrintStream pStream) {
// for (int i=0; i<getNumChild(); i++) {
// getChild(i).genJavaCCGrammar(pStream);
// }
// }
// void Start.genJavaCCGrammar(PrintStream pStream) {
// pStream.println("Start Start() #Start : {}");
// pStream.println("{");
// pStream.println(" (Device())*");
// pStream.println(" { return jjtThis; }");
// pStream.println("}");
// pStream.println();
// super.genJavaCCGrammar(pStream);
// }
void OwlClassDecl.genJavaCCGrammar(PrintStream pStream) {
pStream.println("void "+getId()+"() : {}");
pStream.println("{");
if (getId().equals("Device")) {
pStream.print(" \"<\"");
// pStream.print("\"<\" ( <DEVICE> \">\"");
// Restrictions restr = getRestrictions();
// for (int i=0; i< getNumRestriction(); i++) {
// OwlRestriction res = restr.getOwlRestriction(i);
// pStream.print("("+res.getRestrictionClassId()+")"+
// (res.allValuesFrom()?"*":"")+" ");
// }
// pStream.println(" #Device("+getNumRestriction()+"))");
// // List all subclasses
// Iterator subIter = getSubClasses().iterator();
// while (subIter.hasNext()) {
// OwlClassDecl subClass = (OwlClassDecl) subIter.next();
// pStream.println("| "+subClass.getId()+"()");
// }
}
pStream.print(" ( <"+getId().toUpperCase()+"> \">\"");
Restrictions restr = getRestrictions();
for (int i=0; i< getNumRestriction(); i++) {
OwlRestriction res = restr.getOwlRestriction(i);
pStream.print("("+res.getRestrictionClassId()+"())"+
(res.allValuesFrom()?"*":"")+" ");
}
pStream.println(" #"+getId()+"("+getNumRestriction()+"))");
// List all subclasses
Iterator subIter = getSubClasses().iterator();
while (subIter.hasNext()) {
OwlClassDecl subClass = (OwlClassDecl) subIter.next();
pStream.println(" | "+subClass.getId()+"()");
}
pStream.println("}");
}
}
// void OwlClassDecl.genJavaCCGrammar(PrintStream pStream) {
// pStream.println("void "+getId()+"() : {}");
// pStream.println("{");
// if (getId().equals("Device")) {
// pStream.print(" \"<\"");
// // pStream.print("\"<\" ( <DEVICE> \">\"");
// // Restrictions restr = getRestrictions();
// // for (int i=0; i< getNumRestriction(); i++) {
// // OwlRestriction res = restr.getOwlRestriction(i);
// // pStream.print("("+res.getRestrictionClassId()+")"+
// // (res.allValuesFrom()?"*":"")+" ");
// // }
// // pStream.println(" #Device("+getNumRestriction()+"))");
// // // List all subclasses
// // Iterator subIter = getSubClasses().iterator();
// // while (subIter.hasNext()) {
// // OwlClassDecl subClass = (OwlClassDecl) subIter.next();
// // pStream.println("| "+subClass.getId()+"()");
// // }
// }
// pStream.print(" ( <"+getId().toUpperCase()+"> \">\"");
// Restrictions restr = getRestrictions();
// for (int i=0; i< getNumRestriction(); i++) {
// OwlRestriction res = restr.getOwlRestriction(i);
// pStream.print("("+res.getRestrictionClassId()+"())"+
// (res.allValuesFrom()?"*":"")+" ");
// }
// pStream.println(" #"+getId()+"("+getNumRestriction()+"))");
// // List all subclasses
// Iterator subIter = getSubClasses().iterator();
// while (subIter.hasNext()) {
// OwlClassDecl subClass = (OwlClassDecl) subIter.next();
// pStream.println(" | "+subClass.getId()+"()");
// }
// pStream.println("}");
// }
// }
aspect ParserGen {
public void Start.genParser(PrintStream pStream) {
......
......@@ -23,14 +23,21 @@ public class GenCompiler extends Parser {
fileName = "siaras/Siaras.ast";
PrintStream pStream = new PrintStream(new File(fileName));
ast.genAbsGrammar(pStream);
// Generate JavaCC configuration
// fileName = "siaras/Siaras.jjt";
// pStream = new PrintStream(new File(fileName));
// ast.genJavaCC(pStream);
// Generate Parser.java
fileName = "siaras/Parser.java";
pStream = new PrintStream(new File(fileName));
ast.genParser(pStream);
// Generate PrettyPrinter
fileName = "siaras/PrettyPrinter.jrag";
pStream = new PrintStream(new File(fileName));
ast.genPrettyPrinter(pStream);
} catch (java.io.FileNotFoundException e) {
System.out.println("Could not create file: "+fileName);
e.printStackTrace();
......
import AST.Start;
public class DumpTree extends Parser {
public static void main(String args[]) {
Start ast = parse(args);
// PrettyPrint all OWL instances found in input
ast.dumpTree("",System.out);
}
}
import AST.Start;
public class PrettyPrinter extends Parser {
public static void main(String args[]) {
Start ast = parse(args);
// PrettyPrint all OWL instances found in input
ast.prettyPrint("",System.out);
}
}
......@@ -51,10 +51,6 @@ SPECIAL_TOKEN : /* COMMENTS */
}// COMMENTS
// <BOUNDS> TOKEN: {
// < BEGINCOMMENT : "<!--" >
// | < ENDCOMMENT : "-->" >
// }
<DEFAULT, BOUNDS> TOKEN : /* RESERVED WORDS AND LITERALS */
{
......@@ -167,11 +163,6 @@ SPECIAL_TOKEN : /* COMMENTS */
)*
"\""
>
// |
// < TOKEN_LITERAL:
// ( (~[" ","\\","\n","\r"])
// )*
// >
}// LITERALS
<DEFAULT, BOUNDS> TOKEN : /* IDENTIFIERS */
......@@ -275,12 +266,6 @@ SPECIAL_TOKEN : /* COMMENTS */
}// OPERATORS
// <VALUESTATE,BOUNDS> TOKEN : /* TEXT */
// {
// < VALUE_LITERAL:
// (<LETTER> | <DIGIT>) ( ~["<"] )*
// >
// }// TEXT
......@@ -295,25 +280,6 @@ Start Start() #Start : {}
ComplexElement()
{ return jjtThis; }
}
// "<" Identifier() ">" "</" Identifier() ">"
// void Specification() #Specification : {}
// {
// XmlHeader()
// RdfDeclaration()
// }
// void XmlHeader() #XmlHeader : {}
// {
// "<?" <XML> AttributeList() "?>"
// }
// void RdfDeclaration() #RdfDeclaration : {}
// {
// "<" <RDF_RDF> AttributeList() ">"
// ElementList()
// "</" <RDF_RDF> ">"
// }
void ElementList() #List : {}
{
......@@ -330,7 +296,6 @@ void ComplexElement() #ComplexElement : {}
"<" Identifier() AttributeList()
(">" ElementList() "</" <IDENTIFIER> ">" | ElementList() "/>")
}
// "<" Identifier() ">" "</" Identifier() ">"
void Identifier() #OwlIdentifier : {Token t;}
{
......@@ -356,7 +321,6 @@ void SimpleElement() : {}
{
IntElement()
| FloatElement()
// | LOOKAHEAD({getToken(1).kind != START_ENDTAG}) StringElement()
| StringElement()
}
......@@ -372,243 +336,13 @@ void FloatElement() #FloatElement : { Token t; }
{jjtThis.setFLOAT_LITERAL(t.image);}
}
// JAVACODE
// public void StringElement() {
// StringElement se = new StringElement();
// Token tok;
// StringBuffer buf = new StringBuffer();
// jjtree.openNodeScope(se);
// try {
// while (true) {
// tok = getToken(1);
// if (tok.kind == START_ENDTAG) break;
// if (tok.kind == EOF) throw new ParseException();
// buf.append(tok.image);
// tok = getNextToken();
// }
// } finally {
// jjtree.closeNodeScope(se,true);
// se.setIDENTIFIER(buf.toString());
// }
// }
void StringElement() #StringElement : {} //{ Token t; }
void StringElement() #StringElement : {}
{
Identifier() | OtherToken()
// WordList()
// t = <VALUE_LITERAL>
// {jjtThis.setIDENTIFIER(t.image);}
}
// void OwlElement() : {}
// {
// OwlOntology()
// | OwlAllDifferent()
// | OwlAllValuesFrom()
// | OwlCardinality()
// | OwlClass()
// | OwlDataRange()
// | OwlDatatypeProperty()
// | OwlDisjointWith()
// | OwlEquivalentClass()
// | OwlFunctionalProperty()
// | OwlImports()
// | OwlIntersectionOf()
// | OwlInverseOf()
// | OwlMinCardinality()
// | OwlObjectProperty()
// | OwlOneOf()
// | OwlOnProperty()
// | OwlSomeValuesFrom()
// | OwlRestriction()
// | OwlUnionOf()
// }
// void OwlOntology() #OwlOntology : {}
// {
// <OWL_ONTOLOGY> AttributeList()
// (">" ElementList() "</" <OWL_ONTOLOGY> ">" | ElementList() "/>")
// }
// void OwlAllDifferent() #OwlAllDifferent : {}
// {
// <OWL_ALLDIFFERENT> AttributeList()
// (">" ElementList() "</" <OWL_ALLDIFFERENT> ">" | ElementList() "/>")
// }
// void OwlAllValuesFrom() #OwlAllValuesFrom : {}
// {
// <OWL_ALLVALUESFROM> AttributeList()
// (">" ElementList() "</" <OWL_ALLVALUESFROM> ">" | ElementList() "/>")
// }
// void OwlCardinality() #OwlCardinality : {}
// {
// <OWL_CARDINALITY> AttributeList()
// (">" ElementList() "</" <OWL_CARDINALITY> ">" | ElementList() "/>")
// }
// void OwlClass() #OwlClass : {}
// {
// <OWL_CLASS> AttributeList()
// (">" ElementList() "</" <OWL_CLASS> ">" | ElementList() "/>")
// }
// void OwlDataRange() #OwlDataRange : {}
// {
// <OWL_DATARANGE> AttributeList()
// (">" ElementList() "</" <OWL_DATARANGE> ">" | ElementList() "/>")
// }
// void OwlDatatypeProperty() #OwlDatatypeProperty : {}
// {
// <OWL_DATATYPEPROPERTY> AttributeList()
// (">" ElementList() "</" <OWL_DATATYPEPROPERTY> ">" | ElementList() "/>")
// }
// void OwlDisjointWith() #OwlDisjointWith : {}
// {
// <OWL_DISJOINTWITH> AttributeList()
// (">" ElementList() "</" <OWL_DISJOINTWITH> ">" | ElementList() "/>")
// }
// void OwlEquivalentClass() #OwlEquivalentClass : {}
// {
// <OWL_EQUIVALENTCLASS> AttributeList()
// (">" ElementList() "</" <OWL_EQUIVALENTCLASS> ">" | ElementList() "/>")
// }
// void OwlFunctionalProperty() #OwlFunctionalProperty : {}
// {
// <OWL_FUNCTIONALPROPERTY> AttributeList()
// (">" ElementList() "</" <OWL_FUNCTIONALPROPERTY> ">" | ElementList() "/>")
// }
// void OwlImports () #OwlImports : {}
// {
// <OWL_IMPORTS> AttributeList()
// (">" ElementList() "</" <OWL_IMPORTS> ">" | ElementList() "/>")
// }
// void OwlIntersectionOf () #OwlIntersectionOf : {}
// {
// <OWL_INTERSECTIONOF> AttributeList()
// (">" ElementList() "</" <OWL_INTERSECTIONOF> ">" | ElementList() "/>")
// }
// void OwlInverseOf () #OwlInverseOf : {}
// {
// <OWL_INVERSEOF> AttributeList()
// (">" ElementList() "</" <OWL_INVERSEOF> ">" | ElementList() "/>")
// }
// void OwlMinCardinality () #OwlMinCardinality : {}
// {
// <OWL_MINCARDINALITY> AttributeList()
// (">" ElementList() "</" <OWL_MINCARDINALITY> ">" | ElementList() "/>")
// }
// void OwlObjectProperty () #OwlObjectProperty : {}
// {
// <OWL_OBJECTPROPERTY> AttributeList()
// (">" ElementList() "</" <OWL_OBJECTPROPERTY> ">" | ElementList() "/>")
// }
// void OwlOneOf () #OwlOneOf : {}
// {
// <OWL_ONEOF> AttributeList()
// (">" ElementList() "</" <OWL_ONEOF> ">" | ElementList() "/>")
// }
// void OwlOnProperty () #OwlOnProperty : {}
// {
// <OWL_ONPROPERTY> AttributeList()
// (">" ElementList() "</" <OWL_ONPROPERTY> ">" | ElementList() "/>")
// }
// void OwlSomeValuesFrom () #OwlSomeValuesFrom : {}
// {
// <OWL_SOMEVALUESFROM> AttributeList()
// (">" ElementList() "</" <OWL_SOMEVALUESFROM> ">" | ElementList() "/>")
// }
// void OwlRestriction () #OwlRestriction : {}
// {
// <OWL_RESTRICTION> AttributeList()
// (">" ElementList() "</" <OWL_RESTRICTION> ">" | ElementList() "/>")
// }
// void OwlUnionOf () #OwlUnionOf : {}
// {
// <OWL_UNIONOF> AttributeList()
// (">" ElementList() "</" <OWL_UNIONOF> ">" | ElementList() "/>")
// }
// void RdfElement() : {}
// {
// RdfType()
// | RdfFirst()
// | RdfRest()
// | RdfsComment()
// | RdfsDomain()
// | RdfsRange()
// | RdfsSubClassOf()
// | RdfsSubPropertyOf()
// }
// void RdfType() #RdfType : {}
// {
// <RDF_TYPE> AttributeList()
// (">" ElementList() "</" <RDF_TYPE> ">" | ElementList() "/>")
// }
// void RdfFirst() #RdfFirst : {}
// {
// <RDF_FIRST> AttributeList()
// (">" ElementList() "</" <RDF_FIRST> ">" | ElementList() "/>")
// }
// void RdfRest() #RdfRest : {}
// {
// <RDF_REST> AttributeList()
// (">" ElementList() "</" <RDF_REST> ">" | ElementList() "/>")
// }
// void RdfsComment() #RdfsComment : {}
// {
// <RDFS_COMMENT> AttributeList()
// (">" ElementList() "</" <RDFS_COMMENT> ">" | ElementList() "/>")
// }
// void RdfsDomain() #RdfsDomain : {}
// {
// <RDFS_DOMAIN> AttributeList()
// (">" ElementList() "</" <RDFS_DOMAIN> ">" | ElementList() "/>")
// }
// void RdfsRange() #RdfsRange : {}
// {
// <RDFS_RANGE> AttributeList()
// (">" ElementList() "</" <RDFS_RANGE> ">" | ElementList() "/>")
// }
// void RdfsSubClassOf() #RdfsSubClassOf : {}
// {
// <RDFS_SUBCLASSOF> AttributeList()
// (">" ElementList() "</" <RDFS_SUBCLASSOF> ">" | ElementList() "/>")
// }
// void RdfsSubPropertyOf() #RdfsSubPropertyOf : {}
// {
// <RDFS_SUBPROPERTYOF> AttributeList()
// (">" ElementList() "</" <RDFS_SUBPROPERTYOF> ">" | ElementList() "/>")
// }
// void AttributeList() #List : {}
// {
// (Attribute())*
// }
void Attribute() : {}
{
......
// package programs;
import AST.Start;
import java.io.File;
import java.io.PrintStream;
public class SiarasPrettyPrinter extends Parser {
public static void main(String args[]) {
Start ast = parse(args);