Commits

Shu Zong Chen committed 6ccc325

Saving work

Comments (0)

Files changed (4)

 
 _lr_method = 'LALR'
 
-_lr_signature = '\x14\x10\xdb\xc3C\xf0\x9c\x92f\x1dZh6+\xf4\xc1'
+_lr_signature = 'dK\xfb\x87\xcc\rj\x9f\xbc\x8bf\x91\xec\xce\x978'
     
-_lr_action_items = {'SEMICOLON':([3,5,8,10,17,19,],[-12,9,9,-11,-10,-9,]),'VALUE':([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,],[3,-4,-3,-12,3,10,-1,-2,10,3,-11,3,17,19,10,-7,3,-10,3,-9,17,-6,3,-8,]),'OPEN_BRACE':([3,5,8,10,12,17,19,],[-12,11,11,-11,18,-10,-9,]),'COMMA':([3,5,8,10,14,17,19,],[-12,13,13,-11,13,-10,-9,]),'CLOSE_BRACE':([1,2,3,6,7,10,11,14,15,16,17,19,21,22,23,],[-4,-3,-12,-1,-2,-11,15,-5,-7,21,-10,-9,-6,23,-8,]),'COLON':([3,5,8,10,14,17,19,],[-12,12,12,-11,20,-10,-9,]),'$end':([1,2,3,4,6,7,10,14,15,17,19,21,23,],[-4,-3,-12,0,-1,-2,-11,-5,-7,-10,-9,-6,-8,]),}
+_lr_action_items = {'SEMICOLON':([1,2,5,7,9,13,17,18,],[-11,-15,13,13,-13,-10,-12,-14,]),'VALUE':([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,],[2,-11,-15,2,9,-4,-3,-2,-1,-13,2,2,18,-10,2,-8,2,9,-14,-5,2,-9,2,-7,2,-6,]),'OPEN_BRACE':([2,4,9,11,17,18,],[-15,10,-13,16,22,-14,]),'COMMA':([2,4,9,17,18,],[-15,12,-13,12,-14,]),'CLOSE_BRACE':([1,2,5,6,7,8,9,10,13,14,15,16,17,18,19,20,21,23,24,25,],[-11,-15,-4,-3,-2,-1,-13,15,-10,19,-8,21,-12,-14,-5,23,-9,-7,25,-6,]),'COLON':([2,4,9,18,],[-15,11,-13,-14,]),'$end':([1,2,3,5,6,7,8,9,13,15,17,18,19,21,23,25,],[-11,-15,0,-4,-3,-2,-1,-13,-10,-8,-12,-14,-5,-9,-7,-6,]),}
 
 _lr_action = { }
 for _k, _v in _lr_action_items.items():
       _lr_action[_x][_k] = _y
 del _lr_action_items
 
