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

Method zip_map

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

Source from the content-addressed store, hash-verified

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

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