Java Code Examples for org.antlr.v4.runtime.Parser#getTokenStream()

The following examples show how to use org.antlr.v4.runtime.Parser#getTokenStream() . 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: ErrorHandler.java    From presto with Apache License 2.0 5 votes vote down vote up
public Analyzer(
        Parser parser,
        Map<Integer, String> specialRules,
        Map<Integer, String> specialTokens,
        Set<Integer> ignoredRules)
{
    this.parser = parser;
    this.stream = parser.getTokenStream();
    this.atn = parser.getATN();
    this.vocabulary = parser.getVocabulary();
    this.specialRules = specialRules;
    this.specialTokens = specialTokens;
    this.ignoredRules = ignoredRules;
}
 
Example 2
Source File: GenerateLexerRulesForLiteralsAction.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@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);
		}
	}
 
Example 3
Source File: ExtractRuleAction.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void extractSelection(@NotNull PsiFile psiFile, Editor editor, SelectionModel selectionModel) {
	Document doc = editor.getDocument();
	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 = psiFile.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";

	runWriteCommandAction(project, () -> {
		// do all as one operation.
		if ( insertionPoint >= doc.getTextLength() ) {
			doc.insertString(doc.getTextLength(), newRule);
		} else {
			doc.insertString(insertionPoint, newRule);
		}
		doc.replaceString(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd(), nameChooser.ruleName);
	});

	// TODO: only allow selection of fully-formed syntactic entity.
	// E.g., "A (',' A" is invalid grammatically as a rule.
}
 
Example 4
Source File: InlineRuleAction.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@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();
}