forked from jasder/antlr
change the example to use the new stream interface
This commit is contained in:
parent
c3ed9a992d
commit
1fcb5951c6
|
@ -15,7 +15,7 @@ grammar IP;
|
|||
|
||||
file : ip+ (MARKER ip)* ;
|
||||
|
||||
ip : BYTE '.' BYTE '.' BYTE '.' BYTE ;
|
||||
ip : BYTE BYTE BYTE BYTE ;
|
||||
|
||||
MARKER : '\u00CA' '\u00FE' ;
|
||||
BYTE : '\u0000'..'\u00FF' ;
|
||||
|
@ -57,7 +57,8 @@ The `ISO-8859-1` encoding is just the 8-bit char encoding for LATIN-1, which eff
|
|||
|
||||
|
||||
```java
|
||||
ANTLRFileStream bytesAsChar = new ANTLRFileStream("resources/ips", "ISO-8859-1");
|
||||
//ANTLRFileStream bytesAsChar = new ANTLRFileStream("resources/ips", "ISO-8859-1"); DEPRECATED in 4.7
|
||||
CharStream bytesAsChar = CharStreams.fromFileName("/tmp/ips", StandardCharsets.ISO_8859_1);
|
||||
IPLexer lexer = new IPLexer(bytesAsChar);
|
||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||
IPParser parser = new IPParser(tokens);
|
||||
|
@ -93,6 +94,8 @@ We can't just print out the text because we are not reading in text. We need to
|
|||
|
||||
## Custom stream
|
||||
|
||||
(*ANTLRFileStream was deprecated in 4.7*)
|
||||
|
||||
If you want to play around with the stream, you can. Here's an example that alters how "text" is computed from the byte stream (which changes how tokens print out their text as well):
|
||||
|
||||
```java
|
||||
|
@ -155,9 +158,9 @@ Error handling proceeds exactly like any other parser. For example, let's alter
|
|||
|
||||
```java
|
||||
public static final byte[] bytes = {
|
||||
(byte)172, '.', 0, '.', '.', 1, (byte)0xCA, (byte)0xFE, // OOOPS
|
||||
(byte)10, '.', 10, '.', 10, '.', 1, (byte)0xCA, (byte)0xFE,
|
||||
(byte)10, '.', 10, '.', 10, '.', 99
|
||||
(byte)172, 0, 1, (byte)0xCA, (byte)0xFE, // OOOPS
|
||||
(byte)10, 10, 10, 1, (byte)0xCA, (byte)0xFE,
|
||||
(byte)10, 10, 10, 99
|
||||
};
|
||||
```
|
||||
|
||||
|
|
Loading…
Reference in New Issue