Commits

Shu Zong Chen  committed 9b9ff03

First order of business

  • Participants

Comments (0)

Files changed (4)

+
+# parsetab.py
+# This file is automatically generated. Do not edit.
+_tabversion = '3.2'
+
+_lr_method = 'LALR'
+
+_lr_signature = '\\\x8f\xc3\xa4\xe1\x1b(>MvB\x06\xc6(\xa5\r'
+    
+_lr_action_items = {'SEMICOLON':([2,7,12,14,],[-8,-7,-6,16,]),'VALUE':([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,],[2,-2,-8,2,7,-1,-3,-7,2,12,2,2,-6,-4,7,-9,-5,]),'OPEN_BRACE':([2,4,7,10,12,],[-8,8,-7,8,-6,]),'COLON':([2,4,7,12,],[-8,10,-7,-6,]),'CLOSE_BRACE':([1,5,6,11,13,15,16,],[-2,-1,-3,15,-4,-9,-5,]),'COMMA':([2,4,7,12,14,],[-8,9,-7,-6,9,]),'$end':([1,3,5,6,13,15,16,],[-2,0,-1,-3,-4,-9,-5,]),}
+
+_lr_action = { }
+for _k, _v in _lr_action_items.items():
+   for _x,_y in zip(_v[0],_v[1]):
+      if not _x in _lr_action:  _lr_action[_x] = { }
+      _lr_action[_x][_k] = _y
+del _lr_action_items
+
+_lr_goto_items = {'ruleblock':([4,10,],[6,13,]),'definitions':([0,8,],[3,11,]),'defines':([0,3,8,10,11,],[4,4,4,14,4,]),'definition':([0,3,8,11,],[1,5,1,5,]),}
+
+_lr_goto = { }
+for _k, _v in _lr_goto_items.items():
+   for _x,_y in zip(_v[0],_v[1]):
+       if not _x in _lr_goto: _lr_goto[_x] = { }
+       _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 -> definition','definitions',1,'p_definitions','sass_parser.py',13),
+  ('definition -> defines ruleblock','definition',2,'p_definition','sass_parser.py',22),
+  ('definition -> defines COLON ruleblock','definition',3,'p_definition','sass_parser.py',23),
+  ('definition -> defines COLON defines SEMICOLON','definition',4,'p_definition','sass_parser.py',24),
+  ('defines -> defines COMMA VALUE','defines',3,'p_defines','sass_parser.py',31),
+  ('defines -> defines VALUE','defines',2,'p_defines','sass_parser.py',32),
+  ('defines -> VALUE','defines',1,'p_defines','sass_parser.py',33),
+  ('ruleblock -> OPEN_BRACE definitions CLOSE_BRACE','ruleblock',3,'p_ruleblock','sass_parser.py',46),
+  ('empty -> C_COMMENT','empty',1,'p_empty','sass_parser.py',50),
+  ('empty -> INLINE_COMMENT','empty',1,'p_empty','sass_parser.py',51),
+  ('empty -> <empty>','empty',0,'p_empty','sass_parser.py',52),
+]
+#main p {
+  color: #00ff00;
+  width: 97%;
+
+  .redbox {
+    background-color: #ff0000;
+    color: #000000;
+  }
+}
+#main {
+  width: 97%;
+
+  p, div {
+    font-size: 2em;
+    a { font-weight: bold; }
+  }
+
+  pre { font-size: 3em; }
+}
+a {
+  font-weight: bold;
+  text-decoration: none;
+  &:hover { text-decoration: underline; }
+  body.firefox & { font-weight: normal; }
+}
+#main {
+  color: black;
+  a {
+    font-weight: bold;
+    &:hover { color: red; }
+  }
+}
+.funky {
+  font: {
+    family: fantasy;
+    size: 30em;
+    weight: bold;
+  }
+}
+.funky {
+  font: 2px/3px {
+    family: fantasy;
+    size: 30em;
+    weight: bold;
+  }
+}
+/* This comment is
+ * several lines long.
+ * since it uses the CSS comment syntax,
+ * it will appear in the CSS output. */
+body { color: black; }
+
+// These comments are only one line long each.
+// They won't appear in the CSS output,
+// since they use the single-line comment syntax.
+a { color: green; }
+$width: 5em;
+#main {
+  width: $width;
+}
+@mixin firefox-message($selector) {
+  body.firefox #{$selector}:before {
+    content: "Hi, Firefox users!"; } }
+
+@include firefox-message(".header");
+p {
+  width: 1in + 8pt;
+}
+p {
+  font: 10px/8px;             // Plain CSS, no division
+  $width: 1000px;
+  width: $width/2;            // Uses a variable, does division
+  height: (500px/2);          // Uses parentheses, does division
+  margin-left: 5px + 8px/2px; // Uses +, does division
+}
+p {
+  $font-size: 12px;
+  $line-height: 30px;
+  font: #{$font-size}/#{$line-height};
+}
+p {
+  color: #010203 + #040506;
+}
+p {
+  color: #010203 * 2;
+}
+p {
+  color: rgba(255, 0, 0, 0.75) + rgba(0, 255, 0, 0.75);
+}
+$translucent-red: rgba(255, 0, 0, 0.5);
+p {
+  color: opacify($translucent-red, 0.8);
+  background-color: transparentize($translucent-red, 50%);
+}
+p {
+  cursor: e + -resize;
+}
+p:before {
+  content: "Foo " + Bar;
+  font-family: sans- + "serif"; }
+p {
+  margin: 3px + 4px auto;
+}
+p:before {
+  content: "I ate #{5 + 10} pies!"; }
+p {
+  width: 1em + (2em * 3);
+}
+p {
+  color: hsl(0, 100%, 0.5);
+}
+$name: foo;
+$attr: border;
+p.#{$name} { #{$attr}-color: blue }
+p {
+  $font-size: 12px;
+  $line-height: 30px;
+  font: #{$font-size}/#{$line-height};
+}
+$content: "First content";
+$content: "Second content?" !default;
+$new_content: "First time reference" !default;
+
+#main {
+  content: $content;
+  new-content: $new_content;
+}
+@import "foo.scss";
+.error {
+  border: 1px #f00;
+  background-color: #fdd;
+}
+.seriousError {
+  @extend .error;
+  border-width: 3px;
+}
+
+import ply.lex
+
+class SASSLexer(object):
+    def __init__(self, **kwargs):
+        self.lexer = ply.lex.lex(object=self, **kwargs)
+        
+    tokens = (
+        'OPEN_BRACE',
+        'CLOSE_BRACE',
+        'SEMICOLON',
+        'COLON',
+        'VALUE',
+        'COMMA',
+        'C_COMMENT',
+        'INLINE_COMMENT',
+    )
+    states = (
+#        ('define', 'exclusive'),
+#        ('value', 'exclusive'),
+    )
+    def t_C_COMMENT(self, t):
+        r'\/\*[^*]*\*+([^/*][^*]*\*+)*\/'
+        
+    def t_INLINE_COMMENT(self, t):
+        r'\/\/[^\r\n]*'
+    
+    def t_OPEN_BRACE(self, t):
+        r'\{'
+        #t.lexer.begin('define')
+        return t 
+
+    def t_CLOSE_BRACE(self, t):
+        r'\}'
+        return t
+    
+    t_SEMICOLON = r';'
+    t_COLON = r':'
+    t_COMMA = r','
+    
+    t_VALUE = '[^ \r\t\n\{\}:;,]+'
+    
+    t_ignore = '[^ \r\t\n]+'
+    
+#    t_value_VALUE = r'([^ \r\t\n\{\};:\'"]+|\'(\\.|[^\'])*\'|"(\\.|[^"])*")+'
+
+    def t_ANY_error(self, t):
+        print "ERROR WITH ", t
+        t.lexer.skip(1)
+
+    def parse(self, fh):
+        data = fh.read()
+        self.lexer.input(data)
+
+    def run_parser(self, fh):
+        self.parse(fh)
+        while True:
+            tok = self.lexer.token()
+            if not tok:
+                break;
+            print tok
+
+if __name__ == "__main__":
+    parser = SASSLexer()
+    with open('sample.scss') as fh:
+        parser.run_parser(fh)
+

