Search in sources :

Example 1 with RuleHead

use of org.h2.bnf.RuleHead in project jackrabbit-oak by apache.

the class BnfSyntax method getLink.

/**
 * Get the HTML link to the given token.
 *
 * @param bnf the BNF
 * @param token the token
 * @return the HTML link
 */
String getLink(Bnf bnf, String token) {
    RuleHead found = null;
    String key = Bnf.getRuleMapKey(token);
    for (int i = 0; i < token.length(); i++) {
        String test = StringUtils.toLowerEnglish(key.substring(i));
        RuleHead r = bnf.getRuleHead(test);
        if (r != null) {
            found = r;
            break;
        }
    }
    if (found == null) {
        return token;
    }
    if (found.getRule() instanceof RuleFixed) {
        found.getRule().accept(this);
        return html;
    }
    String link = found.getTopic().toLowerCase().replace(' ', '_');
    link = "#" + StringUtils.urlEncode(link);
    return "<a href=\"" + link + "\">" + token + "</a>";
}
Also used : RuleFixed(org.h2.bnf.RuleFixed) RuleHead(org.h2.bnf.RuleHead)

Example 2 with RuleHead

use of org.h2.bnf.RuleHead in project h2database by h2database.

the class Bnf method parse.

private void parse(Reader reader) throws SQLException, IOException {
    Rule functions = null;
    statements = New.arrayList();
    Csv csv = new Csv();
    csv.setLineCommentCharacter('#');
    ResultSet rs = csv.read(reader, null);
    while (rs.next()) {
        String section = rs.getString("SECTION").trim();
        if (section.startsWith("System")) {
            continue;
        }
        String topic = rs.getString("TOPIC");
        syntax = rs.getString("SYNTAX").trim();
        currentTopic = section;
        tokens = tokenize();
        index = 0;
        Rule rule = parseRule();
        if (section.startsWith("Command")) {
            rule = new RuleList(rule, new RuleElement(";\n\n", currentTopic), false);
        }
        RuleHead head = addRule(topic, section, rule);
        if (section.startsWith("Function")) {
            if (functions == null) {
                functions = rule;
            } else {
                functions = new RuleList(rule, functions, true);
            }
        } else if (section.startsWith("Commands")) {
            statements.add(head);
        }
    }
    addRule("@func@", "Function", functions);
    addFixedRule("@ymd@", RuleFixed.YMD);
    addFixedRule("@hms@", RuleFixed.HMS);
    addFixedRule("@nanos@", RuleFixed.NANOS);
    addFixedRule("anything_except_single_quote", RuleFixed.ANY_EXCEPT_SINGLE_QUOTE);
    addFixedRule("anything_except_double_quote", RuleFixed.ANY_EXCEPT_DOUBLE_QUOTE);
    addFixedRule("anything_until_end_of_line", RuleFixed.ANY_UNTIL_EOL);
    addFixedRule("anything_until_end_comment", RuleFixed.ANY_UNTIL_END);
    addFixedRule("anything_except_two_dollar_signs", RuleFixed.ANY_EXCEPT_2_DOLLAR);
    addFixedRule("anything", RuleFixed.ANY_WORD);
    addFixedRule("@hex_start@", RuleFixed.HEX_START);
    addFixedRule("@concat@", RuleFixed.CONCAT);
    addFixedRule("@az_@", RuleFixed.AZ_UNDERSCORE);
    addFixedRule("@af@", RuleFixed.AF);
    addFixedRule("@digit@", RuleFixed.DIGIT);
    addFixedRule("@open_bracket@", RuleFixed.OPEN_BRACKET);
    addFixedRule("@close_bracket@", RuleFixed.CLOSE_BRACKET);
}
Also used : Csv(org.h2.tools.Csv) ResultSet(java.sql.ResultSet) DbContextRule(org.h2.bnf.context.DbContextRule)

Example 3 with RuleHead

use of org.h2.bnf.RuleHead in project h2database by h2database.

the class BnfRandom method getRandomSQL.

public String getRandomSQL() {
    int sid = random.nextInt(statements.size());
    RuleHead r = statements.get(sid);
    level = 0;
    r.getRule().accept(this);
    sql = sql.trim();
    if (sql.length() > 0) {
        if (sql.indexOf("TRACE_LEVEL_") < 0 && sql.indexOf("COLLATION") < 0 && sql.indexOf("SCRIPT ") < 0 && sql.indexOf("CSVWRITE") < 0 && sql.indexOf("BACKUP") < 0 && sql.indexOf("DB_CLOSE_DELAY") < 0) {
            if (SHOW_SYNTAX) {
                System.out.println("  " + sql);
            }
            return sql;
        }
    }
    return null;
}
Also used : RuleHead(org.h2.bnf.RuleHead)

Example 4 with RuleHead

use of org.h2.bnf.RuleHead in project h2database by h2database.

the class BnfSyntax method getLink.

/**
 * Get the HTML link to the given token.
 *
 * @param bnf the BNF
 * @param token the token
 * @return the HTML link
 */
String getLink(Bnf bnf, String token) {
    RuleHead found = null;
    String key = Bnf.getRuleMapKey(token);
    for (int i = 0; i < token.length(); i++) {
        String test = StringUtils.toLowerEnglish(key.substring(i));
        RuleHead r = bnf.getRuleHead(test);
        if (r != null) {
            found = r;
            break;
        }
    }
    if (found == null) {
        return token;
    }
    String page = "grammar.html";
    if (found.getSection().startsWith("Data Types")) {
        page = "datatypes.html";
    } else if (found.getSection().startsWith("Functions")) {
        page = "functions.html";
    } else if (token.equals("@func@")) {
        return "<a href=\"functions.html\">Function</a>";
    } else if (found.getRule() instanceof RuleFixed) {
        found.getRule().accept(this);
        return html;
    }
    String link = found.getTopic().toLowerCase().replace(' ', '_');
    link = page + "#" + StringUtils.urlEncode(link);
    return "<a href=\"" + link + "\">" + token + "</a>";
}
Also used : RuleFixed(org.h2.bnf.RuleFixed) RuleHead(org.h2.bnf.RuleHead)

Aggregations

RuleHead (org.h2.bnf.RuleHead)3 RuleFixed (org.h2.bnf.RuleFixed)2 ResultSet (java.sql.ResultSet)1 DbContextRule (org.h2.bnf.context.DbContextRule)1 Csv (org.h2.tools.Csv)1