//throws RecognitionException public void singleTemplateArg(AST _t, StringTemplate embedded, IDictionary argumentContext ) { antlr.stringtemplate.language.StringTemplateAST singleTemplateArg_AST_in = (antlr.stringtemplate.language.StringTemplateAST)_t; Object e = null; try { // for error handling AST __t41 = _t; antlr.stringtemplate.language.StringTemplateAST tmp22_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t; match((AST)_t,SINGLEVALUEARG); _t = _t.getFirstChild(); e=expr(_t); _t = retTree_; _t = __t41; _t = _t.getNextSibling(); if ( e!=null ) { String soleArgName = null; // find the sole defined formal argument for embedded bool error = false; IDictionary formalArgs = embedded.getFormalArguments(); if ( formalArgs!=null ) { ICollection argNames = formalArgs.Keys; if ( argNames.Count==1 ) { string[] argNamesArray = new string[argNames.Count]; argNames.CopyTo(argNamesArray,0); soleArgName = argNamesArray[0]; //System.out.println("sole formal arg of "+embedded.getName()+" is "+soleArgName); } else { error=true; } } else { error=true; } if ( error ) { self.error("template "+embedded.getName()+ " must have exactly one formal arg in template context "+ self.getEnclosingInstanceStackString()); } else { self.rawSetArgumentAttribute(embedded,argumentContext,soleArgName,e); } } } catch (RecognitionException ex) { reportError(ex); if (null != _t) { _t = _t.getNextSibling(); } } retTree_ = _t; }
// HELP ROUTINES CALLED BY EVALUATOR TREE WALKER /// <summary> /// For names,phones:{n,p | ...} treat the names, phones as lists /// to be walked in lock step as n=names[i], p=phones[i]. /// </summary> public virtual Object applyTemplateToListOfAttributes(StringTemplate self, IList attributes, StringTemplate templateToApply) { if ( attributes==null || templateToApply==null || attributes.Count==0 ) { return null; // do not apply if missing templates or empty values } IDictionary argumentContext = null; IList results = new ArrayList(); // convert all attributes to iterators even if just one value for (int a = 0; a < attributes.Count; a++) { Object o = attributes[a]; o = convertAnythingToIterator(o); attributes[a] = o; } int numAttributes = attributes.Count; // ensure arguments line up IDictionary formalArguments = templateToApply.getFormalArguments(); if ( formalArguments==null || formalArguments.Count==0 ) { self.error("missing arguments in anonymous"+ " template in context "+self.getEnclosingInstanceStackString()); return null; } ICollection keys = formalArguments.Keys; Object[] formalArgumentNames = new object[keys.Count]; keys.CopyTo(formalArgumentNames,0); if ( formalArgumentNames.Length!=numAttributes ) { self.error("number of arguments "+SupportClass.CollectionToString(formalArguments.Keys)+ " mismatch between attribute list and anonymous"+ " template in context "+self.getEnclosingInstanceStackString()); // truncate arg list to match smaller size int shorterSize = Math.Min(formalArgumentNames.Length, numAttributes); numAttributes = shorterSize; Object[] newFormalArgumentNames = new Object[shorterSize]; System.Array.Copy(formalArgumentNames, 0, newFormalArgumentNames, 0, shorterSize); formalArgumentNames = newFormalArgumentNames; } // keep walking while at least one attribute has values while ( true ) { argumentContext = new Hashtable(); // get a value for each attribute in list; put into arg context // to simulate template invocation of anonymous template int numEmpty = 0; for (int a = 0; a < numAttributes; a++) { IEnumerator it = (IEnumerator)attributes[a]; if ( it.MoveNext() ) { String argName = (String)formalArgumentNames[a]; Object iteratedValue = it.Current; argumentContext[argName] = iteratedValue; } else { numEmpty++; } } if ( numEmpty==numAttributes ) { break; } StringTemplate embedded = templateToApply.getInstanceOf(); embedded.setEnclosingInstance(self); embedded.setArgumentContext(argumentContext); results.Add(embedded); } return results; }
protected void setSoleFormalArgumentToIthValue(StringTemplate embedded, IDictionary argumentContext, Object ithValue) { IDictionary formalArgs = embedded.getFormalArguments(); if ( formalArgs!=null ) { String soleArgName = null; bool isAnonymous = embedded.getName().Equals(StringTemplate.ANONYMOUS_ST_NAME); if ( formalArgs.Count==1 || (isAnonymous&&formalArgs.Count>0) ) { if ( isAnonymous && formalArgs.Count>1 ) { embedded.error("too many arguments on {...} template: "+formalArgs); } // if exactly 1 arg or anonymous, give that the value of // "it" as a convenience like they said // $list:template(arg=it)$ ICollection argNames = formalArgs.Keys; string[] argNamesArray = new string[argNames.Count]; argNames.CopyTo(argNamesArray,0); soleArgName = argNamesArray[0]; argumentContext[soleArgName] = ithValue; } } }
/// <summary>Find "missing attribute" and "cardinality mismatch" errors. /// Excecuted before a template writes its chunks out. /// When you find a problem, throw an IllegalArgumentException. /// We must check the attributes as well as the incoming arguments /// in argumentContext. /// protected void checkAttributesAgainstFormalArguments() { /// Set args = formalArguments.keySet(); /// /* /// if ( (attributes==null||attributes.size()==0) && /// (argumentContext==null||argumentContext.size()==0) && /// formalArguments.size()!=0 ) /// { /// throw new IllegalArgumentException("missing argument(s): "+args+" in template "+getName()); /// } /// Iterator iter = args.iterator(); /// while ( iter.hasNext() ) { /// String argName = (String)iter.next(); /// FormalArgument arg = getFormalArgument(argName); /// int expectedCardinality = arg.getCardinality(); /// Object value = getAttribute(argName); /// int actualCardinality = getActualArgumentCardinality(value); /// // if intersection of expected and actual is empty, mismatch /// if ( (expectedCardinality&actualCardinality)==0 ) { /// throw new IllegalArgumentException("cardinality mismatch: "+ /// argName+"; expected "+ /// FormalArgument.getCardinalityName(expectedCardinality)+ /// " found cardinality="+getObjectLength(value)); /// } /// } /// } /// </summary> /// <summary>A reference to an attribute with no value, must be compared against /// the formal parameter to see if it exists; if it exists all is well, /// but if not, throw an exception. /// /// Don't do the check if no formal parameters exist for this template; /// ask enclosing. /// </summary> protected internal virtual void checkNullAttributeAgainstFormalArguments(StringTemplate self, String attribute) { if (self.getFormalArguments() == FormalArgument.UNKNOWN) { // bypass unknown arg lists if (self.enclosingInstance != null) { checkNullAttributeAgainstFormalArguments(self.enclosingInstance, attribute); } return ; } FormalArgument formalArg = self.lookupFormalArgument(attribute); if (formalArg == null) { throw new ArgumentOutOfRangeException("attribute", "no such attribute: " + attribute + " in template context " + getEnclosingInstanceStackString()); } }