private static Set<TokenType> getKeywordsValidAfter(final TokenType lastKeyword,
                                                        final List<TokenType> lastKeywordSequence) {
        final Set<TokenType> keywordsValidAfter;
        if (lastKeyword == null) {
            // FROM must be first keyword
            keywordsValidAfter = Set.of(TokenType.FROM);
        } else if (lastKeyword == TokenType.GROUP || lastKeyword == TokenType.SORT) {
            if (lastKeywordSequence.contains(TokenType.BY)) {
                keywordsValidAfter = TokenType.getKeywordsValidAfter(lastKeyword);
            } else {
                keywordsValidAfter = Set.of(TokenType.BY);
            }
        } else {
            keywordsValidAfter = TokenType.getKeywordsValidAfter(lastKeyword);
//            if (lastKeywordSequence.size() <= 3) {
//                // e.g. 'select xxx'
//                keywordsValidAfter = Collections.emptySet();
//            } else {
//                keywordsValidAfter = TokenType.getKeywordsValidAfter(lastKeyword);
//            }
        }
        return keywordsValidAfter;
    }
Here to help testing, so we don't have to work out the row/col postitions
stroom-query/stroom-query-impl/src/main/java/stroom/query/impl/QueryServiceImpl.java