Search in sources :

Example 6 with IToken

use of org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken in project scout.rt by eclipse.

the class SqlParser method parseAtom.

private Atom parseAtom(List<IToken> list, ParseContext ctx) {
    // (BracketExpr Outer | Statement | OrExpr | FunExpr | Name | Text | BinaryOp['*']) (OuterJoinToken)? (Name["AS"])? (Name[alias])?
    ParseStep lock = ctx.checkAndAdd("Atom", list);
    if (lock == null) {
        return null;
    }
    try {
        IToken t = null;
        if ((t = parseBracketExpr(list, ctx)) != null) {
        // ok
        } else if ((t = parseStatement(list, ctx)) != null) {
        // ok
        } else if ((t = parseOrExpr(list, ctx)) != null) {
        // ok
        } else if ((t = parseFunExpr(list, ctx)) != null) {
        // ok
        } else if ((t = removeToken(list, Name.class)) != null) {
        // ok
        } else if ((t = removeToken(list, Text.class)) != null) {
        // ok
        } else if ((t = removeToken(list, MathOp.class, "*")) != null) {
        // ok
        } else {
            return null;
        }
        // found a match
        Atom a = new Atom();
        a.addChild(t);
        if ((t = removeToken(list, OuterJoinToken.class)) != null) {
            a.addChild(t);
        }
        if ((t = removeToken(list, Name.class)) != null) {
            a.addChild(t);
        }
        return a;
    } finally {
        ctx.remove(lock);
    }
}
Also used : IToken(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken) OuterJoinToken(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.OuterJoinToken) MathOp(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.MathOp) Atom(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Atom) Name(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Name)

Example 7 with IToken

use of org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken in project scout.rt by eclipse.

the class SqlParser method tokenize.

/**
 * Tokenize a string into Whitespace (containing Comment and HintComment), Text and Token items. There are no Raw
 * objects left.
 *
 * @throws ParseException
 */
private List<IToken> tokenize(String s, ParseContext ctx) {
    s = encodeBinds(s, ctx);
    s = s.replaceAll("[\\n\\r]+", " ");
    List<IToken> list = new ArrayList<IToken>();
    list.add(new Raw(s));
    // 
    list = tokenizeRaw(list, COMMENT_PAT, Comment.class, true);
    list = tokenizeRaw(list, APOS_PAT, Text.class, true);
    list = tokenizeRaw(list, APOS_PAT, Text.class, false);
    list = tokenizeRaw(list, QUOT_PAT, Name.class, true);
    list = tokenizeRaw(list, QUOT_PAT, Name.class, false);
    // replace all remaining whitespace by a single space and convert to upper case
    for (IToken item : list) {
        if (item instanceof Raw) {
            String text = item.getText();
            text = text.replaceAll("[\\s]+", " ");
            text = text.toUpperCase();
            item.setText(text);
        }
    }
    list = tokenizeRaw(list, UNION_PAT, UnionToken.class, false);
    list = tokenizeRaw(list, PART_PAT, PartToken.class, false);
    list = tokenizeRaw(list, OUTER_JOIN_PAT, OuterJoinToken.class, false);
    list = tokenizeRaw(list, OR_OP_PAT, OrOp.class, false);
    list = tokenizeRaw(list, AND_OP_PAT, AndOp.class, false);
    list = tokenizeRaw(list, MATH_OP_PAT1, MathOp.class, false);
    list = tokenizeRaw(list, MATH_OP_PAT2, MathOp.class, false);
    list = tokenizeRaw(list, UNARY_PREFIX_PAT, UnaryPrefix.class, false);
    list = tokenizeRaw(list, NAME_PAT, Name.class, false);
    list = tokenizeRaw(list, OPEN_BRACKET_PAT, OpenBracketToken.class, false);
    list = tokenizeRaw(list, CLOSE_BRACKET_PAT, CloseBracketToken.class, false);
    list = tokenizeRaw(list, LIST_SEPARATOR_PAT, ListSeparator.class, false);
    // eliminate all empty Raw
    for (Iterator<IToken> it = list.iterator(); it.hasNext(); ) {
        IToken item = it.next();
        if (item instanceof Raw && !StringUtility.hasText(item.getText())) {
            it.remove();
        }
    }
    // check the rest, no more Raw, convert to comment with warning
    for (int i = 0; i < list.size(); i++) {
        IToken tok = list.get(i);
        if (tok instanceof Raw) {
            list.remove(i);
            Comment c = new Comment();
            c.setText("/*X" + "X" + "X" + " unexpected token: " + tok.getText() + "*/");
            list.add(i, c);
        }
    }
    // associate comments with the successor token
    for (int i = 0; i < list.size(); i++) {
        if (list.get(i) instanceof Comment) {
            // find successor
            IToken succ = null;
            for (int k = i + 1; k < list.size(); k++) {
                if (!(list.get(k) instanceof Comment)) {
                    succ = list.get(k);
                    break;
                }
            }
            if (succ == null) {
                for (int k = 0; k < list.size(); k++) {
                    if (!(list.get(k) instanceof Comment)) {
                        succ = list.get(k);
                        break;
                    }
                }
            }
            if (succ != null) {
                succ.addComment((Comment) list.get(i));
            }
        }
    }
    for (Iterator<IToken> it = list.iterator(); it.hasNext(); ) {
        IToken item = it.next();
        if (item instanceof Comment) {
            it.remove();
        }
    }
    decodeBinds(list, ctx);
    return list;
}
Also used : Comment(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Comment) OrOp(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.OrOp) ListSeparator(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.ListSeparator) PartToken(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.PartToken) ArrayList(java.util.ArrayList) UnionToken(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.UnionToken) Raw(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Raw) Text(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Text) OpenBracketToken(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.OpenBracketToken) Name(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Name) CloseBracketToken(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.CloseBracketToken) IToken(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken) OuterJoinToken(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.OuterJoinToken) MathOp(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.MathOp) UnaryPrefix(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.UnaryPrefix) AndOp(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.AndOp)

