org.antlr.v4.runtime.tree.xpath.XPath Java Examples

The following examples show how to use org.antlr.v4.runtime.tree.xpath.XPath. 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: Translator.java    From bookish with MIT License 6 votes vote down vote up
/** Find all x={...} attributes and translate those from bookish to appropriate
	 *  target output format.  Replace existing attribute value with translation.
	 *
	 *  Side effect: alters xml attribute map annotation of attrs rule nodes.
	 */
	public void translateXMLAttributes(DocInfo docInfo) {
		Collection<ParseTree> attrsNodes = XPath.findAll(docInfo.tree, "//attrs", docInfo.parser);
		for (ParseTree attrsNode : attrsNodes) {
			for (int i = 0; i<attrsNode.getChildCount(); i++) {
				ParseTree assignment = attrsNode.getChild(i);
				ParseTree key = assignment.getChild(0);
				ParseTree value = assignment.getChild(2);
				if ( key!=null && value!=null ) {
					String v = value.getText();
					if ( v.charAt(0)=='{' ) {
						v = stripQuotes(v);
						BookishParser.AttrsContext a = (BookishParser.AttrsContext) attrsNode;
						String location = docInfo.getSourceName()+" "+a.start.getLine()+":"+a.start.getCharPositionInLine();
						v = tool.translateString(docInfo, v, location);
						a.attributes.put(key.getText(), v);
//						System.out.println("ALTER "+key.getText()+" from "+value.getText()+" ->" + v);
					}
				}
			}
		}
	}
 
Example #2
Source File: RefactorUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static TerminalNode getRuleDefNameNode(Parser parser, ParseTree tree, String ruleName) {
	Collection<ParseTree> ruleDefRuleNodes;
	if ( Grammar.isTokenName(ruleName) ) {
		ruleDefRuleNodes = XPath.findAll(tree, "//lexerRule/TOKEN_REF", parser);
	}
	else {
		ruleDefRuleNodes = XPath.findAll(tree, "//parserRuleSpec/RULE_REF", parser);
	}
	for (ParseTree node : ruleDefRuleNodes) {
		String r = node.getText(); // always a TerminalNode; just get rule name of this def
		if ( r.equals(ruleName) ) {
			return (TerminalNode)node;
		}
	}
	return null;
}
 
Example #3
Source File: RefactorUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static List<TerminalNode> getAllRuleRefNodes(Parser parser, ParseTree tree, String ruleName) {
	List<TerminalNode> nodes = new ArrayList<TerminalNode>();
	Collection<ParseTree> ruleRefs;
	if ( Grammar.isTokenName(ruleName) ) {
		ruleRefs = XPath.findAll(tree, "//lexerRuleBlock//TOKEN_REF", parser);
	}
	else {
		ruleRefs = XPath.findAll(tree, "//ruleBlock//RULE_REF", parser);
	}
	for (ParseTree node : ruleRefs) {
		TerminalNode terminal = (TerminalNode)node;
		Token rrefToken = terminal.getSymbol();
		String r = rrefToken.getText();
		if ( r.equals(ruleName) ) {
			nodes.add(terminal);
		}
	}
	if ( nodes.size()==0 ) return null;
	return nodes;
}
 
Example #4
Source File: Tool.java    From bookish with MIT License 5 votes vote down vote up
/** check all references to entities in all files
 * track list of entity refs for each paragraph
 */
public void verifyAllEntityRefs(Artifact artifact) {
	for (ChapDocInfo doc : artifact.docs) {
		Collection<ParseTree> refNodes =
			XPath.findAll(doc.tree, "//REF", doc.parser);
		for (ParseTree t : refNodes) {
			String label = stripQuotes(t.getText());
			EntityDef d = doc.getEntity(label);
			if ( d==null ) {
				Token tok = ((TerminalNode) t).getSymbol();
				System.err.printf("%s line %d:%d unknown label '%s'\n",
				                  doc.getSourceName(),
				                  tok.getLine(),
				                  tok.getCharPositionInLine(),
				                  label);
				continue;
			}
			d.refCount++;
			List<? extends Tree> ancestors = Trees.getAncestors(t);
			Tree p = ParrtCollections.findLast(ancestors, (Tree a) -> a instanceof BookishParser.ParagraphContext);
			// track all side items ref'd within this paragraph
			if ( d.refCount==1 && p!=null && d.isSideItem() ) {
				// for first ref, annotate paragraph if this ref is inside a paragraph
				((BookishParser.ParagraphContext)p).entitiesRefd.add(d);
			}
		}
	}
}
 