File sass_parser.py

+import ply.yacc
+from sass_lex import SASSLexer
+import pprint
+
+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
+                       | definition'''
+        if len(p) == 3:
+            p[0] = p[1]
+            p[0].append(p[2])
+        else:
+            p[0] = []
+            p[0].append(p[1])
+        
+    def p_definition(self, p):
+        '''definition : defines ruleblock
+                      | defines COLON ruleblock
+                      | defines COLON defines SEMICOLON'''
+        if len(p) == 3:
+            p[0] = (p[1], p[2])
+        else:
+            p[0] = (p[1], p[3])
+    
+    def p_defines(self, p):
+        '''defines : defines COMMA 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_ruleblock(self, p):
+        '''ruleblock : OPEN_BRACE definitions CLOSE_BRACE'''
+        p[0] = p[2] 
+        
+    def p_empty(self, p):
+        '''empty : C_COMMENT
+                 | INLINE_COMMENT
+                 |'''
+        p[0] = None
+    
+    def p_error(self, p):
+        try:
+            print "Syntax error with {type} ({value})".format(
+                type=p.type,
+                value=p.value
+            )
+            while 1:
+                tok = ply.yacc.token()
+                if not tok:
+                        break
+                print tok.value
+        except AttributeError:
+            pass
+        #print "Syntax error."
+        #ply.yacc.token()
+        ply.yacc.errok()
+       
+    def parse_string(self, data):
+        result = self.parser.parse(data, lexer = self.lexer.lexer)
+        for i in result:
+            pprint.pprint(i)
+        #print result
+
+if __name__ == "__main__":
+    with open('sample.scss', 'r') as fh:
+        data = fh.read()
+    sassparser = SASSParser()
+    sassparser.parse_string(data)