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());
}
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();
}
Aggregations