Files
grammars-v4/bcpl/Java/ChannelCommonTokenStream.java
Ken Domino 9fb4b99b4e [bcpl] Make working bcpl grammar. (#3436)
* Working version of bcpl grammar.

Note, this grammar is very slow, requires a non-standard test driver, and is CSharp specific.

* Clean up grammar.

Remove useless parser symbols. Use assop rule.

* Remove Antlr anti-pattern.

Remove Antlr anti-pattern.

* Removed useless parentheses.

Removed parentheses that are unnecessary.

* Split of the grammar.

* Add lexer base class.

* Changes to hide ChannelsCommonTokenStream.

* Clean up.

* Ported bcpl grammar to Java target.

* Fixed semantics check for newlines. The lookahead must be screened for either default or the channel given when asking for lookahead or lookback.
2023-06-04 21:56:23 -06:00

106 lines
2.4 KiB
Java

import org.antlr.v4.runtime.*;
public class ChannelCommonTokenStream extends CommonTokenStream
{
public ChannelCommonTokenStream(TokenSource tokenSource)
{
super(tokenSource);
}
public ChannelCommonTokenStream(TokenStream input)
{
this(((CommonTokenStream)input).getTokenSource());
}
protected int myPreviousTokenOnChannel(int i, int channel) {
sync(i);
if (i >= size()) {
// the EOF token is on every channel
return size() - 1;
}
while (i >= 0) {
Token token = tokens.get(i);
if (token.getType() == Token.EOF || token.getChannel() == channel || token.getChannel() == Lexer.DEFAULT_TOKEN_CHANNEL) {
return i;
}
i--;
}
return i;
}
protected int myNextTokenOnChannel(int i, int channel) {
sync(i);
if (i >= size()) {
return size() - 1;
}
Token token = tokens.get(i);
while ( token.getChannel() != channel && token.getChannel() != Lexer.DEFAULT_TOKEN_CHANNEL ) {
if ( token.getType()==Token.EOF ) {
return i;
}
i++;
sync(i);
token = tokens.get(i);
}
return i;
}
protected Token LB(int k, int ch)
{
if (k == 0 || (p - k) < 0)
{
return null;
}
int i = p;
int n = 1;
// find k good tokens looking backwards
while (n <= k)
{
// skip off-channel tokens
i = myPreviousTokenOnChannel(i - 1, ch);
n++;
}
if (i < 0)
{
return null;
}
return tokens.get(i);
}
public Token LT(int k, int ch)
{
//System.out.println("enter LT("+k+")");
lazyInit();
if (k == 0)
{
return null;
}
if (k < 0)
{
return LB(-k, ch);
}
int i = p;
int n = 1;
// we know tokens[p] is a good one
// find k good tokens
while (n < k)
{
// skip off-channel tokens, but make sure to not look past EOF
if (sync(i + 1))
{
i = myNextTokenOnChannel(i + 1, ch);
}
n++;
}
// if ( i>range ) range = i;
return tokens.get(i);
}
}