def _parse(self): file = open(self.model.filepath, 'r') cont = file.read() # Leading and trailing blank lines cause problems in the builtin # "compile" function, so we strip them. In order to provide correct # line numbers we store the offset - we use in case of an Exception... before = len(cont.split(os.linesep)) cont = cont.lstrip() after = len(cont.split(os.linesep)) cont = cont.rstrip() self.model._buffer = cont.split(os.linesep) offset = before - after file.close() self.model.readlines = list() self._extractencoding() self.model.bufstart = 0 self.model.bufend = len(self.model._buffer) self.model.bufoffset = offset try: self.model.astnode = ast.parse( os.linesep.join(self.model.buffer).strip(), self.model.filepath) except SyntaxError, e: # Since the python source files are being stripped we have to # add an offset to the line number we get thrown from compile() ex = exceptions.SyntaxError((e[0], \ (e[1][0], e[1][1] + offset, e[1][2], e[1][3]))) # <- don't read that raise ex
def __init__(self, instr_line): '''Parses a single line of text into structured instruction representation''' self.opcode = '' self.operands = [] self.text = '' self.raw = [] self.addr = int(instr_line[0:instr_line.find(':')], 0) self.addr = '0x{:0>16x}'.format( int(instr_line[0:instr_line.find(':')], 0)) # First, get the positions of the two pipes pipe_loc = [-1, -1] pipe_loc[0] = instr_line.find('|') pipe_loc[1] = instr_line.rfind('|') if pipe_loc[0] < 1 or pipe_loc[1] <= pipe_loc[0]: raise exceptions.SyntaxError( "Poorly formatted line: {0}".format(instr_line)) # Next, extract the byte values: for v in instr_line[instr_line.find(':') + 1:pipe_loc[0]].strip().split(' '): self.raw.append(int('0x' + v, 0)) # Next, extract the text describing the instruction self.text = instr_line[pipe_loc[1] + 1:].strip() self.opcode = self.text[0:self.text.find(' ')] self.operands = self.text[self.text.find(' '):].strip().split(', ')
def do(self, evt): if evt.line[0] == '{': return fieldMatch = self.structFieldRegex.match(evt.line) if fieldMatch is None: syntaxError = exceptions.SyntaxError() syntaxError.message = 'encounter syntax error when parse struct %s ,line %s' % ( self.spec.getName(), evt.line) raise syntaxError else: typeName = fieldMatch.group(1) fieldType = ParseUtil.getType(typeName, self.__messageSpecDict) if fieldType is None: raise CannotFindType(typeName + ' when parse ' + self.spec.getName()) self.spec.addField(fieldMatch.group(2), fieldType)
def __parseModule(self, path, module): protoFile = open(path, 'r') for line in protoFile: line = line.strip() if 0 != len(line) and self.__validLine(line): self.mainStateMachine.do(self.lineEvent(line)) protoFile.close() if self.mainStateMachine.getCurrentState( ) == self.messageState and self.messageState.getCurrentState( ) == self.initState: self.mainStateMachine.do(self.eofEvent) if self.mainStateMachine.getCurrentState( ) != self.initState or self.messageState.getCurrentState( ) != self.initState: syntaxError = exceptions.SyntaxError() syntaxError.message = 'uncompleted module' + module raise syntaxError
def do(self, evt): if evt.line[0] == '{': return valueMatch = self.enumValueRegex.match(evt.line) if valueMatch is None: syntaxError = exceptions.SyntaxError() syntaxError.message = 'encounter syntax error when parse enum %s ,line %s' % ( self.spec.getName(), evt.line) raise syntaxError enumValue = valueMatch.group(2) if enumValue is not None: if enumValue.startswith('0x') or enumValue.startswith('0X'): self.lastValue = int(enumValue, 16) else: self.lastValue = int(enumValue) self.spec.addValue(valueMatch.group(1), self.lastValue) self.lastValue = self.lastValue + 1
def error(self, p): if p: raise exceptions.SyntaxError(p.lineno) self.errok() raise exceptions.SyntaxError(p.lineno)
def error(self, t): raise exceptions.SyntaxError(self.lineno)