MCPcopy Index your code
hub / github.com/antlr/codebuff / zip_map

Method zip_map

output/java8/1.4.13/Interpreter.java:826–886  ·  view source on GitHub ↗
(InstanceScope scope, List<Object> exprs, ST prototype)

Source from the content-addressed store, hash-verified

824 // todo: i, i0 not set unless mentioned? map:{k,v | ..}?
825
826 protected ST.AttributeList zip_map(InstanceScope scope, List<Object> exprs, ST prototype) {
827 if ( exprs==null || prototype==null || exprs.size()==0) {
828 return null; // do not apply if missing templates or empty values
829 }
830 // make everything iterable
831 for (int i = 0; i < exprs.size(); i++) {
832 Object attr = exprs.get(i);
833 if ( attr!=null ) exprs.set(i, convertAnythingToIterator(scope, attr));
834 }
835
836 // ensure arguments line up
837
838 int numExprs = exprs.size();
839 CompiledST code = prototype.impl;
840 Map<String, FormalArgument> formalArguments = code.formalArguments;
841 if ( !code.hasFormalArgs || formalArguments==null ) {
842 errMgr.runTimeError(this, scope, ErrorType.MISSING_FORMAL_ARGUMENTS);
843 return null;
844 }
845
846 // todo: track formal args not names for efficient filling of locals
847 String[] formalArgumentNames = formalArguments.keySet().toArray(new String[formalArguments.size()]);
848 int nformalArgs = formalArgumentNames.length;
849 if ( prototype.isAnonSubtemplate() ) nformalArgs -= predefinedAnonSubtemplateAttributes.size();
850 if ( nformalArgs!= numExprs ) {
851 errMgr.runTimeError(this, scope, ErrorType.MAP_ARGUMENT_COUNT_MISMATCH, numExprs, nformalArgs);
852 // TODO just fill first n
853 // truncate arg list to match smaller size
854 int shorterSize = Math.min(formalArgumentNames.length, numExprs);
855 numExprs = shorterSize;
856 String[] newFormalArgumentNames = new String[shorterSize];
857 System.arraycopy(formalArgumentNames, 0, newFormalArgumentNames, 0, shorterSize);
858 formalArgumentNames = newFormalArgumentNames;
859 }
860
861 // keep walking while at least one attribute has values
862 ST.AttributeList results = new ST. AttributeList();
863 int i = 0; // iteration number from 0
864 while ( true ) {
865 // get a value for each attribute in list; put into ST instance
866 int numEmpty = 0;
867 ST embedded = group.createStringTemplateInternally(prototype);
868 embedded.rawSetAttribute("i0", i);
869 embedded.rawSetAttribute("i", i+1);
870 for (int a = 0; a < numExprs; a++) {
871 Iterator<?> it = (Iterator<?>)exprs.get(a);
872 if ( it!=null && it.hasNext() ) {
873 String argName = formalArgumentNames[a];
874 Object iteratedValue = it.next();
875 embedded.rawSetAttribute(argName, iteratedValue);
876 }
877 else {
878 numEmpty++;
879 }
880 }
881 if ( numEmpty==numExprs ) break;
882 results.add(embedded);
883 i++;

Callers 1

_execMethod · 0.95

Calls 14

rawSetAttributeMethod · 0.95
sizeMethod · 0.65
getMethod · 0.65
setMethod · 0.65
runTimeErrorMethod · 0.65
keySetMethod · 0.65
nextMethod · 0.65
addMethod · 0.65
toArrayMethod · 0.45
isAnonSubtemplateMethod · 0.45
minMethod · 0.45

Tested by

no test coverage detected