def and_test(self, node, line=-1, idx=0): if find_node(node, self.keyword["and"], depth=1): _not_tests = find_all(node, self.symbol.not_test, depth=1) for sub in _not_tests: if find_node(sub, self.symbol.test): self.run(sub, line=line, idx=idx) else: # find not_test nodes for item in find_all_gen(node, self.symbol.atom): if len(item) > 2: first_line = item[1][2] else: continue if isinstance(first_line, int): break else: continue if first_line == line: idx += 1 else: line = first_line idx = 1 _num = self.fn.Number(len(monitor.Monitor().expr_sensors)) monitor.ExprSensor(first_line, idx) self.run(sub, line=line, idx=idx) cloned = clone_node(sub) call_measure_expr = self.fn.CallFunc( "measure_expr", [cloned, _num]) replace_node(sub, self.fn.not_test(call_measure_expr))
def and_test(self, node, line = -1, idx = 0): if find_node(node, self.keyword["and"],depth = 1): _not_tests = find_all(node, self.symbol.not_test, depth=1) for sub in _not_tests: if find_node(sub, self.symbol.test): self.run(sub, line = line, idx = idx) else: # find not_test nodes for item in find_all_gen(node, self.symbol.atom): if len(item)>2: first_line = item[1][2] else: continue if isinstance(first_line, int): break else: continue if first_line == line: idx+=1 else: line = first_line idx = 1 _num = self.fn.Number(len(monitor.Monitor().expr_sensors)) monitor.ExprSensor(first_line, idx) self.run(sub, line = line, idx = idx) cloned = clone_node(sub) call_measure_expr = self.fn.CallFunc("measure_expr",[cloned, _num]) replace_node(sub, self.fn.not_test(call_measure_expr))
def suite(self, node): # special case: no use of sensors in 'if __main__...' stmts of modules that are not __main__. _stmts = find_all(node, self.symbol.stmt, depth=1) _num = self.fn.Number(len(monitor.Monitor().stmt_sensors)) # compile a call 'measure_stmt(_num)' into each suite call_measure_stmt = self.fn.CallFunc("measure_stmt", [_num]) _sensor_stmt = self.fn.stmt(call_measure_stmt) IDX = 0 for i, item in enumerate(node[1:]): if item[0] == self.symbol.stmt: if find_node(item, self.symbol.flow_stmt, depth=3): # measure_stmt shall be execed before IDX = i # return, break, continue break IDX = i if IDX: suite_begin, suite_end = self.get_line_info(node) monitor.StmtSensor(suite_begin, suite_end) _small = find_node(node[i], self.symbol.small_stmt, depth=3) if _small and self.fn.is_atomic(_small) and find_node( _small, self.token.STRING): node.insert(IDX + 2, _sensor_stmt) else: node.insert(IDX + 1, _sensor_stmt)
def test_normalize(self): cst = self.cover.parse("def foo(): pass\n") suite = find_node(cst, self.cover.parse_symbol.suite) suite_z = self.fn_cov.normalize(suite, self.cover.parse_symbol.simple_stmt, self.cover.parse_symbol.stmt) self.assertTrue( find_node(suite_z, self.cover.parse_token.INDENT) is not None)
def normalize(self, node, A_symbol, Z_symbol): A_node = find_node(node, A_symbol) if A_node: Z_builder = self.get_node_builder(Z_symbol) Z_node = Z_builder(A_node) nd_builder = self.get_node_builder(node[0]) return nd_builder(Z_node) else: raise ValueError("Subnode of type '%s' could not be found"% self.langlet.get_node_name(A_symbol))
def normalize(self, node, A_symbol, Z_symbol): A_node = find_node(node, A_symbol) if A_node: Z_builder = self.get_node_builder(Z_symbol) Z_node = Z_builder(A_node) nd_builder = self.get_node_builder(node[0]) return nd_builder(Z_node) else: raise ValueError("Subnode of type '%s' could not be found" % self.langlet.get_node_name(A_symbol))
def suite(self, node): # special case: no use of sensors in 'if __main__...' stmts of modules that are not __main__. _stmts = find_all(node, self.symbol.stmt,depth = 1) _num = self.fn.Number(len(monitor.Monitor().stmt_sensors)) # compile a call 'measure_stmt(_num)' into each suite call_measure_stmt = self.fn.CallFunc("measure_stmt",[_num]) _sensor_stmt = self.fn.stmt(call_measure_stmt) IDX = 0 for i,item in enumerate(node[1:]): if item[0] == self.symbol.stmt: if find_node(item, self.symbol.flow_stmt, depth=3): # measure_stmt shall be execed before IDX = i # return, break, continue break IDX = i if IDX: suite_begin, suite_end = self.get_line_info(node) monitor.StmtSensor(suite_begin, suite_end) _small = find_node(node[i], self.symbol.small_stmt,depth = 3) if _small and self.fn.is_atomic(_small) and find_node(_small, self.token.STRING): node.insert(IDX+2, _sensor_stmt) else: node.insert(IDX+1, _sensor_stmt)
def fit(self, node, target_symbol): ''' Attempts to fit node to target_symbol. First try to vertical-interpolate node using ``target_symbol``. If this fails try to find subnode of type target_symbol in node. If both attempts fail raise an exception. ''' name = self.langlet.get_node_name(target_symbol) nd_builder = getattr(self, name) try: nd = nd_builder(node) return nd except Exception, e: nd = find_node(node, target_symbol) if nd is None: raise else: return nd
def replace_all_nodes(old_node, new_node, contains = None): ''' Replace all nodes N within i{context} where nid(N) = i{in_nid} by i{node} when a node M with nid(M) = i{nid} can be found in N. @param context: contextual cst node @param nid: node id that constraints the node to be replaced. @param in_nid: node id of the target node of replacement. @param node: substitution. ''' from langscape.csttools.cstsearch import find_node, find_all nid = new_node[0] for node in find_all(old_node, nid): if contains: if find_node(node, contains): replace_node(node, new_node) else: replace_node(node, new_node)
def replace_all_nodes(old_node, new_node, contains=None): ''' Replace all nodes N within i{context} where nid(N) = i{in_nid} by i{node} when a node M with nid(M) = i{nid} can be found in N. @param context: contextual cst node @param nid: node id that constraints the node to be replaced. @param in_nid: node id of the target node of replacement. @param node: substitution. ''' from langscape.csttools.cstsearch import find_node, find_all nid = new_node[0] for node in find_all(old_node, nid): if contains: if find_node(node, contains): replace_node(node, new_node) else: replace_node(node, new_node)
else: symbols.append("(%d.%s, %s)"%(llid, s, self.langlet.get_node_name(s))) s = ["One of the following symbols must be used:\n"] indent = " "*2 if symbols: for S in symbols: s.append(indent+S) return "\n".join(s)+"\n" return "" def print_error_msg(self): warnings.warn_explicit("CST doesn't match grammar of target langlet.", GrammarConformanceWarning, "langscape.csttools.cstcheck", lineno = sys._getframe(0).f_lineno) node, idx, selection = self._error print print self.format_node(node, idx) print print self.format_selection(selection) if __name__ == '__main__': import langscape langlet = langscape.load_langlet("python") cst = langlet.parse("def foo(x, y):\n print 89\n print 'a'") print_stmt = find_node(cst, langlet.symbol.print_stmt) print_stmt[0]+=3 nc = NodeChecker(langlet) nc.check_node(cst)
(llid, s, self.langlet.get_node_name(s))) s = ["One of the following symbols must be used:\n"] indent = " " * 2 if symbols: for S in symbols: s.append(indent + S) return "\n".join(s) + "\n" return "" def print_error_msg(self): warnings.warn_explicit("CST doesn't match grammar of target langlet.", GrammarConformanceWarning, "langscape.csttools.cstcheck", lineno=sys._getframe(0).f_lineno) node, idx, selection = self._error print print self.format_node(node, idx) print print self.format_selection(selection) if __name__ == '__main__': import langscape langlet = langscape.load_langlet("python") cst = langlet.parse("def foo(x, y):\n print 89\n print 'a'") print_stmt = find_node(cst, langlet.symbol.print_stmt) print_stmt[0] += 3 nc = NodeChecker(langlet) nc.check_node(cst)
def test_is_atomic(self): cst = self.langlet.parse("1+2\n") self.assertFalse(self.fn.is_atomic(cst)) self.assertTrue(find_node(cst, self.langlet.parse_symbol.atom)) self.assertTrue(self.fn.is_atomic(self.langlet.fn.Number("4")))
def test_map(self): cst = self.cover.parse("del a,b\n") exprlist = find_node(cst, self.cover.parse_symbol.exprlist) testlist = self.fn_cov.map(exprlist, self.cover.parse_symbol.testlist) self.assertTrue(len(testlist) == 4)
def subst_return(langlet, node): test = find_node(node, langlet.symbol.test) if test: test[:] = langlet.fn.test(langlet.fn.CallFunc( "measure", [test[:]])) return node
def test_expr(self): node = find_node(self.python.parse("A\n"), self.symbol.expr) self.cstbuilder.build_cst(self.symbol.testlist, node)
def subst_return(langlet, node): test = find_node(node, langlet.symbol.test) if test: test[:] = langlet.fn.test(langlet.fn.CallFunc("measure", [test[:]])) return node
def test_testlist(self): stmt = find_node(self.python.parse("A\n"), self.symbol.stmt) self.cstbuilder.build_cst(self.symbol.file_input, stmt) stmt = find_node(self.python.parse("A\n"), self.symbol.atom)