-_lr_goto_items = {'definitions':([0,11,18,],[4,16,22,]),'defines':([0,4,9,11,16,18,22,],[5,8,14,5,8,5,8,]),'embedded_block':([0,4,11,16,18,22,],[2,7,2,7,2,7,]),'definition':([0,4,11,16,18,22,],[1,6,1,6,1,6,]),}
+_lr_goto_items = {'definitions':([0,3,10,14,16,20,22,24,],[5,7,5,7,5,7,5,7,]),'stylesheet':([0,10,16,22,],[3,14,20,24,]),'values':([0,3,10,11,14,16,20,22,24,],[4,4,4,17,4,4,4,4,4,]),'block':([0,3,10,14,16,20,22,24,],[6,8,6,8,6,8,6,8,]),'definition':([0,3,10,14,16,20,22,24,],[1,1,1,1,1,1,1,1,]),}
 
 _lr_goto = { }
 for _k, _v in _lr_goto_items.items():
        _lr_goto[_x][_k] = _y
 del _lr_goto_items
 _lr_productions = [
-  ("S' -> definitions","S'",1,None,None,None),
-  ('definitions -> definitions definition','definitions',2,'p_definitions','sass_parser.py',12),
-  ('definitions -> definitions embedded_block','definitions',2,'p_definitions','sass_parser.py',13),
-  ('definitions -> embedded_block','definitions',1,'p_definitions','sass_parser.py',14),
-  ('definitions -> definition','definitions',1,'p_definitions','sass_parser.py',15),
-  ('definition -> defines SEMICOLON defines','definition',3,'p_definition','sass_parser.py',24),
-  ('embedded_block -> defines OPEN_BRACE definitions CLOSE_BRACE','embedded_block',4,'p_embedded_block','sass_parser.py',28),
-  ('embedded_block -> defines OPEN_BRACE CLOSE_BRACE','embedded_block',3,'p_embedded_block','sass_parser.py',29),
-  ('embedded_block -> defines COLON OPEN_BRACE definitions CLOSE_BRACE','embedded_block',5,'p_embedded_block','sass_parser.py',30),
-  ('defines -> defines COMMA VALUE','defines',3,'p_defines','sass_parser.py',38),
-  ('defines -> defines COLON VALUE','defines',3,'p_defines','sass_parser.py',39),
-  ('defines -> defines VALUE','defines',2,'p_defines','sass_parser.py',40),
-  ('defines -> VALUE','defines',1,'p_defines','sass_parser.py',41),
-  ('empty -> C_COMMENT','empty',1,'p_empty','sass_parser.py',54),
-  ('empty -> INLINE_COMMENT','empty',1,'p_empty','sass_parser.py',55),
-  ('empty -> <empty>','empty',0,'p_empty','sass_parser.py',56),
+  ("S' -> stylesheet","S'",1,None,None,None),
+  ('stylesheet -> stylesheet block','stylesheet',2,'p_stylesheet','sass_parser.py',12),
+  ('stylesheet -> stylesheet definitions','stylesheet',2,'p_stylesheet','sass_parser.py',13),
+  ('stylesheet -> block','stylesheet',1,'p_stylesheet','sass_parser.py',14),
+  ('stylesheet -> definitions','stylesheet',1,'p_stylesheet','sass_parser.py',15),
+  ('block -> values OPEN_BRACE stylesheet CLOSE_BRACE','block',4,'p_block_normal','sass_parser.py',25),
+  ('block -> values COLON values OPEN_BRACE stylesheet CLOSE_BRACE','block',6,'p_block_colonand','sass_parser.py',31),
+  ('block -> values COLON OPEN_BRACE stylesheet CLOSE_BRACE','block',5,'p_block_colon','sass_parser.py',37),
+  ('block -> values OPEN_BRACE CLOSE_BRACE','block',3,'p_block_empty','sass_parser.py',43),
+  ('block -> values COLON OPEN_BRACE CLOSE_BRACE','block',4,'p_block_empty','sass_parser.py',44),
+  ('definitions -> definitions SEMICOLON','definitions',2,'p_definitions','sass_parser.py',50),
+  ('definitions -> definition','definitions',1,'p_definitions','sass_parser.py',51),
+  ('definition -> values COLON values','definition',3,'p_definition','sass_parser.py',57),
+  ('values -> values VALUE','values',2,'p_values_n','sass_parser.py',63),
+  ('values -> values COMMA VALUE','values',3,'p_values_comma','sass_parser.py',67),
+  ('values -> VALUE','values',1,'p_values_u','sass_parser.py',71),
 ]
+#main {
+  color: #a0ff00;
+  width: 97%;
+}
+#main p {
+  color: #b0ff00;
+  width: 97%;
+}
 #main p {
   color: #00ff00;
   width: 97%;
         'COMMA',
         'C_COMMENT',
         'INLINE_COMMENT',
