def test(self): src = "Item { function foo() {} function bar() {} }" lexer = Lexer(src) lexer.tokenize() qmlclass = QmlClass("Foo") qmlparser.parse(lexer.tokens, qmlclass) self.assertEqual(qmlclass.base_name, "Item") functions = qmlclass.get_functions() self.assertEqual(functions[0].name, "foo") self.assertEqual(functions[1].name, "bar") self.assertEqual(len(functions), 2)
def test_function_property(self): src = """Item { property var fnProperty: function (arg1, arg2) { return arg1 + arg2; } }""" lexer = Lexer(src) lexer.tokenize() qmlclass = QmlClass("Foo") qmlparser.parse(lexer.tokens, qmlclass) properties = qmlclass.get_properties() self.assertEqual(properties[0].name, "fnProperty") self.assertEqual(properties[0].type, "var")
def test_var_property(self): src = """Item { property var varProperty: { "key1": "value1", "key2": "value2" } }""" lexer = Lexer(src) lexer.tokenize() qmlclass = QmlClass("Foo") qmlparser.parse(lexer.tokens, qmlclass) properties = qmlclass.get_properties() self.assertEqual(properties[0].name, "varProperty") self.assertEqual(properties[0].type, "var")
def test_normal_arguments(self): src = """Item { function foo(arg1, arg2) { return arg1 + arg2; } }""" lexer = Lexer(src) lexer.tokenize() qmlclass = QmlClass("Foo") qmlparser.parse(lexer.tokens, qmlclass) functions = qmlclass.get_functions() self.assertEqual(functions[0].name, "foo") self.assertEqual(functions[0].type, "void")
def test_keyword_arguments(self): src = """Item { function foo(propertyArgument, signalArgument) { return propertyArgument + signalArgument; } }""" lexer = Lexer(src) lexer.tokenize() qmlclass = QmlClass("Foo") qmlparser.parse(lexer.tokens, qmlclass) functions = qmlclass.get_functions() self.assertEqual(functions[0].name, "foo") self.assertEqual(functions[0].type, "void")
def test_readonly_property(self): src = """Item { /// v1 doc readonly property int v1 /// v2 doc property int v2 }""" lexer = Lexer(src) lexer.tokenize() qmlclass = QmlClass("Foo") qmlparser.parse(lexer.tokens, qmlclass) properties = qmlclass.get_properties() self.assertEqual(properties[0].name, "v1") self.assertEqual(properties[0].type, "int") self.assertEqual(properties[0].doc, "/// v1 doc") self.assert_(properties[0].is_readonly) self.assertEqual(properties[1].name, "v2") self.assertEqual(properties[1].type, "int") self.assertEqual(properties[1].doc, "/// v2 doc") self.assert_(not properties[1].is_readonly)
def main(argv=None, out=None): if argv is None: argv = sys.argv[1:] if out is None: out == sys.stdout args = parse_args(argv) name = args.qml_file namespace = args.namespace text = open(name, encoding="utf-8").read() lexer = Lexer(text) try: lexer.tokenize() except LexerError as exc: logging.error("Failed to tokenize %s" % name) row, msg = info_for_error_at(text, exc.idx) logging.error("Lexer error line %d: %s\n%s", row, exc, msg) if args.debug: raise else: return -1 if args.debug: for token in lexer.tokens: print("%20s %s" % (token.type, token.value)) classname, classversion = find_classname(name, namespace) qml_class = QmlClass(classname, classversion) try: qmlparser.parse(lexer.tokens, qml_class) except qmlparser.QmlParserError as exc: logging.error("Failed to parse %s" % name) row, msg = info_for_error_at(text, exc.token.idx) logging.error("Lexer error line %d: %s\n%s", row, exc, msg) if args.debug: raise else: return -1 print(qml_class, file=out) return 0
def main(): args = parse_args() name = args.qml_file text = open(name).read() lexer = Lexer(text) try: lexer.tokenize() except LexerError as exc: logging.error("Failed to tokenize %s" % name) row, msg = info_for_error_at(text, exc.idx) logging.error("Lexer error line %d: %s\n%s", row, exc, msg) if args.debug: raise else: return -1 if args.debug: for token in lexer.tokens: print("%20s %s" % (token.type, token.value)) classname, classversion = find_classname(name) qml_class = QmlClass(classname, classversion) try: qmlparser.parse(lexer.tokens, qml_class) except qmlparser.QmlParserError as exc: logging.error("Failed to parse %s" % name) row, msg = info_for_error_at(text, exc.token.idx) logging.error("Lexer error line %d: %s\n%s", row, exc, msg) if args.debug: raise else: return -1 print(qml_class) return 0