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

Method zip_map

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

Source from the content-addressed store, hash-verified

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

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