BlockIt
|
00001 #................................................................................ 00002 # Copyright (c) 2009,2010,2011 David Car, david.car7@gmail.com 00003 # Copyright (c) 2009,2010,2011 Michael List, michael.list@gmail.com 00004 # 00005 # This program is free software; you can redistribute it and/or modify it under 00006 # the terms of the GNU General Public License as published by the Free Software 00007 # Foundation; either version 2 of the License, or (at your option) any later 00008 # version. 00009 # 00010 # This program is distributed in the hope that it will be useful, but WITHOUT 00011 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00012 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 00013 # 00014 # You should have received a copy of the GNU General Public License along with 00015 # this program; if not, write to the Free Software Foundation, Inc., 59 Temple 00016 # Place, Suite 330, Boston, MA 02111-1307 USA 00017 #.......................................................................... 00018 __doc__ = """ 00019 (C) 2010,2011 Michael List, michael.list@gmail.com 00020 (C) 2010,2011 David Car, david.car7@gmail.com 00021 """ 00022 00023 import external.pyparsing as pp 00024 00025 VALIDNAME = pp.Word(pp.alphas+'_', pp.alphanums+'_') 00026 00027 NUM = pp.Word(pp.nums) 00028 INTEGER = pp.Combine((pp.Optional('-')|pp.Optional('+'))+pp.Word(pp.nums)) 00029 DECIMAL_NUM = pp.Combine(INTEGER+'.'+pp.Optional(NUM)+pp.Suppress(pp.Optional('f')|pp.Optional('F'))) 00030 HEX_NUM = pp.Combine( pp.Literal( '0x' ) + pp.Word( pp.nums + 'abcdefABCDEF' )) 00031 ENGINEERING_NUM = pp.Suppress(pp.Optional('(')) + pp.Combine( (DECIMAL_NUM | INTEGER) + \ 00032 pp.oneOf( 'e d', caseless=True ) + pp.Optional( pp.oneOf('+ -') ) + NUM ) + \ 00033 pp.Suppress(pp.Optional(')')) 00034 BIN_NUM = pp.Word('01') 00035 00036 #NUM.setParseAction(lambda s,l,t: int(t[0])) 00037 #INTEGER.setParseAction(lambda s,l,t: int(t[0])) 00038 #DECIMAL_NUM.setParseAction(lambda s,l,t: float(t[0])) 00039 #ENGINEERING_NUM.setParseAction(lambda s,l,t: float(t[0])) 00040 00041 def isbin(instr): 00042 try: 00043 out = BIN_NUM.parseString(instr) 00044 return True 00045 except: 00046 return False 00047 00048 def isoct(instr): 00049 return False 00050 00051 def ishex(instr): 00052 try: 00053 out = HEX_NUM.parseString(instr) 00054 return True 00055 except: 00056 return False 00057 00058 UNIXFILE = pp.Group(pp.OneOrMore(pp.Literal("/") + pp.Word(pp.alphanums + '_-.'))) 00059 00060 QUOTESTRING = pp.QuotedString(quoteChar='"') 00061 00062 if __name__ == "__main__": 00063 00064 import unittest 00065 00066 #======================================================================= 00067 class TestBaseGrammar(unittest.TestCase): 00068 #======================================================================= 00069 00070 def setUp(self): pass 00071 def tearDown(self): pass 00072 00073 #------------------------------------------------------------------- 00074 def testIsHex(self): 00075 #------------------------------------------------------------------- 00076 """Testing the ishex function""" 00077 00078 self.assertTrue(ishex('0xff'), "Failed to parse hex number") 00079 self.assertFalse(ishex('33'), "Parsed non-hex number") 00080 self.assertFalse(ishex('ab'), "Parsed non-hex number") 00081 00082 00083 #------------------------------------------------------------------- 00084 def testIsBin(self): 00085 #------------------------------------------------------------------- 00086 """Testing the isbin function""" 00087 00088 self.assertTrue(isbin('0101010'), "Failed to parse bin number") 00089 self.assertFalse(isbin('33'), "Parsed non-bin number") 00090 self.assertFalse(isbin('ab'), "Parsed non-bin number") 00091 self.assertFalse(isbin('0x01'), "Parsed non-bin number") 00092 00093 00094 #------------------------------------------------------------------- 00095 def testFortranHexNum(self): 00096 #------------------------------------------------------------------- 00097 """Testing parsing of a Fortran hex number""" 00098 00099 try: 00100 output = HEX_NUM.parseString("z'0a'") 00101 except: 00102 self.fail("Failed to parse") 00103 00104 00105 #------------------------------------------------------------------- 00106 def testEngineeringNum(self): 00107 #------------------------------------------------------------------- 00108 """Testing parsing of an engineering number""" 00109 00110 try: 00111 output = ENGINEERING_NUM.parseString("-1.0e+04") 00112 except: 00113 self.fail("Failed to parse") 00114 00115 try: 00116 output = ENGINEERING_NUM.parseString("-1.0e-0") 00117 except: 00118 self.fail("Failed to parse") 00119 00120 00121 #------------------------------------------------------------------- 00122 def testUnixFile(self): 00123 #------------------------------------------------------------------- 00124 """Testing parsing of unix style file & path names""" 00125 try: 00126 output = UNIXFILE.parseString("/usr/local/cuda/include/driver_types.h") 00127 except: 00128 self.fail("Failed to parse") 00129 00130 00131 suite = unittest.TestLoader().loadTestsFromTestCase(TestBaseGrammar) 00132 unittest.TextTestRunner(verbosity=2).run(suite)