use of org.antlr.v4.runtime.CharStream in project antlr4 by antlr.
the class TestUnbufferedCharStream method testNestedMarkReleasedTwice.
/**
* The {@link IntStream} interface does not specify the behavior when a mark
* is released twice, but {@link UnbufferedCharStream} handles this case by
* throwing an {@link IllegalStateException}.
*/
@Test(expected = IllegalStateException.class)
public void testNestedMarkReleasedTwice() {
CharStream input = createStream("");
int m1 = input.mark();
int m2 = input.mark();
input.release(m2);
input.release(m2);
}
use of org.antlr.v4.runtime.CharStream in project antlr4 by antlr.
the class TestUnbufferedCharStream method testSeekPastEOF.
@Test
public void testSeekPastEOF() {
CharStream input = createStream("");
assertEquals(0, input.index());
input.seek(1);
assertEquals(0, input.index());
}
use of org.antlr.v4.runtime.CharStream in project antlr4 by antlr.
the class TestUnbufferedCharStream method testSeekBeforeBufferStart.
@Test(expected = IllegalArgumentException.class)
public void testSeekBeforeBufferStart() {
CharStream input = createStream("xyz");
input.consume();
int m1 = input.mark();
assertEquals(1, input.index());
input.consume();
input.seek(0);
}
use of org.antlr.v4.runtime.CharStream in project antlr4 by antlr.
the class TestUnicodeGrammar method binaryGrammar.
@Test
public void binaryGrammar() throws Exception {
String grammarText = "grammar Binary;\n" + "r : HEADER PACKET+ FOOTER;\n" + "HEADER : '\\u0002\\u0000\\u0001\\u0007';\n" + "PACKET : '\\u00D0' ('\\u00D1' | '\\u00D2' | '\\u00D3') +;\n" + "FOOTER : '\\u00FF';\n";
byte[] toParse = new byte[] { (byte) 0x02, (byte) 0x00, (byte) 0x01, (byte) 0x07, (byte) 0xD0, (byte) 0xD2, (byte) 0xD2, (byte) 0xD3, (byte) 0xD3, (byte) 0xD3, (byte) 0xD0, (byte) 0xD3, (byte) 0xD3, (byte) 0xD1, (byte) 0xFF };
CharStream charStream;
try (ByteArrayInputStream is = new ByteArrayInputStream(toParse);
// U+0000 to U+00FF.
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.ISO_8859_1)) {
charStream = new ANTLRInputStream(isr);
}
Grammar grammar = new Grammar(grammarText);
LexerInterpreter lexEngine = grammar.createLexerInterpreter(charStream);
CommonTokenStream tokens = new CommonTokenStream(lexEngine);
GrammarParserInterpreter parser = grammar.createGrammarParserInterpreter(tokens);
ParseTree parseTree = parser.parse(grammar.rules.get("r").index);
InterpreterTreeTextProvider nodeTextProvider = new InterpreterTreeTextProvider(grammar.getRuleNames());
String result = Trees.toStringTree(parseTree, nodeTextProvider);
assertEquals("(r:1 \u0002\u0000\u0001\u0007 \u00D0\u00D2\u00D2\u00D3\u00D3\u00D3 \u00D0\u00D3\u00D3\u00D1 \u00FF)", result);
}
use of org.antlr.v4.runtime.CharStream in project antlr4 by antlr.
the class Tool method parse.
public GrammarRootAST parse(String fileName, CharStream in) {
try {
GrammarASTAdaptor adaptor = new GrammarASTAdaptor(in);
ToolANTLRLexer lexer = new ToolANTLRLexer(in, this);
CommonTokenStream tokens = new CommonTokenStream(lexer);
lexer.tokens = tokens;
ToolANTLRParser p = new ToolANTLRParser(tokens, this);
p.setTreeAdaptor(adaptor);
try {
ParserRuleReturnScope r = p.grammarSpec();
GrammarAST root = (GrammarAST) r.getTree();
if (root instanceof GrammarRootAST) {
((GrammarRootAST) root).hasErrors = lexer.getNumberOfSyntaxErrors() > 0 || p.getNumberOfSyntaxErrors() > 0;
assert ((GrammarRootAST) root).tokenStream == tokens;
if (grammarOptions != null) {
((GrammarRootAST) root).cmdLineOptions = grammarOptions;
}
return ((GrammarRootAST) root);
}
} catch (v3TreeGrammarException e) {
errMgr.grammarError(ErrorType.V3_TREE_GRAMMAR, fileName, e.location);
}
return null;
} catch (RecognitionException re) {
// TODO: do we gen errors now?
ErrorManager.internalError("can't generate this message at moment; antlr recovers");
}
return null;
}
Aggregations