Source

sass_parser / sass_lex.py

Full commit
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',
        'NEWLINE',
    )
    states = (
#        ('define', 'exclusive'),
#        ('value', 'exclusive'),
    )
    def t_C_COMMENT(self, t):
        r'\/\*[^*]*\*+([^/*][^*]*\*+)*\/'
        
    def t_INLINE_COMMENT(self, t):
        r'\/\/[^\r\n]*'
    
    def t_SEMICOLON(self, t):
        r';'
        return t

    t_COLON = r':'
    t_COMMA = r','
    t_OPEN_BRACE = r'\{'
    t_CLOSE_BRACE = r'\}'
    
    #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)

    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;
            if tok.type == "NEWLINE":
                print ""
            else:
                print tok.type,

if __name__ == "__main__":
    parser = SASSLexer()
    with open('sample.scss') as fh:
        parser.run_parser(fh)