Ejemplo n.º 1
0
  public void checkAmbiguous(List<Script> scripts) throws ConfigCompileException {
    // for (int i = 0; i < scripts.size(); i++) {
    List<Construct> thisCommand = this.cleft;
    for (int j = 0; j < scripts.size(); j++) {
      List<Construct> thatCommand = scripts.get(j).cleft;
      if (thatCommand == null) {
        // it hasn't been compiled yet.
        return;
      }
      if (this.cleft == scripts.get(j).cleft) {
        // Of course this command is going to match it's own signature
        continue;
      }
      boolean soFarAMatch = true;
      for (int k = 0; k < thisCommand.size(); k++) {
        try {
          Construct c1 = thisCommand.get(k);
          Construct c2 = thatCommand.get(k);
          if (c1.getCType() != c2.getCType()
              || ((c1 instanceof Variable) && !((Variable) c1).isOptional())) {
            soFarAMatch = false;
          } else {
            // It's a literal, check to see if it's the same literal
            if (c1.nval() == null || !c1.val().equals(c2.val())) {
              soFarAMatch = false;
            }
          }
        } catch (IndexOutOfBoundsException e) {
          /**
           * The two commands: /cmd $var1 [$var2] /cmd $var1 would cause this exception to be
           * thrown, but the signatures are the same, so the fact that they've matched this far
           * means they are ambiguous. However, /cmd $var1 $var2 /cmd $var1 is not ambiguous
           */
          // thatCommand is the short one
          if (!(thisCommand.get(k) instanceof Variable)
              || (thisCommand.get(k) instanceof Variable
                  && !((Variable) thisCommand.get(k)).isOptional())) {
            soFarAMatch = false;
          }
        }
      }
      if (thatCommand.size() > thisCommand.size()) {
        int k = thisCommand.size();
        // thisCommand is the short one
        if (!(thatCommand.get(k) instanceof Variable)
            || (thatCommand.get(k) instanceof Variable
                && !((Variable) thatCommand.get(k)).isOptional())) {
          soFarAMatch = false;
        }
      }

      if (soFarAMatch) {
        String commandThis = "";
        for (Construct c : thisCommand) {
          commandThis += c.val() + " ";
        }
        String commandThat = "";
        for (Construct c : thatCommand) {
          commandThat += c.val() + " ";
        }
        scripts.get(j).compilerError = true;
        this.compilerError = true;
        throw new ConfigCompileException(
            "The command "
                + commandThis.trim()
                + " is ambiguous because it "
                + "matches the signature of "
                + commandThat.trim(),
            thisCommand.get(0).getTarget());
      }
    }

    //        //Also, check for undefined variables on the right, and unused variables on the left
    //        ArrayList<String> left_copy = new ArrayList<String>();
    //        for (Map.Entry<String, Variable> v : left_vars.entrySet()) {
    //            left_copy.add(v.getValue().getName());
    //        }
    //        Arrays.asList(new String[]{}).toArray(new String[]{});
    //        for (ParseTree gtn : cright) {
    //            GenericTree<Construct> tree = new GenericTree<Construct>();
    //            tree.setRoot(gtn);
    //            List<ParseTree> builtTree = tree.build(GenericTreeTraversalOrderEnum.PRE_ORDER);
    //            for (ParseTree c : builtTree) {
    //                if (c.getData() instanceof Variable) {
    //                    for (Map.Entry<String, Variable> v : left_vars.entrySet()) {
    //                        if (v.getValue().getName().equals(((Variable) c.getData()).getName()))
    // {
    //                            //Found it, remove this from the left_copy, and break
    //                            left_copy.remove(v.getValue().getName());
    //                            break;
    //                            //TODO: Layton!
    //                        }
    //                    }
    //                }
    //            }
    //        }
    // }
  }