Search in sources :

Example 1 with SimpleTokenType

use of org.apache.camel.language.simple.types.SimpleTokenType in project camel by apache.

the class SimpleTokenizer method acceptToken.

private static boolean acceptToken(SimpleTokenType token, String text, String expression, int index) {
    if (token.isUnary() && text.startsWith(token.getValue())) {
        SimpleTokenType functionEndToken = getFunctionEndToken();
        if (functionEndToken != null) {
            int endLen = functionEndToken.getValue().length();
            // special check for unary as the previous must be a function end, and the next a whitespace
            // to ensure unary operators is only applied on functions as intended
            int len = token.getValue().length();
            String previous = "";
            if (index - endLen >= 0) {
                previous = expression.substring(index - endLen, index);
            }
            String after = text.substring(len);
            boolean whiteSpace = ObjectHelper.isEmpty(after) || after.startsWith(" ");
            boolean functionEnd = previous.equals(functionEndToken.getValue());
            return functionEnd && whiteSpace;
        }
    }
    return text.startsWith(token.getValue());
}
Also used : SimpleTokenType(org.apache.camel.language.simple.types.SimpleTokenType)

Example 2 with SimpleTokenType

use of org.apache.camel.language.simple.types.SimpleTokenType in project camel by apache.

the class SimpleBackwardsCompatibleParser method doParseExpression.

private static Expression doParseExpression(String expression, boolean allowEscape) {
    // should have no function tokens
    for (int i = 0; i < expression.length(); i++) {
        SimpleToken token = SimpleTokenizer.nextToken(expression, i, allowEscape, TokenType.functionStart, TokenType.functionEnd);
        if (token.getType().getType() == TokenType.functionStart || token.getType().getType() == TokenType.functionEnd) {
            return null;
        }
    }
    // okay there is no function tokens, then try to parse it as a simple function expression
    SimpleToken token = new SimpleToken(new SimpleTokenType(TokenType.functionStart, expression), 0);
    SimpleFunctionExpression function = new SimpleFunctionExpression(token);
    function.addText(expression);
    return function.createExpression(expression, false);
}
Also used : SimpleTokenType(org.apache.camel.language.simple.types.SimpleTokenType) SimpleFunctionExpression(org.apache.camel.language.simple.ast.SimpleFunctionExpression) SimpleToken(org.apache.camel.language.simple.types.SimpleToken)

Example 3 with SimpleTokenType

use of org.apache.camel.language.simple.types.SimpleTokenType in project camel by apache.

the class SimpleTokenizer method doNextToken.

private static SimpleToken doNextToken(String expression, int index, boolean allowEscape, TokenType... filters) {
    boolean numericAllowed = acceptType(TokenType.numericValue, filters);
    if (numericAllowed) {
        // is it a numeric value
        StringBuilder sb = new StringBuilder();
        boolean digit = true;
        while (digit && index < expression.length()) {
            digit = Character.isDigit(expression.charAt(index));
            if (digit) {
                char ch = expression.charAt(index);
                sb.append(ch);
                index++;
                continue;
            }
            // is it a dot or comma as part of a floating point number
            boolean decimalSeparator = '.' == expression.charAt(index) || ',' == expression.charAt(index);
            if (decimalSeparator && sb.length() > 0) {
                char ch = expression.charAt(index);
                sb.append(ch);
                index++;
                // assume its still a digit
                digit = true;
                continue;
            }
        }
        if (sb.length() > 0) {
            return new SimpleToken(new SimpleTokenType(TokenType.numericValue, sb.toString()), index);
        }
    }
    boolean escapeAllowed = allowEscape && acceptType(TokenType.escape, filters);
    if (escapeAllowed) {
        StringBuilder sb = new StringBuilder();
        char ch = expression.charAt(index);
        boolean escaped = '\\' == ch;
        if (escaped && index < expression.length() - 1) {
            // grab next character to escape
            char next = expression.charAt(++index);
            // special for new line, tabs and carriage return
            boolean special = false;
            if ('n' == next) {
                sb.append("\n");
                special = true;
            } else if ('t' == next) {
                sb.append("\t");
                special = true;
            } else if ('r' == next) {
                sb.append("\r");
                special = true;
            } else if ('}' == next) {
                sb.append("}");
                special = true;
            } else {
                // not special just a regular character
                sb.append(ch);
            }
            // force 2 as length if special
            return new SimpleToken(new SimpleTokenType(TokenType.character, sb.toString()), index, special ? 2 : 1);
        }
    }
    // it could be any of the known tokens
    String text = expression.substring(index);
    for (SimpleTokenType token : KNOWN_TOKENS) {
        if (acceptType(token.getType(), filters)) {
            if (acceptToken(token, text, expression, index)) {
                return new SimpleToken(token, index);
            }
        }
    }
    // fallback and create a character token
    char ch = expression.charAt(index);
    SimpleToken token = new SimpleToken(new SimpleTokenType(TokenType.character, "" + ch), index);
    return token;
}
Also used : SimpleTokenType(org.apache.camel.language.simple.types.SimpleTokenType) SimpleToken(org.apache.camel.language.simple.types.SimpleToken)

Example 4 with SimpleTokenType

use of org.apache.camel.language.simple.types.SimpleTokenType in project camel by apache.

the class BaseSimpleParser method nextToken.

/**
     * Advances the parser position to the next known {@link SimpleToken}
     * in the input.
     */
protected void nextToken() {
    if (index < expression.length()) {
        SimpleToken next = SimpleTokenizer.nextToken(expression, index, allowEscape);
        // add token
        tokens.add(next);
        token = next;
        // position index after the token
        previousIndex = index;
        index += next.getLength();
    } else {
        // end of tokens
        token = new SimpleToken(new SimpleTokenType(TokenType.eol, null), index);
    }
}
Also used : SimpleTokenType(org.apache.camel.language.simple.types.SimpleTokenType) SimpleToken(org.apache.camel.language.simple.types.SimpleToken)

Example 5 with SimpleTokenType

use of org.apache.camel.language.simple.types.SimpleTokenType in project camel by apache.

the class BaseSimpleParser method nextToken.

/**
     * Advances the parser position to the next known {@link SimpleToken}
     * in the input.
     *
     * @param filter filter for accepted token types
     */
protected void nextToken(TokenType... filter) {
    if (index < expression.length()) {
        SimpleToken next = SimpleTokenizer.nextToken(expression, index, allowEscape, filter);
        // add token
        tokens.add(next);
        token = next;
        // position index after the token
        previousIndex = index;
        index += next.getLength();
    } else {
        // end of tokens
        token = new SimpleToken(new SimpleTokenType(TokenType.eol, null), index);
    }
}
Also used : SimpleTokenType(org.apache.camel.language.simple.types.SimpleTokenType) SimpleToken(org.apache.camel.language.simple.types.SimpleToken)

Aggregations

SimpleTokenType (org.apache.camel.language.simple.types.SimpleTokenType)5 SimpleToken (org.apache.camel.language.simple.types.SimpleToken)4 SimpleFunctionExpression (org.apache.camel.language.simple.ast.SimpleFunctionExpression)1