소스 검색

Fixed some parser features:

- Inline comments are no longer supported.
- Multiline comment detection now actually works.
- Emtpy property definitions do not yield errors anymore.
Taddeus Kroes 13 년 전
부모
커밋
efb00ab5df
1개의 변경된 파일17개의 추가작업 그리고 18개의 파일을 삭제
  1. 17 18
      parse.py

+ 17 - 18
parse.py

@@ -19,20 +19,16 @@ def parse_groups(css):
     current_group = root_group = []
     groups = [(None, root_group)]
     selectors = None
-    multiline_comment = inline_comment = False
+    comment = False
 
     try:
         for c in css:
             char = c
 
-            if multiline_comment:
-                # Multiline comment end?
+            if comment:
+                # Comment end?
                 if c == '/' and prev_char == '*':
-                    multiline_comment = False
-            elif inline_comment:
-                # Inline comment end?
-                if c == '\n':
-                    inline_comment = False
+                    comment = False
             elif c == '{':
                 # Block start
                 if selectors is not None:
@@ -56,22 +52,25 @@ def parse_groups(css):
             elif c == ';':
                 # Property definition
                 assert selectors is not None
-                name, value = map(str.strip, stack.split(':', 1))
-                assert '\n' not in name
-                properties.append((name, value))
-                stack = ''
+
+                if stack.strip():
+                    parts = stack.split(':', 1)
+                    assert len(parts) == 2
+                    name, value = map(str.strip, parts)
+                    assert '\n' not in name
+                    properties.append((name, value))
+                    stack = ''
             elif c == '*' and prev_char == '/':
-                # Multiline comment start
-                multiline_comment = True
-            elif c == '/' and prev_char == '/':
-                # Inline comment start
-                inline_comment = True
+                # Comment start
+                comment = True
+                stack = stack[:-1]
             else:
                 if c == '\n':
                     lineno += 1
 
                 stack += c
-                prev_char = c
+
+            prev_char = c
     except AssertionError:
         raise Exception('unexpected \'%c\' on line %d' % (char, lineno))