Skip to content

Commit

Permalink
Merge pull request #28 from merkrafter/refactor/test-suite
Browse files Browse the repository at this point in the history
Refactor/test suite
  • Loading branch information
merkrafter authored Dec 1, 2019
2 parents 3169fd7 + 384f774 commit d7907ce
Show file tree
Hide file tree
Showing 11 changed files with 1,046 additions and 392 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/merkrafter/lexing/IdentToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public IdentToken(final String ident, final String filename, final long line,
//==============================================================

/**
* @return the keyword this token stands for
* @return the identifier this token stands for
*/
String getIdent() {
return ident;
Expand All @@ -57,7 +57,7 @@ public boolean equals(final Object obj) {
}

/**
* Creates a String representation of this KeywordToken in the following format:
* Creates a String representation of this IdentToken in the following format:
* FILENAME(LINE,POSITION): TYPE(IDENT)
*
* @return a String representation of this IdentToken
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/com/merkrafter/lexing/NumberToken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.merkrafter.lexing;

/****
* This class serves as a token and stores the (integer) number found.
*
* @version v0.2.0
* @author merkrafter
***************************************************************/
public class NumberToken extends Token {
// ATTRIBUTES
//==============================================================
/**
* the number this token stands for
*/
private final long number;

// CONSTRUCTORS
//==============================================================

/****
* Creates a new NumberToken from a number and position data.
***************************************************************/
public NumberToken(final long number, final String filename, final long line,
final int position) {
super(TokenType.NUMBER, filename, line, position);
this.number = number;
}

// GETTER
//==============================================================

/**
* @return the number this token stands for
*/
long getNumber() {
return number;
}

// METHODS
//==============================================================
// public methods
//--------------------------------------------------------------

/**
* Two NumberTokens are equal if both have the type NumberToken and their numbers, line
* numbers, positions and filenames are equal.
*
* @param obj ideally a NumberToken to compare this with
* @return whether this is equal to obj
*/
@Override
public boolean equals(final Object obj) {
if (!super.equals(obj)) {
return false;
}
return obj instanceof NumberToken && ((NumberToken) obj).number == number;
}

/**
* Creates a String representation of this NumberToken in the following format:
* FILENAME(LINE,POSITION): TYPE(NUMBER)
*
* @return a String representation of this NumberToken
*/
@Override
public String toString() {
return super.toString() + String.format("(%d)", number);
}
}
69 changes: 69 additions & 0 deletions src/main/java/com/merkrafter/lexing/OtherToken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.merkrafter.lexing;

/****
* This class serves as a token and stores a string that could not be recognized as another token.
*
* @version v0.2.0
* @author merkrafter
***************************************************************/
public class OtherToken extends Token {
// ATTRIBUTES
//==============================================================
/**
* the string that could not be recognized as another token
*/
private final String string;

// CONSTRUCTORS
//==============================================================

/****
* Creates a new OtherToken from a string and position data.
***************************************************************/
public OtherToken(final String string, final String filename, final long line,
final int position) {
super(TokenType.OTHER, filename, line, position);
this.string = string;
}

// GETTER
//==============================================================

/**
* @return the string that could not be recognized as another token
*/
String getString() {
return string;
}

// METHODS
//==============================================================
// public methods
//--------------------------------------------------------------

/**
* Two OtherTokens are equal if both have the type OtherToken and their strings, line
* numbers, positions and filenames are equal.
*
* @param obj ideally a OtherToken to compare this with
* @return whether this is equal to obj
*/
@Override
public boolean equals(final Object obj) {
if (!super.equals(obj)) {
return false;
}
return obj instanceof OtherToken && ((OtherToken) obj).string.equals(string);
}

/**
* Creates a String representation of this OtherToken in the following format:
* FILENAME(LINE,POSITION): TYPE(STRING)
*
* @return a String representation of this OtherToken
*/
@Override
public String toString() {
return super.toString() + String.format("(%s)", string);
}
}
19 changes: 18 additions & 1 deletion src/main/java/com/merkrafter/lexing/Scanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,11 @@ public void processToken() {
do {
num += ch;
if (!this.loadNextCharSuccessfully()) {
setNumber(); // parse the num attribute to a NumberToken
return;
}
} while (ch >= '0' && ch <= '9');
setNumber(); // parse the num attribute to a NumberToke
break;
case 'a':
case 'b':
Expand Down Expand Up @@ -343,7 +345,7 @@ public void processToken() {
}
break;
default:
sym = new Token(TokenType.OTHER, filename, line, position);
sym = new OtherToken(Character.toString(ch), filename, line, position);
this.loadNextCharSuccessfully();
}
}
Expand Down Expand Up @@ -411,4 +413,19 @@ private void setIdentOrKeyword() {
}
}

/**
* Tests whether num currently holds a number. If that's the case, <code>sym</code> is changed
* to a NumberToken. Else, a OTHER TokenType is emitted in order to indicate an error.
*/
private void setNumber() {
try {
final long number = Long.parseLong(num);
// if this actually is a number:
sym = new NumberToken(number, sym.getFilename(), sym.getLine(), sym.getPosition());
} catch (NumberFormatException ignored) {
// id is not a number
sym = new Token(TokenType.OTHER, sym.getFilename(), sym.getLine(), sym.getPosition());
}
}

}
10 changes: 7 additions & 3 deletions src/main/java/com/merkrafter/parsing/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,9 @@ boolean parseActualParameters() {
return false;
}
}
} else {
return true; // it is okay if no expression comes here
}
// it is okay if no expression comes here
// but it is still necessary to check for the right paren
} else {
return false;
}
Expand Down Expand Up @@ -444,6 +444,10 @@ boolean parseTerm() {

boolean parseFactor() {
if (parseIdentifier()) {
// check whether this actually is a intern procedure call
if (parseActualParameters()) {
return true;
}
return true;
} else if (parseNumber()) {
return true;
Expand All @@ -457,7 +461,7 @@ boolean parseFactor() {
}
return success; // whether the above parseExpression() was successful
}
return parseInternProcedureCall();
return false;
}

/**
Expand Down
Loading

0 comments on commit d7907ce

Please sign in to comment.