Added Iteration support.

Said support allows to use the #iterate & #argument #enditerate
parameters.
Which will duplicate the text block inside of it x amount of times with
arguments being replaced.

The idea being that if code can be replicated the iterate option would
simplify the process.

Multiple arguments can be defined (they will be not part of the output)
and it will simply insert them.
Note that all argument parameters have to be equal size.
Otherwise the iterator processor will fail.

Iterators can be stacked too to create layers upon layers.

On top of that the iterator is part of the Template parser and not of
the template processor meaning its a Pre processor step.
And all mappers still apply themselves on to the output of the
iterators!
This commit is contained in:
Speiger 2023-06-27 13:29:31 +02:00
parent d90a9ad71c
commit d5a47ce568
11 changed files with 206 additions and 59 deletions

View File

@ -1,2 +1,13 @@
arguments=
auto.sync=false
build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
connection.project.dir=
eclipse.preferences.version=1
gradle.user.home=
java.home=
jvm.arguments=
offline.mode=false
override.workspace.settings=false
show.console.view=false
show.executions.view=false

View File

@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8

View File

@ -5,7 +5,7 @@ repositories {
}
archivesBaseName = 'Simple Code Generator'
version = '1.2.2'
version = '1.3.0'
apply plugin: 'maven'
tasks.withType(JavaCompile) {

View File

@ -1 +1 @@
test=e949777578333aa849d74d6a2c651236
test=ddb9367ebd61888ab31a658dd6f6df86

View File

@ -21,3 +21,33 @@ TESTING should not be overriden
TESTING should not be overriden
#endif
#iterate
#argument NUMBER 1 2 3 4 5 6 7 8 9
Lets test number NUMBER
#enditerate
#if TEST0
Lets test a true condition.
#iterate
#argument NUMBER 1 2 3 4 5 6 7 8 9
Lets test number NUMBER
#enditerate
#endif
#if TEST1
Lets test a false condition.
#iterate
#argument NUMBER 1 2 3 4 5 6 7 8 9
Lets test number NUMBER
#enditerate
#endif
Lets test stacked iterators
#iterate
#argument NUMBER1 1 2 3
#iterate
#argument NUMBER2 4 5 6
Lets test numbers NUMBER1 NUMBER2
#enditerate
#enditerate

View File

@ -6,3 +6,36 @@ Test2 should be overriden
TESTING should not be overriden too
Test2 should be overriden too
Lets test number 1
Lets test number 2
Lets test number 3
Lets test number 4
Lets test number 5
Lets test number 6
Lets test number 7
Lets test number 8
Lets test number 9
Lets test a true condition.
Lets test number 1
Lets test number 2
Lets test number 3
Lets test number 4
Lets test number 5
Lets test number 6
Lets test number 7
Lets test number 8
Lets test number 9
Lets test stacked iterators
Lets test numbers 1 4
Lets test numbers 1 5
Lets test numbers 1 6
Lets test numbers 2 4
Lets test numbers 2 5
Lets test numbers 2 6
Lets test numbers 3 4
Lets test numbers 3 5
Lets test numbers 3 6

View File

@ -97,6 +97,10 @@ public class ConditionedSegment
i += parse(fileName, null, lines, i, segmentText.length(), ignoreCounter, childSegments, postSegments);
segmentText.add(lines.get(prev));
}
else if(trimmed.startsWith("#iterate")) {
SegmentIterator.iterateSegment(fileName, trimmed, lines, i, segmentText.length());
i--;
}
continue;
}
segmentText.add(s);

View File

@ -0,0 +1,61 @@
package speiger.src.builder.base;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;
public class SegmentIterator {
public static void iterateSegment(String fileName, String currentLine, List<String> lines, int currentIndex, int startIndex) {
List<String[]> arguments = new ArrayList<>();
StringJoiner toIterate = new StringJoiner("\n");
int skip = 0;
int end = 1;
for(int i = currentIndex+1;i<lines.size();i++, end++)
{
String s = lines.get(i);
String trimmed = s.trim();
if(trimmed.startsWith("#"))
{
if(trimmed.startsWith("#enditerate"))
{
skip--;
if(skip < 0)
{
end++;
break;
}
}
if(trimmed.startsWith("#iterate")) skip++;
if(skip > 0)
{
toIterate.add(s);
continue;
}
if(trimmed.startsWith("#argument"))
{
arguments.add(trimmed.substring(9).trim().split(" "));
continue;
}
}
toIterate.add(s);
}
int size = arguments.get(0).length;
for(int i = 1,m=arguments.size();i<m;i++) {
if(arguments.get(i).length != size) throw new RuntimeException("Iteration arguments in file ["+fileName+"] are not equal size. Arugments="+arguments.stream().flatMap(Arrays::stream).collect(Collectors.toList()));
}
for(int i = 0;i<end;i++) {
lines.remove(currentIndex);
}
String toInsert = toIterate.toString();
for(int i = size-1;i>=1;i--) {
String temp = toInsert;
for(int j = 0;j<arguments.size();j++) {
String[] argument = arguments.get(j);
temp = temp.replace(argument[0], argument[i]);
}
lines.addAll(currentIndex, Arrays.asList(temp.split("\n", -1)));
}
}
}

View File

@ -92,6 +92,11 @@ public class Template
joiner.add(lines.get(prev));
continue;
}
else if(trimmed.startsWith("#iterate")) {
SegmentIterator.iterateSegment(fileName, trimmed, lines, i, joiner.length());
i--;
continue;
}
else if(trimmed.startsWith("#symlink"))
{
return Template.parse(file.getParent().resolve(trimmed.substring(8).trim()));

View File

@ -96,6 +96,7 @@ public abstract class TemplateProcessor
protected Path getDataFolder() { return dataFolder; }
protected boolean isSilencedSuccess() { return silencedSuccess; }
@SuppressWarnings("unchecked")
public final boolean process(boolean force) throws IOException, InterruptedException
{
if(!init)
@ -112,7 +113,7 @@ public abstract class TemplateProcessor
}
AtomicLong[] counters = new AtomicLong[]{new AtomicLong(), new AtomicLong(), new AtomicLong()};
final boolean relative = relativePackages();
Set<IMapper>[] mappers = debugUnusedMappers() ? new Set[]{Collections.synchronizedSet(new HashSet<IMapper>()), Collections.synchronizedSet(new HashSet<IMapper>())} : null;
Set<IMapper>[] mappers = debugUnusedMappers() ? new Set[]{Collections.synchronizedSet(new HashSet<>()), Collections.synchronizedSet(new HashSet<>())} : null;
ThreadPoolExecutor service = (ThreadPoolExecutor)Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
service.setKeepAliveTime(10, TimeUnit.MILLISECONDS);
service.allowCoreThreadTimeOut(true);