adding J20nParser to silme.format.l20n trunk
authorZbigniew Braniecki <gandalf@mozilla.com>
Mon, 23 Aug 2010 13:39:53 +0200
branchtrunk
changeset 563 baa676ecf45d44ce9cb1e865cdae8130a9994a3a
parent 562 610bf8335651f01e916c99275f152117487414cc
child 564 be375e2207f82643638d6f477fc959d3fe8057a1
push id268
push userzbraniecki@mozilla.com
push dateMon, 23 Aug 2010 14:00:52 +0000
adding J20nParser to silme.format.l20n
lib/silme/format/l20n/__init__.py
lib/silme/format/l20n/parser.py
--- a/lib/silme/format/l20n/__init__.py
+++ b/lib/silme/format/l20n/__init__.py
@@ -1,9 +1,10 @@
 from .parser import L20nParser as Parser
+from .parser import J20nParser as J20nParser
 from .structure import LOL as Structure, Entity
 from .serializer import L20nSerializer as Serializer
 from .compiler import L20nCompiler as Compiler
 
 class L20nFormatParser():
     name = 'l20n'
     desc = "L20n reader/writer"
     extensions = ['lol']
@@ -34,10 +35,24 @@ class L20nFormatParser():
         l10nobject = Parser().parse(text)
         return l10nobject
     
     @classmethod
     def compile(cls, structure):
         j20n = Compiler.compile(structure)
         return j20n
 
+class J20nFormatParser():
+    name = 'j20n'
+    desc = 'J20n reader/writer'
+    extensions = ['j20n']
+    encoding = 'utf_8'
+    fallback = None
+
+    @classmethod
+    def get_structure(cls, text, code='default'):
+        structure = J20nParser().parse(text)
+        return structure
+
+
 def register(Manager):
     Manager.register(L20nFormatParser)
+    Manager.register(J20nFormatParser)
--- a/lib/silme/format/l20n/parser.py
+++ b/lib/silme/format/l20n/parser.py
@@ -391,8 +391,193 @@ class L20nParser():
         idref = Idref()
         id = self.get_id()
         idref.append(id)
         while self.content[0]=='.':
             self.content = self.content[1:]
             id = self.get_id()
             idref.append(id)
         return idref
