<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
- <classpathentry kind="src" output="target/test-classes" path="src/test/java">
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<attributes>
- <attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
- <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
+ <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
- <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
+ <classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
+ <attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
public ClassReference(final Tokenizer tokenizer)
throws InvalidSyntaxException {
- name = tokenizer.getToken().token;
+ name = tokenizer.getNextToken().token;
- if (!tokenizer.isNextToken("<"))
+ if (!tokenizer.probeNextToken("<"))
return;
while (true) {
final ClassReference parameterType = new ClassReference(tokenizer);
typeParameters.add(parameterType);
- if (!tokenizer.isNextToken(","))
+ if (!tokenizer.probeNextToken(","))
break;
}
- tokenizer.expectToken(">");
+ tokenizer.expectNextToken(">");
}
@Override
private final String packageName;
private final String className;
+ private final boolean isInterface;
public ClassReference superClass;
public List<ClassReference> implementedInterfaces = new ArrayList<ClassReference>();
public Clazz(final String packageName, final String className,
- final Tokenizer tokenizer) throws InvalidSyntaxException {
+ final Tokenizer tokenizer, final boolean isInterface)
+ throws InvalidSyntaxException {
+
this.packageName = packageName;
this.className = className;
+ this.isInterface = isInterface;
while (true) {
- final TokenizerMatch match = tokenizer.getToken();
+ final TokenizerMatch match = tokenizer.getNextToken();
if ("extends".equals(match.token)) {
superClass = new ClassReference(tokenizer);
while (true) {
implementedInterfaces.add(new ClassReference(tokenizer));
- if (tokenizer.isNextToken(","))
+ if (tokenizer.probeNextToken(","))
continue;
break;
}
public void parseClassBody(final Tokenizer tokenizer) {
- tokenizer.skipUtilEnd();
+ tokenizer.skipUntilDataEnd();
}
@Override
public String toString() {
final EnumerationBuffer result = new EnumerationBuffer();
- result.append(packageName + " -> " + className + "\n");
+ result.append(packageName + " -> " + className + " ");
+
+ if (isInterface)
+ result.append("(interface)");
+ else
+ result.append("(class)");
+ result.append("\n");
+
if (superClass != null)
result.append(" super: " + superClass.toString() + "\n");
readFile();
final Tokenizer tokenizer = new Tokenizer(contents.toString());
+
+ // empty space
tokenizer.addTerminator(" ", true);
tokenizer.addTerminator("\t", true);
tokenizer.addTerminator("\n", true);
tokenizer.addTerminator(">", false);
tokenizer.addTerminator(",", false);
+ // comments
+ tokenizer.addTerminator("//", "\n", true);
+ tokenizer.addTerminator("/*", "*/", true);
+
final Modifiers modifiers = new Modifiers();
while (true) {
- final TokenizerMatch match = tokenizer.getToken();
+ final TokenizerMatch match = tokenizer.getNextToken();
if (match == null)
break;
continue;
}
+ if ("interface".equals(match.token)) {
+ parseInterface(tokenizer);
+ continue;
+ }
+
System.out.println(" " + modifiers.toString() + " "
+ match.token);
modifiers.reset();
private void parseClass(final Tokenizer tokenizer)
throws InvalidSyntaxException {
- final TokenizerMatch match = tokenizer.getToken();
- final Clazz clazz = new Clazz(packageName, match.token, tokenizer);
+ final TokenizerMatch match = tokenizer.getNextToken();
+ final Clazz clazz = new Clazz(packageName, match.token, tokenizer,
+ false);
System.out.println(clazz.toString());
-
}
private void parseImport(final Tokenizer tokenizer)
final Import imp = new Import();
- final TokenizerMatch match = tokenizer.getToken();
+ final TokenizerMatch match = tokenizer.getNextToken();
if (match.token.equals("static")) {
imp.isStatic = true;
- imp.path = tokenizer.getToken().token;
+ imp.path = tokenizer.getNextToken().token;
} else
imp.path = match.token;
imports.add(imp);
- tokenizer.expectToken(";");
+ tokenizer.expectNextToken(";");
+ }
+
+ private void parseInterface(final Tokenizer tokenizer)
+ throws InvalidSyntaxException {
+
+ final TokenizerMatch match = tokenizer.getNextToken();
+ final Clazz clazz = new Clazz(packageName, match.token, tokenizer, true);
+ System.out.println(clazz.toString());
}
private void parsePackage(final Tokenizer tokenizer)
throws InvalidSyntaxException {
- final TokenizerMatch match = tokenizer.getToken();
+ final TokenizerMatch match = tokenizer.getNextToken();
packageName = match.token;
- tokenizer.expectToken(";");
+ tokenizer.expectNextToken(";");
}
private void readFile() throws FileNotFoundException, IOException {
public void skipUntilSemicolon(final Tokenizer tokenizer) {
while (true) {
- final TokenizerMatch token = tokenizer.getToken();
+ final TokenizerMatch token = tokenizer.getNextToken();
+
+ if (token == null)
+ return;
+
if (token.token.equals(";"))
return;
}
boolean isFinal = false;
+ boolean isAbstract = false;
+
public boolean parseModifier(final String string) {
for (final Access access : Access.values())
if (access.name.equals(string)) {
return true;
}
+ if ("abstract".equals(string)) {
+ isAbstract = true;
+ return true;
+ }
+
return false;
}
public class Terminator {
- String value;
- boolean empty;
+ String startSequence;
+ String endSequence;
+ boolean ignoreTerminator;
- public Terminator(final String value, final boolean empty) {
- this.value = value;
- this.empty = empty;
+ public Terminator(final String startPattern, final boolean ignoreTerminator) {
+ this.startSequence = startPattern;
+ this.ignoreTerminator = ignoreTerminator;
}
+
+ public Terminator(final String startSequence, final String endSequence,
+ final boolean ignoreTerminator) {
+
+ this.startSequence = startSequence;
+ this.endSequence = endSequence;
+ this.ignoreTerminator = ignoreTerminator;
+ }
+
}
this.source = source;
}
- public void addTerminator(final String terminator, final boolean empty) {
+ public void addTerminator(final String startSequence,
+ final boolean ignoreTerminator) {
+ terminators.add(new Terminator(startSequence, ignoreTerminator));
+ }
- terminators.add(new Terminator(terminator, empty));
+ public void addTerminator(final String startSequence,
+ final String endSequence, final boolean ignoreTerminator) {
+ terminators.add(new Terminator(startSequence, endSequence,
+ ignoreTerminator));
}
- public void expectToken(final String value) throws InvalidSyntaxException {
- final TokenizerMatch match = getToken();
+ public void expectNextToken(final String value)
+ throws InvalidSyntaxException {
+ final TokenizerMatch match = getNextToken();
if (!value.equals(match.token))
throw new InvalidSyntaxException("Expected \"" + value
+ "\" but got \"" + match.token + "\" instead.");
}
- public TokenizerMatch getToken() {
+ public TokenizerMatch getNextToken() {
tokenIndexes.push(currentIndex);
final StringBuffer result = new StringBuffer();
boolean accumulateCurrentChar = true;
findTerminator: for (final Terminator terminator : terminators)
- if (terminatorMatches(terminator))
- // empty space detected
- if (terminator.empty) {
- currentIndex += terminator.value.length();
+ if (sequenceMatches(terminator.startSequence))
+
+ if (terminator.ignoreTerminator) {
+ currentIndex += terminator.startSequence.length();
+
+ if (terminator.endSequence != null)
+ skipUntilSequence(terminator.endSequence);
+
if (result.length() > 0)
return new TokenizerMatch(result.toString(),
terminator);
} else if (result.length() > 0)
return new TokenizerMatch(result.toString(), terminator);
else {
- currentIndex += terminator.value.length();
- return new TokenizerMatch(terminator.value, terminator);
+ currentIndex += terminator.startSequence.length();
+ return new TokenizerMatch(terminator.startSequence,
+ terminator);
}
if (accumulateCurrentChar) {
}
- public boolean isNextToken(final String token) {
- if (token.equals(getToken().token))
+ public boolean probeNextToken(final String token) {
+ if (token.equals(getNextToken().token))
return true;
- rollbackToken();
+ unreadToken();
return false;
}
- public void rollbackToken() {
- currentIndex = tokenIndexes.pop();
+ public boolean sequenceMatches(final String sequence) {
+ if ((currentIndex + sequence.length()) > source.length())
+ return false;
+
+ for (int i = 0; i < sequence.length(); i++)
+ if (sequence.charAt(i) != source.charAt(i + currentIndex))
+ return false;
+
+ return true;
}
- public void skipUtilEnd() {
+ public void skipUntilDataEnd() {
tokenIndexes.push(currentIndex);
currentIndex = source.length();
}
- public boolean terminatorMatches(final Terminator terminator) {
- if ((currentIndex + terminator.value.length()) > source.length())
- return false;
+ public void skipUntilSequence(final String sequence) {
+ while (currentIndex < source.length()) {
+ if (sequenceMatches(sequence)) {
+ currentIndex += sequence.length();
+ return;
+ }
- for (int i = 0; i < terminator.value.length(); i++)
- if (terminator.value.charAt(i) != source.charAt(i + currentIndex))
- return false;
+ currentIndex++;
+ }
+ }
- return true;
+ public void unreadToken() {
+ currentIndex = tokenIndexes.pop();
}
}