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

Method zip_map

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

Source from the content-addressed store, hash-verified

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

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