py/gadialog_srs_anki.py
changeset 1223 d592572cc546
parent 1191 a3a7c8342b1c
child 1228 e38d1d191339
--- a/py/gadialog_srs_anki.py	Fri Aug 21 12:15:51 2020 +0300
+++ b/py/gadialog_srs_anki.py	Fri Aug 21 12:17:35 2020 +0300
@@ -10,7 +10,7 @@
 import tempfile
 import shutil
 
-from gadict_util import ParseException
+from gadict import Parser
 
 import anki
 from anki.exporting import AnkiPackageExporter
@@ -61,84 +61,6 @@
 
 ################################################################
 
-class Parser:
-
-    COMMENT_RE = re.compile("^; ")
-    NUM_RE = re.compile(u"^# ([1-9][0-9]*)$")
-    PHRASE_START_RE = re.compile(u"^- (.*)")
-
-    def __init__(self):
-        pass
-
-    def readline(self):
-        while True:
-            self.line = self.stream.readline()
-            self.eof = len(self.line) == 0
-            if self.eof:
-                break
-            self.lineno += 1
-            if self.COMMENT_RE.search(self.line):
-                continue
-            self.line = self.line.strip(' \n\t')
-            if len(self.line) > 0:
-                break
-
-    def parse(self, stream):
-        self.lineno = 0
-        self.stream = stream
-        self.dom = dict()
-        self.eof = False
-        try:
-            self.parse_prelude()
-            while not self.eof:
-                self.parse_article()
-        except ParseException as ex:
-            if sys.version_info.major == 2:
-                import traceback
-                traceback.print_exc()
-            raise ParseException(ex.msg, self.lineno, self.line)
-        return self.dom
-
-    def parse_prelude(self):
-        while True:
-            self.readline()
-            if self.eof:
-                return
-            m = self.NUM_RE.match(self.line)
-            if m:
-                self.num = m.group(1)
-                break
-
-    def parse_article(self):
-        """Assume we are at ``# NUM`` line."""
-        num = self.num
-        phrase_buf = []
-        phrases = []
-        while True:
-            self.readline()
-            if self.eof:
-                if len(phrase_buf) > 0:
-                    phrases.append(" ".join(phrase_buf))
-                break
-            m = self.NUM_RE.match(self.line)
-            if m:
-                if len(phrase_buf) > 0:
-                    phrases.append(" ".join(phrase_buf))
-                self.num = m.group(1)
-                break
-            m = self.PHRASE_START_RE.match(self.line)
-            if m:
-                if len(phrase_buf) > 0:
-                    phrases.append(" ".join(phrase_buf))
-                phrase_buf = [m.group(1)]
-            else:
-                phrase_buf.append(self.line)
-        if len(phrases) == 0:
-            raise ParseException("""There are no any phrases...""")
-        if num in self.dom:
-            raise ParseException("""Conflicting key: {}...""".format(num))
-        self.dom[num] = phrases
-
 FIN = io.open(FINAME, mode='r', buffering=1, encoding="utf-8")
 
 PARSER = Parser()