public boolean visit()

in ruta-ep-ide-ui/src/main/java/org/apache/uima/ruta/ide/validator/LanguageCheckerVisitor.java [581:844]


  public boolean visit(Expression s) throws Exception {

    if (s instanceof RutaRuleElement) {
      RutaRuleElement re = (RutaRuleElement) s;
      Expression head = re.getHead();
      if (head instanceof FeatureMatchExpression) {
        FeatureMatchExpression fme = (FeatureMatchExpression) head;
        String text = fme.getFeature().getText();
        int lastIndexOf = text.lastIndexOf('.');
        String twf = text.substring(0, lastIndexOf);
        Integer variableType = getVariableType(twf);
        if (variableType != null && variableType == RutaTypeConstants.RUTA_TYPE_AT) {
          matchedType = twf;
        } else {
          twf = expand(twf);
          matchedType = isFeatureMatch(twf);
        }
      } else if (head != null) {
        matchedType = sourceModule.getSource().substring(head.sourceStart(), head.sourceEnd());
      }
      // cache long name
      matchedType = expand(matchedType);
      if (matchedType == null) {
        matchedType = "uima.tcas.Annotation";
      }
    }
    if (s instanceof FeatureMatchExpression) {
      FeatureMatchExpression fme = (FeatureMatchExpression) s;
      String featText = fme.getFeature().getText();
      // HOTFIX: parser creates wrong AST element
      if (allLongTypeNames.contains(featText)) {
        return true;
      }
      if (namespaces.keySet().contains(featText)) {
        // wrong ast elements, alias interpreted as feature expression
        return false;
      }
      checkTypeOfFeatureMatch(featText, fme);
      return true;
    }
    if (s instanceof RutaVariableReference) {
      if (s instanceof NullExpression) {
        return false;
      }
      RutaVariableReference ref = (RutaVariableReference) s;
      if (ref.getType() == RutaTypeConstants.RUTA_TYPE_WT
              || ref.getType() == RutaTypeConstants.RUTA_TYPE_WL) {
        if (StringUtils.isBlank(ref.getName())) {
          // declaration with a string expression: do not check
          return false;
        }
      }
      if ((ref.getType() & RutaTypeConstants.RUTA_TYPE_AT) != 0) {
        // types
        String name = ref.getName();
        if (name.equals("Document")) {
          return false;
        }

        Set<String> set = ambiguousTypeAlias.get(name);
        if (set != null && !set.isEmpty()) {
          pr.reportProblem(
                  problemFactory.createAmbiguousShortName(ref, set, ProblemSeverity.ERROR));
          return false;
        }
        if (namespaces.keySet().contains(name) || namespaces.values().contains(name)
                || allLongTypeNames.contains(name)) {
          return false;
        }
        Integer variableType = getVariableType(name);
        if (variableType != null && (variableType == RutaTypeConstants.RUTA_TYPE_AT
                || variableType == RutaTypeConstants.RUTA_TYPE_UA
                || variableType == RutaTypeConstants.RUTA_TYPE_UAL)) {
          return false;
        }
        if (isFeatureMatch(name) != null) {
          return false;
        }
        if (isLabel(name)) {
          return false;
        }
        if (name.indexOf(".") != -1) {
          String[] split = name.split("[.]");
          if (StringUtils.equals(split[split.length - 1], "ct")
                  || StringUtils.equals(split[split.length - 1], "coveredText")) {
            return false;
          }
          Integer prefixType = getVariableType(split[0]);
          if (prefixType == RutaTypeConstants.RUTA_TYPE_UA
                  || prefixType == RutaTypeConstants.RUTA_TYPE_UAL) {
            return false;
          }
        }

        pr.reportProblem(problemFactory.createTypeProblem(ref, sourceModule));
        return false;
      }
      if (!isVariableDeclared(ref)) {
        return false;
      }
      checkTypeOfVariable(ref);
      return false;
    }
    // check assign types
    if (s instanceof RutaAction) {
      RutaAction tma = (RutaAction) s;

      String actionName = sourceModule.getSource().substring(tma.getNameStart(), tma.getNameEnd());
      String[] keywords = RutaKeywordsManager.getKeywords(IRutaKeywords.ACTION);
      List<String> asList = Arrays.asList(keywords);
      if (!StringUtils.isEmpty(actionName) && !"-".equals(actionName)
              && !asList.contains(actionName) && !implicitString.equals(tma.getName())
              && !actionExtensions.keySet().contains(actionName)) {
        IProblem problem = problemFactory.createUnknownActionProblem(tma);
        pr.reportProblem(problem);
      }

      IRutaExtension extension = actionExtensions.get(actionName);
      if (extension != null) {
        extension.checkSyntax(tma, problemFactory, pr);
      }

      if (tma.getName().equals("GETFEATURE") || tma.getName().equals("SETFEATURE")) {
        List<?> childs = tma.getChilds();
        RutaStringExpression stringExpr = (RutaStringExpression) childs.get(0);
        String feat = stringExpr.toString();
        feat = getFeatureName(stringExpr, feat);
        boolean featureFound = findFeature(matchedType, feat, -1);
        if (!featureFound) {
          IProblem problem = problemFactory.createUnknownFeatureProblem(stringExpr, matchedType);
          pr.reportProblem(problem);
        }
      }

      if (s instanceof RutaStructureAction) {
        RutaStructureAction sa = (RutaStructureAction) s;
        Expression struct = sa.getStructure();
        String structure = null;
        if (struct != null) {
          structure = sourceModule.getSource().substring(struct.sourceStart(), struct.sourceEnd());
          structure = expand(structure);
        }
        Map<Expression, Expression> assignments = sa.getAssignments();
        // hotfix... correct name in ast
        String action = sourceModule.getSource().substring(sa.getNameStart(), sa.getNameEnd());
        if (assignments != null && !action.equals("TRIE")) {
          for (Expression each : assignments.keySet()) {
            // TODO refactor to visitor?
            String feat = each.toString();
            // List<?> childs = each.getChilds();
            feat = getFeatureName(each, feat);
            boolean featureFound = findFeature(structure, feat, -1);
            if (!featureFound) {
              IProblem problem = problemFactory.createUnknownFeatureProblem(each, structure);
              pr.reportProblem(problem);
            }
          }
        } else if (assignments != null && action.equals("TRIE")) {
          for (Expression each : assignments.values()) {
            if (each instanceof RutaListExpression) {
              RutaListExpression rle = (RutaListExpression) each;
              List<?> childs = rle.getExprs().getChilds();
              if (childs.size() != 2 && childs.size() != 3) {
                IProblem problem = problemFactory.createWrongNumberOfArgumentsProblem(actionName,
                        rle, 2);
                pr.reportProblem(problem);
              }
              Object arg1 = childs.get(0);
              if (arg1 instanceof RutaExpression) {
                RutaExpression e1 = (RutaExpression) arg1;
                if (e1.getKind() != RutaTypeConstants.RUTA_TYPE_AT) {
                  IProblem problem = problemFactory.createWrongArgumentTypeProblem(e1, "Type");
                  pr.reportProblem(problem);
                }
              }
              Object arg2 = childs.get(1);
              if (arg2 instanceof RutaExpression) {
                RutaExpression e2 = (RutaExpression) arg2;
                if (e2.getKind() != RutaTypeConstants.RUTA_TYPE_S) {
                  IProblem problem = problemFactory.createWrongArgumentTypeProblem(e2, "String");
                  pr.reportProblem(problem);
                }
              }
            }
          }
        }
      }
    }
    if (s instanceof RutaCondition) {
      RutaCondition cond = (RutaCondition) s;
      String conditionName = sourceModule.getSource().substring(cond.getNameStart(),
              cond.getNameEnd());
      String[] keywords = RutaKeywordsManager.getKeywords(IRutaKeywords.CONDITION);
      List<String> asList = Arrays.asList(keywords);
      if (!StringUtils.isEmpty(conditionName) && !"-".equals(conditionName)
              && !asList.contains(conditionName) && !implicitString.equals(cond.getName())
              && !conditionExtensions.keySet().contains(conditionName)) {
        IProblem problem = problemFactory.createUnknownConditionProblem(cond);
        pr.reportProblem(problem);
      }

      IRutaExtension extension = conditionExtensions.get(conditionName);
      if (extension != null) {
        // boolean checkSyntax =
        extension.checkSyntax(cond, problemFactory, pr);
      }

      if (conditionName.equals("FEATURE")) {
        if (matchedType != null) {
          List<?> args = cond.getChilds();
          RutaStringExpression se = (RutaStringExpression) args.get(0);
          String feat = se.toString();
          feat = getFeatureName(se, feat);
          boolean featureFound = findFeature(matchedType, feat, -1);
          if (!featureFound) {
            String featureMatch = isFeatureMatch(matchedType);
            if (featureMatch != null) {
              featureFound = findFeature(featureMatch, feat, -1);
            }
          }
          if (!featureFound) {
            IProblem problem = problemFactory.createUnknownFeatureProblem(se, matchedType);
            pr.reportProblem(problem);
          }
        }
      }
      if (conditionName.equals("CONTAINS")) {
        List<?> args = cond.getChilds();
        boolean valid = checkContainsArguments(args);
        if (!valid) {
          IProblem problem = problemFactory.createWrongArgumentTypeProblem(cond,
                  "different combination of arguments.");
          pr.reportProblem(problem);
        }
      }

    }
    if (s instanceof RutaFunction) {
      RutaFunction f = (RutaFunction) s;
      String name = f.getName();
      if (s.getKind() == RutaTypeConstants.RUTA_TYPE_AT) {
        IRutaExtension extension = typeFunctionExtensions.get(name);
        if (extension != null) {
          extension.checkSyntax(s, problemFactory, pr);
        }
      } else if (s.getKind() == RutaTypeConstants.RUTA_TYPE_B) {
        IRutaExtension extension = booleanFunctionExtensions.get(name);
        if (extension != null) {
          extension.checkSyntax(s, problemFactory, pr);
        }
      } else if (s.getKind() == RutaTypeConstants.RUTA_TYPE_N) {
        IRutaExtension extension = numberFunctionExtensions.get(name);
        if (extension != null) {
          extension.checkSyntax(s, problemFactory, pr);
        }
      } else if (s.getKind() == RutaTypeConstants.RUTA_TYPE_S) {
        IRutaExtension extension = stringFunctionExtensions.get(name);
        if (extension != null) {
          extension.checkSyntax(s, problemFactory, pr);
        }
      }
    }
    return true;
  }