]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/parser.cpp
Implement a parser mode for JSON files
[libs/datafile.git] / source / parser.cpp
index d298eca44765ca134ae8b65f191c31296830ca92..7e4d3dd7f93942b9d4c9c3b69f2715d93b25b492 100644 (file)
@@ -1,6 +1,7 @@
 #include <msp/strings/format.h>
 #include "binaryparser.h"
-#include "dataerror.h"
+#include "except.h"
+#include "jsonparser.h"
 #include "parser.h"
 #include "statement.h"
 #include "textparser.h"
@@ -14,9 +15,14 @@ Parser::Parser(IO::Base &i, const string &s):
        in(i),
        main_src(s),
        src(s),
-       good(true),
-       mode(new TextParser(in, src))
-{ }
+       good(true)
+{
+       char c = in.peek();
+       if(c=='{' || c=='[')
+               mode = new JsonParser(in, src);
+       else
+               mode = new TextParser(in, src);
+}
 
 Parser::~Parser()
 {
@@ -86,5 +92,26 @@ void Parser::process_control_statement(const Statement &st)
                mode->process_control_statement(st);
 }
 
+const StatementKey *Parser::peek(unsigned level)
+{
+       while(good)
+       {
+               const StatementKey *key = mode->peek(level);
+               if(key && !key->keyword.compare(0, 2, "__"))
+                       process_control_statement(mode->parse());
+               else
+                       return key;
+       }
+
+       return 0;
+}
+
+bool Parser::parse_and_load(unsigned level, Loader &ldr, const LoaderAction &act)
+{
+       // Peek first to get any control statements processed
+       peek(level);
+       return mode->parse_and_load(level, ldr, act);
+}
+
 } // namespace DataFile
 } // namespace Msp