MCPcopy Create free account
hub / github.com/antlr/codebuff / zip_map

Method zip_map

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

Source from the content-addressed store, hash-verified

863 // todo: i, i0 not set unless mentioned? map:{k,v | ..}?
864
865 protected ST.AttributeList zip_map(InstanceScope scope, List<Object> exprs, ST prototype) {
866 if ( exprs==null || prototype==null || exprs.size()==0) {
867 return null; // do not apply if missing templates or empty values
868 }
869 // make everything iterable
870 for (int i = 0; i <exprs.size(); i++) {
871 Object attr = exprs.get(i);
872 if ( attr !=null ) exprs.set(i, convertAnythingToIterator(scope, attr));
873 }
874
875 // ensure arguments line up
876
877 int numExprs = exprs.size();
878 CompiledST code = prototype.impl;
879 Map<String, FormalArgument> formalArguments = code.formalArguments;
880 if ( !code.hasFormalArgs|| formalArguments==null ) {
881 errMgr.runTimeError(this, scope, ErrorType.MISSING_FORMAL_ARGUMENTS);
882 return null;
883 }
884
885 // todo: track formal args not names for efficient filling of locals
886 String[] formalArgumentNames = formalArguments.keySet().toArray(new String[formalArguments.size()]);
887 int nformalArgs = formalArgumentNames.length;
888 if ( prototype.isAnonSubtemplate() ) nformalArgs-= predefinedAnonSubtemplateAttributes.size();
889 if ( nformalArgs != numExprs ) {
890 errMgr.runTimeError(this, scope, ErrorType.MAP_ARGUMENT_COUNT_MISMATCH, numExprs, nformalArgs);
891 // TODO just fill first n
892 // truncate arg list to match smaller size
893 int shorterSize = Math.min(formalArgumentNames.length, numExprs);
894 numExprs = shorterSize;
895 String[] newFormalArgumentNames = new String[shorterSize];
896 System.arraycopy(formalArgumentNames, 0, newFormalArgumentNames, 0, shorterSize);
897 formalArgumentNames = newFormalArgumentNames;
898 }
899
900 // keep walking while at least one attribute has values
901 ST.AttributeList results = new ST.AttributeList();
902 int i = 0; // iteration number from 0
903 while ( true ) {
904 // get a value for each attribute in list; put into ST instance
905 int numEmpty = 0;
906 ST embedded = group.createStringTemplateInternally(prototype);
907 embedded.rawSetAttribute("i0", i);
908 embedded.rawSetAttribute("i", i+1);
909 for (int a = 0; a < numExprs; a++) {
910 Iterator<?> it = (Iterator<?>)exprs.get(a);
911 if ( it !=null && it.hasNext() ) {
912 String argName = formalArgumentNames[a];
913 Object iteratedValue = it.next();
914 embedded.rawSetAttribute(argName, iteratedValue);
915 }
916 else {
917 numEmpty++;
918 }
919 }
920 if ( numEmpty==numExprs ) break;
921 results.add(embedded);
922 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