GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — pycoco/CodeGenerator.py at master · aixp/pycoco Expand file tree/ Copy pathCodeGenerator.pyMore file actions 162 lines (138 loc) · 4.74 KB / Copy pathCodeGenerator.pyFile metadata and controls162 lines (138 loc) · 4.74 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 import os import os.path class CodeGenerator( object ): CR = '\r' LF = '\n' TAB = '\t' EOF = -1 ls = "\n" indent_unit = ' ' sourceDir = None frameDir = None def __init__( self ): self._frameFile = None self._outputFile = None def openFiles( self, frameFileName, sourceFileName, outputFileName, backup=False ): if isinstance(frameFileName, str): frameFileName = [ frameFileName ] self._frameFile = None for frameName in frameFileName: fr = os.path.join( CodeGenerator.sourceDir, frameName ) if not os.path.exists( fr ): if CodeGenerator.frameDir is not None: fr = os.path.join( CodeGenerator.frameDir, frameName ) try: self._frameFile = file( fr, 'r' ) break except IOError: pass if self._frameFile is None: raise RuntimeError( '-- Compiler Error: Cannot open ' + frameFileName[0] ) try: fn = os.path.join( CodeGenerator.sourceDir, outputFileName ) fn = str(fn) if backup and os.path.exists( fn ): if os.path.exists( fn + '.old' ): os.remove( fn + '.old' ) os.rename( fn, fn + '.old' ) self._outputFile = file( fn, 'w' ) except: raise RuntimeError( '-- Compiler Error: Cannot create ' + outputFileName[0] + '.py' ) def close( self ): self._frameFile.close( ) self._outputFile.close( ) def CopyFramePart( self, stop ): assert isinstance( stop, str ) last = 0 startCh = stop[0] endOfStopString = len(stop) - 1 ch = self.frameRead( ) while ch != CodeGenerator.EOF: if ch == startCh: i = 0 if i == endOfStopString: return # stop[0..i] found ch = self.frameRead( ) i += 1 while ch == stop[i]: if i == endOfStopString: return # stop[0..i] found ch = self.frameRead( ) i += 1 # stop[0..i-1] found; continue with last read character self._outputFile.write( stop[0:i] ) elif ch == CodeGenerator.LF: if last != CodeGenerator.CR: self._outputFile.write( '\n' ) last = ch ch = self.frameRead( ) elif ch == CodeGenerator.CR: self._outputFile.write( '\n' ) last = ch ch = self.frameRead() else: self._outputFile.write( str(ch) ) last = ch ch = self.frameRead( ) raise RuntimeError( ' -- Compiler Error: incomplete or corrupt parser frame file' ) def CopySourcePart( self, pos, indent, forceOutput=False ): if pos is None: if forceOutput: self.Indent( indent ) self._outputFile.write( 'pass' ) return code = pos.getSubstring( ) col = pos.col - 1 lines = code.splitlines( True ) pos = 0 if (lines is None) or (len(lines) == 0): if forceOutput: self.Indent( indent ) self._outputFile.write( 'pass' ) return while lines[0][pos] in ( ' ', '\t' ): col += 1 newLineList = [ lines[0] ] lines.pop(0) if col < 0: col = 0 for line in lines: newLineList.append( line[ col : ] ) for line in newLineList: self.Indent( indent ) self._outputFile.write( line ) if indent > 0: self._outputFile.write( '\n' ) return def frameRead( self ): try: return self._frameFile.read( 1 ) except IOError: #raise RuntimeError('-- Compiler Error: error reading Parser.frame') return ParserGen.EOF def Indent( self, n ): assert isinstance( n, int ) self._outputFile.write( CodeGenerator.indent_unit * n ) def Ch(ch): if isinstance(self, ch, int): ch = str( ch ) if ch < ' ' or ch >= str(127) or ch == '\'' or ch == '\\': return ch else: return "ord('" + ch + "')" def ReportCh(self, ch): if isinstance(ch, (str,unicode)): ch = ord(ch) if (ch < ord(' ') or ch >= 127 or ch == ord('\'') or ch == ord('\\')): return str(ch) else: return ''.join( [ "'", chr(ch), "'" ] ) def ChCond(self, ch, relOpStr='=='): if isinstance(ch, (str,unicode)): ch = ord(ch) if (ch < ord(' ') or ch >= 127 or ch == ord('\'') or ch == ord('\\')): return ''.join( [ 'ord(self.ch) ', relOpStr, " ", str(ch) ] ) else: return ''.join( [ 'self.ch ', relOpStr, " '", chr(ch), "'" ] ) def write( self, aString ): self._outputFile.write( aString ) |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |