本文整理汇总了Java中org.antlr.v4.runtime.Parser.getTokenStream方法的典型用法代码示例。如果您正苦于以下问题:Java Parser.getTokenStream方法的具体用法?Java Parser.getTokenStream怎么用?Java Parser.getTokenStream使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.antlr.v4.runtime.Parser
的用法示例。
在下文中一共展示了Parser.getTokenStream方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: actionPerformed
import org.antlr.v4.runtime.Parser; //导入方法依赖的package包/类
@Override
public void actionPerformed(AnActionEvent e) {
LOG.info("actionPerformed GenerateLexerRulesForLiteralsAction");
final Project project = e.getProject();
final PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
if (psiFile == null) {
return;
}
String inputText = psiFile.getText();
ParsingResult results = ParsingUtils.parseANTLRGrammar(inputText);
final Parser parser = results.parser;
final ParseTree tree = results.tree;
Collection<ParseTree> literalNodes = XPath.findAll(tree, "//ruleBlock//STRING_LITERAL", parser);
LinkedHashMap<String, String> lexerRules = new LinkedHashMap<String, String>();
for (ParseTree node : literalNodes) {
String literal = node.getText();
String ruleText = String.format("%s : %s ;",
RefactorUtils.getLexerRuleNameFromLiteral(literal), literal);
lexerRules.put(literal, ruleText);
}
// remove those already defined
String lexerRulesXPath = "//lexerRule";
String treePattern = "<TOKEN_REF> : <STRING_LITERAL>;";
ParseTreePattern p = parser.compileParseTreePattern(treePattern, ANTLRv4Parser.RULE_lexerRule);
List<ParseTreeMatch> matches = p.findAll(tree, lexerRulesXPath);
for (ParseTreeMatch match : matches) {
ParseTree lit = match.get("STRING_LITERAL");
if (lexerRules.containsKey(lit.getText())) { // we have rule for this literal already
lexerRules.remove(lit.getText());
}
}
final LiteralChooser chooser =
new LiteralChooser(project, new ArrayList<String>(lexerRules.values()));
chooser.show();
List<String> selectedElements = chooser.getSelectedElements();
// chooser disposed automatically.
final Editor editor = e.getData(PlatformDataKeys.EDITOR);
final Document doc = editor.getDocument();
final CommonTokenStream tokens = (CommonTokenStream) parser.getTokenStream();
// System.out.println(selectedElements);
if (selectedElements != null) {
String text = doc.getText();
int cursorOffset = editor.getCaretModel().getOffset();
// make sure it's not in middle of rule; put between.
// System.out.println("offset "+cursorOffset);
Collection<ParseTree> allRuleNodes = XPath.findAll(tree, "//ruleSpec", parser);
for (ParseTree r : allRuleNodes) {
Interval extent = r.getSourceInterval(); // token indexes
int start = tokens.get(extent.a).getStartIndex();
int stop = tokens.get(extent.b).getStopIndex();
// System.out.println("rule "+r.getChild(0).getText()+": "+start+".."+stop);
if (cursorOffset < start) {
// before this rule, so must be between previous and this one
cursorOffset = start; // put right before this rule
break;
}
else if (cursorOffset >= start && cursorOffset <= stop) {
// cursor in this rule
cursorOffset = stop + 2; // put right before this rule (after newline)
if (cursorOffset >= text.length()) {
cursorOffset = text.length();
}
break;
}
}
String allRules = Utils.join(selectedElements.iterator(), "\n");
text =
text.substring(0, cursorOffset) +
"\n" + allRules + "\n" +
text.substring(cursorOffset, text.length());
MyPsiUtils.replacePsiFileFromText(project, psiFile, text);
}
}
示例2: actionPerformed
import org.antlr.v4.runtime.Parser; //导入方法依赖的package包/类
@Override
public void actionPerformed(AnActionEvent e) {
PsiElement el = MyActionUtils.getSelectedPsiElement(e);
if ( el==null ) return;
final PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
if (psiFile == null) return;
Editor editor = e.getData(PlatformDataKeys.EDITOR);
if ( editor==null ) return;
final Document doc = editor.getDocument();
SelectionModel selectionModel = editor.getSelectionModel();
String grammarText = psiFile.getText();
ParsingResult results = ParsingUtils.parseANTLRGrammar(grammarText);
final Parser parser = results.parser;
final ParserRuleContext tree = (ParserRuleContext)results.tree;
TokenStream tokens = parser.getTokenStream();
int selStart = selectionModel.getSelectionStart();
int selStop = selectionModel.getSelectionEnd()-1; // I'm inclusive and they are exclusive for end offset
// find appropriate tokens for bounds, don't include WS
Token start = RefactorUtils.getTokenForCharIndex(tokens, selStart);
Token stop = RefactorUtils.getTokenForCharIndex(tokens, selStop);
if ( start==null || stop==null ) {
return;
}
if ( start.getType()==ANTLRv4Lexer.WS ) {
start = tokens.get(start.getTokenIndex()+1);
}
if ( stop.getType()==ANTLRv4Lexer.WS ) {
stop = tokens.get(stop.getTokenIndex()-1);
}
selectionModel.setSelection(start.getStartIndex(), stop.getStopIndex() + 1);
final Project project = e.getProject();
final ChooseExtractedRuleName nameChooser = new ChooseExtractedRuleName(project);
nameChooser.show();
if ( nameChooser.ruleName==null ) return;
// make new rule string
final String ruleText = selectionModel.getSelectedText();
final int insertionPoint = RefactorUtils.getCharIndexOfNextRuleStart(tree, start.getTokenIndex());
final String newRule = "\n"+nameChooser.ruleName + " : " + ruleText + " ;" + "\n";
final Token start_ = start;
final Token stop_ = stop;
WriteCommandAction setTextAction = new WriteCommandAction(project) {
@Override
protected void run(final Result result) throws Throwable {
// do all as one operation.
if ( insertionPoint>=doc.getTextLength() ) {
doc.insertString(doc.getTextLength(), newRule);
}
else {
doc.insertString(insertionPoint, newRule);
}
doc.replaceString(start_.getStartIndex(), stop_.getStopIndex()+1, nameChooser.ruleName);
}
};
setTextAction.execute();
// TODO: only allow selection of fully-formed syntactic entity.
// E.g., "A (',' A" is invalid grammatically as a rule.
}
示例3: actionPerformed
import org.antlr.v4.runtime.Parser; //导入方法依赖的package包/类
@Override
public void actionPerformed(AnActionEvent e) {
PsiElement el = MyActionUtils.getSelectedPsiElement(e);
if ( el==null ) return;
final String ruleName = el.getText();
final PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
if ( psiFile==null ) return;
final Project project = e.getProject();
Editor editor = e.getData(PlatformDataKeys.EDITOR);
if ( editor==null ) return;
final Document doc = editor.getDocument();
String grammarText = psiFile.getText();
ParsingResult results = ParsingUtils.parseANTLRGrammar(grammarText);
Parser parser = results.parser;
ParseTree tree = results.tree;
final CommonTokenStream tokens = (CommonTokenStream) parser.getTokenStream();
// find all parser and lexer rule refs
final List<TerminalNode> rrefNodes = RefactorUtils.getAllRuleRefNodes(parser, tree, ruleName);
if ( rrefNodes==null ) return;
// find rule def
ParseTree ruleDefNameNode = RefactorUtils.getRuleDefNameNode(parser, tree, ruleName);
if ( ruleDefNameNode==null ) return;
// identify rhs of rule
final ParserRuleContext ruleDefNode = (ParserRuleContext) ruleDefNameNode.getParent();
String ruleText_ = RefactorUtils.getRuleText(tokens, ruleDefNode);
// if rule has outermost alt, must add (...) around insertion
// Look for ruleBlock, lexerRuleBlock
if ( RefactorUtils.ruleHasMultipleOutermostAlts(parser, ruleDefNode) ) {
ruleText_ = "("+ruleText_+")";
}
final String ruleText = ruleText_; // we ref from inner class; requires final
// replace rule refs with rule text
WriteCommandAction setTextAction = new WriteCommandAction(project) {
@Override
protected void run(final Result result) throws Throwable {
// do in a single action so undo works in one go
replaceRuleRefs(doc,tokens,ruleName,rrefNodes,ruleText);
}
};
setTextAction.execute();
}