+        'NEWLINE',
     )
     states = (
 #        ('define', 'exclusive'),
     def t_INLINE_COMMENT(self, t):
         r'\/\/[^\r\n]*'
     
-    t_SEMICOLON = r';'
+    def t_SEMICOLON(self, t):
+        r';'
+        return t
+
     t_COLON = r':'
     t_COMMA = r','
     t_OPEN_BRACE = r'\{'
     t_CLOSE_BRACE = r'\}'
     
-    #t_VALUE = '[^ \r\t\n\{\}:;,]+'
-    
-    t_ignore = '[^ \r\t\n]+'
+    #t_ignore = '[^ \r\t]+'
+    t_ignore = '[^ \r\n\t]+'
     
     t_VALUE = '([^ \r\t\n\{\};:\'"]+|\'(\\.|[^\'])*\'|"(\\.|[^"])*")+|\{\$[^\}]+\}'
-
+    
+    #t_NEWLINE = '[\n]'
+    
     def t_ANY_error(self, t):
         print "ERROR WITH ", t
         t.lexer.skip(1)
             tok = self.lexer.token()
             if not tok:
                 break;
-            print tok
+            if tok.type == "NEWLINE":
+                print ""
+            else:
+                print tok.type,
 
 if __name__ == "__main__":
     parser = SASSLexer()
 from sass_lex import SASSLexer
 import pprint
 
+class SASSBlock(object):
+    def __init__(self, value, children):
+        self.value = value
+        self.children = children
+
+    def __repr__(self):
+        return "##{value}##\n{children}".format(value=self.value, children=self.children)
+
+class SASSDefinition(object):
+    def __init__(self, property, value):
+        self.property = property
+        self.value = value
+
+    def __repr__(self):
+        return "{prop}:{value}".format(prop=self.property, value=self.value)
+
 class SASSParser(object):
     def __init__(self, **kwargs):
         self.tokens = SASSLexer.tokens
         self.parser = ply.yacc.yacc(module=self)
         self.lexer = SASSLexer()
     
-    def p_definitions(self, p):
-        '''definitions : definitions definition
-                       | definitions embedded_block
-                       | embedded_block
-                       | definition'''
+    def p_stylesheet(self, p):
+        '''stylesheet : stylesheet block
+                      | stylesheet definitions
+                      | block
+                      | definitions'''
         if len(p) == 3:
             p[0] = p[1]
             p[0].append(p[2])
         else:
-            p[0] = []
-            p[0].append(p[1])
+            p[0] = [p[1],]
+        print "Stylesheet"
+        pprint.pprint( p[0] )
+
+    def p_block_normal(self, p):
+        '''block : values OPEN_BRACE stylesheet CLOSE_BRACE'''
+        print "Block"
+        p[0] = SASSBlock(value=p[1], children=p[3])
+        print p[0]
+
+    def p_block_colonand(self, p):
+        '''block : values COLON values OPEN_BRACE stylesheet CLOSE_BRACE'''
+        print 'Block'
+        p[0] = SASSBlock(value=p[1] + p[2] + p[3], children=p[5])
+        print p[0]
+
+    def p_block_colon(self, p):
+        '''block : values COLON OPEN_BRACE stylesheet CLOSE_BRACE'''
+        print "Block"
+        p[0] = SASSBlock(value=p[1], children=p[4])
+        print p[0]
+
+    def p_block_empty(self, p):
+        '''block : values OPEN_BRACE CLOSE_BRACE
+                 | values COLON OPEN_BRACE CLOSE_BRACE'''
+        p[0] = SASSBlock(value=p[1], children=None)
+        print "Block"
+        print p[0]
     
+    def p_definitions(self, p):
+        '''definitions : definitions SEMICOLON
+                       | definition'''
+        print "Definitions"
+        p[0] = p[1]
+        print p[0]
+
     def p_definition(self, p):
-        '''definition : defines SEMICOLON defines'''
-        p[0] = (p[1], p[2], p[3])
-    
-    def p_embedded_block(self, p):
-        '''embedded_block : defines OPEN_BRACE definitions CLOSE_BRACE
-                          | defines OPEN_BRACE CLOSE_BRACE
-                          | defines COLON OPEN_BRACE definitions CLOSE_BRACE'''
-        if len(p) == 3:
-            p[0] = (p[1], p[2])
-        else:
-            p[0] = (p[1], p[3])
+        '''definition : values COLON values'''
+        p[0] = SASSDefinition(property=p[1], value=p[3])
+        print "Definition"
+        print p[0]
         
-        
-    def p_defines(self, p):
-        '''defines : defines COMMA VALUE
-                   | defines COLON VALUE
-                   | defines VALUE
-                   | VALUE'''
-        if len(p) == 4:
-            p[0] = p[1]
-            p[0].append(p[2])
-            p[0].append(p[3])
-        elif len(p) == 3:
-            p[0] = p[1]
-            p[0].append(p[2])
-        else:
-            p[0] = []
-            p[0].append(p[1])
-    
-    def p_empty(self, p):
-        '''empty : C_COMMENT
-                 | INLINE_COMMENT
-                 |'''
-        p[0] = None
-    
+    def p_values_n(self, p):
+        '''values : values VALUE'''
+        p[0] = p[1] + ' ' + p[2]
+
+    def p_values_comma(self, p):
+        '''values : values COMMA VALUE'''
+        p[0] = p[1] + ', ' + p[2]
+
+    def p_values_u(self, p):
+        '''values : VALUE'''
+        p[0] = p[1]
+                  
     def p_error(self, p):
         try:
             print "Syntax error with {type} ({value})".format(