本文整理汇总了Java中org.antlr.v4.runtime.TokenStream.getText方法的典型用法代码示例。如果您正苦于以下问题:Java TokenStream.getText方法的具体用法?Java TokenStream.getText怎么用?Java TokenStream.getText使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.antlr.v4.runtime.TokenStream
的用法示例。
在下文中一共展示了TokenStream.getText方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: dot
import org.antlr.v4.runtime.TokenStream; //导入方法依赖的package包/类
/**
"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;
}
示例2: reportNoViableAlternative
import org.antlr.v4.runtime.TokenStream; //导入方法依赖的package包/类
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);
}
示例3: syntaxError
import org.antlr.v4.runtime.TokenStream; //导入方法依赖的package包/类
@Override
public final void syntaxError(final Recognizer<?, ?> recognizer,
final Object offendingSymbol, final int line,
final int charPositionInLine, final String msg,
final RecognitionException e) {
String input;
if (recognizer instanceof Lexer) {
final CharStream cs = ((Lexer) recognizer).getInputStream();
input = cs.getText(new Interval(0, cs.size()));
} else if (recognizer instanceof Parser) {
final TokenStream tokens = ((Parser) recognizer).getInputStream();
if (tokens != null) {
input = tokens.getText();
} else {
input = "<unknown input>";
}
} else {
input = "<unknown input>";
}
throw new AntlrParseException(input, line, charPositionInLine, msg);
}
示例4: isBinaryOp
import org.antlr.v4.runtime.TokenStream; //导入方法依赖的package包/类
/**
"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;
}
示例5: isPrefixOp
import org.antlr.v4.runtime.TokenStream; //导入方法依赖的package包/类
/**
"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;
}
示例6: isOperator
import org.antlr.v4.runtime.TokenStream; //导入方法依赖的package包/类
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);
}
示例7: enterMethodDeclaration
import org.antlr.v4.runtime.TokenStream; //导入方法依赖的package包/类
/** Listen to matches of methodDeclaration */
@Override
public void enterMethodDeclaration(
JavaParser.MethodDeclarationContext ctx
)
{
// need parser to get tokens
TokenStream tokens = parser.getTokenStream();
String type = "void";
if ( ctx.type()!=null ) {
type = tokens.getText(ctx.type());
}
String args = tokens.getText(ctx.formalParameters());
System.out.println("\t"+type+" "+ctx.Identifier()+args+";");
}
示例8: noViableAlt
import org.antlr.v4.runtime.TokenStream; //导入方法依赖的package包/类
public static String noViableAlt(Parser recognizer, NoViableAltException e) {
TokenStream tokens = recognizer.getInputStream();
String input = null;
if (tokens != null) {
Token startToken = e.getStartToken();
if (startToken.getType() == Token.EOF) {
input = "<EOF>";
} else {
input = tokens.getText(
startToken, e.getOffendingToken()
);
}
}
return "syntax error at input:" + input;
}
示例9: isOperator
import org.antlr.v4.runtime.TokenStream; //导入方法依赖的package包/类
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);
}
示例10: enterAliasRule
import org.antlr.v4.runtime.TokenStream; //导入方法依赖的package包/类
@Override
public void enterAliasRule(AliasRuleContext ctx) {
TokenStream tokens = parser.getTokenStream();
String newName = null;
if ( ctx.name()!=null && mapping.getDestination() != null) {
newName = tokens.getText(ctx.name());
mapping.getDestination().setName(newName);
}
}
示例11: enterCardinality
import org.antlr.v4.runtime.TokenStream; //导入方法依赖的package包/类
@Override
public void enterCardinality(CardinalityContext ctx) {
TokenStream tokens = parser.getTokenStream();
String low = null;
String high = null;
if ( ctx.INTEGER()!=null && mapping.getDestination() != null) {
low = ctx.INTEGER().getText();
mapping.getDestination().setLow(Integer.valueOf(low));
}
if ( ctx.high()!=null && mapping.getDestination() != null) {
high = tokens.getText(ctx.high());
high = (high.equals("*"))?"-1":high;
mapping.getDestination().setHigh(Integer.valueOf(high));
}
}
示例12: reportNoViableAlternative
import org.antlr.v4.runtime.TokenStream; //导入方法依赖的package包/类
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;
}
示例13: printCurrentToken
import org.antlr.v4.runtime.TokenStream; //导入方法依赖的package包/类
private void printCurrentToken(String flag, ParserRuleContext ctx) {
if (logger.isDebugEnabled()) {
TokenStream tokens = parser.getTokenStream(); // need parser to get tokens
String text = tokens.getText(ctx);
logger.debug(flag + ":" + getTokenText(ctx));
}
}
示例14: popupAmbigTreesDialog
import org.antlr.v4.runtime.TokenStream; //导入方法依赖的package包/类
public static void popupAmbigTreesDialog(PreviewState previewState, AmbiguityInfo ambigInfo) {
// pop up subtrees for ambig intrepretation
ShowAmbigTreesDialog dialog = new ShowAmbigTreesDialog();
Parser parser = previewState.parsingResult.parser;
int startRuleIndex = parser.getRuleIndex(previewState.startRuleName);
List<ParserRuleContext> ambiguousParseTrees = null;
try {
ambiguousParseTrees =
GrammarParserInterpreter.getAllPossibleParseTrees(previewState.g,
parser,
parser.getTokenStream(),
ambigInfo.decision,
ambigInfo.ambigAlts,
ambigInfo.startIndex,
ambigInfo.stopIndex,
startRuleIndex);
} catch (ParseCancellationException pce) {
// should be no errors for ambiguities, unless original
// input itself has errors. Just display error in this case.
JBPanel errPanel = new JBPanel(new BorderLayout());
errPanel.add(new JBLabel("Cannot display ambiguous trees while there are syntax errors in your input."));
dialog.treeScrollPane.setViewportView(errPanel);
}
if ( ambiguousParseTrees!=null ) {
TokenStream tokens = previewState.parsingResult.parser.getInputStream();
String phrase = tokens.getText(Interval.of(ambigInfo.startIndex, ambigInfo.stopIndex));
if ( phrase.length()>MAX_PHRASE_WIDTH ) {
phrase = phrase.substring(0, MAX_PHRASE_WIDTH)+"...";
}
String title = ambiguousParseTrees.size()+
" Interpretations of Ambiguous Input Phrase: "+
phrase;
dialog.ambigPhraseLabel.setText(title);
dialog.setTrees(previewState, ambiguousParseTrees, title, 0,
ambigInfo.startIndex, ambigInfo.stopIndex, true);
}
dialog.pack();
dialog.setVisible(true);
}
示例15: getTokenText
import org.antlr.v4.runtime.TokenStream; //导入方法依赖的package包/类
private String getTokenText(ParserRuleContext ctx) {
TokenStream tokens = parser.getTokenStream(); // need parser to get tokens
return tokens.getText(ctx);
}