def testParseNodeShould(self): "test ParseNode should allow registering of node classes" class CustomParseNode(ParseNode): def __init__(self, children): ParseNode.__init__(self, "CUSTOM_NODE_TYPE", children) ParseNode.register_node_type("CUSTOM_NODE_TYPE", CustomParseNode) try: node = ParseNode.construct_node("CUSTOM_NODE_TYPE", []) self.assertEquals( type(node), CustomParseNode, "ParseNode.construct_node didn't dispatch to CustomParseNode") self.assertEquals(node.type, "CUSTOM_NODE_TYPE", "Wrong type attribute") self.assertEquals(node.children, [], "Wrong children") node = ParseNode.construct_node("FISH BOWL", ["gox blamp"]) self.assertEquals( type(node), ParseNode, "ParseNode.construct_node didn't fall back to ParseNode") self.assertEquals(node.type, "FISH BOWL", "Wrong type attribute") self.assertEquals(node.children, ["gox blamp"], "Wrong children") self.assertIsNotNone(ParseNode.classRegistry, "ParseNode didn't have a class registry") finally: del ParseNode.classRegistry["CUSTOM_NODE_TYPE"]
def testRegisteredWithParse(self): "test registered with ParseNode" self.assertEquals( type( ParseNode.construct_node(ParseNode.FL_COLUMN_REFERENCE, ['A_'])), FLColumnReferenceParseNode, "Class is not registered with ParseNode")
def testRegisteredWithParse(self): "test registered with ParseNode" self.assertEquals( type( ParseNode.construct_node(ParseNode.FL_CELL_RANGE, ['a1', ':', 'b4'])), FLCellRangeParseNode, "Class is not registered with ParseNode")
def testRegisteredWithParse(self): "test registered with ParseNode" self.assertEquals( type(ParseNode.construct_node(ParseNode.FL_CELL_RANGE, ["a1", ":", "b4"])), FLCellRangeParseNode, "Class is not registered with ParseNode", )
def test_rewrite_parse_node_should_return_parse_node_with_children_rewritten( self, mock_recursive_rewrite): node_type = "junk node type" children = [ParseNode(node_type, None), ParseNode(node_type, None)] input = ParseNode(node_type, children) mock_recursive_results = [object(), object()] def mock_recursive_results_generator_func(*args): yield mock_recursive_results[0] yield mock_recursive_results[1] mock_recursive_results_generator = mock_recursive_results_generator_func( ) mock_recursive_rewrite.side_effect = lambda *_: mock_recursive_results_generator.next( ) self.assertEquals(id(rewrite(input)), id(input)) self.assertEquals(mock_recursive_rewrite.call_args_list, [((children[0], ), {}), ((children[1], ), {})]) self.assertEquals(input.children, mock_recursive_results)
def testParseNodeShould(self): "test ParseNode should allow registering of node classes" class CustomParseNode(ParseNode): def __init__(self, children): ParseNode.__init__(self, "CUSTOM_NODE_TYPE", children) ParseNode.register_node_type("CUSTOM_NODE_TYPE", CustomParseNode) try: node = ParseNode.construct_node("CUSTOM_NODE_TYPE", []) self.assertEquals(type(node), CustomParseNode, "ParseNode.construct_node didn't dispatch to CustomParseNode") self.assertEquals(node.type, "CUSTOM_NODE_TYPE", "Wrong type attribute") self.assertEquals(node.children, [], "Wrong children") node = ParseNode.construct_node("FISH BOWL", ["gox blamp"]) self.assertEquals(type(node), ParseNode, "ParseNode.construct_node didn't fall back to ParseNode") self.assertEquals(node.type, "FISH BOWL", "Wrong type attribute") self.assertEquals(node.children, ["gox blamp"], "Wrong children") self.assertIsNotNone(ParseNode.classRegistry, "ParseNode didn't have a class registry") finally: del ParseNode.classRegistry["CUSTOM_NODE_TYPE"]
def ListIter(children): return ParseNode.construct_node(ParseNode.LIST_ITER, children)
def ListFor(children): return ParseNode.construct_node(ParseNode.LIST_FOR, children)
def FLDeletedReference(children): return ParseNode.construct_node(ParseNode.FL_DELETED_REFERENCE, children)
def __init__(self, children): assert len(children) == 3 ParseNode.__init__(self, ParseNode.FL_CELL_RANGE, children)
def testCombinedConvenienceConstructors(self): randomChild = "hello" self.assertEquals( ParseNode(ParseNode.ARITH_EXPR, [ParseNode(ParseNode.TERM, [randomChild])]), ArithExpr_Term(randomChild)) self.assertEquals( ParseNode(ParseNode.EXPR, [ ParseNode(ParseNode.CONCAT_EXPR, [ParseNode(ParseNode.SHIFT_EXPR, [randomChild])]) ]), Expr_ConcatExpr_ShiftExpr(randomChild)) self.assertEquals( ParseNode(ParseNode.FACTOR, [ ParseNode(ParseNode.POWER, [ ParseNode(ParseNode.FL_REFERENCE, [ParseNode(ParseNode.ATOM, [randomChild])]) ]) ]), Factor_Power_FLReference_Atom(randomChild)) self.assertEquals( ParseNode(ParseNode.TEST, [ ParseNode(ParseNode.AND_TEST, [ ParseNode(ParseNode.NOT_TEST, [ParseNode(ParseNode.COMPARISON, [randomChild])]) ]) ]), Test_AndTest_NotTest_Comparison(randomChild)) self.assertEquals( ParseNode(ParseNode.EXPR, [ ParseNode(ParseNode.CONCAT_EXPR, [ ParseNode(ParseNode.SHIFT_EXPR, [ ParseNode(ParseNode.ARITH_EXPR, [ ParseNode(ParseNode.TERM, [ ParseNode(ParseNode.FACTOR, [ ParseNode(ParseNode.POWER, [ ParseNode(ParseNode.FL_REFERENCE, [ ParseNode(ParseNode.ATOM, [ ParseNode( ParseNode.NAME, [randomChild]) ]) ]) ]) ]) ]) ]) ]) ]) ]), ExprFromNameChild(randomChild)) self.assertEquals( ParseNode(ParseNode.EXPR, [ ParseNode(ParseNode.CONCAT_EXPR, [ ParseNode(ParseNode.SHIFT_EXPR, [ ParseNode(ParseNode.ARITH_EXPR, [ ParseNode(ParseNode.TERM, [ ParseNode(ParseNode.FACTOR, [ ParseNode(ParseNode.POWER, [ ParseNode(ParseNode.FL_REFERENCE, [ ParseNode(ParseNode.ATOM, [randomChild]) ]) ]) ]) ]) ]) ]) ]) ]), ExprFromAtomChild(randomChild)) self.assertEquals( ParseNode(ParseNode.TEST, [ ParseNode(ParseNode.AND_TEST, [ ParseNode(ParseNode.NOT_TEST, [ ParseNode(ParseNode.COMPARISON, [ ParseNode(ParseNode.EXPR, [ ParseNode(ParseNode.CONCAT_EXPR, [ ParseNode(ParseNode.SHIFT_EXPR, [ ParseNode(ParseNode.ARITH_EXPR, [ ParseNode(ParseNode.TERM, [ ParseNode( ParseNode.FACTOR, [ ParseNode( ParseNode.POWER, [ ParseNode( ParseNode. FL_REFERENCE, [ ParseNode( ParseNode . ATOM, [ randomChild ]) ]) ]) ]) ]) ]) ]) ]) ]) ]) ]) ]) ]), TestFromAtomChild(randomChild)) self.assertEquals( ParseNode(ParseNode.TEST, [ ParseNode(ParseNode.AND_TEST, [ ParseNode(ParseNode.NOT_TEST, [ ParseNode(ParseNode.COMPARISON, [ ParseNode(ParseNode.EXPR, [ ParseNode(ParseNode.CONCAT_EXPR, [ ParseNode(ParseNode.SHIFT_EXPR, [ ParseNode(ParseNode.ARITH_EXPR, [ ParseNode(ParseNode.TERM, [ ParseNode( ParseNode.FACTOR, [ ParseNode( ParseNode.POWER, [randomChild]) ]) ]) ]) ]) ]) ]) ]) ]) ]) ]), TestFromPowerChild(randomChild))
def __init__(self, children): ParseNode.__init__(self, ParseNode.FL_ROW_REFERENCE, children)
def CompOperator(children): return ParseNode.construct_node(ParseNode.COMP_OPERATOR, children)
def FLRoot(children): return ParseNode.construct_node(ParseNode.FL_ROOT, children)
def Comparison(children): return ParseNode.construct_node(ParseNode.COMPARISON, children)
def FLReference(children): return ParseNode.construct_node(ParseNode.FL_REFERENCE, children)
def FLNakedWorksheetReference(children): return ParseNode.construct_node(ParseNode.FL_NAKED_WORKSHEET_REFERENCE, children)
def FLInvalidReference(children): return ParseNode.construct_node(ParseNode.FL_INVALID_REFERENCE, children)
def Name(children): return ParseNode.construct_node(ParseNode.NAME, children)
def DictMaker(children): return ParseNode.construct_node(ParseNode.DICT_MAKER, children)
def Expr(children): return ParseNode.construct_node(ParseNode.EXPR, children)
def FPDef(children): return ParseNode.construct_node(ParseNode.FP_DEF, children)
def testRegisteredWithParse(self): "test registered with ParseNode" self.assertEquals(type(ParseNode.construct_node(ParseNode.FL_NAMED_ROW_REFERENCE, ['dfytjdky'])), FLNamedRowReferenceParseNode, "Class is not registered with ParseNode")
def GenFor(children): return ParseNode.construct_node(ParseNode.GEN_FOR, children)
localReference = property(__getLocalReference, __setLocalReference) @property def coords(self): return cell_name_to_coordinates(self.plainCellName) def offset(self, dx, dy, move_absolute=False): (col, row) = cell_name_to_coordinates(self.plainCellName) if move_absolute or not self.colAbsolute: col += dx if move_absolute or not self.rowAbsolute: row += dy newName = coordinates_to_cell_name(col, row, colAbsolute=self.colAbsolute, rowAbsolute=self.rowAbsolute) if newName is None: newName = "#Invalid!" self.localReference = newName + self.whitespace def canonicalise(self, wsNames): self.localReference = self.localReference.upper() FLReferenceParseNode.canonicalise(self, wsNames) ParseNode.register_node_type(ParseNode.FL_CELL_REFERENCE, FLCellReferenceParseNode)
def GenIter(children): return ParseNode.construct_node(ParseNode.GEN_ITER, children)
def ExprList(children): return ParseNode.construct_node(ParseNode.EXPR_LIST, children)
def FLDDECall(children): return ParseNode.construct_node(ParseNode.FL_DDE_CALL, children)
def FPList(children): return ParseNode.construct_node(ParseNode.FP_LIST, children)
def Factor(children): return ParseNode.construct_node(ParseNode.FACTOR, children)
def GenIf(children): return ParseNode.construct_node(ParseNode.GEN_IF, children)
def LambDef(children): return ParseNode.construct_node(ParseNode.LAMBDEF, children)
def ListIf(children): return ParseNode.construct_node(ParseNode.LIST_IF, children)
def ListMaker(children): return ParseNode.construct_node(ParseNode.LIST_MAKER, children)
class FLCellRangeParseNode(ParseNode): def __init__(self, children): assert len(children) == 3 ParseNode.__init__(self, ParseNode.FL_CELL_RANGE, children) def __get_first_cell_reference(self): return self.children[0] def __set_first_cell_reference(self, cellRef): self.children[0] = cellRef first_cell_reference = property(__get_first_cell_reference, __set_first_cell_reference) def __get_second_cell_reference(self): return self.children[2] def __set_second_cell_reference(self, cellRef): self.children[2] = cellRef second_cell_reference = property(__get_second_cell_reference, __set_second_cell_reference) @property def colon(self): return self.children[1] ParseNode.register_node_type(ParseNode.FL_CELL_RANGE, FLCellRangeParseNode)
# Copyright (c) 2005-2009 Resolver Systems Ltd, PythonAnywhere LLP # See LICENSE.md # from dirigible.sheet.parser.parse_node import ParseNode from dirigible.sheet.parser.fl_reference_parse_node import FLReferenceParseNode class FLNamedColumnReferenceParseNode(FLReferenceParseNode): def __init__(self, children): FLReferenceParseNode.__init__(self, ParseNode.FL_NAMED_COLUMN_REFERENCE, children) @property def header(self): return self.children[-1].rstrip().replace("##", "#")[1:-2] ParseNode.register_node_type(ParseNode.FL_NAMED_COLUMN_REFERENCE, FLNamedColumnReferenceParseNode)
plainRowName = property(__getPlainRowName, __setPlainRowName) @property def rowIndex(self): return int(self.plainRowName) @property def coords(self): return 0, self.rowIndex def offset(self, _, count, moveAbsolute=False): if not moveAbsolute and self.isAbsolute: return newIndex = self.rowIndex + count if newIndex > 0: self.plainRowName = str(newIndex) else: self.localReference = '#Invalid!' + self.whitespace def __getLocalReference(self): return self.children[-1] def __setLocalReference(self, newRow): self.children[-1] = newRow localReference = property(__getLocalReference, __setLocalReference) ParseNode.register_node_type(ParseNode.FL_ROW_REFERENCE, FLRowReferenceParseNode)
@property def rowIndex(self): return int(self.plainRowName) @property def coords(self): return 0, self.rowIndex def offset(self, _, count, moveAbsolute=False): if not moveAbsolute and self.isAbsolute: return newIndex = self.rowIndex + count if newIndex > 0: self.plainRowName = str(newIndex) else: self.localReference = '#Invalid!' + self.whitespace def __getLocalReference(self): return self.children[-1] def __setLocalReference(self, newRow): self.children[-1] = newRow localReference = property(__getLocalReference, __setLocalReference) ParseNode.register_node_type(ParseNode.FL_ROW_REFERENCE, FLRowReferenceParseNode)
def testSimpleConvenienceConstructors(self): randomList = ["foo", "bar", 27] self.assertEquals(ParseNode(ParseNode.AND_TEST, randomList), AndTest(randomList)) self.assertEquals(ParseNode(ParseNode.ARG_LIST, randomList), ArgList(randomList)) self.assertEquals(ParseNode(ParseNode.ARGUMENT, randomList), Argument(randomList)) self.assertEquals(ParseNode(ParseNode.ARITH_EXPR, randomList), ArithExpr(randomList)) self.assertEquals(ParseNode(ParseNode.ATOM, randomList), Atom(randomList)) self.assertEquals(ParseNode(ParseNode.COMPARISON, randomList), Comparison(randomList)) self.assertEquals(ParseNode(ParseNode.COMP_OPERATOR, randomList), CompOperator(randomList)) self.assertEquals(ParseNode(ParseNode.CONCAT_EXPR, randomList), ConcatExpr(randomList)) self.assertEquals(ParseNode(ParseNode.DICT_MAKER, randomList), DictMaker(randomList)) self.assertEquals(ParseNode(ParseNode.EXPR, randomList), Expr(randomList)) self.assertEquals(ParseNode(ParseNode.EXPR_LIST, randomList), ExprList(randomList)) self.assertEquals(ParseNode(ParseNode.FACTOR, randomList), Factor(randomList)) self.assertEquals(FLCellRangeParseNode(randomList), FLCellRange(randomList)) self.assertEquals(FLCellReferenceParseNode(randomList), FLCellReference(randomList)) self.assertEquals(FLColumnReferenceParseNode(randomList), FLColumnReference(randomList)) self.assertEquals(FLRowReferenceParseNode(randomList), FLRowReference(randomList)) self.assertEquals(ParseNode(ParseNode.FL_DDE_CALL, randomList), FLDDECall(randomList)) self.assertEquals( ParseNode(ParseNode.FL_DELETED_REFERENCE, randomList), FLDeletedReference(randomList)) self.assertEquals( ParseNode(ParseNode.FL_INVALID_REFERENCE, randomList), FLInvalidReference(randomList)) self.assertEquals( ParseNode(ParseNode.FL_NAKED_WORKSHEET_REFERENCE, randomList), FLNakedWorksheetReference(randomList)) self.assertEquals(ParseNode(ParseNode.FL_REFERENCE, randomList), FLReference(randomList)) self.assertEquals(ParseNode(ParseNode.FL_ROOT, randomList), FLRoot(randomList)) self.assertEquals(ParseNode(ParseNode.FP_DEF, randomList), FPDef(randomList)) self.assertEquals(ParseNode(ParseNode.FP_LIST, randomList), FPList(randomList)) self.assertEquals(ParseNode(ParseNode.GEN_FOR, randomList), GenFor(randomList)) self.assertEquals(ParseNode(ParseNode.GEN_IF, randomList), GenIf(randomList)) self.assertEquals(ParseNode(ParseNode.GEN_ITER, randomList), GenIter(randomList)) self.assertEquals(ParseNode(ParseNode.LAMBDEF, randomList), LambDef(randomList)) self.assertEquals(ParseNode(ParseNode.LIST_FOR, randomList), ListFor(randomList)) self.assertEquals(ParseNode(ParseNode.LIST_IF, randomList), ListIf(randomList)) self.assertEquals(ParseNode(ParseNode.LIST_ITER, randomList), ListIter(randomList)) self.assertEquals(ParseNode(ParseNode.LIST_MAKER, randomList), ListMaker(randomList)) self.assertEquals(ParseNode(ParseNode.NAME, randomList), Name(randomList)) self.assertEquals(ParseNode(ParseNode.NOT_TEST, randomList), NotTest(randomList)) self.assertEquals(ParseNode(ParseNode.NUMBER, randomList), Number(randomList)) self.assertEquals(ParseNode(ParseNode.POWER, randomList), Power(randomList)) self.assertEquals(ParseNode(ParseNode.SHIFT_EXPR, randomList), ShiftExpr(randomList)) self.assertEquals(ParseNode(ParseNode.SLICE_OP, randomList), SliceOp(randomList)) self.assertEquals(ParseNode(ParseNode.STRINGLITERAL, randomList), StringLiteral(randomList)) self.assertEquals(ParseNode(ParseNode.SUBSCRIPT, randomList), Subscript(randomList)) self.assertEquals(ParseNode(ParseNode.SUBSCRIPT_LIST, randomList), SubscriptList(randomList)) self.assertEquals(ParseNode(ParseNode.TERM, randomList), Term(randomList)) self.assertEquals(ParseNode(ParseNode.TEST, randomList), Test(randomList)) self.assertEquals(ParseNode(ParseNode.TEST_LIST, randomList), TestList(randomList)) self.assertEquals(ParseNode(ParseNode.TEST_LIST_GEXP, randomList), TestListGexp(randomList)) self.assertEquals(ParseNode(ParseNode.TRAILER, randomList), Trailer(randomList)) self.assertEquals(ParseNode(ParseNode.VAR_ARGS_LIST, randomList), VarArgsList(randomList))
def testRegisteredWithParse(self): "test registered with ParseNode" self.assertEquals(type(ParseNode.construct_node(ParseNode.FL_COLUMN_REFERENCE, ['A_'])), FLColumnReferenceParseNode, "Class is not registered with ParseNode")
# from dirigible.sheet.parser.parse_node import ParseNode class FLCellRangeParseNode(ParseNode): def __init__(self, children): assert len(children) == 3 ParseNode.__init__(self, ParseNode.FL_CELL_RANGE, children) def __get_first_cell_reference(self): return self.children[0] def __set_first_cell_reference(self, cellRef): self.children[0] = cellRef first_cell_reference = property(__get_first_cell_reference, __set_first_cell_reference) def __get_second_cell_reference(self): return self.children[2] def __set_second_cell_reference(self, cellRef): self.children[2] = cellRef second_cell_reference = property(__get_second_cell_reference, __set_second_cell_reference) @property def colon(self): return self.children[1] ParseNode.register_node_type(ParseNode.FL_CELL_RANGE, FLCellRangeParseNode)
def colIndex(self): return column_name_to_index(self.plainColumnName) @property def coords(self): return self.colIndex, 0 def offset(self, count, _, moveAbsolute=False): if not moveAbsolute and self.isAbsolute: return newName = column_index_to_name(self.colIndex + count) if newName: self.plainColumnName = newName else: self.localReference = '#Invalid!' + self.whitespace def __getLocalReference(self): return self.children[-1] def __setLocalReference(self, newCol): self.children[-1] = newCol localReference = property(__getLocalReference, __setLocalReference) def canonicalise(self, wsNames): self.localReference = self.localReference.upper() FLReferenceParseNode.canonicalise(self, wsNames) ParseNode.register_node_type(ParseNode.FL_COLUMN_REFERENCE, FLColumnReferenceParseNode)