BlockIt
|
00001 #................................................................................ 00002 # The MIT License 00003 # 00004 # Copyright (c) 2009-2010 David Car, david.car7@gmail.com 00005 # Copyright (c) 2009-2010 Michael List, michael.list@gmail.com 00006 # 00007 # Permission is hereby granted, free of charge, to any person obtaining a copy 00008 # of this software and associated documentation files (the "Software"), to deal 00009 # in the Software without restriction, including without limitation the rights 00010 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00011 # copies of the Software, and to permit persons to whom the Software is 00012 # furnished to do so, subject to the following conditions: 00013 # 00014 # The above copyright notice and this permission notice shall be included in all 00015 # copies or substantial portions of the Software. 00016 # 00017 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00018 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00020 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00021 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00022 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00023 # SOFTWARE. 00024 #................................................................................ 00025 00026 __doc__ = """ 00027 (C) 2010 Michael List, michael.list@gmail.com 00028 (C) 2010 David Car, david.car7@gmail.com 00029 """ 00030 00031 from blockit.parsers import pyp as pp 00032 import blockit.basegrammar as gram 00033 00034 00035 #GENERALLINE = pp.Group( pp.OneOrMore( pp.Word( pp.printables ) ) ) 00036 00037 CPPCOMMENT = pp.Literal("//") + pp.restOfLine 00038 CCOMMENT = pp.cStyleComment 00039 FCOMMENT = pp.Literal( "!" ) + pp.restOfLine 00040 00041 LINES = pp.Combine(pp.OneOrMore( pp.Word( pp.printables ) )) + pp.restOfLine 00042 00043 POUND = pp.Literal( '#' ).suppress() 00044 00045 # num file num num ... 00046 POUNDLINE = POUND + pp.restOfLine 00047 POUNDNUMFILE = pp.lineStart + POUND + gram.NUM + pp.QuotedString("\"", multiline=False) + pp.Group(pp.restOfLine) + pp.lineEnd 00048 POUNDNUMFILE = pp.lineStart + POUND + gram.NUM + pp.QuotedString("\"", multiline=False) + pp.ZeroOrMore(gram.NUM) 00049 00050 #if 00051 IF = pp.CaselessKeyword('if') 00052 POUNDIF = pp.LineStart() + pp.Group( POUND + IF + pp.Group( pp.restOfLine ) ) 00053 POUNDIF.ignore( CPPCOMMENT ) 00054 POUNDIF.ignore( CCOMMENT ) 00055 POUNDIF.ignore( FCOMMENT ) 00056 00057 #else 00058 ELSE = pp.CaselessKeyword('else') 00059 POUNDELSE = pp.LineStart() + POUND + ELSE 00060 POUNDELSE.ignore( CPPCOMMENT ) 00061 POUNDELSE.ignore( CCOMMENT ) 00062 POUNDELSE.ignore( FCOMMENT ) 00063 00064 #endif 00065 ENDIF = pp.CaselessKeyword('endif') 00066 POUNDENDIF = pp.LineStart() + pp.Group( POUND + ENDIF ) 00067 POUNDENDIF.ignore( CPPCOMMENT ) 00068 POUNDENDIF.ignore( CCOMMENT ) 00069 POUNDENDIF.ignore( FCOMMENT ) 00070 00071 #define 00072 DEFINE = pp.Keyword('define') 00073 POUNDDEFINE = pp.LineStart() + POUND + DEFINE 00074 POUNDDEFINE = POUND + DEFINE 00075 00076 #ifdef 00077 IFDEF = pp.CaselessKeyword('ifdef') 00078 POUNDIFDEF = pp.LineStart() + pp.Group( POUND + IFDEF + pp.Group( pp.restOfLine ) ) 00079 POUNDIFDEF.ignore( CPPCOMMENT ) 00080 POUNDIFDEF.ignore( CCOMMENT ) 00081 POUNDIFDEF.ignore( FCOMMENT ) 00082 00083 #ifndef 00084 IFNDEF = pp.CaselessKeyword('ifndef') 00085 POUNDIFNDEF = pp.LineStart() + pp.Group( POUND + IFNDEF + pp.Group( pp.restOfLine ) ) 00086 POUNDIFNDEF.ignore( CPPCOMMENT ) 00087 POUNDIFNDEF.ignore( CCOMMENT ) 00088 POUNDIFNDEF.ignore( FCOMMENT ) 00089 00090 #undef 00091 UNDEF = pp.CaselessKeyword('undef') 00092 POUNDUNDEF = pp.LineStart() + pp.Group( POUND + UNDEF + pp.Group( pp.restOfLine ) ) 00093 POUNDUNDEF.ignore( CPPCOMMENT ) 00094 POUNDUNDEF.ignore( CCOMMENT ) 00095 POUNDUNDEF.ignore( FCOMMENT ) 00096 00097 # 00098 # #define constant value 00099 # 00100 CONSTANTDEFINE = pp.Suppress( POUNDDEFINE ) + gram.VALIDNAME + \ 00101 ( gram.VALIDNAME | gram.HEX_NUM | gram.ENGINEERING_NUM | gram.DECIMAL_NUM | gram.INTEGER | gram.NUM ) 00102 CONSTANTDEFINE.ignore( CPPCOMMENT ) 00103 CONSTANTDEFINE.ignore( CCOMMENT ) 00104 CONSTANTDEFINE.ignore( FCOMMENT ) 00105 00106 # 00107 # #if ... #else ... #endif 00108 # 00109 CHUNK = pp.Group( POUNDIF | POUNDIFDEF | POUNDIFNDEF ) + \ 00110 pp.Group( pp.ZeroOrMore( LINES ) ) + pp.Optional(POUNDELSE) + pp.Group( pp.ZeroOrMore( LINES ) ) + \ 00111 pp.Group( POUNDENDIF ) 00112 CHUNK.ignore( CPPCOMMENT ) 00113 CHUNK.ignore( CCOMMENT ) 00114 CHUNK.ignore( FCOMMENT ) 00115 00116 CHUNK2 = pp.Group( POUNDIFDEF + pp.Group( pp.ZeroOrMore( LINES ) ) + pp.Optional( POUNDELSE ) + \ 00117 pp.Group( pp.ZeroOrMore( LINES ) ) + POUNDENDIF ) 00118 CHUNK2.ignore( CPPCOMMENT ) 00119 CHUNK2.ignore( CCOMMENT ) 00120 CHUNK2.ignore( FCOMMENT ) 00121 00122 def _parseCONSTANTDEFINE( ln ): 00123 try: 00124 for match,start,end in CONSTANTDEFINE.scanString( ln ): 00125 print match 00126 except pp.ParseException, pe: 00127 print pe.msg 00128 00129 if __name__ == "__main__": 00130 _parseCONSTANTDEFINE('#define apple 4') 00131 _parseCONSTANTDEFINE('/* just a comment */') 00132 _parseCONSTANTDEFINE('# define hello 1 ! fcomment') 00133 _parseCONSTANTDEFINE('#\tdefine hello 1 /* ccomment */') 00134 _parseCONSTANTDEFINE('#\tdefine hello -3.46e-01') 00135 _parseCONSTANTDEFINE(' #define cuMemcpyHtoD cuMemcpyHtoD_v2') 00136 _parseCONSTANTDEFINE(' #define cuDeviceTotalMem cuDeviceTotalMem_v2') 00137 _parseCONSTANTDEFINE(''' 00138 #\tdefine hellofloat 3.32e-01 /* comment */ 00139 #define hellohex 0x01 00140 #define helloint -42 00141 ''') 00142 00143 tmpstr = """ 00144 #ifdef NEVERDEFINE 00145 do something 00146 and more 00147 #else /* random comment */ 00148 do other 00149 do do 00150 #endif 00151 00152 #ifndef NEVERDEFINE 00153 unsigned int x,y,z; 00154 #endif 00155 """ 00156 print "POUNDIFDEF ------------------" 00157 for match,start,end in POUNDIFDEF.scanString( tmpstr ): 00158 print match 00159 print "POUNDIFNDEF ------------------" 00160 for match,start,end in POUNDIFNDEF.scanString( tmpstr ): 00161 print match 00162 print "POUNDELSE ------------------" 00163 for match,start,end in POUNDELSE.scanString( tmpstr ): 00164 print match 00165 print "POUNDENDIF ------------------" 00166 for match,start,end in POUNDENDIF.scanString( tmpstr ): 00167 print match 00168 print "CHUNK -----------------------" 00169 for match,start,end in CHUNK.scanString( tmpstr ): 00170 print match 00171 print "CHUNK2 -----------------------" 00172 for match,start,end in CHUNK2.scanString( tmpstr ): 00173 print match 00174 print "LINES -----------------------" 00175 for match,start,end in LINES.scanString( tmpstr ): 00176 print match 00177 00178 tmpstr = """ 00179 # 145 "<built-in>" 00180 # 438 "/usr/local/cuda/include/driver_types.h" 1 2 4 00181 cudaSuccess = 0, 00182 # 452 "/usr/local/cuda/include/driver_types.h" 00183 cudaErrorReturnFailure, 00184 """ 00185 print "POUNDNUMLINES ---------------" 00186 for match,start,end in POUNDNUMFILE.scanString( tmpstr ): 00187 print match