Example #5
Source File: Tool.java    From bookish with MIT License 5 votes vote down vote up
public List<String> collectIncludeFilenames(RootDocInfo doc) {
	List<String> includes = new ArrayList<>();
	Collection<ParseTree> includeNodes = XPath.findAll(doc.tree, "//include", doc.parser);
	for (ParseTree includeNode : includeNodes) {
		includes.add(getAttr(includeNode, "file"));
	}
	return includes;
}
 
Example #6
Source File: Tool.java    From bookish with MIT License 5 votes vote down vote up
/** For each attrs node in tree, set attributes field to dictionary with
 *  all x=y attribute pairs.
 *
 *  Verify that attribute names are in the list of valid attributes for
 *  that invocation of attrs rule.
 */
public static void verifyAndGatherXMLAttributes(Parser parser, ParserRuleContext tree) {
	Collection<ParseTree> attrsNodes = XPath.findAll(tree, "//attrs", parser);
	for (ParseTree attrsNode : attrsNodes) {
		BookishParser.AttrsContext ctx = (BookishParser.AttrsContext) attrsNode;
		Map<String,String> attributes = new HashMap<>();
		for (int i = 0; i<attrsNode.getChildCount(); i++) {
			ParseTree assignment = attrsNode.getChild(i);
			TerminalNode key = (TerminalNode)assignment.getChild(0);
			ParseTree value = assignment.getChild(2);
			// verify attr name is valid
			if ( !ctx.valid.contains(key.getText()) ) {
				System.err.printf("%s line %d:%d attribute name '%s' is not in valid list: %s\n",
				                  ParrtIO.basename(parser.getInputStream().getSourceName()),
				                  key.getSymbol().getLine(),
				                  key.getSymbol().getCharPositionInLine(),
				                  key.getText(),
				                  ctx.valid);
				continue; // don't set key-value pair
			}
			// set key-value pair into attributes list
			if ( value!=null ) {
				String v = value.getText();
				if ( v.charAt(0)=='"' || v.charAt(0)=='{' ) {
					v = stripQuotes(v);
				}
				attributes.put(key.getText(), v);
			}
		}
		ctx.attributes = attributes;
	}
}
 
Example #7
Source File: Translator.java    From bookish with MIT License 5 votes vote down vote up
@Override
public OutputModelObject visitParagraph_content(BookishParser.Paragraph_contentContext ctx) {
	List<OutputModelObject> elements = new ArrayList<>();
	for (ParseTree el : ctx.children) {
		OutputModelObject c = visit(el);
		if ( c!=null ) {
			elements.add(c);
		}
	}
	// find all REFs within paragraph
	Collection<ParseTree> refNodes =
		XPath.findAll(ctx, "//REF", new BookishParser(null));
	List<EntityDef> entitiesRefd = new ArrayList<>();
	for (ParseTree t : refNodes) {
		String label = stripQuotes(t.getText());
		EntityDef def = document.getEntity(label);
		if ( def!=null ) {
			if ( !book.entitiesRendered.contains(def) &&
				 !document.entitiesRendered.contains(def) )
			{
				entitiesRefd.add(def); // Nobody has shown it yet
				if ( document.entitiesRendered.contains(def) ) {
					document.entitiesRendered.add(def);
				}
				if ( book.entitiesRendered.contains(def) ) {
					book.entitiesRendered.add(def);
				}
			}
		}
		else {
			System.err.printf("line %d: Unknown label '%s'\n", ctx.start.getLine(), label);
		}
	}
	return new Paragraph(elements, entitiesRefd);
}
 
Example #8
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 #9
Source File: RefactorUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static boolean ruleHasMultipleOutermostAlts(Parser parser, ParseTree ruleTree) {
	Collection<ParseTree> ors = XPath.findAll(ruleTree, "/parserRuleSpec/ruleBlock/ruleAltList/OR", parser);
	if ( ors.size()>=1 ) return true;
	ors = XPath.findAll(ruleTree, "/lexerRule/lexerRuleBlock/lexerAltList/OR", parser);
	return ors.size()>=1;
}