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

Method zip_map

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

Source from the content-addressed store, hash-verified

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

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