Skip to content

Commit

Permalink
Improved some error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
mbosecke committed Feb 8, 2014
1 parent ce3c653 commit 21c47e9
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 43 deletions.
67 changes: 66 additions & 1 deletion src/main/java/com/mitchellbosecke/pebble/lexer/Lexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,73 @@

import com.mitchellbosecke.pebble.error.ParserException;


public interface Lexer {

public TokenStream tokenize(String source, String name) throws ParserException;

/**
* @return the commentOpenDelimiter
*/
public String getCommentOpenDelimiter();

/**
* @param commentOpenDelimiter
* the commentOpenDelimiter to set
*/
public void setCommentOpenDelimiter(String commentOpenDelimiter);

/**
* @return the commentCloseDelimiter
*/
public String getCommentCloseDelimiter();

/**
* @param commentCloseDelimiter
* the commentCloseDelimiter to set
*/
public void setCommentCloseDelimiter(String commentCloseDelimiter);

/**
* @return the executeOpenDelimiter
*/
public String getExecuteOpenDelimiter();

/**
* @param executeOpenDelimiter
* the executeOpenDelimiter to set
*/
public void setExecuteOpenDelimiter(String executeOpenDelimiter);

/**
* @return the executeCloseDelimiter
*/
public String getExecuteCloseDelimiter();

/**
* @param executeCloseDelimiter
* the executeCloseDelimiter to set
*/
public void setExecuteCloseDelimiter(String executeCloseDelimiter);

/**
* @return the printOpenDelimiter
*/
public String getPrintOpenDelimiter();

/**
* @param printOpenDelimiter
* the printOpenDelimiter to set
*/
public void setPrintOpenDelimiter(String printOpenDelimiter);

/**
* @return the printCloseDelimiter
*/
public String getPrintCloseDelimiter();

/**
* @param printCloseDelimiter
* the printCloseDelimiter to set
*/
public void setPrintCloseDelimiter(String printCloseDelimiter);
}
6 changes: 3 additions & 3 deletions src/main/java/com/mitchellbosecke/pebble/lexer/LexerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ private void lexExecute() throws ParserException {
// check if we are at the execute closing delimiter
if (brackets.isEmpty() && matcher.lookingAt()) {
String token = source.substring(cursor, cursor + matcher.end());
pushToken(Token.Type.EXECUTE_END);
pushToken(Token.Type.EXECUTE_END, delimiterExecuteClose);
moveCursor(token);
popState();
} else {
Expand All @@ -258,7 +258,7 @@ private void lexPrint() throws ParserException {
// check if we are at the print closing delimiter
if (brackets.isEmpty() && matcher.lookingAt()) {
String token = source.substring(cursor, cursor + matcher.end());
pushToken(Token.Type.PRINT_END);
pushToken(Token.Type.PRINT_END, delimiterPrintClose);
moveCursor(token);
popState();
} else {
Expand Down Expand Up @@ -389,7 +389,7 @@ else if (")]}".indexOf(character) >= 0) {
}

// we should have found something and returned by this point
throw new ParserException(null, String.format("Unexpected character \"%s\"", source.charAt(cursor)),
throw new ParserException(null, String.format("Unexpected character [%s]", source.charAt(cursor)),
lineNumber, filename);

}
Expand Down
57 changes: 20 additions & 37 deletions src/main/java/com/mitchellbosecke/pebble/lexer/TokenStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,64 +50,47 @@ public Token next() {

/**
* Checks the current token to see if it matches the provided type. If it
* doesn't match this will throw a SyntaxException. This will consume
* a token.
* doesn't match this will throw a SyntaxException. This will consume a
* token.
*
* @param type
* The type of token that we expect
* @return The current token
* @throws ParserException
* @throws ParserException
*/
public Token expect(Token.Type type) throws ParserException {
return expect(type, null, null);
return expect(type, null);
}

/**
* Checks the current token to see if it matches the provided type. If it
* doesn't match this will throw a SyntaxException. This will consume
* a token.
* doesn't match this will throw a SyntaxException. This will consume a
* token.
*
* @param type
* The type of token that we expect
* @return The current token
* @throws ParserException
* @throws ParserException
*/
public Token expect(Token.Type type, String value) throws ParserException {
return expect(type, value, null);
}


/**
* Checks the current token to see if it matches the provided type and
* value. If it doesn't match this will throw a SyntaxException with the
* provided message. This will consume a token.
*
* @param type
* The type of token that we expect
* @param value
* The value of the token that we expect, or null if we only care
* about the type
* @param message
* The message of the exception if the expectation fails
* @return The current token
* @throws ParserException
*/
public Token expect(Token.Type type, String value, String message) throws ParserException {
// TODO: message isn't used
Token token = tokens.get(current);

boolean success = true;
String message = null;
if (value == null) {
if (!token.test(type)) {
throw new ParserException(null, "Unexpected token of value ["
+ token.getValue() + "] expected token of type " + type
+ " ", token.getLineNumber(), filename);
success = token.test(type);
if (!success && message == null) {
message = "Unexpected token of value [" + token.getValue() + "] expected token of type " + type;
}
} else {
if (!token.test(type, value)) {
throw new ParserException(null, "Unexpected token of value ["
+ token.getValue() + "] expected [" + value
+ "] ", token.getLineNumber(), filename);
success = token.test(type, value);
if (!success && message == null) {
message = "Unexpected token of value [" + token.getValue() + "] expected [" + value + "] ";
}
}
if (!success) {
throw new ParserException(null, message, token.getLineNumber(), filename);
}
this.next();
return token;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private NodeExpression parseExpression(int minPrecedence) throws ParserException

stream.next();
expression = parseExpression();
stream.expect(Token.Type.PUNCTUATION, ")", "An opened parenthesis is not properly closed");
stream.expect(Token.Type.PUNCTUATION, ")");
expression = parsePostfixExpression(expression);

} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public NodeBody subparse(StoppingCondition stopCondition) throws ParserException
nodes.add(new NodePrint(expression, token.getLineNumber()));

// we expect to see a print closing delimiter
stream.expect(Token.Type.PRINT_END);
stream.expect(Token.Type.PRINT_END, engine.getLexer().getPrintCloseDelimiter());

break;

Expand Down

0 comments on commit 21c47e9

Please sign in to comment.