+
+
+class J20nParser():
+    def __init__(self):
+        self.patterns = {}
+        self.patterns['ws'] = re.compile('^\s+')
+        self.patterns['id'] = re.compile('^[\w-]+')
+        self.patterns['entry'] = re.compile('(^this\.)|(^\/\*)|(^var)')
+
+    def parse(self, content):
+        self.content = content
+        self.lol = LOL()
+        self.get_ws()
+        while self.content:
+            self.get_entry()
+        return self.lol
+
+    def get_entry(self):
+        entry = None
+        match = self.patterns['entry'].match(self.content)
+        if not match:
+            print self.content[0:20]
+            raise Exception()
+
+        if self.content[0] == '/':
+            entry = self.get_comment()
+        elif self.content[0] == 'v':
+            entry = self.get_var()
+        elif self.content[0] == 't':
+            entry = self.get_entity()
+        else:
+            raise Exception()
+        self.lol.add(entry)
+        self.get_ws()
+        return entry
+
+
+    def get_ws(self):
+        match = self.patterns['ws'].match(self.content)
+        if not match:
+            return None
+        self.content = self.content[match.end(0):]
+        #return WS(match.group(0)) # this line costs a lot
+
+    def get_entity(self):
+        if self.content[:5] != 'this.':
+            raise Exception()
+        self.content = self.content[5:]
+        id = self.get_id()
+        entity = Entity(id)
+        self.get_ws()
+        if self.content[0] != '=':
+            raise Exception()
+        self.content = self.content[1:]
+        self.get_ws()
+        if self.content[0] == '{':
+            self.content = self.content[1:]
+            self.get_ws()
+            while self.content[0] != '}':
+                kvpair = self.get_key_value_pair()
+                entity.kvplist[kvpair.key] = kvpair.value
+                self.get_ws()
+                if self.content[0] == ',':
+                    self.content = self.content[1:]
+                    self.get_ws()
+            self.content = self.content[1:]
+        return entity
+
+    def get_id(self):
+        match = self.patterns['id'].match(self.content)
+        if not match:
+            print self.content[:100]
+            raise Exception()
+        self.content = self.content[match.end(0):]
+        return match.group(0)
+
+    def get_value(self):
+
+        #print self.content[:100]
+        if self.content[0]=="'" or \
+            self.content[0]=='"':
+            value = self.get_string()
+        elif self.content[0]=='[':
+            value = self.get_array()
+        elif self.content[0]=='{':
+            value = self.get_hash()
+        else:
+            end = self.content.find(',')
+            end2 = self.content.find('\n')
+            end3 = self.content.find('}')
+            if end2<end:
+                end = end2
+            if end3<end:
+                end = end3
+            value = self.content[:end]
+            #self.content = self.content[end+1:]
+        return value
+
+    def get_key_value_pair(self):
+        str_end = ''
+        if self.content[0] == "'":
+            self.content = self.content[1:]
+            str_end = "'"
+        elif self.content[0] == '"':
+            self.content = self.content[1:]
+            str_end = '"'
+        key_value_pair = KeyValuePair()
+        key_value_pair.key = self.get_id()
+        if str_end:
+            if self.content[0] != str_end:
+                print self.content[:100]
+                raise Exception()
+            else:
+                self.content = self.content[1:]
+        self.get_ws()
+        if self.content[0]!=':':
+            print self.content[:100]
+            raise Exception()
+        self.content = self.content[1:]
+        #key_value_pair.ws.append(self.get_ws())
+        self.get_ws()
+        value = self.get_value()
+        key_value_pair.value = value
+        return key_value_pair
+
+    def get_comment(self):
+        if self.content[:2] != '/*':
+            raise Exception()
+        pattern = re.compile('\*\/')
+        m = pattern.search(self.content)
+        if not m:
+            raise Exception()
+        comment = self.content[2:m.start(0)]
+        self.content = self.content[m.end(0):]
+        self.lol.add(Comment(comment))
+
+    def get_string(self):
+        if self.content[0]!='"' and \
+             self.content[0]!="'":
+            raise Exception()
+        str_end = self.content[0]
+        buffer = ''
+        #literal = re.compile('^([^\\\\$'+str_end+']+)')
+        literal = re.compile('^([^\\'+str_end+']+)')
+        self.content = self.content[1:]
+        while self.content[0]!=str_end:
+            '''
+            print self.content[:100]
+            if self.content[0]=='\\':
+                self.content = self.content[1:]
+                buffer = buffer + this.content[0]
+                self.content = self.content[1:]
+                print 'AAA'
+          
+            if self.content[0]=='$':
+                self.content = self.content[1:]
+                if self.content[0]!='{':
+                    raise Exception()
+                self.content = self.content[1:]
+                expander = Expander()
+                expression = self.get_expression()
+                expander.expression = expression
+                if self.content[0]!='}':
+                    raise Exception()
+                self.content = self.content[1:]
+                if not (self.content[0]=='s' or \
+                    self.content[0]=='i'):
+                    raise Exception()
+                expander.flag = self.content[0]
+                self.content = self.content[1:]
+            '''
+            m = literal.match(self.content)
+            if m:
+                buffer = buffer + m.group(1)
+                self.content = self.content[m.end(0):]
+        self.content = self.content[1:]
+        if self.content[0] == '+':
+            end  = self.content.find(',')
+            buffer = buffer + self.content[:end]
+            self.content = self.content[end+1:]
+        return buffer
+
+
+    def get_var(self):
+        self.content = self.content[self.content.find('\n')+1:]