Parse flags and nested types such as vectors

This commit is contained in:
Lonami Exo 2018-07-23 13:55:09 +02:00
parent 68a6d4a49f
commit 703e5d1a51
1 changed files with 73 additions and 5 deletions

View File

@ -1,21 +1,89 @@
package io.github.lonamiwebs.overgram.parser; package io.github.lonamiwebs.overgram.parser;
import java.util.ArrayList;
import java.util.List;
public class TLArg { public class TLArg {
public final String name; public final String name;
public final String type; public final List<String> types;
public final boolean flags;
public final Flag flag;
private TLArg(final String name, final String type) { private TLArg(final String name, final List<String> types, final boolean flags, final Flag flag) {
this.name = name; this.name = name;
this.type = type; this.types = types;
this.flags = flags;
this.flag = flag;
} }
public static TLArg fromString(String string) { public static TLArg fromString(String string) {
final String[] tmp = string.split(":"); final String[] tmp = string.split(":");
return new TLArg(tmp[0], tmp[1]);
boolean flags = false;
if (tmp[1].equals("#")) {
tmp[1] = "";
flags = true;
}
Flag flag = null;
final int flagIndex = tmp[1].indexOf('?');
if (flagIndex != -1) {
flag = Flag.fromString(tmp[1].substring(0, flagIndex));
tmp[1] = tmp[1].substring(flagIndex + 1);
}
final List<String> types = new ArrayList<>(2);
int angleBracket;
while ((angleBracket = tmp[1].indexOf("<")) != -1) {
types.add(tmp[1].substring(0, angleBracket));
tmp[1] = tmp[1].substring(angleBracket + 1, tmp[1].length() - 1);
}
types.add(tmp[1]);
return new TLArg(tmp[0], types, flags, flag);
} }
@Override @Override
public String toString() { public String toString() {
return name + ":" + type; final StringBuilder builder = new StringBuilder();
builder.append(name);
builder.append(':');
if (flag != null) {
builder.append(flag.toString());
}
if (flags) {
builder.append('#');
} else {
for (final String type : types) {
builder.append(type);
builder.append('<');
}
builder.setLength(builder.length() - 1);
for (int i = 1; i < types.size(); ++i) {
builder.append('>');
}
}
return builder.toString();
}
private static final class Flag {
private final String flagName;
private final int flagIndex;
private Flag(final String flagName, final int flagIndex) {
this.flagName = flagName;
this.flagIndex = flagIndex;
}
private static Flag fromString(final String string) {
final String[] tmp = string.split("\\.");
return new Flag(tmp[0], Integer.parseInt(tmp[1]));
}
@Override
public String toString() {
return flagName + "." + flagIndex + "?";
}
} }
} }