Java Code Examples for org.antlr.v4.runtime.TokenStream#getText()

The following examples show how to use org.antlr.v4.runtime.TokenStream#getText() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KsqlParserErrorStrategy.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 6 votes vote down vote up
protected void reportNoViableAlternative(Parser recognizer, NoViableAltException e) {
  TokenStream tokens = recognizer.getInputStream();
  String input;
  if (tokens != null) {
    if (e.getStartToken().getType() == -1) {
      input = "<EOF>";
    } else {
      input = tokens.getText(e.getStartToken(), e.getOffendingToken());
    }
  } else {
    input = "<unknown input>";
  }

  String msg = "no viable alternative at input " + this.escapeWSAndQuote(input);
  recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
}
 
Example 2
Source File: SwiftSupport.java    From swift-js-transpiler with MIT License 6 votes vote down vote up
/**
 "If an operator has whitespace on the right side only, it is treated as a
 postfix unary operator. As an example, the ++ operator in a++ b is treated
 as a postfix unary operator."
 "If an operator has no whitespace on the left but is followed immediately
 by a dot (.), it is treated as a postfix unary operator. As an example,
 the ++ operator in a++.b is treated as a postfix unary operator (a++ .b
 rather than a ++ .b)."
 */
public static boolean isPostfixOp(TokenStream tokens) {
	int stop = getLastOpTokenIndex(tokens);
	if ( stop==-1 ) return false;

	int start = tokens.index();
	Token prevToken = tokens.get(start-1); // includes hidden-channel tokens
	Token nextToken = tokens.get(stop+1);
	boolean prevIsWS = isLeftOperatorWS(prevToken);
	boolean nextIsWS = isRightOperatorWS(nextToken);
	boolean result =
		!prevIsWS && nextIsWS ||
		!prevIsWS && nextToken.getType()==SwiftParser.DOT;
	String text = tokens.getText(Interval.of(start, stop));
	//System.out.println("isPostfixOp: '"+prevToken+"','"+text+"','"+nextToken+"' is "+result);
	return result;
}
 
Example 3
Source File: SwiftSupport.java    From swift-js-transpiler with MIT License 5 votes vote down vote up
/**
 "If an operator has whitespace around both sides or around neither side,
 it is treated as a binary operator. As an example, the + operator in a+b
 and a + b is treated as a binary operator."
 */
public static boolean isBinaryOp(TokenStream tokens) {
	int stop = getLastOpTokenIndex(tokens);
	if ( stop==-1 ) return false;

	int start = tokens.index();
	Token prevToken = tokens.get(start-1); // includes hidden-channel tokens
	Token nextToken = tokens.get(stop+1);
	boolean prevIsWS = isLeftOperatorWS(prevToken);
	boolean nextIsWS = isRightOperatorWS(nextToken);
	boolean result = prevIsWS && nextIsWS || (!prevIsWS && !nextIsWS);
	String text = tokens.getText(Interval.of(start, stop));
	//System.out.println("isBinaryOp: '"+prevToken+"','"+text+"','"+nextToken+"' is "+result);
	return result;
}
 
Example 4
Source File: SwiftSupport.java    From swift-js-transpiler with MIT License 5 votes vote down vote up
/**
 "If an operator has whitespace on the left side only, it is treated as a
 prefix unary operator. As an example, the ++ operator in a ++b is treated
 as a prefix unary operator."
*/
public static boolean isPrefixOp(TokenStream tokens) {
	int stop = getLastOpTokenIndex(tokens);
	if ( stop==-1 ) return false;

	int start = tokens.index();
	Token prevToken = tokens.get(start-1); // includes hidden-channel tokens
	Token nextToken = tokens.get(stop+1);
	boolean prevIsWS = isLeftOperatorWS(prevToken);
	boolean nextIsWS = isRightOperatorWS(nextToken);
	boolean result = prevIsWS && !nextIsWS;
	String text = tokens.getText(Interval.of(start, stop));
	//System.out.println("isPrefixOp: '"+prevToken+"','"+text+"','"+nextToken+"' is "+result);
	return result;
}
 
Example 5
Source File: SwiftSupport.java    From swift-js-transpiler with MIT License 5 votes vote down vote up
public static boolean isOperator(TokenStream tokens, String op) {
	int stop = getLastOpTokenIndex(tokens);
	if ( stop==-1 ) return false;

	int start = tokens.index();
	String text = tokens.getText(Interval.of(start, stop));
	return text.equals(op);
}
 
Example 6
Source File: BeetlAntlrErrorStrategy.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void reportNoViableAlternative(@NotNull Parser recognizer, @NotNull NoViableAltException e)
{
	TokenStream tokens = recognizer.getInputStream();
	String input;
	if (tokens instanceof TokenStream)
	{
		if (e.getStartToken().getType() == Token.EOF)
			input = "<文件尾>";
		else
			input = tokens.getText(e.getStartToken(), e.getOffendingToken());
	}
	else
	{
		input = "<未知输入>";
	}
	BeetlException exception = null;
	if(keys.contains(e.getOffendingToken().getText())){
		 exception = new BeetlParserException(BeetlException.PARSER_VIABLE_ERROR,
					"不允许"+e.getOffendingToken().getText()+"关键出现在这里"+":"+escapeWSAndQuote(input), e);
	}else{
		exception = new BeetlParserException(BeetlException.PARSER_VIABLE_ERROR,
				escapeWSAndQuote(input), e);
	}
	//		String msg = "no viable alternative at input " + escapeWSAndQuote(input);

	exception.pushToken(this.getGrammarToken(e.getOffendingToken()));

	throw exception;
}