diff --git a/src/main/java/io/github/lonamiwebs/overgram/parser/TLArg.java b/src/main/java/io/github/lonamiwebs/overgram/parser/TLArg.java index 13533f6..b0c97e8 100644 --- a/src/main/java/io/github/lonamiwebs/overgram/parser/TLArg.java +++ b/src/main/java/io/github/lonamiwebs/overgram/parser/TLArg.java @@ -1,21 +1,89 @@ package io.github.lonamiwebs.overgram.parser; +import java.util.ArrayList; +import java.util.List; + public class TLArg { public final String name; - public final String type; + public final List types; + public final boolean flags; + public final Flag flag; - private TLArg(final String name, final String type) { + private TLArg(final String name, final List types, final boolean flags, final Flag flag) { this.name = name; - this.type = type; + this.types = types; + this.flags = flags; + this.flag = flag; } public static TLArg fromString(String string) { 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 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 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 + "?"; + } } }