A language conversion method, device, electronic device and storage medium
By using syntax tree analysis and node labeling technology in the process of file language conversion, the problem of high labor and time costs and prone to omissions and errors in the existing technology is solved, and efficient and accurate language conversion is achieved.
Patent Information
- Application Number
- CN202111585300.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-22
- Publication Date
- 2025-02-25
- Estimated Expiration
- 2041-12-22
AI Technical Summary
In the prior art, a lot of manpower and time cost is required to be invested in the process of document language conversion, and there are repeated mechanical work and easy to cause omissions and errors.
By obtaining and parsing the pending files, a syntax tree is generated, and in the syntax tree, the target nodes that meet the annotation conditions are searched upwards step by step by step, obtaining target attribute information, adding child nodes and generating declaration information, updating the syntax tree, and finally generating the target file.
It greatly saves time and labor costs for manual migration, reduces possible mistakes caused by labor, and reduces the risks and costs of the language conversion process.
Smart Images

Figure CN114691104B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computers, and in particular to a language conversion method, device, electronic device and storage medium. Background Art
[0002] For the sustainable iterative development of computer projects, it is very necessary to convert a file in one language (which we can call the first file) into a file in another language (which we can call the second file). Currently, the way to convert the first file into the second file is usually to modify the suffix of the first file to the suffix of the second file.
[0003] The inventors found that the above method has problems: the obtained second file cannot pass the language verification of the second file, a lot of work still needs to be done manually, the manpower and time costs required are very high, there is a lot of repetitive and mechanical work, and omissions and errors are prone to occur. Summary of the invention
[0004] The present disclosure provides a language conversion method, device, electronic device and storage medium to at least solve the problems in the related technology that the manpower and time costs required are very high, there is a lot of repetitive mechanical work, and it is easy to make mistakes. The technical solution of the present disclosure is as follows:
[0005] According to a first aspect of an embodiment of the present disclosure, a language conversion method is provided, including:
[0006] Acquire and parse a file to be processed, and generate a syntax tree of the file to be processed, wherein the file to be processed is a first language file, and the syntax tree includes a plurality of nodes, and the nodes correspond one-to-one to the language structures included in the file to be processed;
[0007] In the syntax tree, searching for a target node from the to-be-annotated nodes that meet the annotation conditions step by step upward, the target node corresponding to the class declaration or class expression in the to-be-processed file;
[0008] In a case where the main information of the target node does not include the name of the node to be labeled, acquiring target attribute information of the node to be labeled, where the target attribute information is used to indicate the type of the parent node of the node to be labeled;
[0009] According to the target attribute information, a child node is added to the syntax tree for the target node, declaration information of the child node is generated, and the declaration information is inserted into the main information of the target node to obtain an updated syntax tree;
[0010] A target file is generated according to the updated syntax tree, where the target file is a second language file.
[0011] Optionally, in the syntax tree, searching for a target node level by level from the to-be-annotated nodes that meet the annotation condition includes:
[0012] Traverse the nodes in the syntax tree. If the type of any node is a member expression, the type of the object node corresponding to any node is an execution context expression, and the type of the attribute node corresponding to any node is an identifier, then take the any node as a node to be annotated, and search for the target node from the node to be annotated step by step upward.
[0013] Optionally, in the syntax tree, searching for a target node level by level from the to-be-annotated nodes that meet the annotation condition includes:
[0014] In the syntax tree, obtaining path information of the nodes to be annotated that meet the annotation conditions;
[0015] According to the path information, the node to be labeled is queried for parent nodes step by step. If the type of any parent node queried is a class declaration or a class expression, any parent node is used as a target node.
[0016] Optionally, the method further includes:
[0017] If the type of any parent node found is a function declaration or a function expression, or if the type of any parent node found is not a class declaration or a class expression, it is determined that there is no target node corresponding to the node to be annotated.
[0018] Optionally, after searching for a target node in the syntax tree from the to-be-annotated nodes that meet the annotation condition, the method further includes:
[0019] Obtaining the main information of the target node, wherein the main information includes a target array, each element in the target array corresponds to any method or attribute included in the class corresponding to the target node, and each element has a corresponding element key value;
[0020] When there is no element key value corresponding to the target key value of the node to be annotated, it is determined that the name of the node to be annotated is not included in the main information of the target node, and the target key value is the name field of the attribute node corresponding to the node to be annotated.
[0021] Optionally, adding a child node to the target node in the syntax tree according to the target attribute information and generating declaration information of the child node includes:
[0022] If the target attribute information is the first preset field, a child node is added to the target node in the syntax tree, the type of the child node is a method declaration, and the return value of the child node is an arbitrary type;
[0023] Generate declaration information of the child node, where the declaration information includes the type of the child node and the return value type.
[0024] Optionally, adding a child node to the target node in the syntax tree according to the target attribute information and generating declaration information of the child node includes:
[0025] If the target attribute information is the second preset field, a child node is added to the target node in the syntax tree, the type of the child node is a class declaration, and the initialization value of the child node is the assigned attribute of the parent node of the node to be annotated;
[0026] Determining attribute information of the child node according to the type of the initialization value;
[0027] Generate declaration information of the child node, where the declaration information includes the type, initialization value, and attribute information of the child node.
[0028] Optionally, the attribute information of the child node includes optional attributes and / or declaration types, and determining the attribute information of the child node according to the type of the initialization value includes:
[0029] If the initialization value is empty, determining that the optional attribute of the child node is a preset field;
[0030] If the type of the initialization value is any data type, determining the declared type of the child node to be any data type;
[0031] If the type of the initialization value is an identifier, determine the scope of the target node, search for the reference path of the identifier in the scope, determine the reference node corresponding to the reference path, update the initialization value of the child node according to the type and assignment attributes of the reference node, and determine the declaration type of the child node.
[0032] Optionally, updating the initialization value of the child node and determining the declaration type of the child node according to the type and the assigned attribute of the reference node includes:
[0033] If the type of the reference node is an assignment expression or a variable declaration, the assignment attribute of the reference node is used as the initialization value of the child node, and the step of determining the attribute information of the child node according to the type of the initialization value is returned.
[0034] Optionally, the acquiring and parsing the file to be processed and generating a syntax tree of the file to be processed includes:
[0035] Get at least one file to be processed;
[0036] Parsing the file to be processed to generate a syntax tree of the file to be processed, wherein the syntax tree includes a file path of the file to be processed, and the file path is used to indicate a location of the file to be processed in the target project;
[0037] After generating the target file according to the updated syntax tree, the method further includes:
[0038] The file path of the file to be processed is read, and the target file is output to a storage location corresponding to the file path.
[0039] According to a second aspect of an embodiment of the present disclosure, there is provided a language conversion device, including:
[0040] A parsing unit is configured to obtain and parse a file to be processed, and generate a syntax tree of the file to be processed, wherein the file to be processed is a first language file, and the syntax tree includes a plurality of nodes, and the nodes correspond one-to-one to the language structures included in the file to be processed;
[0041] A query unit is configured to execute in the syntax tree, searching for a target node from the to-be-annotated node that meets the annotation condition, step by step upward, wherein the target node corresponds to a class declaration or a class expression in the to-be-processed file;
[0042] an acquiring unit, configured to acquire target attribute information of the node to be labeled when the subject information of the target node does not include the name of the node to be labeled, wherein the target attribute information is used to indicate a type of a parent node of the node to be labeled;
[0043] A generating unit is configured to add a child node to the target node in the syntax tree according to the target attribute information, generate declaration information of the child node, insert the declaration information into the main information of the target node, and obtain an updated syntax tree;
[0044] The conversion unit is configured to generate a target file according to the updated syntax tree, where the target file is a second language file.
[0045] Optionally, the query unit is configured to execute:
[0046] Traverse the nodes in the syntax tree. If the type of any node is a member expression, the type of the object node corresponding to any node is an execution context expression, and the type of the attribute node corresponding to any node is an identifier, then take the any node as a node to be annotated, and search for the target node from the node to be annotated step by step upward.
[0047] Optionally, the query unit is configured to execute:
[0048] In the syntax tree, obtaining path information of the nodes to be annotated that meet the annotation conditions;
[0049] According to the path information, the node to be labeled is queried for parent nodes step by step. If the type of any parent node queried is a class declaration or a class expression, any parent node is used as a target node.
[0050] Optionally, the query unit is configured to execute:
[0051] If the type of any parent node found is a function declaration or a function expression, or if the type of any parent node found is not a class declaration or a class expression, it is determined that there is no target node corresponding to the node to be annotated.
[0052] Optionally, the query unit is configured to execute:
[0053] Obtaining the main body information of the target node, wherein the main body information includes a target array, each element in the target array corresponds to any device or attribute included in the class corresponding to the target node, and each element has a corresponding element key value;
[0054] When there is no element key value corresponding to the target key value of the node to be annotated, it is determined that the name of the node to be annotated is not included in the main information of the target node, and the target key value is the name field of the attribute node corresponding to the node to be annotated.
[0055] Optionally, the generating unit is configured to execute:
[0056] If the target attribute information is a first preset field, a child node is added to the target node in the syntax tree, the type of the child node is a device declaration, and the return value of the child node is an arbitrary type;
[0057] Generate declaration information of the child node, where the declaration information includes the type of the child node and the return value type.
[0058] Optionally, the generating unit is configured to execute:
[0059] If the target attribute information is the second preset field, a child node is added to the target node in the syntax tree, the type of the child node is a class declaration, and the initialization value of the child node is the assigned attribute of the parent node of the node to be annotated;
[0060] Determining attribute information of the child node according to the type of the initialization value;
[0061] Generate declaration information of the child node, where the declaration information includes the type, initialization value, and attribute information of the child node.
[0062] Optionally, the attribute information of the child node includes optional attributes and / or declaration types, and the generating unit is configured to execute:
[0063] If the initialization value is empty, determining that the optional attribute of the child node is a preset field;
[0064] If the type of the initialization value is any data type, determining the declared type of the child node to be any data type;
[0065] If the type of the initialization value is an identifier, determine the scope of the target node, search for the reference path of the identifier in the scope, determine the reference node corresponding to the reference path, update the initialization value of the child node according to the type and assignment attributes of the reference node, and determine the declaration type of the child node.
[0066] Optionally, the generating unit is configured to execute:
[0067] If the type of the reference node is an assignment expression or a variable declaration, the assignment attribute of the reference node is used as the initialization value of the child node, and the step of determining the attribute information of the child node according to the type of the initialization value is returned.
[0068] Optionally, the parsing unit is configured to execute:
[0069] Get at least one file to be processed;
[0070] Parsing the file to be processed to generate a syntax tree of the file to be processed, wherein the syntax tree includes a file path of the file to be processed, and the file path is used to indicate a location of the file to be processed in the target project;
[0071] The conversion unit is further configured to execute:
[0072] The file path of the file to be processed is read, and the target file is output to a storage location corresponding to the file path.
[0073] According to a third aspect of an embodiment of the present disclosure, there is provided a language conversion electronic device, including:
[0074] processor;
[0075] a memory for storing instructions executable by the processor;
[0076] The processor is configured to execute the instructions to implement any of the above-mentioned language conversion methods.
[0077] According to a fourth aspect of an embodiment of the present disclosure, a computer-readable storage medium is provided. When instructions in the computer-readable storage medium are executed by a processor of a language conversion electronic device, the language conversion electronic device can perform any of the above-mentioned language conversion methods.
[0078] According to a fifth aspect of an embodiment of the present disclosure, there is provided a computer program product, comprising a computer program / instruction, wherein when the computer program / instruction is executed by a processor, the language conversion method described in any one of the above items is implemented.
[0079] The technical solution provided by the embodiments of the present disclosure brings at least the following beneficial effects:
[0080] The method comprises the following steps: obtaining and parsing a file to be processed, generating a syntax tree of the file to be processed, wherein the file to be processed is a first language file, and the syntax tree includes a plurality of nodes, wherein the nodes correspond to the language structures included in the file to be processed one by one; searching for a target node in the syntax tree step by step from the nodes to be labeled that meet the labeling conditions, wherein the target node corresponds to a class declaration or a class expression in the file to be processed; obtaining target attribute information of the node to be labeled when the main information of the target node does not include the name of the node to be labeled, wherein the target attribute information is used to indicate the type of the parent node of the node to be labeled; adding a child node to the target node in the syntax tree according to the target attribute information, generating declaration information of the child node, inserting the declaration information into the main information of the target node, and obtaining an updated syntax tree; generating a target file according to the updated syntax tree, wherein the target file is a second language file.
[0081] In this way, by analyzing the nodes in the syntax tree, we can infer and annotate as much type information of class-related language structures as possible, greatly saving the time and labor costs of manual migration, while reducing errors that may be caused by human labor and minimizing the risks and costs of the language conversion process.
[0082] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS
[0083] The drawings herein are incorporated into and constitute a part of the specification, illustrate embodiments consistent with the present disclosure, and together with the description are used to explain the principles of the present disclosure, and do not constitute improper limitations on the present disclosure.
[0084] Figure 1 The present invention is a flowchart of a language conversion method according to an exemplary embodiment.
[0085] Figure 2 The figure is a flow chart showing another language conversion method according to an exemplary embodiment.
[0086] Figure 3 The figure is a block diagram of a language conversion device according to an exemplary embodiment.
[0087] Figure 4 The invention is a block diagram of an electronic device for language conversion according to an exemplary embodiment.
[0088] Figure 5 It is a block diagram of a device for language conversion according to an exemplary embodiment. DETAILED DESCRIPTION
[0089] In order to enable ordinary persons in the art to better understand the technical solutions of the present disclosure, the technical solutions in the embodiments of the present disclosure will be clearly and completely described below in conjunction with the accompanying drawings.
[0090] It should be noted that the terms "first", "second", etc. in the specification and claims of the present disclosure and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present disclosure described herein can be implemented in an order other than those illustrated or described herein. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present disclosure. Instead, they are merely examples of devices and methods consistent with some aspects of the present disclosure as detailed in the appended claims.
[0091] Figure 1 is a flow chart of a language conversion method according to an exemplary embodiment. Figure 1 As shown, the language conversion method includes the following steps.
[0092] In step S11, a file to be processed is obtained and parsed to generate a syntax tree of the file to be processed. The file to be processed is a first language file. The syntax tree includes a plurality of nodes, and the nodes correspond one-to-one to the language structures included in the file to be processed.
[0093] Due to the development of computer programming languages, new programming languages continue to emerge. Usually, the development of a project is based on an old programming language. Therefore, it is necessary to convert the files of the old programming language into files of the new programming language to maintain the sustainable iterative development of the project. For the convenience of description, in this disclosure, the files using the old programming language are referred to as first language files, and the files using the new programming language are referred to as second language files.
[0094] The present disclosure may be run in an electronic device that supports running the first language file and the second language file, which may be a mobile terminal device, a client device, a server, etc., without specific limitation.
[0095] For example, the first language file can be a JavaScript code file, that is, the file to be processed is a code file compiled from the sample JavaScript language. It can be understood that JavaScript code files usually include a variety of different language structures, such as class declarations, class expressions, function declarations, function expressions, and member expressions, etc., which will not be repeated here.
[0096] You can use the Babel tool to process JavaScript code files and generate a syntax tree for the file to be processed. Each node in the syntax tree corresponds to the language structure included in the file to be processed. The syntax tree is AST (Abstract Syntax Tree), which is a tree-like abstract representation of the syntax structure in the code file. Through a series of operations on the AST tree nodes, the code can be modified efficiently and accurately. The Babel tool is a compiler for writing the next generation of JavaScript. It has a complete set of tool chains that provide the ability to parse source code into AST, traverse and transform AST, and generate target code from AST. The source code can be JavaScript code and the target code can be TypeScript code.
[0097] In step S12, in the syntax tree, the target node is searched upward level by level from the to-be-annotated nodes that meet the annotation conditions, and the target node corresponds to a class declaration or a class expression in the file to be processed.
[0098] In this step, it is necessary to filter out nodes that meet the annotation conditions from the syntax tree. Specifically, the nodes in the syntax tree can be traversed first. If the type of any node is a member expression, the type of the corresponding object node is an execution context expression, and the type of the corresponding property node is an identifier, then the node is used as a node to be annotated. Among them, the execution context expression is such as this expression. This expression is different from a function expression and is an expression that represents the execution context.
[0099] Among them, member expressions, objects, and attributes are all language structures in the file to be processed. Member expressions include two parts: objects and attributes. Correspondingly, in the syntax tree, member expressions, objects, and attributes are different nodes, and object nodes and attribute nodes are both child nodes of member expression nodes.
[0100] In this way, nodes related to the classes in the language file can be screened out from a large number of nodes in the syntax tree, which makes it easier to infer and annotate the language structures represented by these nodes in the future, so as to achieve automatic language conversion and improve language conversion efficiency.
[0101] In one implementation, in a syntax tree, a target node is searched for level by level from a node to be annotated that meets the annotation conditions, including: obtaining path information of the node to be annotated; based on the path information, searching for a parent node level by level from the node to be annotated, and if the type of any parent node found is a class declaration or a class expression, any parent node is used as a target node.
[0102] Among them, the path information is generated based on the position of the node to be annotated in the syntax tree, and can represent the association relationship between nodes. The path information can include various information such as the information of the node to be annotated, the information of the parent node of the node to be annotated, the information of the parent node of the parent node of the node to be annotated, etc. For example, each node's path information has a parentPath attribute, which represents the parent path of the current node. By accessing this attribute, you can search upwards step by step until the parent path is empty, indicating that you have reached the top of the syntax tree.
[0103] In this way, according to the path information, the node to be annotated can be searched upward step by step until the node corresponding to the class to which the node to be annotated belongs is found as the target node. Then, by analyzing the nodes in the syntax tree, the class to which the language structure represented by each node belongs in the file to be processed can be determined.
[0104] According to the path information, the node to be annotated can be queried upward step by step. If the type of any node queried is a class declaration or a class expression, then the node can be used as the target node. Among them, class is a language concept, and there are two types of nodes that can represent classes, namely class declaration (ClassDeclaration) and class expression (ClassExpression).
[0105] In addition, if the type of any parent node found is a function declaration (FunctionDeclaration) or a function expression (FunctionExpression), or if the type of any parent node found is not a class declaration or a class expression, it is determined that there is no target node corresponding to the node to be annotated.
[0106] It is understandable that in the process of searching upwards level by level, in some cases, a function may generate a new scope, and the node may not necessarily point to an instance of a class. In order to eliminate the interference of this situation, when the type of any parent node is a function declaration or a function expression, a null value can be directly returned, indicating that the corresponding class is not found and no relevant processing will be performed. When the type of the queried parent node is not a class declaration or a class expression, it means that the language structure represented by the node to be annotated does not belong to any class in the file to be processed. In other words, the corresponding class is not found and no relevant processing will be performed.
[0107] In the present disclosure, the pre-declared getClass method can be used to search for the target node from the node to be annotated step by step. The input parameter of the getClass method is the path information of the node to be annotated, and the return value is the path information of the target node. In the getClass method, the path.find method can be used to traverse the parent of the node to be annotated step by step:
[0108] If the type of the traversed node is a function declaration or function expression, a null value is returned directly; if the type of the traversed node is a class declaration or class expression, the path of the node is returned and the node is used as the target node; in other cases, the parent path is continued to be traversed until there is no parent path, and a null value is returned.
[0109] In step S13, when the main information of the target node does not include the name of the node to be labeled, the target attribute information of the node to be labeled is obtained, and the target attribute information is used to indicate the type of the parent node of the node to be labeled.
[0110] In the present disclosure, after searching for the target node from the node to be labeled step by step, it can be determined whether the main information of the target node includes the name of the node to be labeled. Specifically:
[0111] The main body information of the target node can be obtained, and the main body information is the body attribute information, and the body attribute information includes a target array, and the target array is the body array. Each element in the target array corresponds to any method or attribute included in the class corresponding to the target node, and each element has a corresponding element key value (key). When there is no element key value corresponding to the target key value of the node to be annotated, it is determined that the name of the node to be annotated is not included in the main body information of the target node, wherein the target key value is the name field (name) of the attribute node corresponding to the node to be annotated.
[0112] In the above embodiment, if there is no element key value corresponding to the target key value of the node to be annotated, it means that the member expression corresponding to the node to be annotated is not included in the class of the target node, and it is necessary to continue the subsequent steps and supplement the relevant declarations. If it exists, the name of the node to be annotated is included in the main information of the target node. At this time, it means that the member expression corresponding to the node to be annotated is already included in the class of the target node, so exit directly without subsequent modification. In this way, repeated annotation of the language structure represented by the node to be annotated can be avoided, and the efficiency and success rate of automatic language conversion can be improved.
[0113] In step S14, according to the target attribute information, a child node is added to the syntax tree for the target node, declaration information of the child node is generated, and the declaration information is inserted into the main information of the target node to obtain an updated syntax tree.
[0114] In this step, according to the target attribute information, the specific steps of adding a child node to the target node in the syntax tree and generating declaration information of the child node may include:
[0115] If the target attribute information is the first preset field, a child node is added to the target node in the syntax tree, the type of the child node is a method declaration, and the return value of the child node is an arbitrary type; then, the declaration information of the child node is generated, and the declaration information includes the type of the child node and the return value type.
[0116] Among them, the first preset field can be "callee". In this case, a new node corresponding to the method declaration is created (tsDeclareMethod), and its return value is set to any type (tsAnyKeyWOrd). The first preset field is "callee" which indicates that the node to be annotated represents the called function in a function call expression. Therefore, a child node representing the method declaration can be added to the target node in the syntax tree. The child node is used to annotate the node to be annotated, so that automatic language conversion can be achieved.
[0117] If the target attribute information is the second preset field, a child node is added to the target node in the syntax tree, the type of the child node is a class declaration, and the initialization value of the child node is the assigned attribute of the parent node of the node to be annotated; according to the type of the initialization value, the attribute information of the child node is determined; then, the declaration information of the child node is generated, and the declaration information includes the type of the child node, the initialization value and the attribute information.
[0118] Among them, the second preset field can be "left". In this case, a declaration of a class attribute is created, and the assignment (right) attribute of the parent node of the node to be annotated is obtained, which is regarded as the initialization value, and then the attribute information of the child node is determined according to the type of the initialization value. The second preset field is "left" indicating that the node to be annotated is on the left side of the equal sign in an assignment expression, that is, the object to be assigned. Therefore, the assignment attribute of the parent node of the node to be annotated can be obtained, that is, the right side of the equal sign in the assignment expression corresponding to the node to be annotated, and then a child node representing the class declaration is added to the target node in the syntax tree. The child node is used to annotate the node to be annotated, so that automatic language conversion can be achieved.
[0119] Specifically, the attribute information of the child node includes optional attributes and / or declared types. Correspondingly, the attribute information of the child node is determined according to the type of the initialization value, which may include:
[0120] If the initialization value is empty, the optional attribute of the child node is determined to be a preset field, wherein the preset field can be true, that is, if the initialization value is null or undefined, the value of its optional attribute (optional) is marked as true.
[0121] If the type of the initialization value is any data type, the declared type of the child node is determined to be that data type.
[0122] For example, if the initialization value is a BooleanLiteral, mark the declared type of the child node as a Boolean type (tsBooleanKeyword); if the initialization value is a NumericLiteral, mark the declared type of the child node as a numeric type (tsNumberKeyword); if the initialization value is a StringLiteral, mark the declared type of the child node as a numeric type (tsNumberKeyword); if the initialization value is an array expression (ArrayExpression), mark the declared type of the child node as an array (tsArrayType), and at the same time, the type of each item in the array can be marked as any type; if the initialization value is a construction expression (NewExpression), mark the declared type of the child node as the reference type (tsTypeReference) of the callee of the expression.
[0123] If the type of the initialization value is an identifier, determine the scope of the target node, search for the reference path of the identifier in the scope, determine the reference node corresponding to the reference path, update the initialization value of the child node according to the type and assignment attributes of the reference node, and determine the declaration type of the child node.
[0124] That is to say, if the initialization value is an identifier, try to find the binding information corresponding to the identifier in the scope of the target node. The binding information describes the usage of a variable in the current scope, which stores an array of the reference path of the current variable. Therefore, the reference path corresponding to the identifier can be found through the binding information, and then the corresponding reference node can be determined. Among them, the target node and the node to be annotated have the same scope.
[0125] Specifically, according to the type and assignment attribute of the reference node, the initialization value of the child node is updated and the declaration type of the child node is determined, including: if the type of the reference node is an assignment expression or a variable declaration, the assignment attribute of the reference node is used as the initialization value of the child node, and the step of determining the attribute information of the child node according to the type of the initialization value is returned.
[0126] In this way, the reference path and reference node corresponding to the identifier are found through the binding information, so as to further improve the annotation of the node to be annotated.
[0127] In some cases, the right attribute of the parent node of the node to be annotated may be a variety of different types of values. If multiple of the above situations are encountered in a class at the same time, the declared type of the child node can be marked as a union type (tsUnionType) composed of the above types.
[0128] In addition, for other cases, the declared type of the child node can be marked as any type, and its specific type needs to be manually annotated later.
[0129] In this way, according to different initialization values, different declaration types of child nodes can be determined, and then the annotation of the nodes to be annotated can be realized, thereby saving the time and labor costs of manual migration, while reducing the errors that may be caused by manual labor, and minimizing the risks and costs of the language conversion process.
[0130] In this step, after the type inference is completed, the declaration information is inserted into the main information of the target node, that is, the declaration of the method or attribute of the newly created class is inserted into the head of the body array of the class body attribute.
[0131] In step S15, a target file is generated according to the updated syntax tree, and the target file is a second language file.
[0132] For example, the second language file can be a TypeScript code file. The Babel tool generation module can be used to generate the updated syntax tree into TypeScript code. When outputting, the corresponding file suffix is changed from .js to .ts to obtain the target file.
[0133] That is to say, converting the first language file into the second language file is to convert the JavaScript code file into the TypeScript code file. Compared with the JavaScript language, the TypeScript language provides a clear type declaration definition, which greatly improves the readability and maintainability of the code and has a powerful IDE (Intergreated Development Environment). In other words, the TypeScript code file is more robust than the JavaScript code file. Therefore, converting the first language file into the second language file is conducive to the sustainable iterative development of computer projects.
[0134] In one implementation, the to-be-processed files are multiple files in a project. In this case, at least one to-be-processed file can be obtained, and then the to-be-processed file is parsed to generate a syntax tree of the to-be-processed file. The syntax tree includes the file path of the to-be-processed file, and the file path is used to indicate the location of the to-be-processed file in the target project. That is, a new field is added to each AST to record the path of the file corresponding to the current code.
[0135] In this way, the target file can be generated according to the updated syntax tree; the file path of the file to be processed can be read, and the target file can be output to the storage location corresponding to the file path. For a project including multiple files to be processed, the multiple files to be processed can be migrated at one time, and each file to be processed can be output to the corresponding storage location at one time, further reducing manual operations and minimizing the risk and cost of language conversion.
[0136] As can be seen from the above, the technical solution provided by the embodiment of the present disclosure can infer and annotate as much type information of class-related language structures as possible by analyzing the nodes in the syntax tree, which greatly saves the time and labor cost of manual migration, while reducing the errors that may be caused by manual labor, and minimizes the risk and cost of the language conversion process. At the same time, the language conversion process will not conflict with the project demand iteration process, and other similar scenarios can also refer to this solution for automatic batch processing of large amounts of code, and can also combine code annotations and other means to generate more detailed type information.
[0137] Figure 2 is a flow chart of a language conversion method according to an exemplary embodiment. Figure 2 As shown, the language conversion method includes the following steps.
[0138] In step S21, a file to be processed is obtained and parsed to generate a syntax tree of the file to be processed. The file to be processed is a first language file. The syntax tree includes a plurality of nodes, and the nodes correspond one-to-one to the language structures included in the file to be processed.
[0139] For example, the first language file can be a JavaScript code file, that is, the file to be processed is a code file compiled from the sample JavaScript language. It can be understood that JavaScript code files usually include a variety of different language structures, such as class declarations, class expressions, function declarations, function expressions, and member expressions, etc., which will not be repeated here.
[0140] You can use the Babel tool to process JavaScript code files and generate a syntax tree for the file to be processed. Each node in the syntax tree corresponds to the language structure included in the file to be processed. The syntax tree is AST (Abstract Syntax Tree), which is a tree-like abstract representation of the syntax structure in the code file. Through a series of operations on the AST tree nodes, the code can be modified efficiently and accurately. The Babel tool is a compiler for writing the next generation of JavaScript. It has a complete set of tool chains that provide the ability to parse source code into AST, traverse and transform AST, and generate target code from AST. The source code can be JavaScript code and the target code can be TypeScript code.
[0141] In step S22, the nodes in the syntax tree are traversed, and if the type of any node is a member expression, the type of the corresponding object node is an execution context expression, and the type of the corresponding attribute node is an identifier, the node is taken as a node to be labeled.
[0142] Among them, the execution context expression is also called this expression (ThisExpression). This expression is different from the function expression and is an expression representing the execution context.
[0143] Among them, member expressions, objects, and attributes are all language structures in the file to be processed. Member expressions include two parts: objects and attributes. Correspondingly, in the syntax tree, member expressions, objects, and attributes are different nodes, and object nodes and attribute nodes are both child nodes of member expression nodes.
[0144] In this way, nodes related to the classes in the language file can be screened out from a large number of nodes in the syntax tree, which makes it easier to infer and annotate the language structures represented by these nodes in the future, so as to achieve automatic language conversion and improve language conversion efficiency.
[0145] In step S23, in the syntax tree, the path information of the node to be annotated that meets the annotation conditions is obtained; according to the path information, the parent node is queried from the node to be annotated step by step, and if the type of any parent node queried is a class declaration or a class expression, any parent node is used as the target node.
[0146] Among them, the path information is generated based on the position of the node to be annotated in the syntax tree, and can represent the association relationship between nodes. The path information can include various information such as the information of the node to be annotated, the information of the parent node of the node to be annotated, the information of the parent node of the parent node of the node to be annotated, etc. For example, each node's path information has a parentPath attribute, which represents the parent path of the current node. By accessing this attribute, you can search upwards step by step until the parent path is empty, indicating that you have reached the top of the syntax tree.
[0147] In this way, according to the path information, the node to be annotated can be searched upward step by step until the node corresponding to the class to which the node to be annotated belongs is found as the target node. Then, by analyzing the nodes in the syntax tree, the class to which the language structure represented by each node belongs in the file to be processed can be determined.
[0148] According to the path information, the node to be annotated can be queried upward step by step. If the type of any node queried is a class declaration or a class expression, then the node can be used as the target node. Among them, class is a language concept, and there are two types of nodes that can represent classes, namely class declaration (ClassDeclaration) and class expression (ClassExpression).
[0149] In addition, if the type of any parent node found is a function declaration (FunctionDeclaration) or a function expression (FunctionExpression), or if the type of any parent node found is not a class declaration or a class expression, it is determined that there is no target node corresponding to the node to be annotated.
[0150] It is understandable that in the process of searching upwards level by level, in some cases, a function may generate a new scope, and the node may not necessarily point to an instance of a class. In order to eliminate the interference of this situation, when the type of any parent node is a function declaration or a function expression, a null value can be directly returned, indicating that the corresponding class is not found and no relevant processing will be performed. When the type of the queried parent node is not a class declaration or a class expression, it means that the language structure represented by the node to be annotated does not belong to any class in the file to be processed. In other words, the corresponding class is not found and no relevant processing will be performed.
[0151] In the present disclosure, the pre-declared getClass method can be used to search for the target node from the node to be annotated step by step. The input parameter of the getClass method is the path information of the node to be annotated, and the return value is the path information of the target node. In the getClass method, the path.find method can be used to traverse the parent of the node to be annotated step by step:
[0152] If the type of the traversed node is a function declaration or function expression, a null value is returned directly; if the type of the traversed node is a class declaration or class expression, the path of the node is returned and the node is used as the target node; in other cases, the parent path is continued to be traversed until there is no parent path, and a null value is returned.
[0153] In step S24, the main information of the target node is obtained, and the main information includes a target array. Each element in the target array corresponds to any method or attribute included in the class corresponding to the target node, and each element has a corresponding element key value; when there is no element key value corresponding to the target key value of the node to be annotated, the target attribute information of the node to be annotated is obtained, and the target attribute information is used to indicate the type of the parent node of the node to be annotated.
[0154] In the present disclosure, after searching for the target node from the node to be labeled step by step, it can be determined whether the main information of the target node includes the name of the node to be labeled. Specifically:
[0155] The main body information of the target node can be obtained, and the main body information is the body attribute information, and the body attribute information includes a target array, and the target array is the body array. Each element in the target array corresponds to any method or attribute included in the class corresponding to the target node, and each element has a corresponding element key value (key). When there is no element key value corresponding to the target key value of the node to be annotated, it is determined that the name of the node to be annotated is not included in the main body information of the target node, wherein the target key value is the name field (name) of the attribute node corresponding to the node to be annotated.
[0156] In other words, if there is no element key value corresponding to the target key value of the node to be annotated, it means that the member expression corresponding to the node to be annotated is not included in the class of the target node, and you need to continue with the subsequent steps and add relevant declarations. If it exists, the name of the node to be annotated is included in the main information of the target node. At this time, it means that the member expression corresponding to the node to be annotated is already included in the class of the target node, so exit directly without subsequent modification. In this way, repeated annotation of the language structure represented by the node to be annotated can be avoided, and the efficiency and success rate of automatic language conversion can be improved.
[0157] In step S25, if the target attribute information is the first preset field, a child node is added to the target node in the syntax tree, the type of the child node is a method declaration, and the return value of the child node is an arbitrary type; the declaration information of the child node is generated, and the declaration information includes the type of the child node and the return value type.
[0158] Among them, the first preset field can be "callee". In this case, a new node corresponding to the method declaration is created (tsDeclareMethod), and its return value is set to any type (tsAnyKeyWOrd). The first preset field is "callee" which indicates that the node to be annotated represents the called function in a function call expression. Therefore, a child node representing the method declaration can be added to the target node in the syntax tree. The child node is used to annotate the node to be annotated, so that automatic language conversion can be achieved.
[0159] In step S26, if the target attribute information is the second preset field, a child node is added to the target node in the syntax tree, the type of the child node is a class declaration, and the initialization value of the child node is the assigned attribute of the parent node of the node to be annotated; according to the type of the initialization value, the attribute information of the child node is determined; and the declaration information of the child node is generated, and the declaration information includes the type of the child node, the initialization value and the attribute information.
[0160] Among them, the second preset field can be "left". In this case, a declaration of a class attribute is created, and the assignment (right) attribute of the parent node of the node to be annotated is obtained, which is regarded as the initialization value, and then the attribute information of the child node is determined according to the type of the initialization value. The second preset field is "left" indicating that the node to be annotated is on the left side of the equal sign in an assignment expression, that is, the object to be assigned. Therefore, the assignment attribute of the parent node of the node to be annotated can be obtained, that is, the right side of the equal sign in the assignment expression corresponding to the node to be annotated, and then a child node representing the class declaration is added to the target node in the syntax tree. The child node is used to annotate the node to be annotated, so that automatic language conversion can be achieved.
[0161] Specifically, the attribute information of the child node includes optional attributes and / or declared types. Correspondingly, the attribute information of the child node is determined according to the type of the initialization value, which may include:
[0162] If the initialization value is empty, the optional attribute of the child node is determined to be a preset field, wherein the preset field can be true, that is, if the initialization value is null or undefined, the value of its optional attribute (optional) is marked as true.
[0163] If the type of the initialization value is any data type, the declared type of the child node is determined to be that data type.
[0164] For example, if the initialization value is a BooleanLiteral, mark the declared type of the child node as a Boolean type (tsBooleanKeyword); if the initialization value is a NumericLiteral, mark the declared type of the child node as a numeric type (tsNumberKeyword); if the initialization value is a StringLiteral, mark the declared type of the child node as a numeric type (tsNumberKeyword); if the initialization value is an array expression (ArrayExpression), mark the declared type of the child node as an array (tsArrayType), and at the same time, the type of each item in the array can be marked as any type; if the initialization value is a construction expression (NewExpression), mark the declared type of the child node as the reference type (tsTypeReference) of the callee of the expression.
[0165] If the type of the initialization value is an identifier, determine the scope of the target node, search for the reference path of the identifier in the scope, determine the reference node corresponding to the reference path, update the initialization value of the child node according to the type and assignment attributes of the reference node, and determine the declaration type of the child node.
[0166] That is to say, if the initialization value is an identifier, try to find the binding information corresponding to the identifier in the scope of the target node. The binding information describes the usage of a variable in the current scope, which stores an array of the reference path of the current variable. Therefore, the reference path corresponding to the identifier can be found through the binding information, and then the corresponding reference node can be determined. Among them, the target node and the node to be annotated have the same scope.
[0167] Specifically, according to the type and assignment attribute of the reference node, the initialization value of the child node is updated and the declaration type of the child node is determined, including: if the type of the reference node is an assignment expression or a variable declaration, the assignment attribute of the reference node is used as the initialization value of the child node, and the step of determining the attribute information of the child node according to the type of the initialization value is returned.
[0168] In this way, the reference path and reference node corresponding to the identifier are found through the binding information, so as to further improve the annotation of the node to be annotated.
[0169] In some cases, the right attribute of the parent node of the node to be annotated may be a variety of different types of values. If multiple of the above situations are encountered in a class at the same time, the declared type of the child node can be marked as a union type (tsUnionType) composed of the above types.
[0170] In addition, for other cases, the declared type of the child node can be marked as any type, and its specific type needs to be manually annotated later.
[0171] In this way, according to different initialization values, different declaration types of child nodes can be determined, and then the annotation of the nodes to be annotated can be realized, thereby saving the time and labor costs of manual migration, while reducing the errors that may be caused by manual labor, and minimizing the risks and costs of the language conversion process.
[0172] In this step, after the type inference is completed, the declaration information is inserted into the main information of the target node, that is, the declaration of the method or attribute of the newly created class is inserted into the head of the body array of the class body attribute.
[0173] In step S27, a target file is generated according to the updated syntax tree, and the target file is a second language file.
[0174] For example, the second language file can be a TypeScript code file. The Babel tool generation module can be used to generate the updated syntax tree into TypeScript code. When outputting, the corresponding file suffix is changed from .js to .ts to obtain the target file.
[0175] That is to say, converting the first language file into the second language file is to convert the JavaScript code file into the TypeScript code file. Compared with the JavaScript language, the TypeScript language provides a clear type declaration definition, which greatly improves the readability and maintainability of the code and has a powerful IDE. In other words, the TypeScript code file is more robust than the JavaScript code file. Therefore, converting the first language file into the second language file is conducive to the sustainable iterative development of computer projects.
[0176] In one implementation, the to-be-processed files are multiple files in a project. In this case, at least one to-be-processed file can be obtained, and then the to-be-processed file is parsed to generate a syntax tree of the to-be-processed file. The syntax tree includes the file path of the to-be-processed file, and the file path is used to indicate the location of the to-be-processed file in the target project. That is, a new field is added to each AST to record the path of the file corresponding to the current code.
[0177] In this way, the target file can be generated according to the updated syntax tree; the file path of the file to be processed can be read, and the target file can be output to the storage location corresponding to the file path. For a project including multiple files to be processed, the multiple files to be processed can be migrated at one time, and each file to be processed can be output to the corresponding storage location at one time, further reducing manual operations and minimizing the risk and cost of language conversion.
[0178] As can be seen from the above, the technical solution provided by the embodiment of the present disclosure can infer and annotate as much type information of class-related language structures as possible by analyzing the nodes in the syntax tree, which greatly saves the time and labor cost of manual migration, while reducing the errors that may be caused by manual labor, and minimizes the risk and cost of the language conversion process. At the same time, the language conversion process will not conflict with the project demand iteration process, and other similar scenarios can also refer to this solution for automatic batch processing of large amounts of code, and can also combine code annotations and other means to generate more detailed type information.
[0179] Figure 3 is a block diagram of a language conversion device according to an exemplary embodiment, the device comprising:
[0180] The parsing unit 301 is configured to obtain and parse a file to be processed, and generate a syntax tree of the file to be processed, wherein the file to be processed is a first language file, and the syntax tree includes a plurality of nodes, and the nodes correspond one-to-one to the language structures included in the file to be processed;
[0181] A query unit 302 is configured to execute in the syntax tree, searching for a target node from the to-be-annotated node that meets the annotation condition, the target node corresponding to the class declaration or class expression in the to-be-processed file;
[0182] An acquiring unit 303 is configured to acquire target attribute information of the node to be labeled when the main information of the target node does not include the name of the node to be labeled, wherein the target attribute information is used to indicate a type of a parent node of the node to be labeled;
[0183] A generating unit 304 is configured to perform, according to the target attribute information, adding a child node to the target node in the syntax tree, generating declaration information of the child node, inserting the declaration information into the main information of the target node, and obtaining an updated syntax tree;
[0184] The conversion unit 305 is configured to generate a target file according to the updated syntax tree, where the target file is a second language file.
[0185] In one implementation, the query unit 302 is configured to execute:
[0186] Traverse the nodes in the syntax tree. If the type of any node is a member expression, the type of the object node corresponding to any node is an execution context expression, and the type of the attribute node corresponding to any node is an identifier, then take the any node as a node to be annotated, and search for the target node from the node to be annotated step by step upward.
[0187] In one implementation, the query unit 302 is configured to execute:
[0188] In the syntax tree, obtaining path information of the nodes to be annotated that meet the annotation conditions;
[0189] According to the path information, the node to be labeled is queried for parent nodes step by step. If the type of any parent node queried is a class declaration or a class expression, any parent node is used as a target node.
[0190] In one implementation, the query unit 302 is configured to execute:
[0191] If the type of any parent node found is a function declaration or a function expression, or if the type of any parent node found is not a class declaration or a class expression, it is determined that there is no target node corresponding to the node to be annotated.
[0192] In one implementation, the query unit 302 is configured to execute:
[0193] Obtaining the main body information of the target node, wherein the main body information includes a target array, each element in the target array corresponds to any device or attribute included in the class corresponding to the target node, and each element has a corresponding element key value;
[0194] When there is no element key value corresponding to the target key value of the node to be annotated, it is determined that the name of the node to be annotated is not included in the main information of the target node, and the target key value is the name field of the attribute node corresponding to the node to be annotated.
[0195] In one implementation, the generating unit 304 is configured to execute:
[0196] If the target attribute information is a first preset field, a child node is added to the target node in the syntax tree, the type of the child node is a device declaration, and the return value of the child node is an arbitrary type;
[0197] Generate declaration information of the child node, where the declaration information includes the type of the child node and the return value type.
[0198] In one implementation, the generating unit 304 is configured to execute:
[0199] If the target attribute information is the second preset field, a child node is added to the target node in the syntax tree, the type of the child node is a class declaration, and the initialization value of the child node is the assigned attribute of the parent node of the node to be annotated;
[0200] Determining attribute information of the child node according to the type of the initialization value;
[0201] Generate declaration information of the child node, where the declaration information includes the type, initialization value, and attribute information of the child node.
[0202] In one implementation, the attribute information of the child node includes optional attributes and / or declaration types, and the generating unit 304 is configured to execute:
[0203] If the initialization value is empty, determining that the optional attribute of the child node is a preset field;
[0204] If the type of the initialization value is any data type, determining the declared type of the child node to be any data type;
[0205] If the type of the initialization value is an identifier, determine the scope of the target node, search for the reference path of the identifier in the scope, determine the reference node corresponding to the reference path, update the initialization value of the child node according to the type and assignment attributes of the reference node, and determine the declaration type of the child node.
[0206] In one implementation, the generating unit 304 is configured to execute:
[0207] If the type of the reference node is an assignment expression or a variable declaration, the assignment attribute of the reference node is used as the initialization value of the child node, and the step of determining the attribute information of the child node according to the type of the initialization value is returned.
[0208] In one implementation, the parsing unit 301 is configured to execute:
[0209] Get at least one file to be processed;
[0210] Parsing the file to be processed to generate a syntax tree of the file to be processed, wherein the syntax tree includes a file path of the file to be processed, and the file path is used to indicate a location of the file to be processed in the target project;
[0211] The conversion unit 305 is further configured to execute:
[0212] The file path of the file to be processed is read, and the target file is output to a storage location corresponding to the file path.
[0213] It can be seen from the above that the technical solution provided by the embodiments of the present disclosure can infer and annotate as much type information of class-related language structures as possible by analyzing the nodes in the syntax tree, which greatly saves the time and labor costs of manual migration, while reducing the errors that may be caused by human labor, and minimizes the risks and costs of the language conversion process.
[0214] Regarding the device in the above embodiment, the specific manner in which each module performs operations has been described in detail in the embodiment of the method, and will not be elaborated here.
[0215] Figure 3 The invention is a block diagram of an electronic device for language conversion according to an exemplary embodiment.
[0216] In an exemplary embodiment, a computer-readable storage medium including instructions is also provided, such as a memory including instructions, and the above instructions can be executed by a processor of an electronic device to perform the above method. Optionally, the computer-readable storage medium can be a ROM, a random access memory (RAM), a CD-ROM, a magnetic tape, a floppy disk, an optical data storage device, etc.
[0217] In an exemplary embodiment, a computer program product is also provided. When the computer program product is executed on a computer, the computer implements the above-mentioned language conversion method.
[0218] It can be seen from the above that the technical solution provided by the embodiments of the present disclosure can infer and annotate as much type information of class-related language structures as possible by analyzing the nodes in the syntax tree, which greatly saves the time and labor costs of manual migration, while reducing the errors that may be caused by human labor, and minimizes the risks and costs of the language conversion process.
[0219] Figure 4 It is a block diagram of a device 800 for language conversion according to an exemplary embodiment.
[0220] For example, apparatus 800 may be a mobile phone, a computer, a digital broadcast electronic device, a messaging device, a game console, a tablet device, a medical device, a fitness device, a personal digital assistant, etc.
[0221] Reference Figure 4 , the device 800 may include one or more of the following components: a processing component 802 , a memory 804 , a power component 806 , a multimedia component 808 , an audio component 810 , an input / output (I / O) interface 812 , a sensor component 814 , and a communication component 816 .
[0222] The processing component 802 generally controls the overall operation of the device 800, such as operations associated with display, phone calls, data communications, camera operations, and recording operations. The processing component 802 may include one or more processors 820 to execute instructions to complete all or part of the steps of the above-mentioned method. In addition, the processing component 802 may include one or more modules to facilitate the interaction between the processing component 802 and other components. For example, the processing component 802 may include a multimedia module to facilitate the interaction between the multimedia component 808 and the processing component 802.
[0223] The memory 804 is configured to store various types of data to support operations on the device 800. Examples of such data include instructions for any application or method operating on the device 800, contact data, phone book data, messages, pictures, videos, etc. The memory 804 can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk.
[0224] The power supply component 807 provides power to the various components of the device 800. The power supply component 807 may include a power management system, one or more power supplies, and other components associated with generating, managing, and distributing power for the device 800.
[0225] The multimedia component 808 includes a screen that provides an output interface between the device 800 and the user. In some embodiments, the screen may include a liquid crystal display (LCD) and a touch panel (TP). If the screen includes a touch panel, the screen may be implemented as a touch screen to receive input signals from the user. The touch panel includes one or more touch sensors to sense touch, slide, and gestures on the touch panel. The touch sensor may not only sense the boundaries of the touch or slide action, but also detect the duration and pressure associated with the touch or slide operation. In some embodiments, the multimedia component 808 includes a front camera and / or a rear camera. When the device 800 is in an operating mode, such as a shooting mode or a video mode, the front camera and / or the rear camera may receive external multimedia data. Each front camera and rear camera may be a fixed optical lens system or have a focal length and optical zoom capability.
[0226] The audio component 810 is configured to output and / or input audio signals. For example, the audio component 810 includes a microphone (MIC), and when the device 800 is in an operating mode, such as a call mode, a recording mode, and a speech recognition mode, the microphone is configured to receive an external audio signal. The received audio signal can be further stored in the memory 804 or sent via the communication component 816. In some embodiments, the audio component 810 also includes a speaker for outputting audio signals.
[0227] I / O interface 812 provides an interface between processing component 802 and peripheral interface modules, such as keyboards, click wheels, buttons, etc. These buttons may include but are not limited to: home button, volume button, start button, and lock button.
[0228] The sensor assembly 814 includes one or more sensors for providing various aspects of status assessment for the device 800. For example, the sensor assembly 814 can detect the open / closed state of the device 800, the relative positioning of components, such as the display and keypad of the device 800, and the sensor assembly 814 can also detect the position change of the device 800 or a component of the device 800, the presence or absence of user contact with the device 800, the orientation or acceleration / deceleration of the device 800, and the temperature change of the device 800. The sensor assembly 814 can include a proximity sensor configured to detect the presence of nearby objects without any physical contact. The sensor assembly 814 can also include an optical sensor, such as a CMOS or CCD image sensor, for use in imaging applications. In some embodiments, the sensor assembly 814 can also include an acceleration sensor, a gyroscope sensor, a magnetic sensor, a pressure sensor, or a temperature sensor.
[0229] The communication component 816 is configured to facilitate wired or wireless communication between the device 800 and other devices. The device 800 can access a wireless network based on a communication standard, such as WiFi, a carrier network (such as 2G, 3G, 4G or 5G), or a combination thereof. In an exemplary embodiment, the communication component 816 receives a broadcast signal or broadcast-related information from an external broadcast management system via a broadcast channel. In an exemplary embodiment, the communication component 816 also includes a near field communication (NFC) module to facilitate short-range communication. For example, the NFC module can be implemented based on radio frequency identification (RFID) technology, infrared data association (IrDA) technology, ultra-wideband (UWB) technology, Bluetooth (BT) technology and other technologies.
[0230] In an exemplary embodiment, the apparatus 800 may be implemented by one or more application-specific integrated circuits (ASICs), digital signal processors (DSPs), digital signal processing devices (DSPDs), programmable logic devices (PLDs), field programmable gate arrays (FPGAs), controllers, microcontrollers, microprocessors or other electronic components to execute the methods described in the first and second aspects.
[0231] In an exemplary embodiment, a non-transitory computer-readable storage medium including instructions is also provided, such as a memory 804 including instructions, and the instructions can be executed by the processor 820 of the device 800 to complete the above method. Optionally, for example, the storage medium can be a non-transitory computer-readable storage medium, for example, the non-transitory computer-readable storage medium can be a ROM, a random access memory (RAM), a CD-ROM, a magnetic tape, a floppy disk, an optical data storage device, etc.
[0232] In an exemplary embodiment, a computer program product including instructions is also provided. When the computer program product is run on a computer, the computer is enabled to perform the language conversion method described in any one of the above embodiments.
[0233] It can be seen from the above that the technical solution provided by the embodiments of the present disclosure can infer and annotate as much type information of class-related language structures as possible by analyzing the nodes in the syntax tree, which greatly saves the time and labor costs of manual migration, while reducing the errors that may be caused by human labor, and minimizes the risks and costs of the language conversion process.
[0234] Those skilled in the art will readily appreciate other embodiments of the present disclosure after considering the specification and practicing the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of the present disclosure that follow the general principles of the present disclosure and include common knowledge or customary techniques in the art that are not disclosed in the present disclosure. The specification and examples are intended to be exemplary only, and the true scope and spirit of the present disclosure are indicated by the following claims.
[0235] It should be understood that the present disclosure is not limited to the exact structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present disclosure is limited only by the appended claims.
Claims
1. A language conversion method, characterized in that: include: Acquire and parse a file to be processed, and generate a syntax tree of the file to be processed, wherein the file to be processed is a first language file, and the syntax tree includes a plurality of nodes, and the nodes correspond one-to-one to the language structures included in the file to be processed; In the syntax tree, searching for a target node from the to-be-annotated nodes that meet the annotation conditions step by step upward, the target node corresponding to the class declaration or class expression in the to-be-processed file; In a case where the main information of the target node does not include the name of the node to be labeled, acquiring target attribute information of the node to be labeled, where the target attribute information is used to indicate the type of the parent node of the node to be labeled; According to the target attribute information, a child node is added to the syntax tree for the target node, declaration information of the child node is generated, and the declaration information is inserted into the main information of the target node to obtain an updated syntax tree; Generate a target file according to the updated syntax tree, wherein the target file is a second language file; The adding a child node to the target node in the syntax tree according to the target attribute information and generating declaration information of the child node includes: Determining attribute information of the child node according to the type of the initialization value of the child node; The attribute information of the child node includes optional attributes and / or declaration types, and the determining the attribute information of the child node according to the type of the initialization value includes: If the initialization value is empty, determining that the optional attribute of the child node is a preset field; If the type of the initialization value is any data type, determining the declared type of the child node to be any data type; If the type of the initialization value is an identifier, determine the scope of the target node, search for the reference path of the identifier in the scope, determine the reference node corresponding to the reference path, update the initialization value of the child node according to the type and assignment attributes of the reference node, and determine the declaration type of the child node.
2. The language conversion method according to claim 1, characterized in that: The step of searching for a target node in the syntax tree from the to-be-annotated nodes that meet the annotation conditions step by step upwards includes: Traverse the nodes in the syntax tree. If the type of any node is a member expression, the type of the object node corresponding to any node is an execution context expression, and the type of the attribute node corresponding to any node is an identifier, then take the any node as a node to be annotated, and search for the target node from the node to be annotated step by step upward.
3. The language conversion method according to claim 1, characterized in that: The step of searching for a target node in the syntax tree from the to-be-annotated nodes that meet the annotation conditions step by step upwards includes: In the syntax tree, obtaining path information of the nodes to be annotated that meet the annotation conditions; According to the path information, the node to be labeled is queried for parent nodes step by step. If the type of any parent node queried is a class declaration or a class expression, any parent node is used as a target node.
4. The language conversion method according to claim 3, characterized in that: The method further comprises: If the type of any parent node found is a function declaration or a function expression, or if the type of any parent node found is not a class declaration or a class expression, it is determined that there is no target node corresponding to the node to be annotated.
5. The language conversion method according to claim 1, characterized in that: After searching for a target node in the syntax tree from the to-be-annotated nodes that meet the annotation condition, the method further includes: Obtaining the main information of the target node, wherein the main information includes a target array, each element in the target array corresponds to any method or attribute included in the class corresponding to the target node, and each element has a corresponding element key value; When there is no element key value corresponding to the target key value of the node to be annotated, it is determined that the name of the node to be annotated is not included in the main information of the target node, and the target key value is the name field of the attribute node corresponding to the node to be annotated.
6. The language conversion method according to claim 1, characterized in that: The adding a child node to the target node in the syntax tree according to the target attribute information and generating declaration information of the child node includes: If the target attribute information is the first preset field, a child node is added to the target node in the syntax tree, the type of the child node is a method declaration, and the return value of the child node is an arbitrary type; Generate declaration information of the child node, where the declaration information includes the type of the child node and the return value type.
7. The language conversion method according to claim 1, characterized in that: The adding a child node to the target node in the syntax tree according to the target attribute information and generating declaration information of the child node also includes: If the target attribute information is the second preset field, a child node is added to the target node in the syntax tree, the type of the child node is a class declaration, and the initialization value of the child node is the assigned attribute of the parent node of the node to be annotated; Generate declaration information of the child node, where the declaration information includes the type, initialization value, and attribute information of the child node.
8. The language conversion method according to claim 1, characterized in that: The updating of the initialization value of the child node according to the type and the assignment attribute of the reference node and determining the declaration type of the child node includes: If the type of the reference node is an assignment expression or a variable declaration, the assignment attribute of the reference node is used as the initialization value of the child node, and the step of determining the attribute information of the child node according to the type of the initialization value is returned.
9. The language conversion method according to claim 1, characterized in that: The obtaining and parsing of the to-be-processed file and generating a syntax tree of the to-be-processed file includes: Get at least one file to be processed; Parsing the file to be processed to generate a syntax tree of the file to be processed, wherein the syntax tree includes a file path of the file to be processed, and the file path is used to indicate a location of the file to be processed in the target project; After generating the target file according to the updated syntax tree, the method further includes: The file path of the file to be processed is read, and the target file is output to a storage location corresponding to the file path.
10. A language conversion device, characterized in that: include: A parsing unit is configured to obtain and parse a file to be processed, and generate a syntax tree of the file to be processed, wherein the file to be processed is a first language file, and the syntax tree includes a plurality of nodes, and the nodes correspond one-to-one to the language structures included in the file to be processed; A query unit is configured to execute in the syntax tree, searching for a target node from the to-be-annotated node that meets the annotation condition, step by step upward, wherein the target node corresponds to a class declaration or a class expression in the to-be-processed file; an acquiring unit, configured to acquire target attribute information of the node to be labeled when the subject information of the target node does not include the name of the node to be labeled, wherein the target attribute information is used to indicate a type of a parent node of the node to be labeled; A generating unit is configured to add a child node to the target node in the syntax tree according to the target attribute information, generate declaration information of the child node, insert the declaration information into the main information of the target node, and obtain an updated syntax tree; A conversion unit, configured to generate a target file according to the updated syntax tree, wherein the target file is a second language file; The generating unit is configured to execute: Determining attribute information of the child node according to the type of the initialization value of the child node; The attribute information of the child node includes optional attributes and / or declaration types, and the generating unit is configured to execute: If the initialization value is empty, determining that the optional attribute of the child node is a preset field; If the type of the initialization value is any data type, determining the declared type of the child node to be any data type; If the type of the initialization value is an identifier, determine the scope of the target node, search for the reference path of the identifier in the scope, determine the reference node corresponding to the reference path, update the initialization value of the child node according to the type and assignment attributes of the reference node, and determine the declaration type of the child node.
11. The language conversion device according to claim 10, characterized in that: The query unit is configured to execute: Traverse the nodes in the syntax tree. If the type of any node is a member expression, the type of the object node corresponding to any node is an execution context expression, and the type of the attribute node corresponding to any node is an identifier, then take the any node as a node to be annotated, and search for the target node step by step from the node to be annotated.
12. The language conversion device according to claim 10, characterized in that: The query unit is configured to execute: In the syntax tree, obtaining path information of the nodes to be annotated that meet the annotation conditions; According to the path information, the node to be labeled is queried for parent nodes step by step. If the type of any parent node queried is a class declaration or a class expression, any parent node is used as a target node.
13. The language conversion device according to claim 12, characterized in that: The query unit is configured to execute: If the type of any parent node found is a function declaration or a function expression, or if the type of any parent node found is not a class declaration or a class expression, it is determined that there is no target node corresponding to the node to be annotated.
14. The language conversion device according to claim 10, characterized in that: The query unit is configured to execute: Obtaining the main body information of the target node, wherein the main body information includes a target array, each element in the target array corresponds to any device or attribute included in the class corresponding to the target node, and each element has a corresponding element key value; When there is no element key value corresponding to the target key value of the node to be annotated, it is determined that the name of the node to be annotated is not included in the main information of the target node, and the target key value is the name field of the attribute node corresponding to the node to be annotated.
15. The language conversion device according to claim 10, characterized in that: The generating unit is configured to execute: If the target attribute information is a first preset field, a child node is added to the target node in the syntax tree, the type of the child node is a device declaration, and the return value of the child node is an arbitrary type; Generate declaration information of the child node, where the declaration information includes the type of the child node and the return value type.
16. The language conversion device according to claim 10, characterized in that: The generating unit is further configured to execute: If the target attribute information is the second preset field, a child node is added to the target node in the syntax tree, the type of the child node is a class declaration, and the initialization value of the child node is the assigned attribute of the parent node of the node to be annotated; Generate declaration information of the child node, where the declaration information includes the type, initialization value, and attribute information of the child node.
17. The language conversion device according to claim 10, characterized in that: The generating unit is configured to execute: If the type of the reference node is an assignment expression or a variable declaration, the assignment attribute of the reference node is used as the initialization value of the child node, and the step of determining the attribute information of the child node according to the type of the initialization value is returned.
18. The language conversion device according to claim 10, characterized in that: The parsing unit is configured to perform: Get at least one file to be processed; Parsing the file to be processed to generate a syntax tree of the file to be processed, wherein the syntax tree includes a file path of the file to be processed, and the file path is used to indicate a location of the file to be processed in the target project; The conversion unit is further configured to execute: The file path of the file to be processed is read, and the target file is output to a storage location corresponding to the file path.
19. An electronic device, characterized in that: include: processor; a memory for storing instructions executable by the processor; The processor is configured to execute the instructions to implement the language conversion method according to any one of claims 1 to 9.
20. A computer-readable storage medium, characterized in that: When the instructions in the computer-readable storage medium are executed by a processor of a language conversion electronic device, the language conversion electronic device is enabled to perform the language conversion method according to any one of claims 1 to 9.
21. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the language conversion method according to any one of claims 1 to 9 is implemented.
Citation Information
Patent Citations
Language switching method and device, equipment and storage medium
CN111831384A