Commits

Shu Zong Chen  committed 75e25de

Fixed grammar to solve semicolon problem

  • Participants
  • Parent commits 074a831

Comments (0)

Files changed (2)

File css_parser.py

         t.lexer.begin('define')
         return t 
 
-    def t_define_CLOSE_BRACE(self, t):
+    def t_ANY_CLOSE_BRACE(self, t):
         r'\}'
         t.lexer.begin('INITIAL')
         return t
     def t_value_SEMICOLON(self, t):
         r';'
         t.lexer.begin('define')
-        return t
+        #return t
 
     t_ANY_COMMA = r','
     t_ANY_ignore = ' \t\r\n'
         self.parser = ply.yacc.yacc(module=self)
         self.lexer = CSSLexer()
         
-    def p_stylesheet(self, p):
-        '''stylesheet : stylesheet ruleset
-                      | ruleset'''
-        if (len(p) == 3):
-            p[0] = p[1]
-            p[0].append(p[2])
-        else:
-            p[0] = []
-            p[0].append(p[1])
+    def p_stylesheet_r(self, p):
+        '''stylesheet : stylesheet ruleset'''
+        p[0] = p[1]
+        p[0].append(p[2])
+
+    def p_stylesheet_u(self, p):
+        '''stylesheet : ruleset'''
+        p[0] = [p[1],]
     
-    def p_ruleset(self, p):
-        'ruleset : selectors OPEN_BRACE rules CLOSE_BRACE'
+    def p_ruleset_u(self, p):
+        '''ruleset : selectors OPEN_BRACE rules CLOSE_BRACE'''
         p[0] = dict(selector=p[1], rules=p[3])
+
+    def p_ruleset_empty(self, p):
+        '''ruleset : selectors OPEN_BRACE empty CLOSE_BRACE'''
+        p[0] = dict(selector=p[1], rules=[])
     
-    def p_rules(self, p):
-        '''rules : rules rule
-                 | rule
-                 | empty'''
-        if (len(p) == 3):
-            p[0] = p[1]
-            p[0].append(p[2])
-        else:
-            p[0] = []
-            p[0].append(p[1])
+    def p_rules_r(self, p):
+        '''rules : rules rule'''
+        p[0] = p[1]
+        p[0].append(p[2])
+
+    def p_rules_u(self, p):
+        '''rules : rule'''
+        p[0] = [p[1],]
     
     def p_rule(self, p):
-        'rule : PROPERTY COLON values SEMICOLON'
+        '''rule : PROPERTY COLON values'''
         p[0] = (p[1], p[3])
     
     def p_values(self, p):
     def p_selectors(self, p):
         '''selectors : selectors COMMA SELECTOR
                      | selectors SELECTOR
-                     | SELECTOR
-                     | empty'''
+                     | SELECTOR'''
         if (len(p) == 4):
             p[0] = p[1] + ", " + p[3]
         elif (len(p) == 3):
             p[0] = p[1] + " " + p[2]
         else:
             p[0] = p[1]
-            
+    
     def p_empty(self, p):
-        'empty :'
-        p[0] = None
-    
+        '''empty :'''
+        pass
+
     def p_error(self, p):
-        print "Syntax error with",p.type
+        print "ERROR:"
+        if p is not None:
+            print p.value,
+        else:
+            print "*None*",
+            while 1:
+                tok = ply.yacc.token()
+                if not tok:
+                    break
+                if tok is not None:
+                    print tok.value
         ply.yacc.token()
-        ply.yacc.errok()
-        
+				
     
     def parse_string(self, data):
         result = self.parser.parse(data, lexer = self.lexer.lexer)
     with open('example.css', 'r') as fh:
         data = fh.read()
     cssparser = CSSParser()
-    cssparser.parse_string(data)
+    cssparser.parse_string(data)
 .very_tricky:before{
     content: 'blah \'\';;;a"sdl"f{}kj';
 }
+a:after, span a:visited{
+    padding: 20px;
+}
+
+#no_semicolon{
+    font-size: 10px;
+    font-weight: bold
+}