I use the normal whitespace separation into the hidden channel but I have one rule where I would like to include any whitespace for later processing but any example I have found requires some very strange manual coding.
Is there no easy option to read from multiple channels like the option to put the whitespace there from the beginning.
Ex. this is the WhiteSpace lexer rule
WS : ( ' '
| '\t'
| '\r'
| '\n'
) {$channel=HIDDEN;}
;
And this is my rule where I would like to include whitespace
raw : '{'? (~('{'))*;
Basically it's a catch all rule to capture any content that does not match other rules to be processed by another pattern and therefore I need the original stream.
I was hoping for a {$channel==DEFAULT || $channel==HIDDEN}
syntax example but cannot find any.
My target will be C# but I can rewrite Java examples if required.
An ANTLR lexer creates a Token object after matching a lexical rule. Each request for a token starts in Lexer. nextToken , which calls emit once it has identified a token. emit collects information from the current state of the lexer to build the token.
ANTLR can generate lexers, parsers, tree parsers, and combined lexer-parsers. Parsers can automatically generate parse trees or abstract syntax trees, which can be further processed with tree parsers. ANTLR provides a single consistent notation for specifying lexers, parsers, and tree parsers.
ANTLR 4 allows you to define lexer and parser rules in a single combined grammar file. This makes it really easy to get started. To get familiar with working with ANTLR, let's take a look at what a simple JSON grammar would look like and break it down.
AFAIK, that is not possible. However, you could extend the UnbufferedTokenStream
to change the channel
during parsing. You can't use the CommonTokenStream
since it buffers a variable amount of tokens (and there can be tokens in the buffer that are on the wrong channel!). Note that you need at least ANTLR 3.3: in previous versions the UnbufferedTokenStream
wasn't included yet.
Let's say you want to parse (and display) either lower- or upper case letters. Upper case letters are put on the HIDDEN
channel, so by deafult, only lower case letters will be parsed. However, when the parser stumbles upon a lower case "q"
, we want to change to the HIDDEN
channel. Once parsing on the HIDDEN
channel, we want the "Q"
to bring us back to the DEFAULT_CHANNEL
again.
So when parsing the source "aAbBcqCdDQeE"
, first "a"
, "b"
and "c"
are printed, then the channel is changed, then "C"
and "D"
get printed, then the channel is changed again, and finally "e"
is printed to the console.
Here's an ANTLR grammar that does this:
grammar ChannelDemo;
@parser::members {
private void handle(String letter) {
if("Q".equals(letter)) {
((ChangeableChannelTokenStream)input).setChannel(Token.DEFAULT_CHANNEL);
}
else if("q".equals(letter)) {
((ChangeableChannelTokenStream)input).setChannel(HIDDEN);
}
else {
System.out.println(letter);
}
}
}
parse
: any* EOF
;
any
: letter=(LOWER | UPPER) {handle($letter.getText());}
;
LOWER
: 'a'..'z'
;
UPPER
: 'A'..'Z' {$channel=HIDDEN;}
;
And here's the custom token stream class:
import org.antlr.runtime.*;
public class ChangeableChannelTokenStream extends UnbufferedTokenStream {
public ChangeableChannelTokenStream(TokenSource source) {
super(source);
}
public Token nextElement() {
Token t = null;
while(true) {
t = super.tokenSource.nextToken();
t.setTokenIndex(tokenIndex++);
if(t.getChannel() == super.channel) break;
}
return t;
}
public void setChannel(int ch) {
super.channel = ch;
}
}
And a small Main class to test it all:
import org.antlr.runtime.*;
public class Main {
public static void main(String[] args) throws Exception {
ANTLRStringStream in = new ANTLRStringStream("aAbBcqCdDQeE");
ChannelDemoLexer lexer = new ChannelDemoLexer(in);
ChangeableChannelTokenStream tokens = new ChangeableChannelTokenStream(lexer);
ChannelDemoParser parser = new ChannelDemoParser(tokens);
parser.parse();
}
}
Finally, generate a lexer/parser (1), compile all source files (2) and run the Main class (3):
java -cp antlr-3.3.jar org.antlr.Tool ChannelDemo.g
javac -cp antlr-3.3.jar *.java
java -cp .:antlr-3.3.jar Main
java -cp .;antlr-3.3.jar Main
which will cause the following to be printed to the console:
a b c C D e
You can include the class in your grammar file like this:
grammar ChannelDemo;
@parser::members {
private void handle(String letter) {
if("Q".equals(letter)) {
((ChangeableChannelTokenStream)input).setChannel(Token.DEFAULT_CHANNEL);
}
else if("q".equals(letter)) {
((ChangeableChannelTokenStream)input).setChannel(HIDDEN);
}
else {
System.out.println(letter);
}
}
public static class ChangeableChannelTokenStream extends UnbufferedTokenStream {
private boolean anyChannel;
public ChangeableChannelTokenStream(TokenSource source) {
super(source);
anyChannel = false;
}
@Override
public Token nextElement() {
Token t = null;
while(true) {
t = super.tokenSource.nextToken();
t.setTokenIndex(tokenIndex++);
if(t.getChannel() == super.channel || anyChannel) break;
}
return t;
}
public void setAnyChannel(boolean enable) {
anyChannel = enable;
}
public void setChannel(int ch) {
super.channel = ch;
}
}
}
parse
: any* EOF
;
any
: letter=(LOWER | UPPER) {handle($letter.getText());}
| STAR {((ChangeableChannelTokenStream)input).setAnyChannel(true);}
;
STAR
: '*'
;
LOWER
: 'a'..'z'
;
UPPER
: 'A'..'Z' {$channel=HIDDEN;}
;
The parser that gets generated from the grammar above will enable reading from all channels when it encounters a "*"
. So when parsing "aAbB*cCdDeE"
:
import org.antlr.runtime.*;
public class Main {
public static void main(String[] args) throws Exception {
ANTLRStringStream in = new ANTLRStringStream("aAbB*cCdDeE");
ChannelDemoLexer lexer = new ChannelDemoLexer(in);
ChannelDemoParser.ChangeableChannelTokenStream tokens =
new ChannelDemoParser.ChangeableChannelTokenStream(lexer);
ChannelDemoParser parser = new ChannelDemoParser(tokens);
parser.parse();
}
}
the following gets printed:
a b c C d D e E
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With