1. fab31
  2. zicbee-lib

Commits

fab31  committed 16e35a9

fixed parser on tricky case (feat. minor cleanup)

  • Participants
  • Parent commits 0ef29c2
  • Branches default

Comments (0)

Files changed (1)

File zicbee_lib/parser.py

View file
         assert isinstance(other, basestring)
         if not self.is_sensitive():
             other = other.lower()
-        self.substr.append(other)
+        self.substr.append(other.strip())
         return self
 
     def from_name(self, name=None):
             else:
                 infos['tag'] = None
             res.append(token)
-    # strips eventual pure text objects
-    for i, r in enumerate(res):
+
+    skip_count = 0
+    for i, r in reversed(list(enumerate(res))):
+        if skip_count:
+            skip_count -= 1
+            continue
         if isinstance(r, basestring):
-            res[i] = Node(r.strip())
+            if i > 1 and res[i-1] in OPERATORS and isinstance(res[i-2], Tag):
+                res[i-2] += res[i-1].name
+                res[i-2] += r
+                res[i-1:i+1] = []
+                skip_count += 2
+            else:
+                res[i] = Node(r.strip())
     # Inserts missing operators
     i = enumerate(res)
     prev = None
             i.next()
         prev_prev = prev
         prev = tok
+
     # converts tag:, ( ,str1 , op1, str2, op2, str3, )
     # to: ( ,tag-str1, op1, tag-str2, op2, tag-str3, )
     i = enumerate(res)
         res = [ARTIST.from_name() + val, OR, ALBUM.from_name() + val, OR, TITLE.from_name() + val]
     return res
 
-
 def tokens2python(tokens):
     """ Convert a list of tokens into python code
 
         print "-"*80
         print st
         print string2python(st)[0]
+    to("artist: (Bob marley and the wa or tricky)")
+    to("artist: bob marley and the waillers")
+    raise SystemExit()
+    to("artist: cool and the gang")
     to("artist: wax tailor")
     to("artist: wax tailor and ! title: foo")
-    raise SystemExit()
     to("artist: björk or artist:  foobar auto:")
     to("artist: (björk or foobar) auto:")
     to("auto: artist: (björk or foobar)")