Parse Emacs org mode file to object graph. WIP.
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Mon, 8 Jun 2020 18:08:26 +0000 (21:08 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Mon, 8 Jun 2020 18:08:26 +0000 (21:08 +0300)
pom.xml
src/main/java/eu/svjatoslav/sixth/core/document/Document.java [new file with mode: 0644]
src/main/java/eu/svjatoslav/sixth/core/document/OrgParser.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 65f2da6..e8e9fea 100644 (file)
--- a/pom.xml
+++ b/pom.xml
 
         <dependency>
             <groupId>eu.svjatoslav</groupId>
+            <artifactId>svjatoslavcommons</artifactId>
+            <version>1.8-SNAPSHOT</version>
+        </dependency>
+
+        <dependency>
+            <groupId>eu.svjatoslav</groupId>
             <artifactId>javainspect</artifactId>
             <version>1.5</version>
             <scope>test</scope>
diff --git a/src/main/java/eu/svjatoslav/sixth/core/document/Document.java b/src/main/java/eu/svjatoslav/sixth/core/document/Document.java
new file mode 100644 (file)
index 0000000..2c3d2f6
--- /dev/null
@@ -0,0 +1,4 @@
+package eu.svjatoslav.sixth.core.document;
+
+public class Document {
+}
diff --git a/src/main/java/eu/svjatoslav/sixth/core/document/OrgParser.java b/src/main/java/eu/svjatoslav/sixth/core/document/OrgParser.java
new file mode 100644 (file)
index 0000000..f420c48
--- /dev/null
@@ -0,0 +1,138 @@
+package eu.svjatoslav.sixth.core.document;
+
+import eu.svjatoslav.commons.string.String2;
+import eu.svjatoslav.commons.string.tokenizer.InvalidSyntaxException;
+import eu.svjatoslav.commons.string.tokenizer.Tokenizer;
+import eu.svjatoslav.commons.string.tokenizer.TokenizerMatch;
+
+import java.io.File;
+import java.io.IOException;
+
+import static eu.svjatoslav.commons.file.IOHelper.getFileContentsAsString;
+import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.DROP;
+import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.PRESERVE;
+
+public class OrgParser {
+
+    public Document parse(File file) throws IOException, InvalidSyntaxException {
+        Document document = new Document();
+
+        Tokenizer lineTokenizer = getFileToLineTokenizer(getFileContentsAsString(file));
+
+        while (true) {
+            final TokenizerMatch line = lineTokenizer.getNextToken();
+            if (line == null)
+                break; // EOF
+
+            parseLine(line.token);
+        }
+
+        return document;
+    }
+
+    private void parseLine(String line) throws InvalidSyntaxException {
+        Tokenizer lineTokenizer = getLineTokenizer(line + "\n");
+
+        TokenizerMatch token = lineTokenizer.getNextToken();
+        if (token == null) return;
+
+        if (token.terminator == null)
+            return;
+
+        if (token.token.startsWith("*")){
+            System.out.println("LINE: " + line);
+            System.out.println(token);
+            System.out.println();
+        }
+    }
+
+
+    private Tokenizer getLineTokenizer(String contents) {
+        final Tokenizer tokenizer = new Tokenizer(contents);
+        for (int i = 1; i<50; i++){
+            String prefix = new String2("*").repeat(i).toString();
+            tokenizer.addTerminator(prefix +" ","\n", PRESERVE);
+        }
+        return tokenizer;
+    }
+
+    private Tokenizer getFileToLineTokenizer(String contents) {
+        final Tokenizer tokenizer = new Tokenizer(contents);
+
+        // empty space
+//        tokenizer.addTerminator(" ", DROP);
+//        tokenizer.addTerminator("\t", DROP);
+//        tokenizer.addTerminator("\n", DROP);
+
+        // newline
+        tokenizer.addTerminator("\n", DROP);
+
+//        tokenizer.addTerminator(";", PRESERVE);
+//        tokenizer.addTerminator("{", PRESERVE);
+//        tokenizer.addTerminator("}", PRESERVE);
+//        tokenizer.addTerminator("(", PRESERVE);
+//        tokenizer.addTerminator(")", PRESERVE);
+//        tokenizer.addTerminator("[", PRESERVE);
+//        tokenizer.addTerminator("]", PRESERVE);
+//        tokenizer.addTerminator("<", PRESERVE);
+//        tokenizer.addTerminator(">", PRESERVE);
+//        tokenizer.addTerminator(",", PRESERVE);
+//        tokenizer.addTerminator("@", PRESERVE);
+
+        // comments
+//        tokenizer.addTerminator("//", "\n", DROP);
+//        tokenizer.addTerminator("/*", "*/", DROP);
+        return tokenizer;
+    }
+//
+//    private void parseImport(final Tokenizer tokenizer)
+//            throws InvalidSyntaxException {
+//
+//        final Import imp = new Import();
+//
+//        final TokenizerMatch match = tokenizer.getNextToken();
+//
+//        if (match.token.equals("static")) {
+//            imp.isStatic = true;
+//            imp.path = tokenizer.getNextToken().token;
+//        } else
+//            imp.path = match.token;
+//
+//        imports.add(imp);
+//
+//        tokenizer.expectAndConsumeNextToken(";");
+//    }
+//
+//    private void parseInterface(final Tokenizer tokenizer)
+//            throws InvalidSyntaxException {
+//
+//        final TokenizerMatch match = tokenizer.getNextToken();
+//        final Clazz clazz = new Clazz(packageName, match.token, tokenizer, true);
+//        // System.out.println(clazz.toString());
+//        classes.add(clazz);
+//    }
+//
+//    private void parsePackage(final Tokenizer tokenizer)
+//            throws InvalidSyntaxException {
+//
+//        final TokenizerMatch match = tokenizer.getNextToken();
+//
+//        packageName = match.token;
+//
+//        tokenizer.expectAndConsumeNextToken(";");
+//    }
+
+    public void skipUntilSemicolon(final Tokenizer tokenizer) throws InvalidSyntaxException {
+        while (true) {
+            final TokenizerMatch token = tokenizer.getNextToken();
+
+            if (token == null)
+                return;
+
+            if (token.token.equals(";"))
+                return;
+        }
+    }
+
+
+}