Example 8 with IToken

use of org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken in project scout.rt by eclipse.

the class SqlParser method parseAndExpr.

private AndExpr parseAndExpr(List<IToken> list, ParseContext ctx) {
    // MathExpr (BinaryOp['AND'] MathExpr)*
    ParseStep lock = ctx.checkAndAdd("AndExpr", list);
    if (lock == null) {
        return null;
    }
    try {
        IToken me = parseMathExpr(list, ctx);
        if (me == null) {
            return null;
        }
        AndExpr ae = new AndExpr();
        ae.addChild(me);
        AndOp ao;
        while ((ao = removeToken(list, AndOp.class)) != null && (me = parseMathExpr(list, ctx)) != null) {
            ae.addChild(ao);
            ae.addChild(me);
        }
        // remaining?
        if (ao != null) {
            ao.addComment(new Comment("/*syntax warning*/"));
            ae.addChild(ao);
        }
        return ae;
    } finally {
        ctx.remove(lock);
    }
}
Also used : AndExpr(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.AndExpr) Comment(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Comment) IToken(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken) AndOp(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.AndOp)

Example 9 with IToken

use of org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken in project scout.rt by eclipse.

the class SqlParser method parseUnaryPrefixExpr.

private UnaryPrefixExpr parseUnaryPrefixExpr(List<IToken> list, ParseContext ctx) {
    // UnaryPrefix Atom
    ParseStep lock = ctx.checkAndAdd("UnaryPrefixExpr", list);
    if (lock == null) {
        return null;
    }
    try {
        UnaryPrefix up = null;
        IToken a = null;
        if ((up = removeToken(list, UnaryPrefix.class)) != null && (a = parseAtom(list, ctx)) != null) {
        // ok
        } else {
            // restore incomplete
            if (up != null) {
                list.add(0, up);
            }
            return null;
        }
        UnaryPrefixExpr e = new UnaryPrefixExpr();
        e.addChild(up);
        e.addChild(a);
        return e;
    } finally {
        ctx.remove(lock);
    }
}
Also used : IToken(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken) UnaryPrefix(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.UnaryPrefix) UnaryPrefixExpr(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.UnaryPrefixExpr)

Example 10 with IToken

use of org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken in project scout.rt by eclipse.

the class SqlFormatter method formatUnaryPrefixExpr.

private void formatUnaryPrefixExpr(UnaryPrefixExpr stm, FormatContext ctx) {
    for (IToken t : stm.getChildren()) {
        if (t instanceof UnaryPrefix) {
            formatDefault(t, ctx);
            ctx.print(" ");
        } else if (t instanceof Atom) {
            formatAtom((Atom) t, ctx);
        } else {
            formatDefault(t, ctx);
        }
    }
}
Also used : IToken(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken) UnaryPrefix(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.UnaryPrefix) Atom(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Atom)

Aggregations

IToken (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken)16 MathOp (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.MathOp)5 SingleStatement (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.SingleStatement)5 Statement (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Statement)5 ArrayList (java.util.ArrayList)4 BracketExpr (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.BracketExpr)4 OpenBracketToken (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.OpenBracketToken)3 UnaryPrefix (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.UnaryPrefix)3 UnionToken (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.UnionToken)3 AndOp (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.AndOp)2 Atom (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Atom)2 CloseBracketToken (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.CloseBracketToken)2 Comment (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Comment)2 ListExpr (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.ListExpr)2 Name (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Name)2 OuterJoinToken (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.OuterJoinToken)2 Part (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Part)2 PartToken (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.PartToken)2 Raw (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Raw)2 Unparsed (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Unparsed)2