/** * Determines if this token is a keyword. * <p> * If a list of <tt>symbols</tt> is given, this method checks that the trigger matches one of them. * * @param keywords the keywords to check for. If the list es empty, only the token type is checked. * @return <tt>true</tt> if this token is a keyword and matches one of the given <tt>keywords</tt> if the list * is not empty. */ public boolean isKeyword(String... keywords) { if (keywords.length == 0) { return is(TokenType.KEYWORD); } for (String keyword : keywords) { if (matches(TokenType.KEYWORD, keyword)) { return true; } } return false; }
/** * Determines if this token is a special identifier. * <p> * If a list of <tt>triggers</tt> is given, this method checks that the trigger matches one of them. * * @param triggers the triggers to check for. If the list es empty, only the token type is checked. * @return <tt>true</tt> if this token is a special identifier and matches one of the given <tt>triggers</tt> * if the list is not empty. */ public boolean isSpecialIdentifier(String... triggers) { if (triggers.length == 0) { return is(TokenType.SPECIAL_ID); } for (String possibleTrigger : triggers) { if (matches(TokenType.SPECIAL_ID, possibleTrigger)) { return true; } } return false; }
/** * Determines if this token is a symbol. * <p> * If a list of <tt>symbols</tt> is given, this method checks that the trigger matches one of them. * * @param symbols the symbols to check for. If the list es empty, only the token type is checked. * @return <tt>true</tt> if this token is a symbol and matches one of the given <tt>symbols</tt> if the list * is not empty. */ public boolean isSymbol(String... symbols) { if (symbols.length == 0) { return is(TokenType.SYMBOL); } for (String symbol : symbols) { if (matches(TokenType.SYMBOL, symbol)) { return true; } } return false; }
/** * Determines if this token is an identifier. * <p> * If a list of <tt>values</tt> is given, this method checks that the content matches one of them. * * @param values the values to check for. If the list es empty, only the token type is checked. * @return <tt>true</tt> if this token is an identifier and matches one of the given <tt>values</tt> if the list * is not empty. */ public boolean isIdentifier(String... values) { if (values.length == 0) { return is(TokenType.ID); } for (String value : values) { if (matches(TokenType.ID, value)) { return true; } } return false; }
/** * Determines if this token is a special identifier. * <p> * If a list of <tt>triggers</tt> is given, this method checks that the trigger matches one of them. * * @param triggers the triggers to check for. If the list es empty, only the token type is checked. * @return <tt>true</tt> if this token is a special identifier and matches one of the given <tt>triggers</tt> * if the list is not empty. */ public boolean isSpecialIdentifier(String... triggers) { if (triggers.length == 0) { return is(TokenType.SPECIAL_ID); } for (String possibleTrigger : triggers) { if (matches(TokenType.SPECIAL_ID, possibleTrigger)) { return true; } } return false; }
/** * Determines if this token is a symbol. * <p> * If a list of <tt>symbols</tt> is given, this method checks that the trigger matches one of them. * * @param symbols the symbols to check for. If the list es empty, only the token type is checked. * @return <tt>true</tt> if this token is a symbol and matches one of the given <tt>symbols</tt> if the list * is not empty. */ public boolean isSymbol(String... symbols) { if (symbols.length == 0) { return is(TokenType.SYMBOL); } for (String symbol : symbols) { if (matches(TokenType.SYMBOL, symbol)) { return true; } } return false; }
/** * Determines if this token is a keyword. * <p> * If a list of <tt>symbols</tt> is given, this method checks that the trigger matches one of them. * * @param keywords the keywords to check for. If the list es empty, only the token type is checked. * @return <tt>true</tt> if this token is a keyword and matches one of the given <tt>keywords</tt> if the list * is not empty. */ public boolean isKeyword(String... keywords) { if (keywords.length == 0) { return is(TokenType.KEYWORD); } for (String keyword : keywords) { if (matches(TokenType.KEYWORD, keyword)) { return true; } } return false; }
/** * Determines if this token is an identifier. * <p> * If a list of <tt>values</tt> is given, this method checks that the content matches one of them. * * @param values the values to check for. If the list es empty, only the token type is checked. * @return <tt>true</tt> if this token is an identifier and matches one of the given <tt>values</tt> if the list * is not empty. */ public boolean isIdentifier(String... values) { if (values.length == 0) { return is(TokenType.ID); } for (String value : values) { if (matches(TokenType.ID, value)) { return true; } } return false; }
/** * Determines if this token is a special identifier with the given trigger. * <p> * If a list of <tt>contents</tt> is given, this method checks that the content matches one of them. * * @param trigger the trigger of the special id * @param contents the content to check for. If the list es empty, only the token type and the trigger is checked. * @return <tt>true</tt> if this token is a special identifier with the given trigger. * If <tt>contents</tt> is not empty, the content must also match one of the elements. */ public boolean isSpecialIdentifierWithContent(String trigger, String... contents) { if (!matches(TokenType.SPECIAL_ID, trigger)) { return false; } if (contents.length == 0) { return true; } for (String content : contents) { if (content != null && content.equals(getContents())) { return true; } } return false; }
/** * Determines if this token is a special identifier with the given trigger. * <p> * If a list of <tt>contents</tt> is given, this method checks that the content matches one of them. * * @param trigger the trigger of the special id * @param contents the content to check for. If the list es empty, only the token type and the trigger is checked. * @return <tt>true</tt> if this token is a special identifier with the given trigger. * If <tt>contents</tt> is not empty, the content must also match one of the elements. */ public boolean isSpecialIdentifierWithContent(String trigger, String... contents) { if (!matches(TokenType.SPECIAL_ID, trigger)) { return false; } if (contents.length == 0) { return true; } for (String content : contents) { if (content != null && content.equals(getContents())) { return true; } } return false; }
/** * Signals that the given token is expected. * <p> * If the current input is pointing at the specified token, it will be consumed. If not, an error will be added * to the error list and the input remains unchanged. * * @param type the type of the expected token * @param trigger the trigger of the expected token */ protected void expect(Token.TokenType type, String trigger) { if (tokenizer.current().matches(type, trigger)) { tokenizer.consume(); } else { errors.add(ParseError.error(tokenizer.current(), String.format("Unexpected token '%s'. Expected: '%s'", tokenizer.current().getSource(), trigger))); } } }
/** * Signals that the given token is expected. * <p> * If the current input is pointing at the specified token, it will be consumed. If not, an error will be added * to the error list and the input remains unchanged. * * @param type the type of the expected token * @param trigger the trigger of the expected token */ protected void expect(Token.TokenType type, String trigger) { if (tokenizer.current().matches(type, trigger)) { tokenizer.consume(); } else { errors.add(ParseError.error(tokenizer.current(), String.format("Unexpected token '%s'. Expected: '%s'", tokenizer.current().getSource(), trigger))); } } }
/** * Consumes the current token, expecting it to be as <tt>SYMBOL</tt> with the given content * * @param symbol the expected trigger of the current token */ public void consumeExpectedSymbol(String symbol) { if (current().matches(Token.TokenType.SYMBOL, symbol)) { consume(); } else { addError(current(), "Unexpected token: '%s'. Expected: '%s'", current().getSource(), symbol); } }
/** * Consumes the current token, expecting it to be as <tt>KEYWORD</tt> with the given content * * @param keyword the expected content of the current token */ public void consumeExpectedKeyword(String keyword) { if (current().matches(Token.TokenType.KEYWORD, keyword)) { consume(); } else { addError(current(), "Unexpected token: '%s'. Expected: '%s'", current().getSource(), keyword); } }
/** * Consumes the current token, expecting it to be as <tt>KEYWORD</tt> with the given content * * @param keyword the expected content of the current token */ public void consumeExpectedKeyword(String keyword) { if (current().matches(Token.TokenType.KEYWORD, keyword)) { consume(); } else { addError(current(), "Unexpected token: '%s'. Expected: '%s'", current().getSource(), keyword); } }
/** * Consumes the current token, expecting it to be as <tt>SYMBOL</tt> with the given content * * @param symbol the expected trigger of the current token */ public void consumeExpectedSymbol(String symbol) { if (current().matches(Token.TokenType.SYMBOL, symbol)) { consume(); } else { addError(current(), "Unexpected token: '%s'. Expected: '%s'", current().getSource(), symbol); } }