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

Method zip_map

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

Source from the content-addressed store, hash-verified

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

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