Examples of BnfRule


Examples of org.intellij.grammar.psi.BnfRule

    return psiElement instanceof BnfRule;
  }

  @Override
  public void inlineElement(Project project, Editor editor, PsiElement psiElement) {
    BnfRule rule = (BnfRule)psiElement;
    BnfAttrs attrs = rule.getAttrs();
    if (PsiTreeUtil.hasErrorElements(rule)) {
      CommonRefactoringUtil.showErrorHint(project, editor, "Rule has errors", "Inline Rule", null);
      return;
    }

    if (attrs != null && !attrs.getAttrList().isEmpty()) {
      CommonRefactoringUtil.showErrorHint(project, editor, "Rule has attributes", "Inline Rule", null);
      return;
    }

    Collection<PsiReference> allReferences = ReferencesSearch.search(psiElement).findAll();
    if (allReferences.isEmpty()) {
      CommonRefactoringUtil.showErrorHint(project, editor, "Rule is never used", "Inline Rule", null);
      return;
    }

    boolean hasNonAttributeRefs = false;
    for (PsiReference ref : allReferences) {
      if (!GrammarUtil.isInAttributesReference(ref.getElement())) {
        hasNonAttributeRefs = true;
        break;
      }
    }
    if (!hasNonAttributeRefs) {
      CommonRefactoringUtil.showErrorHint(project, editor, "Rule is referenced only in attributes", "Inline Rule", null);
      return;
    }
    if (!CommonRefactoringUtil.checkReadOnlyStatus(project, rule)) return;
    PsiReference reference = editor != null ? TargetElementUtilBase.findReference(editor, editor.getCaretModel().getOffset()) : null;
    if (reference != null && !rule.equals(reference.resolve())) {
      reference = null;
    }

    InlineRuleDialog dialog = new InlineRuleDialog(project, rule, reference);
    dialog.show();
View Full Code Here

Examples of org.intellij.grammar.psi.BnfRule

    final String name = location.method().name();
    int lineNumber = location.lineNumber() - 1;

    for (PsiFile file : myGrammars.get(qname)) {
      BnfExpression expression = findExpression(file, name);
      BnfRule rule = PsiTreeUtil.getParentOfType(expression, BnfRule.class);
      if (expression != null && qname.equals(ParserGeneratorUtil.getAttribute(rule, KnownAttribute.PARSER_CLASS))) {
        for (BnfExpression expr : ParserGeneratorUtil.getChildExpressions(expression)) {
          int line = getLineNumber(expr, qname, lineNumber);
          if (line == lineNumber) {
            return SourcePosition.createFromElement(expr);
View Full Code Here

Examples of org.intellij.grammar.psi.BnfRule

    final Ref<BnfExpression> result = Ref.create(null);
    file.acceptChildren(new PsiRecursiveElementWalkingVisitor() {
      @Override
      public void visitElement(PsiElement element) {
        if (element instanceof BnfRule) {
          BnfRule rule = (BnfRule) element;
          String ruleName = rule.getName();
          if (name.startsWith(ruleName)) {
            if (name.equals(ruleName)) {
              result.set(((BnfRule)element).getExpression());
              stopWalking();
            }
View Full Code Here

Examples of org.intellij.grammar.psi.BnfRule

  private int getLineNumber(PsiElement element, String parserClass, int currentLine) {
    int line = 0;
    AccessToken token = ReadAction.start();
    try {
      BnfRule rule = PsiTreeUtil.getParentOfType(element, BnfRule.class);
      PsiClass aClass = JavaPsiFacade.getInstance(myProcess.getProject()).findClass(parserClass, myProcess.getSearchScope());
      Document document = aClass != null? PsiDocumentManager.getInstance(myProcess.getProject()).getDocument(aClass.getContainingFile()) : null;
      if (rule != null && document != null) {
        return getLineNumber(aClass, document, currentLine, rule, element);
      }
View Full Code Here

Examples of org.intellij.grammar.psi.BnfRule

  @NotNull
  private String getParserClass(SourcePosition classPosition) throws NoDataException {
    AccessToken token = ReadAction.start();
    try {
      BnfRule rule = getRuleAt(classPosition);
      String parserClass = ParserGeneratorUtil.getAttribute(rule, KnownAttribute.PARSER_CLASS);
      if (StringUtil.isEmpty(parserClass)) throw new NoDataException();
      return parserClass;
    }
    finally {
View Full Code Here

Examples of org.intellij.grammar.psi.BnfRule

  @NotNull
  private BnfRule getRuleAt(SourcePosition position) throws NoDataException {
    PsiFile file = position.getFile();
    if (!(file instanceof BnfFileImpl)) throw new NoDataException();
    BnfRule rule = PsiTreeUtil.getParentOfType(position.getElementAt(), BnfRule.class);
    if (rule == null) throw new NoDataException();
    return rule;
  }
View Full Code Here

Examples of org.intellij.grammar.psi.BnfRule

  }

  @Nullable
  public String generateDoc(final PsiElement element, final PsiElement originalElement) {
    if (element instanceof BnfRule) {
      final BnfRule rule = (BnfRule)element;
      BnfFirstNextAnalyzer analyzer = new BnfFirstNextAnalyzer();
      Set<String> first = analyzer.asStrings(analyzer.calcFirst(rule));
      Set<String> next = analyzer.asStrings(analyzer.calcNext(rule).keySet());

      StringBuilder docBuilder = new StringBuilder();
      String[] firstS = first.toArray(new String[first.size()]);
      Arrays.sort(firstS);
      docBuilder.append("<h1>Starts with:</h1>");
      docBuilder.append("<code>").append(StringUtil.escapeXml(StringUtil.join(firstS, " | "))).append("</code>");

      String[] nextS = next.toArray(new String[next.size()]);
      Arrays.sort(nextS);
      docBuilder.append("<br><h1>Followed by:</h1>");
      docBuilder.append("<code>").append(StringUtil.escapeXml(StringUtil.join(nextS, " | "))).append("</code>");

      BnfFile file = (BnfFile)rule.getContainingFile();
      String recover = file.findAttributeValue(rule, KnownAttribute.RECOVER_WHILE, null);
      if (BnfConstants.RECOVER_AUTO.equals(recover)) {
        docBuilder.append("<br><h1>#auto recovery predicate:</h1>");
        docBuilder.append("<code>");
        docBuilder.append("private ").append(rule.getName()).append("_recover ::= !(");
        boolean f = true;
        for (String s : nextS) {
          if (s.startsWith("-") || s.startsWith("<")) continue;
          if (file.getRule(s) != null) continue;
          if (f) f = false;
View Full Code Here

Examples of org.intellij.grammar.psi.BnfRule

      }
      return ((BnfRule)psiElement).getId().getText();
    }
    else if (psiElement instanceof BnfAttr) {
      if (location == UsageViewTypeLocation.INSTANCE) {
        BnfRule rule = PsiTreeUtil.getParentOfType(psiElement, BnfRule.class);
        return (rule == null ? "Grammar " : "Rule ") + "Attribute";
      }
      return ((BnfAttr)psiElement).getId().getText();
    }
    else if (psiElement instanceof BnfCompositeElement) {
View Full Code Here

Examples of org.intellij.grammar.psi.BnfRule

      RuleGraphHelper ruleGraphHelper = RuleGraphHelper.getCached(myFile);
      ((BnfDiagramProvider)getProvider()).myGraphHelper = ruleGraphHelper;

      Map<BnfRule, DiagramNode<PsiNamedElement>> nodeMap = new THashMap<BnfRule, DiagramNode<PsiNamedElement>>();
      List<BnfRule> rules = myFile.getRules();
      BnfRule root = ContainerUtil.getFirstItem(rules);
      for (BnfRule rule : rules) {
        if (rule != root && !RuleGraphHelper.shouldGeneratePsi(rule, true)) continue;
        DiagramNode<PsiNamedElement> diagramNode = new PsiDiagramNode<PsiNamedElement>(rule, getProvider()) {
          @Override
          public String getTooltip() {
            return getIdentifyingElement().getName();
          }
        };
        nodeMap.put(rule, diagramNode);
        myNodes.add(diagramNode);
      }
      for (BnfRule rule : rules) {
        if (rule != root && !RuleGraphHelper.shouldGeneratePsi(rule, true)) continue;
        Map<PsiElement, RuleGraphHelper.Cardinality> map = ruleGraphHelper.getFor(rule);

        BnfRule superRule = myFile.getRule(getAttribute(rule, KnownAttribute.EXTENDS));
        if (superRule != null) {
          DiagramNode<PsiNamedElement> source = nodeMap.get(rule);
          DiagramNode<PsiNamedElement> target = nodeMap.get(superRule);

          myEdges.add(new DiagramEdgeBase<PsiNamedElement>(source, target, new DiagramRelationshipInfoAdapter("EXTENDS", DiagramLineType.DASHED, "extends") {
View Full Code Here

Examples of org.intellij.grammar.psi.BnfRule

      }
      else if (element instanceof PsiErrorElement) {
        return "PsiErrorElement: '" + ((PsiErrorElement)element).getErrorDescription() + "'";
      }
      else if (elementType instanceof LivePreviewParser.RuleElementType) {
        BnfRule rule = ((LivePreviewParser.RuleElementType)elementType).rule;
        String prefix = getPsiClassPrefix((BnfFile)rule.getContainingFile());
        String className = getRulePsiClassName(rule, prefix);
        return className + ": '" + StringUtil.first(element.getText(), 30, true) +"'";
      }
      return elementType + "";
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.