JavaParser fixed

This commit is contained in:
JanUlrich 2014-09-16 11:31:15 +02:00
parent 2c0ece66b9
commit 18e7b68927
6 changed files with 89 additions and 61 deletions

View File

@ -1,9 +1,13 @@
package de.dhbwstuttgart.antlr;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Vector;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
import org.junit.Test;
import de.dhbwstuttgart.antlr.Java8Parser.ClassDeclarationContext;
import de.dhbwstuttgart.antlr.Java8Parser.CompilationUnitContext;
@ -12,17 +16,27 @@ import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.Class;
public class Test {
public static void main(String[] args) throws Exception {
ANTLRInputStream input = new ANTLRInputStream(System.in);
public class VisitorTest {
private final static String testFile = System.getProperty("user.dir")+"/test/parser/AntlrTest.jav";
@Test
public void test() throws IOException{
ANTLRInputStream input = new ANTLRInputStream(new FileInputStream(testFile));
Java8Lexer lexer = new Java8Lexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
Java8Parser parser = new Java8Parser(tokens);
ParseTree tree = parser.compilationUnit(); // begin parsing at init rule
System.out.println(tree.toStringTree(parser)); // print LISP-style tree
// Create a generic parse tree walker that can trigger callbacks
ParseTreeWalker walker = new ParseTreeWalker();
}
//VISITOR:
EvalVisitor visitor = new EvalVisitor();
SyntaxTreeNode sourceFile = visitor.visit(tree);
System.out.println(sourceFile);
// Create a generic parse tree walker that can trigger callbacks
// ParseTreeWalker walker = new ParseTreeWalker();
}
}
class EvalVisitor extends Java8BaseVisitor<SyntaxTreeNode> {
@ -35,7 +49,7 @@ class EvalVisitor extends Java8BaseVisitor<SyntaxTreeNode> {
classDefinitions.add(cl);
}
return new SourceFile();
return new SourceFile(classDefinitions);
}
@Override
@ -46,7 +60,9 @@ class EvalVisitor extends Java8BaseVisitor<SyntaxTreeNode> {
@Override
public Class visitClassDeclaration(ClassDeclarationContext ctx) {
return null;
String name = ctx.Identifier().getText();
int offset = ctx.Identifier().getSymbol().getStartIndex();
return new Class(name, offset);
}
}

View File

@ -5,6 +5,7 @@ package de.dhbwstuttgart.parser;
// ino.module.InterfaceList.8615.import
import java.util.Vector;
import de.dhbwstuttgart.syntaxtree.Interface;
import de.dhbwstuttgart.syntaxtree.misc.UsedId;
// ino.class.InterfaceList.24383.description type=javadoc
@ -35,11 +36,15 @@ public class InterfaceList
// ino.end
// ino.method.getVector.24392.definition
public Vector<UsedId> getVector()
public Vector<Interface> getVector()
// ino.end
// ino.method.getVector.24392.body
{
return superif;
Vector<Interface> ret = new Vector<>();
for(UsedId name : superif){
ret.add(new Interface(name.get_Name_1Element(), name.getOffset()));
}
return ret;
}
// ino.end

View File

@ -950,7 +950,7 @@ case 34:
{
Interface ic = new Interface(((InterfaceAndParameter)yyVals[-2+yyTop]).getName(), ((Token)yyVals[-3+yyTop]).getOffset());
ic.setParaList(((InterfaceAndParameter)yyVals[-2+yyTop]).getParaVector());
ic.setSuperInterfaces(((InterfaceList)yyVals[-1+yyTop]).getVector());
/*ic.setSuperInterfaces($3.getVector());*/
ic.setInterfaceBody(((InterfaceBody)yyVals[0+yyTop]));
ic.setContainedTypes(containedTypes);
initContainedTypes();
@ -962,7 +962,7 @@ case 35:
{
Interface ic = new Interface(((InterfaceAndParameter)yyVals[-2+yyTop]).getName(), ((Modifiers)yyVals[-4+yyTop]), ((Token)yyVals[-3+yyTop]).getOffset());
ic.setParaList(((InterfaceAndParameter)yyVals[-2+yyTop]).getParaVector());
ic.setSuperInterfaces(((InterfaceList)yyVals[-1+yyTop]).getVector());
/*ic.setSuperInterfaces($4.getVector());*/
ic.setInterfaceBody(((InterfaceBody)yyVals[0+yyTop]));
ic.setContainedTypes(containedTypes);
initContainedTypes();
@ -986,7 +986,7 @@ case 37:
// line 493 "./../src/de/dhbwstuttgart/parser/JavaParser.jay"
{
ParaList pl = new ParaList();
RefType t = new RefType( ((Token)yyVals[-3+yyTop]).getLexem(),((Token)yyVals[-3+yyTop]).getOffset() );
RefType t = new RefType( ((Token)yyVals[-3+yyTop]).getLexem(),null,((Token)yyVals[-3+yyTop]).getOffset() );
t.set_ParaList( ((ParaList)yyVals[-1+yyTop]).get_ParaList() );
pl.getParalist().addElement(t);
org.apache.log4j.Logger.getLogger("parser").debug( "IDENTIFIER '<' paralist '>' --> Paralist f<>r " + ((Token)yyVals[-3+yyTop]).getLexem() + ": RefType");
@ -1018,7 +1018,7 @@ case 39:
case 40:
// line 521 "./../src/de/dhbwstuttgart/parser/JavaParser.jay"
{
RefType t = new RefType( ((Token)yyVals[-3+yyTop]).getLexem() ,((Token)yyVals[-3+yyTop]).getOffset() );
RefType t = new RefType( ((Token)yyVals[-3+yyTop]).getLexem(),null ,((Token)yyVals[-3+yyTop]).getOffset() );
t.set_ParaList( ((ParaList)yyVals[-1+yyTop]).get_ParaList() );
((ParaList)yyVals[-5+yyTop]).getParalist().addElement(t);
org.apache.log4j.Logger.getLogger("parser").debug( "paralist ',' IDENTIFIER '<' paralist '>' --> Paralist f<>r " + ((Token)yyVals[-3+yyTop]).getLexem() + ": RefType");
@ -1036,7 +1036,7 @@ case 42:
// line 535 "./../src/de/dhbwstuttgart/parser/JavaParser.jay"
{
/*Luar 29.11.06 Offset auf -1, da keine Angabe vorhanden*/
WildcardType wc = new WildcardType(-1);
WildcardType wc = new WildcardType(null,-1);
yyVal = wc;
}
break;
@ -1193,7 +1193,7 @@ case 64:
// line 663 "./../src/de/dhbwstuttgart/parser/JavaParser.jay"
{
/*PL 05-07-30 eingefuegt containedTypes ANFANG*/
RefType RT = new RefType(-1);
RefType RT = new RefType(null,-1);
/*RT.set_UsedId($1);*/
/*RT.setName(RT.get_UsedId().get_Name_1Element());*/
RT.set_ParaList(((UsedId)yyVals[0+yyTop]).get_RealParaList());
@ -1660,7 +1660,7 @@ case 122:
case 123:
// line 1071 "./../src/de/dhbwstuttgart/parser/JavaParser.jay"
{
Void Voit = new Void(((Token)yyVals[-1+yyTop]).getOffset());
Void Voit = new Void(((Method)yyVals[0+yyTop]),((Token)yyVals[-1+yyTop]).getOffset());
((Method)yyVals[0+yyTop]).setType(Voit);
yyVal=((Method)yyVals[0+yyTop]);
}
@ -1668,7 +1668,7 @@ case 123:
case 124:
// line 1077 "./../src/de/dhbwstuttgart/parser/JavaParser.jay"
{
Void voit = new Void(((Token)yyVals[-1+yyTop]).getOffset());
Void voit = new Void(((Method)yyVals[0+yyTop]),((Token)yyVals[-1+yyTop]).getOffset());
((Method)yyVals[0+yyTop]).set_Modifiers(((Modifiers)yyVals[-2+yyTop]));
((Method)yyVals[0+yyTop]).setType(voit);
yyVal=((Method)yyVals[0+yyTop]);
@ -1677,7 +1677,7 @@ case 124:
case 125:
// line 1084 "./../src/de/dhbwstuttgart/parser/JavaParser.jay"
{
Void voyt = new Void(((Token)yyVals[-2+yyTop]).getOffset());
Void voyt = new Void(((Method)yyVals[-1+yyTop]),((Token)yyVals[-2+yyTop]).getOffset());
((Method)yyVals[-1+yyTop]).setType(voyt);
((Method)yyVals[-1+yyTop]).set_ExceptionList(((ExceptionList)yyVals[0+yyTop]));
yyVal=((Method)yyVals[-1+yyTop]);
@ -1686,7 +1686,7 @@ case 125:
case 126:
// line 1091 "./../src/de/dhbwstuttgart/parser/JavaParser.jay"
{
Void voyd = new Void(((Token)yyVals[-2+yyTop]).getOffset());
Void voyd = new Void(((Method)yyVals[-1+yyTop]),((Token)yyVals[-2+yyTop]).getOffset());
((Method)yyVals[-1+yyTop]).set_Modifiers(((Modifiers)yyVals[-3+yyTop]));
((Method)yyVals[-1+yyTop]).setType(voyd);
((Method)yyVals[-1+yyTop]).set_ExceptionList(((ExceptionList)yyVals[0+yyTop]));
@ -1696,7 +1696,7 @@ case 126:
case 127:
// line 1099 "./../src/de/dhbwstuttgart/parser/JavaParser.jay"
{
Void Voit = new Void(((Token)yyVals[-1+yyTop]).getOffset());
Void Voit = new Void(((Method)yyVals[0+yyTop]),((Token)yyVals[-1+yyTop]).getOffset());
((Method)yyVals[0+yyTop]).setType(Voit);
((Method)yyVals[0+yyTop]).setGenericParameter(((GenericDeclarationList)yyVals[-2+yyTop]));
yyVal=((Method)yyVals[0+yyTop]);
@ -1705,7 +1705,7 @@ case 127:
case 128:
// line 1106 "./../src/de/dhbwstuttgart/parser/JavaParser.jay"
{
Void voit = new Void(((Token)yyVals[-1+yyTop]).getOffset());
Void voit = new Void(((Method)yyVals[0+yyTop]),((Token)yyVals[-1+yyTop]).getOffset());
((Method)yyVals[0+yyTop]).set_Modifiers(((Modifiers)yyVals[-3+yyTop]));
((Method)yyVals[0+yyTop]).setType(voit);
((Method)yyVals[0+yyTop]).setGenericParameter(((GenericDeclarationList)yyVals[-2+yyTop]));
@ -1715,7 +1715,7 @@ case 128:
case 129:
// line 1114 "./../src/de/dhbwstuttgart/parser/JavaParser.jay"
{
Void voyt = new Void(((Token)yyVals[-2+yyTop]).getOffset());
Void voyt = new Void(((Method)yyVals[-1+yyTop]),((Token)yyVals[-2+yyTop]).getOffset());
((Method)yyVals[-1+yyTop]).setType(voyt);
((Method)yyVals[-1+yyTop]).set_ExceptionList(((ExceptionList)yyVals[0+yyTop]));
((Method)yyVals[-1+yyTop]).setGenericParameter(((GenericDeclarationList)yyVals[-3+yyTop]));
@ -1725,7 +1725,7 @@ case 129:
case 130:
// line 1122 "./../src/de/dhbwstuttgart/parser/JavaParser.jay"
{
Void voyd = new Void(((Token)yyVals[-2+yyTop]).getOffset());
Void voyd = new Void(((Method)yyVals[-1+yyTop]),((Token)yyVals[-2+yyTop]).getOffset());
((Method)yyVals[-1+yyTop]).set_Modifiers(((Modifiers)yyVals[-4+yyTop]));
((Method)yyVals[-1+yyTop]).setType(voyd);
((Method)yyVals[-1+yyTop]).set_ExceptionList(((ExceptionList)yyVals[0+yyTop]));
@ -1867,7 +1867,7 @@ case 148:
case 149:
// line 1238 "./../src/de/dhbwstuttgart/parser/JavaParser.jay"
{
RefType RT = new RefType(-1);
RefType RT = new RefType(null,-1);
RT.set_UsedId(((UsedId)yyVals[0+yyTop]));
RT.setName(RT.get_UsedId().get_Name_1Element());
yyVal=RT;
@ -1915,7 +1915,7 @@ case 152:
case 153:
// line 1280 "./../src/de/dhbwstuttgart/parser/JavaParser.jay"
{
BooleanType BT = new BooleanType();
BooleanType BT = new BooleanType(null);
/* #JB# 05.04.2005 */
/* ########################################################### */
/*BT.setName($1.getLexem());*/
@ -1939,7 +1939,7 @@ case 155:
/*((UsedId)$1).vParaOrg = new Vector<Type>( $2.get_ParaList() );*/
}
UsedId uid = ((UsedId)yyVals[-1+yyTop]);
RefType RT = new RefType(uid.getOffset());
RefType RT = new RefType(null,uid.getOffset());
RT.set_ParaList(uid.get_RealParaList());
RT.setName(uid.getQualifiedName());
@ -1956,7 +1956,7 @@ case 156:
// line 1316 "./../src/de/dhbwstuttgart/parser/JavaParser.jay"
{
org.apache.log4j.Logger.getLogger("parser").debug("T->Parser->referenctype: " + ((UsedId)yyVals[0+yyTop]));
RefType RT = new RefType(((UsedId)yyVals[0+yyTop]).getOffset());
RefType RT = new RefType(null,((UsedId)yyVals[0+yyTop]).getOffset());
/*ausgetauscht PL 05-07-30*/
/*RT.set_UsedId($1); */
@ -2110,7 +2110,7 @@ case 174:
case 175:
// line 1481 "./../src/de/dhbwstuttgart/parser/JavaParser.jay"
{
IntegerType IT = new IntegerType();
IntegerType IT = new IntegerType(null);
/* #JB# 05.04.2005 */
/* ########################################################### */
/*IT.setName($1.getLexem());*/
@ -2121,7 +2121,7 @@ case 175:
case 176:
// line 1490 "./../src/de/dhbwstuttgart/parser/JavaParser.jay"
{
CharacterType CT = new CharacterType();
CharacterType CT = new CharacterType(null);
/* #JB# 05.04.2005 */
/* ########################################################### */
/*CT.setName($1.getLexem());*/

View File

@ -461,7 +461,7 @@ interfacedeclaration: INTERFACE interfaceidentifier interfacebody
{
Interface ic = new Interface($2.getName(), $1.getOffset());
ic.setParaList($2.getParaVector());
ic.setSuperInterfaces($3.getVector());
//ic.setSuperInterfaces($3.getVector());
ic.setInterfaceBody($4);
ic.setContainedTypes(containedTypes);
initContainedTypes();
@ -471,7 +471,7 @@ interfacedeclaration: INTERFACE interfaceidentifier interfacebody
{
Interface ic = new Interface($3.getName(), $1, $2.getOffset());
ic.setParaList($3.getParaVector());
ic.setSuperInterfaces($4.getVector());
//ic.setSuperInterfaces($4.getVector());
ic.setInterfaceBody($5);
ic.setContainedTypes(containedTypes);
initContainedTypes();
@ -492,7 +492,7 @@ paralist : IDENTIFIER
| IDENTIFIER '<' paralist '>'
{
ParaList pl = new ParaList();
RefType t = new RefType( $1.getLexem(),$1.getOffset() );
RefType t = new RefType( $1.getLexem(),null,$1.getOffset() );
t.set_ParaList( $3.get_ParaList() );
pl.getParalist().addElement(t);
org.apache.log4j.Logger.getLogger("parser").debug( "IDENTIFIER '<' paralist '>' --> Paralist f<>r " + $1.getLexem() + ": RefType");
@ -519,7 +519,7 @@ paralist : IDENTIFIER
| paralist ',' IDENTIFIER '<' paralist '>'
{
RefType t = new RefType( $3.getLexem() ,$3.getOffset() );
RefType t = new RefType( $3.getLexem(),null ,$3.getOffset() );
t.set_ParaList( $5.get_ParaList() );
$1.getParalist().addElement(t);
org.apache.log4j.Logger.getLogger("parser").debug( "paralist ',' IDENTIFIER '<' paralist '>' --> Paralist f<>r " + $3.getLexem() + ": RefType");
@ -534,7 +534,7 @@ paralist : IDENTIFIER
wildcardparameter : '?'
{
//Luar 29.11.06 Offset auf -1, da keine Angabe vorhanden
WildcardType wc = new WildcardType(-1);
WildcardType wc = new WildcardType(null,-1);
$$ = wc;
}
| '?' EXTENDS referencetype
@ -662,7 +662,7 @@ modifier : PUBLIC
classtype : classorinterfacetype
{
//PL 05-07-30 eingefuegt containedTypes ANFANG
RefType RT = new RefType(-1);
RefType RT = new RefType(null,-1);
//RT.set_UsedId($1);
//RT.setName(RT.get_UsedId().get_Name_1Element());
RT.set_ParaList($1.get_RealParaList());
@ -1069,27 +1069,27 @@ methodheader :genericdeclarationlist type methoddeclarator
}
| VOID methoddeclarator
{
Void Voit = new Void($1.getOffset());
Void Voit = new Void($2,$1.getOffset());
$2.setType(Voit);
$$=$2;
}
| modifiers VOID methoddeclarator
{
Void voit = new Void($2.getOffset());
Void voit = new Void($3,$2.getOffset());
$3.set_Modifiers($1);
$3.setType(voit);
$$=$3;
}
| VOID methoddeclarator throws
{
Void voyt = new Void($1.getOffset());
Void voyt = new Void($2,$1.getOffset());
$2.setType(voyt);
$2.set_ExceptionList($3);
$$=$2;
}
| modifiers VOID methoddeclarator throws
{
Void voyd = new Void($2.getOffset());
Void voyd = new Void($3,$2.getOffset());
$3.set_Modifiers($1);
$3.setType(voyd);
$3.set_ExceptionList($4);
@ -1097,14 +1097,14 @@ methodheader :genericdeclarationlist type methoddeclarator
}
| genericdeclarationlist VOID methoddeclarator
{
Void Voit = new Void($2.getOffset());
Void Voit = new Void($3,$2.getOffset());
$3.setType(Voit);
$3.setGenericParameter($1);
$$=$3;
}
| modifiers genericdeclarationlist VOID methoddeclarator
{
Void voit = new Void($3.getOffset());
Void voit = new Void($4,$3.getOffset());
$4.set_Modifiers($1);
$4.setType(voit);
$4.setGenericParameter($2);
@ -1112,7 +1112,7 @@ methodheader :genericdeclarationlist type methoddeclarator
}
| genericdeclarationlist VOID methoddeclarator throws
{
Void voyt = new Void($2.getOffset());
Void voyt = new Void($3,$2.getOffset());
$3.setType(voyt);
$3.set_ExceptionList($4);
$3.setGenericParameter($1);
@ -1120,7 +1120,7 @@ methodheader :genericdeclarationlist type methoddeclarator
}
| modifiers genericdeclarationlist VOID methoddeclarator throws
{
Void voyd = new Void($3.getOffset());
Void voyd = new Void($4,$3.getOffset());
$4.set_Modifiers($1);
$4.setType(voyd);
$4.set_ExceptionList($5);
@ -1236,7 +1236,7 @@ explicitconstructorinvocation : THIS '(' ')' ';'
classtypelist : classtype
{
RefType RT = new RefType(-1);
RefType RT = new RefType(null,-1);
RT.set_UsedId($1);
RT.setName(RT.get_UsedId().get_Name_1Element());
$$=RT;
@ -1278,7 +1278,7 @@ methoddeclarator :IDENTIFIER '(' ')'
primitivetype :BOOLEAN
{
BooleanType BT = new BooleanType();
BooleanType BT = new BooleanType(null);
/* #JB# 05.04.2005 */
/* ########################################################### */
//BT.setName($1.getLexem());
@ -1299,7 +1299,7 @@ referencelongtype : typename parameter
//((UsedId)$1).vParaOrg = new Vector<Type>( $2.get_ParaList() );
}
UsedId uid = $1;
RefType RT = new RefType(uid.getOffset());
RefType RT = new RefType(null,uid.getOffset());
RT.set_ParaList(uid.get_RealParaList());
RT.setName(uid.getQualifiedName());
@ -1315,7 +1315,7 @@ referencelongtype : typename parameter
referencetype :classorinterfacetype
{
org.apache.log4j.Logger.getLogger("parser").debug("T->Parser->referenctype: " + $1);
RefType RT = new RefType($1.getOffset());
RefType RT = new RefType(null,$1.getOffset());
//ausgetauscht PL 05-07-30
//RT.set_UsedId($1);
@ -1479,7 +1479,7 @@ expression :assignmentexpression
integraltype :INT
{
IntegerType IT = new IntegerType();
IntegerType IT = new IntegerType(null);
/* #JB# 05.04.2005 */
/* ########################################################### */
//IT.setName($1.getLexem());
@ -1488,7 +1488,7 @@ integraltype :INT
}
| CHAR
{
CharacterType CT = new CharacterType();
CharacterType CT = new CharacterType(null);
/* #JB# 05.04.2005 */
/* ########################################################### */
//CT.setName($1.getLexem());

View File

@ -172,12 +172,8 @@ public class Class extends SyntaxTreeNode implements AClassOrInterface, IItemWit
// ino.end
// ino.method.Class.23044.body
{
this.name = name;
this(name, offset);
this.modifiers = mod;
if(name.equals("java.lang.Object")){
superclassid=null;
}
this.offset = offset;
}
// ino.end
@ -193,13 +189,17 @@ public class Class extends SyntaxTreeNode implements AClassOrInterface, IItemWit
// ino.end
// ino.method.Class.23047.body
{
this.name = name;
this(name,offset);
this.modifiers = mod;
if (cb != null) set_ClassBody(cb);
if (ct != null) setContainedTypes(ct);
if (superclass != null) set_UsedId(superclass);
if (superclass != null){
this.superClass = new Class(superClass.getName().toString(),-1);
}
if (superif != null) setSuperInterfaces(superif);
if (paralist != null) this.set_ParaList(paralist);
if (paralist != null){
this.set_ParaList(paralist);
}
if(usedIdsToCheck!=null) this.usedIdsToCheck=usedIdsToCheck;
// HOTI 10.5.06
@ -208,14 +208,14 @@ public class Class extends SyntaxTreeNode implements AClassOrInterface, IItemWit
if(name.equals("java.lang.Object")){
superclassid=null;
}
parserlog.debug("Neue Klasse: " + name);
this.offset = offset;
}
// ino.end
public Vector<Field> getFields()
public Vector<Field> getFields()
{
return fielddecl;
}
@ -1144,6 +1144,9 @@ public class Class extends SyntaxTreeNode implements AClassOrInterface, IItemWit
if(t instanceof GenericTypeVar)this.genericClassParameters.add((GenericTypeVar)t);
else this.genericClassParameters.add(new GenericTypeVar(t.get_Name(),this,-1));
}
for(Type t : this.get_ParaList()){
t.parserPostProcessing(this);
}
/*
for(GenericTypeVar gtv : this.getGenericParameter()){
gtv.setParentClass(this);;

View File

@ -162,7 +162,11 @@ public class SourceFile
}
}
// ino.attribute.imports.21382.decldescription type=javadoc
public SourceFile(Vector<Class> classDefinitions) {
this.KlassenVektor = classDefinitions;
}
// ino.attribute.imports.21382.decldescription type=javadoc
/**
* HOTI 4.5.06
* Beinhaltet alle Imports des aktuell geparsten Files