From d554b69c8233ef67bb6354187c59f9c7e0f0079e Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Mon, 23 Jul 2018 15:35:19 +0200 Subject: [PATCH] Read .tl files while respecting sections --- .../lonamiwebs/overgram/parser/TLReader.java | 93 +++++++++++-------- 1 file changed, 56 insertions(+), 37 deletions(-) diff --git a/src/main/java/io/github/lonamiwebs/overgram/parser/TLReader.java b/src/main/java/io/github/lonamiwebs/overgram/parser/TLReader.java index 5dc076d..88dc59d 100644 --- a/src/main/java/io/github/lonamiwebs/overgram/parser/TLReader.java +++ b/src/main/java/io/github/lonamiwebs/overgram/parser/TLReader.java @@ -6,31 +6,72 @@ import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class TLReader { - - public static List readTlObjects(final File file) { - final List lines = readFileWithDelimiter(new File("telegram_api.tl"), ';'); - sanitizeTlLines(lines); - final List objects = new ArrayList<>(lines.size()); - for (final String line : lines) { - objects.add(TLObject.fromString(line)); + public static final Map> readTlObjects(final File file) { + final Map> result = new HashMap<>(); + for (final Map.Entry> entry : readTl(file).entrySet()) { + final List objects = new ArrayList<>(); + for (final String string : entry.getValue()) { + objects.add(TLObject.fromString(string)); + } + result.put(entry.getKey(), objects); } - return objects; + return result; } - private static List readFileWithDelimiter(final File file, final char delimiter) { - final List result = new ArrayList<>(); + private static Map> readTl(final File file) { + final Map> result = new HashMap<>(); + + String section = "types"; try (final CharacterIterator iter = new CharacterIterator(new FileReader(file))) { + String tmp; final StringBuilder builder = new StringBuilder(); for (char c : iter) { - if (c == delimiter) { - result.add(builder.toString()); - builder.setLength(0); - } else { - builder.append(c); + switch (c) { + // Assume '/' only occurs in "// comments...\n" + case '/': + do { + c = iter.next(); + } while (iter.hasNext() && c != '\n'); + break; + + // Assume '-' only occurs in "---sections---\n" + case '-': + if (builder.length() != 0) { + tmp = builder.toString().trim(); + if (!tmp.isEmpty()) { + result.computeIfAbsent(section, k -> new ArrayList<>()).add(tmp); + } + builder.setLength(0); + } + + do { + c = iter.next(); + if (c != '-') { + builder.append(c); + } + } while (iter.hasNext() && c != '\n'); + section = builder.toString().trim(); + builder.setLength(0); + break; + + // Definitions are separated by ';' + case ';': + tmp = builder.toString().trim(); + if (!tmp.isEmpty()) { + result.computeIfAbsent(section, k -> new ArrayList<>()).add(tmp); + } + builder.setLength(0); + break; + + default: + builder.append(c); + break; } } } catch (IOException ignored) { @@ -38,26 +79,4 @@ public class TLReader { return result; } - - private static void sanitizeTlLines(final List lines) { - String line; - int comment; - int newline; - for (int i = lines.size(); i-- != 0; ) { - line = lines.get(i); - while ((comment = line.indexOf("//")) != -1) { - newline = line.indexOf("\n", comment + 2); - if (newline == -1) { - newline = line.length(); - } - line = line.substring(0, comment) + line.substring(newline); - } - line = line.trim(); - if (line.isEmpty()) { - lines.remove(i); - } else { - lines.set(i, line); - } - } - } }