File indexing completed on 2024-12-08 04:20:20

0001 # -*- coding: utf-8 -*-
0002 import ply.lex as lex
0003 
0004 tokens = (
0005    'SYMBOL',
0006    'LPAREN',
0007    'RPAREN',
0008    'COMMENT',
0009    'STRING'
0010 )
0011 
0012 class CMakeLexerClass(object):
0013     def __init__(self, filename=None):
0014         self._filename = filename
0015 
0016     # states = ( ('command', 'exclusive'), ('arguments', 'exclusive') )
0017 
0018     tokens = tokens
0019 
0020     t_ANY_ignore = " \t"
0021     def t_SYMBOL(self, t):
0022         r'[^\\()\s#"]+(\\"[^()\s#"\\]*)*'
0023         t.value = t.value.replace(r'\"', '"')
0024         return t
0025 
0026     t_LPAREN = r'\('
0027     t_RPAREN = r'\)'
0028     t_ANY_COMMENT = r'\#.*'
0029 
0030     def t_STRING(self, t):
0031         r'"[^"\\]*(\\"[^"\\]*)*"'
0032         t.value  = t.value[1:-1].replace(r'\"', '"')
0033         t.lexer.lineno += t.value.count("\n")
0034         return t
0035 
0036     # Define a rule so we can track line numbers
0037     def t_ANY_newline(self,t):
0038         r'\n+'
0039         t.lexer.lineno += len(t.value)
0040 
0041     def t_ANY_error(self,t):
0042         if self._filename is not None:
0043             print("Illegal character %s at line %i, %s" % (repr(t.value[0]), t.lexer.lineno, self._filename))
0044         else:
0045             print("Illegal character %s at line %i." % (repr(t.value[0]),t.lexer.lineno))
0046         t.lexer.skip(1)
0047 
0048 def CMakeLexer(filename=None):
0049     return lex.lex(object=CMakeLexerClass(filename))