def _parse_class_field(self, params: str, ast_node: Node): """ ---@field [public|protected|private] field_name FIELD_TYPE[|OTHER_TYPE] [@comment] """ # noinspection PyBroadException try: parts = params.split(' ', 2) # split visibility and field name if len(parts) < 3: self._report_error(ast_node, "invalid @field tag: @field %s", params) return field_visibility: LuaVisibility = self._parse_visibility(parts[0]) field_name: str = parts[1] field_type_desc: str = parts[2] doc_type, desc = emmylua.parse_param_field(field_type_desc) field = LuaClassField(name=field_name, desc=desc, lua_type=doc_type, visibility=field_visibility) if self._pending_class: self._pending_class[-1].fields.append(field) return field except Exception: self._report_error(ast_node, "invalid @field tag: @field %s", params)
def _parse_overload(self, params: str, ast_node: Node): # noinspection PyBroadException try: model, desc = emmylua.parse_param_field(params) self._pending_overload.append(model) except Exception as e: self._report_error(ast_node, "invalid @overload field: @overload %s", params)
def test_parse_fun_nested_expr(self): t, desc = parse_param_field("fun(s: string, f: fun(i: number))") self.assertIsInstance(t, model.LuaTypeCallable) self.assertEqual(len(t.arg_types), 2) self.assertIsInstance(t.arg_types[0], model.LuaTypeString) self.assertIsInstance(t.arg_types[1], model.LuaTypeCallable) self.assertEqual(len(t.arg_types[1].arg_types), 1) self.assertIsInstance(t.arg_types[1].arg_types[0], model.LuaTypeNumber)
def test_parse_fun_expr(self): t, desc = parse_param_field("fun(n: number, s: string) : nil") self.assertIsInstance(t, model.LuaTypeCallable) self.assertEqual(len(t.arg_types), 2) self.assertIsInstance(t.arg_types[0], model.LuaTypeNumber) self.assertIsInstance(t.arg_types[1], model.LuaTypeString) self.assertEqual(len(t.return_types), 1) self.assertIsInstance(t.return_types[0], model.LuaTypeNil)
def test_parse_array_expr(self): t, desc = parse_param_field("number[]|string[] some array") self.assertIsInstance(t, model.LuaTypeOr) self.assertEqual(len(t.types), 2) self.assertIsInstance(t.types[0], model.LuaTypeArray) self.assertIsInstance(t.types[0].type, model.LuaTypeNumber) self.assertIsInstance(t.types[1], model.LuaTypeArray) self.assertIsInstance(t.types[1].type, model.LuaTypeString)
def _parse_varargs(self, params: str, ast_node: Node): # noinspection PyBroadException try: doc_type, desc = emmylua.parse_param_field(params) param = LuaParam("...", desc, doc_type) # if function pending, add param to it if self._pending_function: self._pending_function[-1].params.append(param) else: self._pending_param.append(param) except Exception: self._report_error(ast_node, "invalid @param tag: @param %s", params)
def _parse_emmy_lua_return(self, params: str, ast_node: Node): # noinspection PyBroadException try: doc_type, desc = emmylua.parse_param_field(params) lua_return = LuaReturn(desc, doc_type) # if function pending, add param to it if self._pending_function: self._pending_function[-1].returns.append(lua_return) else: self._pending_return.append(lua_return) except Exception: self._report_error(ast_node, "invalid @return tag: @return %s", params)
def _parse_type(self, params: str, ast_node: Node): """ --@type MY_TYPE [@comment] """ # noinspection PyBroadException try: identifier = astutils.get_identifier(ast_node) value = astutils.get_value(ast_node) doc_type, desc = emmylua.parse_param_field(params) lua_data = LuaValue(identifier, doc_type) lua_data.value = value self._pending_data.append(lua_data) return lua_data except Exception as e: self._report_error(ast_node, "invalid @type tag: @type %s", params)
def _parse_emmy_lua_param(self, params: str, ast_node: Node): """ param_name MY_TYPE[|other_type] [@comment] """ # noinspection PyBroadException try: parts = params.split(' ', 1) param_name = parts[0] params = parts[1] doc_type, desc = emmylua.parse_param_field(params) param = LuaParam(param_name, desc, doc_type) # if function pending, add param to it if self._pending_function: self._pending_function[-1].params.append(param) else: self._pending_param.append(param) except Exception: self._report_error(ast_node, "invalid @param tag: @param %s", params)
def test_parse_or_expr(self): t, desc = parse_param_field("string|number the string") self.assertIsInstance(t, model.LuaTypeOr) self.assertEqual(desc, "the string")
def test_parse_table_expr(self): t, desc = parse_param_field("table<string, number> the number table") self.assertIsInstance(t, model.LuaTypeDict) self.assertIsInstance(t.key_type, model.LuaTypeString) self.assertIsInstance(t.value_type, model.LuaTypeNumber) self.assertEqual(desc, "the number table")