Search in sources :

Example 1 with ElementBlock

use of org.antlr.works.grammar.element.ElementBlock in project antlrworks by antlr.

the class TestParser method testSyntaxBlock.

public void testSyntaxBlock() throws Exception {
    parseFile(TestConstants.BLOCKS);
    assertInspector(0);
    assertEquals("grammar name", "demo", getEngine().getGrammarName());
    ElementBlock tokensBlock = getEngine().getBlocks().get(0);
    assertEquals("tokens block", Arrays.asList("FOO", "OTHER", "LAST"), tokensBlock.getDeclaredTokensAsString());
    ElementBlock optionsBlock = getEngine().getBlocks().get(1);
    assertEquals("tokenVocab", "DataViewExpressions", optionsBlock.getTokenVocab());
}
Also used : ElementBlock(org.antlr.works.grammar.element.ElementBlock)

Example 2 with ElementBlock

use of org.antlr.works.grammar.element.ElementBlock in project antlrworks by antlr.

the class DBLocal method getCustomImports.

/**
     * Returns a string of import statement based on the package declaration inside any @header block
     */
private String getCustomImports() {
    List<ElementBlock> blocks = debuggerTab.getBlocks();
    if (blocks == null || blocks.isEmpty()) {
        return "";
    }
    Set<String> imports = new HashSet<String>();
    for (ElementBlock block : blocks) {
        if (!block.name.equals(GrammarSyntaxParser.PARSER_HEADER_BLOCK_NAME) && !block.name.equals(GrammarSyntaxParser.LEXER_HEADER_BLOCK_NAME)) {
            continue;
        }
        List<ATEToken> tokens = block.internalTokens;
        for (int j = 0; j < tokens.size(); j++) {
            ATEToken token = tokens.get(j);
            if (token.type == ATESyntaxLexer.TOKEN_ID && token.getAttribute().equals("package")) {
                StringBuilder sb = new StringBuilder();
                j++;
                while (j < tokens.size()) {
                    ATEToken t = tokens.get(j);
                    String at = t.getAttribute();
                    if (at.equals(";"))
                        break;
                    sb.append(at);
                    j++;
                }
                imports.add(sb.toString());
            }
        }
    }
    if (imports.isEmpty()) {
        return "";
    }
    StringBuilder importLines = new StringBuilder();
    for (String importName : imports) {
        importLines.append("import ");
        importLines.append(importName);
        importLines.append(".*;\n");
    }
    return importLines.toString();
}
Also used : ElementBlock(org.antlr.works.grammar.element.ElementBlock) ATEToken(org.antlr.works.ate.syntax.misc.ATEToken) HashSet(java.util.HashSet)

Aggregations

ElementBlock (org.antlr.works.grammar.element.ElementBlock)2 HashSet (java.util.HashSet)1 ATEToken (org.antlr.works.ate.syntax.misc.ATEToken)1