def _parse_name(self, context): if len(context['command']) <= context['pos']: raise SyntaxException() if context['command'][context['pos']]['name'] != 'word': raise SyntaxException() context['name'] = context['command'][context['pos']]['body'] context['pos'] += 1
def _parse_len_short(self, context): if len(context['command']) <= context['pos']: return if context['command'][context['pos']]['name'] != 'solid_lb': return context['pos'] += 1 context['length_node'], context['pos'] = NameParser( context['command'], context['pos']).parse() if len(context['command']) <= context['pos']: raise SyntaxException() if context['command'][context['pos']]['name'] != 'close_rb': raise SyntaxException() context['pos'] += 1 context['length_complete'] = True
def _parse_len(self, context): if len(context['command']) <= context['pos']: return token = context['command'][context['pos']] if token['name'] != 'word' or token['body'] != 'LENGTH': return if context['length_complete']: raise SyntaxException() context['pos'] += 1 if len(context['command']) <= context['pos']: raise SyntaxException() context['length_node'], context['pos'] = NameParser( context['command'], context['pos']).parse() context['length_complete'] = True
def _parse_decimals(self, context): if len(context['command']) <= context['pos']: return token = context['command'][context['pos']] if token['name'] != 'word' or token['body'] != 'DECIMALS': return if context['decimals_complete']: raise SyntaxException() context['pos'] += 1 if len(context['command']) <= context['pos']: raise SyntaxException() context['decimals_node'], context['pos'] = NameParser( context['command'], context['pos']).parse() context['decimals_complete'] = True
def parse(self, in_command): if in_command[0]['name'] != 'word' or in_command[0]['body'] != 'DATA': raise BadReceiverError() if len(in_command) > 2 and in_command[1][ 'body'] == 'BEGIN' and in_command[2]['body'] == 'OF': if len(in_command) != 4: raise SyntaxException() pass else: context = dict(command=in_command, pos=1, name='', length_complete=False, decimals_complete=False, value_complete=False, length_node=None, decimals_node=None, value_node=None, type_node=None, like=False, type_complete=False, is_table=False, tabkind='', is_range=False, read_only=False) self._parse_name(context) self._parse_len_short(context) self._parse_type(context) if context['is_table']: pass elif context['is_range']: pass else: self._parse_len(context) self._parse_decimals(context) self._parse_value(context) self._parse_is_initial(context) self._parse_read_only(context) node = VariableDefinitionNode( name=context['name'], reftype=VariableDefinitionNode.LIKE if context['like'] else VariableDefinitionNode.TYPE, length=context['length_node'], decimals=context['decimals_node'], value=context['value_node'], typename=context['type_node']) if len(context['command']) > context['pos']: raise SyntaxException() return node
def __init__(self, in_value, in_type): LeafNode.__init__(self) self._node_name = self.NAME if in_type == self.IN_WORD: try: self.value = int(in_value) self.type = self.INT except: raise SyntaxException() elif in_type == self.IN_CHARVAL: if type(in_value) != str and type(in_value) != unicode: raise SeriousException() try: self.value = float(in_value) self.type = self.FLOAT except: self.value = in_value.rstrip() self.type = self.CHARVAL elif in_type == self.IN_STRING: if type(in_value) != str and type(in_value) != unicode: raise SeriousException() self.value = in_value self.type = self.STRING else: raise SeriousException()
def get_var_metadata(self, name): if not self._variables.has_key(name): raise SyntaxException() metaname = self._variables.get(name).metaname if not self._metadatas.has_key(metaname): raise SeriousException() return self._metadatas[name]
def run(self, in_syntax_node, in_context, in_code_execution): if in_syntax_node.get_name() == MathUnaryNode.NAME: var1, _ = in_code_execution.get_math_value( in_syntax_node.get_nodes()[0]) if in_syntax_node.type == MathUnaryNode.MINUS: result = -var1.value elif in_syntax_node.type == MathUnaryNode.PLUS: result = var1.value else: raise SeriousException() elif in_syntax_node.get_name() == MathBinaryNode.NAME: var1, _ = in_code_execution.get_math_value( in_syntax_node.get_nodes()[0]) var2, _ = in_code_execution.get_math_value( in_syntax_node.get_nodes()[1]) if var1.metaname != var2.metaname: raise SyntaxException() #todo add type conversion if in_syntax_node.type == MathBinaryNode.PLUS: result = var1.value + var2.value elif in_syntax_node.type == MathBinaryNode.MINUS: result = var1.value - var2.value elif in_syntax_node.type == MathBinaryNode.DIV: result = var1.value / var2.value elif in_syntax_node.type == MathBinaryNode.MULT: result = var1.value * var2.value elif in_syntax_node.type == MathBinaryNode.INT_DIV: result = var1.value // var2.value elif in_syntax_node.type == MathBinaryNode.INT_MOD: result = var1.value % var2.value else: raise SeriousException() else: return SeriousException() return (RuntimeVariable(var1.metaname, result), runtime.type.VALUE)
def _parse_value(self, context): if len(context['command']) <= context['pos']: return token = context['command'][context['pos']] if token['name'] != 'word' or token['body'] != 'VALUE': return if context['value_complete']: raise SyntaxException() context['pos'] += 1 if len(context['command']) <= context['pos']: raise SyntaxException() expr_source = ExpressionSource(context['command'], context['pos']) context['value_node'] = ExpressionParser(expr_source, False).get_syntax_node() context['pos'] = expr_source.get_index() context['value_complete'] = True
def _parse_type(self, context): if len(context['command']) <= context['pos']: return token = context['command'][context['pos']] if token['name'] != 'word': return if token['body'] == 'TYPE': pass #ok elif token['body'] == 'LIKE': context['like'] = True else: return context['pos'] += 1 if len(context['command']) <= context['pos']: raise SyntaxException() if len(context['command']) > context['pos'] + 1: token = context['command'][context['pos']] token2 = context['command'][context['pos'] + 1] if token['name'] == 'word' and token2['name'] == 'word' and token2[ 'body'] == 'OF': if token['body'] == 'RANGE': context['is_range'] = True elif token['body'] == 'TABLE': context['is_table'] = True if not context['is_table'] and not context['is_range'] and len( context['command']) > context['pos'] + 2: token3 = context['command'][context['pos'] + 2] if (token['name'] == 'word' and token2['name'] == 'word' and token3['name'] == 'word' and token2['body'] == 'TABLE' and token3['body'] == 'OF'): context['is_table'] = True context['typekind'] = token['body'] context['type_node'], context['pos'] = NameParser( context['command'], context['pos']).parse() context['type_complete'] = True
def parse(self, in_command): if (in_command[0]['name'] != 'word' or in_command[0]['body'] != 'WRITE'): raise BadReceiverError() if len(in_command) <= 1: raise SyntaxException() context = dict(new_line=False, text_node=None) token_id = 1 token_id = self.__read_slash(token_id, in_command, context) token_id = self.__read_text(token_id, in_command, context) if (token_id < len(in_command) or context['text_node'] == None): raise SyntaxException() node = WriteNode( context['new_line'], context['text_node'], ) return node
def _parse_is_initial(self, context): if len(context['command']) <= context['pos'] + 1: return token = context['command'][context['pos']] token2 = context['command'][context['pos'] + 1] if (token['name'] != 'word' or token['body'] != 'IS' or token2['name'] != 'word' or token2['body'] != 'INITIAL'): return if context['value_complete']: raise SyntaxException() context['value_complete'] = True
def _parse_read_only(self, context): if len(context['command']) <= context['pos'] + 2: return token = context['command'][context['pos']] token2 = context['command'][context['pos'] + 1] token3 = context['command'][context['pos'] + 2] if (token['name'] != 'word' or token['body'] != 'READ' or token2['name'] != 'field' or token3['name'] != 'word' or token3['body'] != 'ONLY'): return if context['read_only']: raise SyntaxException() context['read_only'] = True context['pos'] += 3
def parse(self): if self._pos > len(self._command): raise SyntaxException() node = None while True: token = self._command[self._pos] is_value = False is_component = False if token['name'] == 'word': try: new_node = ValueNode(token['body'], ValueNode.IN_WORD) is_value = True except: new_node = NameNode(token['body']) if node == None: node = new_node else: node = ComponentNode(node, new_node) is_component = True elif token['name'] == 'charstr': node = ValueNode(token['body'], ValueNode.IN_CHARVAL) is_value = True else: raise SyntaxException() self._pos += 1 if self._pos >= len(self._command): break token_next = self._command[self._pos] if token_next['name'] == 'field': self._pos += 1 else: break if self._pos <= len(self._command): raise SyntaxException() if is_value and is_component: raise SyntaxException() return (node, self._pos)
def run(self, in_syntax_node, in_context, in_code_execution): if in_context.is_variable_exist(in_syntax_node.name): raise SyntaxException() if in_syntax_node.reftype == VariableDefinitionNode.LIKE: try: metadata = in_context.get_var_metadata(in_syntax_node.name) except: raise SyntaxException() elif in_syntax_node.reftype == VariableDefinitionNode.TYPE: typename, _ = in_code_execution.get_node_value( in_syntax_node.typename) if self.__is_type_simple(typename): if in_syntax_node.length == None: length = 0 else: length, _ = in_code_execution.get_node_value( in_syntax_node.length) if in_syntax_node.decimals == None: decimals = 0 else: decimals, _ = in_code_execution.get_node_value( in_syntax_node.decimals) metadata = in_context.request_simple_type_meta( typename=in_code_execution.get_node_value( in_syntax_node.typename)[0], length=length, decimals=decimals, ) else: metadata = in_context.get_type_metadata(typename) else: raise SeriousException() in_context.define_variable( name=in_syntax_node.name, metadata=metadata, ) return (True, None)
def request_simple_type_meta(self, typename, length, decimals): if typename == 'I': default_value = 0 elif typename == 'C': default_value = ' ' else: raise SyntaxException() metadata = RuntimeMetadataSimple(typename=typename, length=length, decimals=decimals, default=default_value) name = metadata.get_full_name() if self._metadatas.has_key(name): return self._metadatas.get(name) else: self._metadatas[name] = metadata return metadata
def get_next(self): command = self._source.get_next() syntax_node = None for syntaxer in self._syntaxers: try: syntax_node = syntaxer().parse(command) # except SyntaxException: # pass except BadReceiverError: continue else: break if syntax_node == None: raise SyntaxException() return syntax_node
def get_syntax_node(self): #nodes creation try: while True: self.create_node() except ParserEndException: pass if len(self._node_line) == 0: raise SyntaxException() #tree creation while True: max_priority = 0 work_list = [] for index in range(len(self._node_line)): node = self._node_line[index] # priority = node.get_priority() priority = self._get_priority(node) if priority > max_priority: max_priority = priority del work_list[:] if priority == max_priority: work_list.append(index) if max_priority == 0: break offset = 0 for index in work_list: index += offset node = self._node_line[index] left_node = None right_node = None if index > 0: left_node = self._node_line[index - 1] if node.send_left(left_node): del self._node_line[index - 1] offset -= 1 index -= 1 if index + 1 < len(self._node_line): right_node = self._node_line[index + 1] if node.send_right(right_node): del self._node_line[index + 1] offset -= 1 #tree check if len(self._node_line) != 1: raise SeriousException() return self._node_line[0]
def parse(self, in_command): command_pos = 0 while True: command_pos_before = command_pos try: variable_node, command_pos = NameParser(in_command, command_pos).parse() except: break; if len(in_command) < command_pos: raise BadReceiverError() if len(in_command) > command_pos: token = in_command[command_pos] if token['name'] == 'word' and token['body'] == '=': self._variable_nodes.append(variable_node) command_pos += 1 else: command_pos = command_pos_before break else: command_pos = command_pos_before break #======================================================================= # while (var_iter * 2 + 1 < len(in_command) # and in_command[var_iter * 2 + 1]['name'] == 'word' # and in_command[var_iter * 2 + 1]['body'] == '='): # if in_command[var_iter * 2]['name'] != 'word': # raise errors.BadReceiverError() # try: # variable_meta = NameParser(in_command['command'], context['pos']).parse() # variable_meta = self._context_meta.get_variable_meta(in_command[var_iter * 2]['body']) # except: # pass # self._variable_metas.append(variable_meta) #======================================================================= if len(self._variable_nodes) == 0: raise BadReceiverError() expr_source = ExpressionSource(in_command, command_pos) self._expression_node = ExpressionParser(expr_source).get_syntax_node() if expr_source.get_index() < len(in_command): raise SyntaxException() return ComputeNode(self._variable_nodes, self._expression_node)
def define_variable(self, name, metadata): if self._variables.has_key(name): raise SyntaxException() variable = RuntimeVariable(metadata.get_full_name(), metadata.default) self._variables[name] = variable return variable
def send_right(self, in_node): if self._subnode_list[1] == None and in_node != None: self._subnode_list[1] = in_node else: raise SyntaxException() return True
def create_node(self): token = self._expr_source.get_next() if self._last_linked: self._last_linked = False if token['name'] == 'word': if token['body'] == '-': self._node_line.append(MathUnaryNode(MathUnaryNode.MINUS)) elif token['body'] == '+': self._node_line.append(MathUnaryNode(MathUnaryNode.PLUS)) else: try: new_node = ValueNode(token['body'], ValueNode.IN_WORD) self._node_line.append(new_node) except SyntaxException: #todo refactor this shit new_node, self._expr_source._index = NameParser( self._expr_source._token_list, self._expr_source._index - 1).parse() self._node_line.append(new_node) elif token['name'] == 'charval': self._node_line.append( ValueNode(token['body'], ValueNode.IN_CHARVAL)) elif token['name'] == 'string': self._node_line.append( ValueNode(token['body'], ValueNode.IN_STRING)) elif token['name'] == 'single_lb': subparser = ExpressionParser(self._expr_source) subparser.get_syntax_node() elif token['name'] == 'single_rb': if self._brackets: raise ParserEndException() else: raise SyntaxException() elif token['name'] == 'far_lb': pass elif token['name'] == 'far_rb': if self._brackets and self._expr_source.is_end(): raise ParserEndException() else: raise SyntaxException() elif token['name'] == 'close_lb': pass elif token['name'] == 'close_rb': raise SyntaxException() elif token['name'] == 'solid_lb': pass elif token['name'] == 'solid_rb': raise SyntaxException() else: raise SeriousException() else: if token['name'] == 'word': if token['body'] == '+': node_type = MathBinaryNode.PLUS elif token['body'] == '-': node_type = MathBinaryNode.MINUS elif token['body'] == '*': node_type = MathBinaryNode.MULT elif token['body'] == '/': node_type = MathBinaryNode.DIV elif token['body'] == 'DIV': node_type = MathBinaryNode.INT_DIV elif token['body'] == 'MOD': node_type = MathBinaryNode.INT_MOD else: raise SeriousException() self._node_line.append(MathBinaryNode(node_type)) self._last_linked = True elif token['name'] == 'static_attr': self._node_line.append(MemberNode(MemberNode.STATIC)) elif token['name'] == 'dynamic_attr': self._node_line.append(MemberNode(MemberNode.DYNAMIC)) elif token['name'] == 'field': self._node_line.append(MemberNode(MemberNode.FIELD)) elif token['name'] == 'offset': self._node_line.append(OffsetNode()) else: raise SeriousException()