Search in sources :

Example 1 with Tokenizer

use of net.sourceforge.sqlexplorer.parsers.Tokenizer in project tdq-studio-se by Talend.

the class StructuredCommentParser method createCommand.

/**
 * Attempts to create a AbstractCommand from a comment token
 * @param comment the comment to parse
 * @return the new AbstractCommand, or null if it is not a structured comment
 * @throws StructuredCommentException
 */
protected Command createCommand(Token comment) throws ParserException {
    StringBuffer sb = new StringBuffer(comment);
    sb.delete(0, 2);
    if (comment.getTokenType() == TokenType.ML_COMMENT)
        sb.delete(sb.length() - 2, sb.length());
    // Make sure it begins ${, but silently ignore it if not
    int pos = sb.indexOf("}", 2);
    if (sb.length() < 3 || !sb.substring(0, 2).equals("${") || pos < 0)
        return null;
    // Extract the command (ie the bit between "${" and "}") and the data (the bit after the "}")
    String data = null;
    if (pos < sb.length()) {
        data = sb.substring(pos + 1).trim();
        if (data.length() == 0)
            data = null;
    }
    sb = new StringBuffer(sb.substring(2, pos));
    // ...and has a word as the first token
    Tokenizer tokenizer = new Tokenizer(sb);
    Token token = tokenizer.nextToken();
    if (token == null)
        return null;
    if (token.getTokenType() != TokenType.WORD)
        throw new StructuredCommentException("Unexpected command in structured comment: " + token.toString(), comment);
    // Create a new AbstractCommand
    CommandType type;
    try {
        // I've kept the determination of CommandType outside of the constructor in case we want
        // to instantiate different classes for the different commands.
        type = CommandType.valueOf(token.toString().toUpperCase());
    } catch (IllegalArgumentException e) {
        throw new StructuredCommentException("Unrecognised structured comment command \"" + token.toString() + "\"", comment);
    }
    return type.createInstance(this, comment, tokenizer, data);
}
Also used : Token(net.sourceforge.sqlexplorer.parsers.Tokenizer.Token) Tokenizer(net.sourceforge.sqlexplorer.parsers.Tokenizer)

Aggregations

Tokenizer (net.sourceforge.sqlexplorer.parsers.Tokenizer)1 Token (net.sourceforge.sqlexplorer.parsers.Tokenizer.Token)1