Transcoding method and apparatus
By obtaining component tags from the source code to generate code import information, and using a code parser to parse and generate target code in a specific format, the problem of low efficiency in converting low-level and professional code is solved, achieving efficient and accurate code conversion.
Patent Information
- Application Number
- CN202210810528.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-07-11
- Publication Date
- 2026-11-24
- Estimated Expiration
- 2042-07-11
AI Technical Summary
In existing technologies, low-code cannot be directly converted into professional code, and the conversion of professional code into low-code requires the construction of an abstract syntax tree, resulting in low conversion efficiency and a high risk of omissions and errors.
By obtaining component tags from the source code, code import information is generated, and a preset code parser is used to execute the target component generation function to parse the assembled code and generate target code in a specific format, thus avoiding the intermediate steps of building an abstract syntax tree.
It improves the adaptability and accuracy of code conversion, avoids omissions and errors, and increases conversion efficiency.
Smart Images

Figure CN115268925B_ABST
Abstract
Description
Technical Field
[0001] The embodiments in this specification relate to the field of computer code writing technology, and in particular to a code conversion method. Background Technology
[0002] With the development of internet technology, internet products targeting various project scenarios are emerging in large numbers. For simple project scenarios, applications can be developed using low-code in a specific format. Low-code, due to its visual and easy-to-use characteristics, allows for application development with less code and at a faster pace. However, for highly complex project scenarios, changes in the project scenario require rewriting low-code protocols, which limits its applicability and leads to insufficient maintainability of internet products. Procode, on the other hand, can be directly written for project scenarios. Using procode for application development makes later maintenance of internet products much easier and improves their stability.
[0003] Currently, converting low-code to professional code is straightforward because low-code lacks underlying content; the conversion can be performed directly by understanding the correspondence between the code levels of the two.
[0004] However, professional code contains underlying content not found in low-code, making it impossible to directly convert professional code to low-code using simple mappings. Therefore, a highly adaptable code conversion method is urgently needed. Summary of the Invention
[0005] In view of the above, embodiments of this specification provide a code conversion method. One or more embodiments of this specification also relate to a code conversion apparatus, a computing device, a computer-readable storage medium, and a computer program, to address the technical deficiencies existing in the prior art.
[0006] According to a first aspect of the embodiments of this specification, a code conversion method is provided, comprising:
[0007] Obtain the source code;
[0008] Identify component tags from the source code;
[0009] Generate code import information based on component tags;
[0010] The source code and code import information are assembled to obtain the assembled code;
[0011] The pre-defined code parser is invoked to execute the target component generation function to parse the assembled code and generate target code in a specific format.
[0012] According to a second aspect of the embodiments of this specification, a code conversion apparatus is provided, comprising:
[0013] The source code retrieval module is configured to retrieve source code.
[0014] The component tag recognition module is configured to recognize component tags from the source code;
[0015] The code import information generation module is configured to generate code import information based on component tags;
[0016] The assembly module is configured to assemble source code and code import information to obtain assembled code;
[0017] The target code generation module is configured to call a preset code parser, execute the target component generation function to parse the assembled code, and generate target code in a specific format.
[0018] According to a third aspect of the embodiments of this specification, a computing device is provided, comprising:
[0019] Memory and processor;
[0020] The memory is used to store computer-executable instructions, and the processor is used to execute the computer-executable instructions, which, when executed by the processor, implement the steps of the above-described code conversion method.
[0021] According to a fourth aspect of the embodiments of this specification, a computer-readable storage medium is provided that stores computer-executable instructions that, when executed by a processor, implement the steps of the above-described code conversion method.
[0022] According to a fifth aspect of the embodiments of this specification, a computer program is provided, wherein when the computer program is executed in a computer, it causes the computer to perform the steps of the above-described code conversion method.
[0023] In one embodiment of this specification, source code is obtained, component tags are identified from the source code, code import information is generated based on the component tags, the source code and code import information are assembled to obtain assembled code, a preset code parser is called, and a target component generation function is executed to parse the assembled code and generate target code in a specific format. Generating code import information based on component tags in the source code, then assembling the source code and code import information to obtain assembled code, and calling the code parser to execute the target component generation function allows for direct code conversion based on the parsing and generation of the assembled code during component generation, thereby avoiding omissions during code conversion and improving the adaptability and accuracy of code conversion. Attached Figure Description
[0024] Figure 1This is a flowchart illustrating a code conversion method provided in one embodiment of this specification;
[0025] Figure 2 This is a flowchart illustrating the processing procedure of a code conversion method for JSX code and low-code underlying protocols, provided in one embodiment of this specification.
[0026] Figure 3 This is a schematic diagram of the processing flow of a code conversion method applied to JSX code and low-code underlying protocols, provided in one embodiment of this specification;
[0027] Figure 4 This is a front-end display effect diagram of a code conversion method applied to JSX code and low-code underlying protocol provided in one embodiment of this specification;
[0028] Figure 5 This is a schematic diagram illustrating a code conversion method provided in one embodiment of this specification;
[0029] Figure 6 This is a schematic diagram of the structure of a code conversion device provided in one embodiment of this specification;
[0030] Figure 7 This is a structural block diagram of a computing device provided in one embodiment of this specification. Detailed Implementation
[0031] Many specific details are set forth in the following description to provide a full understanding of this specification. However, this specification can be implemented in many other ways than those described herein, and those skilled in the art can make similar extensions without departing from the spirit of this specification. Therefore, this specification is not limited to the specific implementations disclosed below.
[0032] The terminology used in one or more embodiments of this specification is for the purpose of describing particular embodiments only and is not intended to be limiting of that specification or embodiment. The singular forms “a,” “described,” and “the” as used in one or more embodiments of this specification and the appended claims are also intended to include the plural forms unless the context clearly indicates otherwise. It should also be understood that the term “and / or” as used in one or more embodiments of this specification refers to and includes any or all possible combinations of one or more of the listed items.
[0033] It should be understood that although the terms first, second, etc., may be used in one or more embodiments of this specification to describe various information, such information should not be limited to these terms. These terms are used only to distinguish information of the same type from one another. For example, first may also be referred to as second without departing from the scope of one or more embodiments of this specification, and similarly, second may also be referred to as first. Depending on the context, the word “if” as used herein may be interpreted as “when”, “in response to a determination”, or “when…”.
[0034] First, the terms and concepts used in one of the embodiments of this specification will be explained.
[0035] Low-code: A digital technology tool for coding that enables product development and application through more efficient methods such as graphical drag-and-drop and parameterized configuration.
[0036] Low-code Schema: Describes the view structure and logic structure by declaring components and attribute parameters.
[0037] Procode: Pure code, written in the development and application of a product to achieve specific functions.
[0038] JSX: is a JavaScript syntax extension that is ultimately rendered as HTML nodes.
[0039] React: A JavaScript framework for building user interfaces, including initial component generation functions (ReactCreateElement).
[0040] Component: A programming object (Object) that is constructed by simply encapsulating data and methods.
[0041] Currently, the method for converting low-code to professional code is as follows: traverse each component in the low-code, parse each component in the low-code, obtain the component tags and component attribute parameters of each component, convert the component tags in the low-code to the component tags in the professional code through the correspondence, convert the component attribute parameters in the low-code to the component attribute interfaces in the professional code, and repeat the above steps until the code conversion of each component in the low-code is completed, and professional code is obtained.
[0042] Taking the conversion of low-code schema to JSX code as an example, the corresponding implementation code for the above method is shown below:
[0043] Parse each component to obtain its component tag and component attribute parameters:
[0044]
[0045]
[0046] By using mapping relationships, component tags in the low-code code are converted to component tags in the professional code, and component attribute parameters in the low-code code are converted to component attribute interfaces in the professional code, until the code conversion of each component in the low-code code is completed:
[0047]
[0048] Currently, the method for converting professional code into low code is as follows: perform syntax parsing on the professional code, construct an abstract syntax tree for the professional code, traverse the abstract syntax tree to obtain the labels and attribute parameters of multiple syntax nodes, and use the labels and attribute parameters of multiple syntax nodes to convert the professional code into low code.
[0049] However, this method requires first constructing an abstract syntax tree (AST) and then transcribing it into low-level code. It doesn't directly convert code based on the code hierarchy, introducing intermediate steps of AST construction and transcribing. This places high demands on the performance of both the AST construction and transcribing tools, making it prone to omissions and errors. To address these issues, continuous optimization of the construction and transcribing tools is necessary, along with the development of analysis tools to perform computational analysis on the resulting low-level code, confirming any omissions or errors. This increases the cost of code conversion and reduces its efficiency.
[0050] To address the aforementioned problems, this specification provides a code conversion method, and also relates to a code conversion apparatus, a computing device, and a computer-readable storage medium, which will be described in detail in the following embodiments.
[0051] See Figure 1 , Figure 1 A flowchart of a code conversion method according to an embodiment of this specification is shown, which specifically includes the following steps.
[0052] Step 102: Obtain the source code.
[0053] The examples in this specification are applicable to integrated development environments or code development platforms.
[0054] The source code is professional object-oriented programming code, such as Java, JavaScript, JSX, C++, C#, HTML, XML, etc.
[0055] The source code includes component code that defines multiple components. A component corresponds to a specific real-world item or a specific element in software, and the element is the object content in web development languages (JSX, HTML, XML), such as an input box or a button in a web page. In the embodiments of this specification, the component and the element are identical.
[0056] Obtain the source code, and the specific implementation is as follows: receive the source code uploaded by a developer through a client, or call a code database to obtain pre-stored source code.
[0057] Illustratively, call a remote code database of Github to obtain pre-stored source code, where the source code is:
[0058]
[0059] By obtaining the source code, data is provided for subsequently identifying component tags therefrom, laying a foundation for subsequent code conversion.
[0060] Step 104: identify a component tag from the source code.
[0061] The component tag is an object name of a component in the source code, and is a code in a string format.
[0062] A specific implementation of identifying a component tag from the source code is as follows: identify the component tag from the source code according to a string feature of the component tag.
[0063] An identification method includes: determining a naming rule of a component tag in advance according to a string feature of the component tag; constructing a component tag list according to the naming rule; and traversing the source code according to the component tag list to obtain the component tag.
[0064] The identification method further includes: training a neural network model in advance according to a string feature of the component tag to obtain a component tag identification model; and identifying the source code by using the component tag identification model to obtain the component tag.
[0065] The identification method further includes: setting a regular expression in advance according to a string feature of the component tag; and identifying the source code by using the regular expression to obtain the component tag.
[0066] Continuing with the foregoing example, according to a string feature of the component tag: an initial letter is capitalized, and the component tag follows the character "<" or "< / ". A training sample is correspondingly constructed according to the string feature, and a neural network model is trained to obtain a component tag identification model. The foregoing source code is identified to obtain component tags "DIV" and "Button".
[0067] By identifying component tags from the source code, a foundation is laid for generating subsequent code import information. At the same time, it accurately corresponds to the component code in the source code, avoiding omissions and improving the accuracy of code conversion.
[0068] Step 106: Generate code import information based on component tags.
[0069] Import information refers to the information used to import objects into the code parser for parsing. For example, if objects "object1" and "object2" are created, generating an object library "objectLib", the objects need to be imported from the object library using the import information "importobject1 from objectLib" for parsing.
[0070] Based on the component tag, code import information is generated. Specifically, a component object is created based on the component tag, and then code import information is generated based on the component object.
[0071] For example, based on the component tags "DIV" and "Button", component objects DIV and Button are created, and code import information is generated based on component objects DIV and Button:
[0072] import DIV from "DIV";
[0073] import Button from "Button";
[0074] Based on the component tags, code import information is generated, laying the information foundation for the subsequent assembly of the assembly code, and also laying the foundation for the subsequent parsing and calling of component objects.
[0075] Step 108: Assemble the source code and code import information to obtain the assembled code.
[0076] The source code and code import information are assembled to obtain the assembled code. The specific method code is as follows:
[0077] const newContent = `import React from 'react';` / / newContent is the code to be assembled, importStr is the code import information, and jsx is the source code of JSX.
[0078] ${importStr}
[0079] ${jsx}
[0080] For example, by assembling the source code and code import information, the assembled code is obtained:
[0081]
[0082]
[0083] By assembling the source code and code import information, we obtain the assembled code, which provides a parsing object for subsequent analysis.
[0084] Step 110: Call the preset code parser, execute the target component generation function to parse the assembled code, and generate target code in a specific format.
[0085] The target code is low-level object-oriented programming code, and it is visual code in a specific format, such as draggable modules in a webpage generator or database builder, or parameterized configuration code. The specific format is: object1name{object1's property parameters; object2name{object2's property parameters}}.
[0086] The target component generator function is a function derived by modifying the initial component generator function in the source code. For example, if the source code is JavaScript, the initial component generator function for JavaScript is `React createElement`, which generates the corresponding component object based on the component code in the source code. By modifying `React createElement`, the target component generator function `injectReactModule` is obtained. Executing `injectReactModule` parses the property parameters of the component object in the source code and passes them to the target code.
[0087] A code parser is a pre-tuned code converter that can parse code. A code converter is a pre-defined converter that translates source code into machine language. For example, for JavaScript, Babel is a code converter that translates JavaScript code into machine language. By modifying Babel, a code parser is created that can parse JavaScript code (source code). By calling the code parser, the JavaScript code is parsed to obtain the corresponding component tags and attribute parameters. For example, the JavaScript code "var a = 12" will be parsed by the code parser to obtain the variable name "a" and the value 12.
[0088] The pre-defined code parser is invoked, and the target component generation function is executed to parse the assembled code and generate target code in a specific format. Specifically, the pre-defined code parser is invoked, the target component generation function is executed to parse the assembled code, obtain the component tag and attribute parameters of the component object, and pass them to the target code to generate target code in a specific format.
[0089] For example, the code compiler is obtained by adjusting the Babel code converter, and the target component generation function injectReactModule is obtained by adjusting the initial component generation function React creatElement. The code compiler is called to execute injectReactModule to parse and assemble the code "import DIV from "DIV";
[0090]
[0091] The component object's property parameters "className="myDiv" and "button" are obtained from the source code. These property parameters are then passed to the target code to obtain target code in a specific format.
[0092]
[0093] In this embodiment, source code is obtained, component tags are identified from the source code, code import information is generated based on the component tags, the source code and code import information are assembled to obtain assembled code, a preset code parser is called, and the target component generation function is executed to parse the assembled code and generate target code in a specific format. Generating code import information based on component tags in the source code, then assembling the source code and code import information to obtain assembled code, and calling the code parser to execute the target component generation function allows for direct code conversion based on the parsing and generation of the assembled code during component generation, avoiding omissions during code conversion and improving the adaptability and accuracy of code conversion.
[0094] Optionally, step 106 includes the following specific steps:
[0095] Create a component object library based on component tags;
[0096] Generate code import information based on the component object library.
[0097] The component object library is a wrapper library for multiple created component objects. These component objects are not included in the source code, but are created based on the component tags of the component objects. The created component objects do not contain the attribute parameters of the component objects.
[0098] Subsequent code parsing requires a defined object to be imported into the code parser. The initial component generation function generates a component object based on the source code. After adjusting the initial component generation function, the target component generation function is obtained. The component object generated by executing the target component generation function is the target code in a specific format, not the corresponding component object. Because the code parser needs a defined object to parse, if the initial component generation function were to directly generate a component object from the source code, the target code would not be obtained. Therefore, a component object needs to be created based on the component tag for subsequent parsing.
[0099] Based on component tags, a component object library is created. Based on the component object library, code import information is generated. Specifically, multiple component objects are created based on multiple component tags, and the multiple component objects are encapsulated to obtain a component object library. Based on the component object library, code import information is generated.
[0100] For example, based on the component tags "DIV" and "Button", component objects fakeDIV and fakeButton are created. These components are then encapsulated to obtain the component object library fakeComponentLib. Based on fakeComponentLib, code import information is generated.
[0101] import DIV from fakeComponentLib;
[0102] import Button from fakeComponentLib;
[0103] Based on component tags, a component object library is created, and based on the component object library, code import information is generated. Creating a component object library corresponding to component tags allows for accurate code conversion of the source code, avoiding omissions and errors during conversion, improving accuracy, and generating code import information provides the parsing object conditions for subsequent parsing, ensuring smooth execution of subsequent parsing.
[0104] Optionally, a component object library is created based on the component tags, including the following specific steps:
[0105] Set each component tag to the attribute parameter of the corresponding component to obtain multiple component objects;
[0106] Build a component object library based on multiple component objects.
[0107] A component object is a code object constructed to correspond to a specific thing in the real world.
[0108] The component's attribute parameters include its execution logic, functional methods, styles, and other attributes. For example, if the component is a button component, its execution logic is as follows: after recognizing a click operation on the front end, execute the "Login" function set for the button. The button's style is: {rectangle, font: SimSun, font size: 4, text color: black, fill color: white}.
[0109] Because the component object library built based on component tags has not yet parsed the assembled code, it is impossible to obtain the component's attribute parameters from the source code. When the code parser is called and the target component generation function is executed to parse the assembled code, the attribute parameters will be passed to the target code accordingly. Therefore, when creating a component object, the corresponding component tag can be directly used as the attribute parameter.
[0110] By setting each component tag as an attribute parameter of its corresponding component, multiple component objects are obtained. The specific code is as follows:
[0111]
[0112] A component object library is built based on multiple component objects. Specifically, multiple component objects are encapsulated and then the component object library is built.
[0113] For example, the component tag "DIV" is set as the property parameter props of the corresponding component fakeDIV, and the component tag "Button" is set as the property parameter props of the corresponding component fakeButton, so as to obtain the component objects fakeDIV and fakeButton. The component objects fakeDIV and fakeButton are then encapsulated to build the component object library fakeComponentLib.
[0114] By setting each component tag as an attribute parameter of its corresponding component, multiple component objects are obtained. A component object library is then built based on these object objects. This method ensures the accuracy of the created component objects and eliminates the need to identify and set component attributes in the source code, thus improving the efficiency of component object creation. Furthermore, building a component object library based on these multiple object objects lays the foundation for generating code import information.
[0115] Optionally, code import information is generated based on the component object library, including the following specific steps:
[0116] Based on the component object library, code import information is generated using a pre-set code import information template.
[0117] Based on the component object library, code import information is generated using a pre-set code import information template. Specifically, code import information is generated based on the component tags of the component objects in the component object library using a pre-set code import information template.
[0118] The code import information template is a code generation template that allows you to import the code of a component object into the code parser. The specific code import information template is as follows:
[0119]
[0120] For example, based on the component tags "DIV" and "Button" of the component object, the code import information is generated using the pre-set code import information template "import ${fakeComponent}from "${fakeComponentName}":
[0121] import DIV from "DIV";
[0122] import Button from "Button";
[0123] Based on the component object library, code import information is generated using pre-defined code import information templates. Using these templates allows for rapid generation of code import information, improving its efficiency and consequently enhancing code conversion efficiency.
[0124] Optionally, step 110 includes the following specific steps:
[0125] Determine the target component based on the component objects in the component object library;
[0126] The pre-defined code parser is invoked to execute the target component generation function, and the target component tag and target attribute parameters corresponding to the target component are obtained.
[0127] Generate target code in a specific format based on the target component label and target attribute parameters.
[0128] The target component is a component object in the source code whose component tag and attribute parameters are not empty. For example, in JavaScript, during the code writing process, there may be empty objects defined as "<>" to make the code execution logic complete. Such component objects have no attribute parameters and cannot be converted into the corresponding target code.
[0129] The target component is determined based on component objects in the component object library. Specifically, it checks if the component object's tag and attribute parameters are empty. If they are, it analyzes other component objects in the library. If they are not empty, it identifies the component object as the target component. The code for this method is as follows:
[0130]
[0131] The pre-defined code parser is invoked to execute the target component generation function, obtaining the target component's tag and target attribute parameters. Specifically, the target component generation function is executed, the strings in the assembled code are analyzed, and the target component's tag and target attribute parameters are parsed from the assembled code. The specific code is as follows:
[0132]
[0133] Based on the target component tag and target attribute parameters, target code in a specific format is generated. Specifically, the target component tag and target attribute parameters are passed in a specific format to generate the target code. For example, if the target component tag is "componentName1" and the target attribute parameters are "prop1;prop2;prop3", the specific format would be:
[0134]
[0135] The generated target code is:
[0136]
[0137] For example, based on the component objects fakeDIV and fakeButton in the component object library fakeComponentLib, the corresponding target components DIV and Button in the assembled code are determined. A preset code parser compiler is called to execute the target component generation function injectReactModule, obtaining the target component tags "DIV" and "Button" and the target attribute parameters "className="myDiv" and "Button" respectively. Based on the target component tags "DIV" and "Button" and the target attribute parameters "className="myDiv" and "Button", target code in a specific format is generated.
[0138]
[0139] Based on component objects in the component object library, the target component is identified. A preset code parser is invoked, and the target component generation function is executed to obtain the target component's tag and target attribute parameters. Based on these tags and parameters, target code in a specific format is generated. Identifying the target component using component objects from the library resolves the issue of syntax differences between the source code and target code, preventing subsequent compilation of the generated target code and improving its accuracy. Invoking the preset code parser and executing the target component generation function to obtain the target component's tag and attribute parameters directly at the code level improves parsing efficiency and ensures the accurate extraction of the target component's tag and attribute parameters, avoiding omissions during code conversion and further enhancing the target code's accuracy.
[0140] Optionally, based on the target component tag and target attribute parameters, target code in a specific format is generated, including the following specific steps:
[0141] Based on the target component tag and target attribute parameters, target code in a specific format is generated using the code template corresponding to the component object library.
[0142] The code template is a template for generating target code that is passed as attribute parameters, and the code template has a specific format.
[0143] Based on the target component tag and target attribute parameters, target code in a specific format is generated using the corresponding code template from the component object library. Specifically, the target component tag and target attribute parameters are passed to the corresponding positions in the specific code template to generate the target code. The code template is as follows:
[0144]
[0145] For example, the target component tags are "DIV" and "Button", and the target attribute parameters are "className="myDiv" and "Button". The target component tags "DIV" and "Button" and the target attribute parameters "className="myDiv" and "Button" are passed to the corresponding positions in a specific format code template to generate target code in a specific format.
[0146]
[0147]
[0148] Based on the target component tags and target attribute parameters, target code in a specific format is generated using the corresponding code template from the component object library. Using the code template from the component object library accurately matches the target component transitions and target attribute parameters, avoiding omissions of component tags and attribute parameters, thus improving the accuracy of code conversion. It also allows for rapid generation of target code, improving the efficiency of code conversion.
[0149] Optionally, prior to step 110, the following specific steps are also included:
[0150] Obtain the initial component generation function, which contains a component generation sub-function that generates a visual component from the source code;
[0151] Replace the component generation function with a preset code generation function to obtain the target component generation function.
[0152] The initial component generation function corresponds to the component generation function of different source code frameworks, such as React CreateElement in the React framework, Bean element generation function in the Spring framework, and Render element generation function in the .NET framework.
[0153] The component generation sub-function is the function in the initial component generation function that converts the source code into a visual component. For example, the source code is:
[0154]
[0155]
[0156] <title> XXX< / title>
[0157]
[0158]
[0159] The component generation sub-function converts the source code into machine language code for a display page with the title "XXX", compiles it, and then displays it on the front end.
[0160] The code generation subfunction is a code transformation function that converts source code into target code. The specific code for the code generation subfunction is as follows:
[0161]
[0162] For example, the initial component generation function `React createElement` in the React framework is retrieved, and the component generation sub-function `createElement` is replaced with the preset code generation sub-function `hijackCreateElement` to obtain the target component generation function:
[0163] const injectReactModule = {
[0164] createElement:(key:string,props:IFieldProps={},)=>{
[0165] return hijackCreateElement(key,props);
[0166] }
[0167] The process involves obtaining an initial component generation function, which includes a component generation sub-function that generates visual components from source code. This sub-function is then replaced with a predefined code generation sub-function to obtain the target component generation function. By replacing the component generation sub-functions of the initial function with the predefined sub-functions, the target component generation function is obtained, providing a functional foundation for subsequent parsing and improving the efficiency and accuracy of code conversion.
[0168] Optionally, step 104 includes the following specific steps:
[0169] Regular expressions are used to search the source code and obtain the component tags in the source code.
[0170] Regular expressions are text patterns that use matching to retrieve the corresponding string from the code.
[0171] This method uses regular expressions to search the source code and retrieve component tags. Specifically, it sets a regular expression based on the string characteristics of the component tags, and then uses this expression to search the source code to obtain the component tags. The string characteristics of component tags are that the left side of the string contains the character "<" and the right side may contain the character ">". Therefore, the regular expression is set to htmlRegx = / <[>]? / , which means that the left side contains the character "<" and the right side may contain the character ">". The specific code is as follows:
[0172]
[0173] Based on the string characteristics of the component code, the regular expression const htmlRegx= / <[>]? / g is used to search the source code and obtain the component tags "DIV" and "Button".
[0174] Regular expressions are used to search the source code and retrieve component tags. This method improves the accuracy of component tag identification, avoids omissions during code conversion, and ensures overall code conversion accuracy.
[0175] Optionally, prior to step 110, the following specific steps are also included:
[0176] Obtain a code converter, which includes an object conversion module;
[0177] Modify the parameters of the object conversion module to obtain the code parser.
[0178] A code converter is a pre-defined converter that translates source code into machine language. It includes an object conversion module that converts the source code into the machine language of a visual component before compilation.
[0179] Modify the parameters of the object conversion module to obtain a code parser. Specifically, based on the code format of the target code, modify the parameters of the object conversion module to obtain a code parser, so that the code parser can parse the component tags and attribute parameters of the source code and generate target code in a specific format.
[0180] For example, based on the code form of the target code:
[0181]
[0182] Modify the parameters of the object conversion module to obtain a code parser. This code parser will then parse the component tags and attribute parameters of the source code to obtain "componentName", "prop1;prop2;prop3", and generate target code in a specific format.
[0183]
[0184] A code converter is obtained, which includes an object conversion module. Modifying the parameters of the object conversion module yields a code parser. By modifying the parameters of the object conversion module within the code converter without rebuilding the code parser, the efficiency of code parsing is improved. This also avoids the need for debugging a rebuilt code parser to parse the assembled code, thus enhancing the accuracy of the conversion.
[0185] The following is in conjunction with the appendix Figure 2 This document uses the code conversion method provided in its description for application in JSX code and low-code underlying protocols as an example to further illustrate the code conversion method. Specifically, Figure 2This specification illustrates a flowchart of a code conversion method for JSX code and low-code underlying protocols, provided by an embodiment of this specification, which specifically includes the following steps.
[0186] Step 202: Obtain the JSX code;
[0187] The specific JSX code is as follows:
[0188]
[0189]
[0190] Step 204: Use regular expressions to identify component tags in the JSX code;
[0191] The specific regular expression is as follows:
[0192] htmlRegx= / <[>]? / .
[0193] Identify the components DIV and Button used in the JSX code and obtain their component tags "DIV" and "Button".
[0194] Step 206: Create multiple corresponding component objects based on the component tags to obtain the component object library;
[0195] Based on the component tags "DIV" and "Button", multiple corresponding component objects fakeDIV and fakeButton are created, resulting in the component object library fakeComponentLib.
[0196] Step 208: Based on the component object library, generate a template using code import information, and generate code import information;
[0197] Generate a template based on the component object library fakeComponentLib and using code import information:
[0198] import${fakeComponent}from“${fakeComponentName}
[0199] Generate code import information:
[0200] import DIV from "DIV";
[0201] import Button from "Button";
[0202] Step 210: Assemble the imported code information and JSX code to obtain the assembled code;
[0203] The specific assembly code is newContent:
[0204]
[0205] Step 212: Inject the component object library and pre-built target component generation functions into the code parser;
[0206] The component object library fakeComponentLib and the pre-built target component generation function injectReactModule are injected into the code parser compiler.
[0207] Step 214: The code parser parses the assembled code to obtain the low-code underlying protocol;
[0208] The specific low-code underlying protocol (Lowcode Schema)
[0209]
[0210]
[0211] Step 216: Generate the corresponding draggable component based on the low-code underlying protocol and display it on the front end.
[0212] Users can divide the page into corresponding areas by dragging and dropping the "DIV" component on the front end, and generate corresponding buttons on the page by dragging and dropping the "Button" component on the front end, thus realizing page development.
[0213] Figure 3 This specification illustrates a schematic diagram of the processing flow of a code conversion method applied to JSX code and low-code underlying protocols, provided in one embodiment of this specification.
[0214] like Figure 3 As shown, the JSX code is identified, the component tags used in the JSX code are identified, and then a corresponding component object library is created based on the component tags. Code import information is generated based on the component object library, and the code import information and JSX code are assembled to obtain the assembled code. The component object library and the pre-built target component generation function are imported into a pre-set code parser, and the code parser is used to parse the assembled code to obtain the low-code underlying protocol (LowcodeSchema).
[0215] Figure 4 The diagram illustrates the front-end display effect of a code conversion method for JSX code and low-code underlying protocols provided in one embodiment of this specification.
[0216] like Figure 4As shown, on the product development front-end interface, the left side is the product display area, and the right side is a list of draggable components, including area components and button components. Users can divide the page into corresponding areas by dragging and dropping area components to the product display area, and users can generate corresponding buttons on the page by dragging and dropping button components to the product display area.
[0217] In this embodiment, regular expressions are used to identify component tags in JSX code, avoiding omissions and errors in components during the conversion to a low-code underlying protocol, thus improving the accuracy of identification. Multiple component objects are created based on the component tags to obtain a component object library. Creating this library based on component tags allows for accurate code conversion of JSX code, avoiding omissions and errors during conversion and improving accuracy. Furthermore, code import information is generated, providing parsing object conditions for subsequent parsing and ensuring smooth execution. Based on the component object library, a template is generated using the code import information to generate the code. The system imports code information quickly, improving the efficiency of code import information generation and thus code conversion efficiency. It calls a code parser to execute the target component generation function, which can parse and generate the assembled code during component generation. Based on the assembled code at the code level, it directly performs code conversion, generating a low-code underlying protocol. This avoids omissions during code conversion, improving the adaptability and accuracy of the conversion. Then, based on the low-code underlying protocol, it generates corresponding draggable components and displays them on the front end. This allows users to develop products simply by dragging and dropping components, reducing the complexity and increasing the efficiency of product development.
[0218] Figure 5 A schematic diagram illustrating a code conversion method provided in one embodiment of this specification is shown.
[0219] like Figure 5 As shown, the method for converting low-code to procode is as follows: By traversing each component in the low-code, parsing each component in the low-code, obtaining the component tags and component attribute parameters of each component, converting the component tags in the low-code to component tags in the procode through the correspondence, and converting the component attribute parameters in the low-code to component attribute interfaces in the procode, repeating the above steps until the code conversion of each component in the low-code is completed, and the procode is obtained.
[0220] Reversibly, methods for converting procode to lowcode include:
[0221] Method 1: Perform syntax parsing on the procode, construct an abstract syntax tree for the procode, traverse the abstract syntax tree to obtain the labels and attribute parameters of multiple syntax nodes, and use the labels and attribute parameters of multiple syntax nodes to convert the procode into lowcode.
[0222] Method 2: Identify component tags from the procode, generate code import information based on the component tags, assemble the procode and code import information to obtain the assembled code, call the preset code parser, execute the target component generation function to parse the assembled code, and generate lowcode in a specific format.
[0223] Corresponding to the above method embodiments, this specification also provides embodiments of a code conversion device. Figure 6 A schematic diagram of a code conversion apparatus according to one embodiment of this specification is shown. Figure 6 As shown, the device includes:
[0224] The source code acquisition module 602 is configured to acquire source code.
[0225] The component label recognition module 604 is configured to recognize component labels from the source code;
[0226] The code import information generation module 606 is configured to generate code import information based on component tags;
[0227] Assembly module 608 is configured to assemble source code and code import information to obtain assembled code;
[0228] The target code generation module 610 is configured to call a preset code parser, execute the target component generation function to parse the assembled code, and generate target code in a specific format.
[0229] Optionally, the code import information generation module 606 is further configured as follows:
[0230] Create a component object library based on component tags;
[0231] Generate code import information based on the component object library.
[0232] Optionally, the code import information generation module 606 is further configured as follows:
[0233] Set each component tag to the attribute parameter of the corresponding component to obtain multiple component objects;
[0234] Build a component object library based on multiple component objects.
[0235] Optionally, the code import information generation module 606 is further configured as follows:
[0236] Based on the component object library, code import information is generated using a pre-set code import information template.
[0237] Optionally, the target code generation module 610 is further configured as follows:
[0238] Determine the target component based on the component objects in the component object library;
[0239] The pre-defined code parser is invoked to execute the target component generation function, and the target component tag and target attribute parameters corresponding to the target component are obtained.
[0240] Generate target code in a specific format based on the target component label and target attribute parameters.
[0241] Optionally, the target code generation module 610 is further configured as follows:
[0242] Based on the target component tag and target attribute parameters, target code in a specific format is generated using the code template corresponding to the component object library.
[0243] Optionally, the device further includes:
[0244] The target component generation function building module is configured to obtain the initial component generation function, which includes a component generation sub-function that generates a visual component from the source code; the component generation sub-function is replaced with a preset code generation sub-function to obtain the target component generation function.
[0245] Optionally, the component label recognition module 604 is further configured as follows:
[0246] Regular expressions are used to search the source code and obtain the component tags in the source code.
[0247] Optionally, the device further includes:
[0248] The code parser building module is configured to obtain a code converter, which includes an object conversion module; by modifying the parameters of the object conversion module, the code parser is obtained.
[0249] In this embodiment, source code is obtained, component tags are identified from the source code, code import information is generated based on the component tags, the source code and code import information are assembled to obtain assembled code, a preset code parser is called, and the target component generation function is executed to parse the assembled code and generate target code in a specific format. Generating code import information based on component tags in the source code, then assembling the source code and code import information to obtain assembled code, and calling the code parser to execute the target component generation function allows for direct code conversion based on the parsing and generation of the assembled code during component generation, avoiding omissions during code conversion and improving the adaptability and accuracy of code conversion.
[0250] The above is an illustrative scheme of a code conversion device according to this embodiment. It should be noted that the technical solution of this code conversion device and the technical solution of the above-described code conversion method belong to the same concept. For details not described in detail in the technical solution of the code conversion device, please refer to the description of the technical solution of the above-described code conversion method.
[0251] Figure 7 A structural block diagram of a computing device according to one embodiment of this specification is shown. The components of the computing device 700 include, but are not limited to, a memory 710 and a processor 720. The processor 720 is connected to the memory 710 via a bus 730, and a database 750 is used to store data.
[0252] The computing device 700 also includes an access device 740, which enables the computing device 700 to communicate via a network 760. Examples of such networks include Public Switched Telephone Network (PSTN), Local Area Network (LAN), Wide Area Network (WAN), Personal Area Network (PAN), or combinations of communication networks such as the Internet. The access device 740 may include one or more of any type of wired or wireless network interface (e.g., a Network Interface Controller (NIC)), such as an IEEE 802.11 Wireless Local Area Networks (WLAN) interface, a Wi-MAX (World Interoperability for Microwave Access) interface, an Ethernet interface, a Universal Serial Bus (USB) interface, a cellular network interface, a Bluetooth interface, a Near Field Communication (NFC) interface, and so on.
[0253] In one embodiment of this specification, the above-described components of the computing device 700 and Figure 7 Other components, not shown, can also be connected to each other, for example, via a bus. It should be understood that... Figure 7 The block diagram of the computing device shown is for illustrative purposes only and is not intended to limit the scope of this specification. Those skilled in the art can add or replace other components as needed.
[0254] The computing device 700 can be any type of stationary or mobile computing device, including mobile computers or mobile computing devices (e.g., tablet computers, personal digital assistants, laptop computers, notebook computers, netbooks, etc.), mobile phones (e.g., smartphones), wearable computing devices (e.g., smartwatches, smart glasses, etc.) or other types of mobile devices, or stationary computing devices such as desktop computers or PCs. The computing device 700 can also be a mobile or stationary server.
[0255] The processor 720 is used to execute the following computer-executable instructions, which, when executed by the processor, implement the steps of the above-described code conversion method.
[0256] The above is an illustrative scheme of a computing device according to this embodiment. It should be noted that the technical solution of this computing device and the technical solution of the above-described code conversion method belong to the same concept. For details not described in detail in the technical solution of the computing device, please refer to the description of the technical solution of the above-described code conversion method.
[0257] An embodiment of this specification also provides a computer-readable storage medium storing computer-executable instructions that, when executed by a processor, implement the steps of the above-described code conversion method.
[0258] The above is an illustrative scheme of a computer-readable storage medium according to this embodiment. It should be noted that the technical solution of this storage medium and the technical solution of the above-described code conversion method belong to the same concept. For details not described in detail in the technical solution of the storage medium, please refer to the description of the technical solution of the above-described code conversion method.
[0259] An embodiment of this specification also provides a computer program, wherein when the computer program is executed in a computer, it causes the computer to perform the steps of the above-described code conversion method.
[0260] The above is an illustrative example of a computer program according to this embodiment. It should be noted that the technical solution of this computer program and the technical solution of the code conversion method described above belong to the same concept. Details not described in detail in the technical solution of the computer program can be found in the description of the technical solution of the code conversion method described above.
[0261] The foregoing has described specific embodiments of this specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims may be performed in a different order than that shown in the embodiments and may still achieve the desired result. Furthermore, the processes depicted in the drawings do not necessarily require the specific or sequential order shown to achieve the desired result. In some embodiments, multitasking and parallel processing are possible or may be advantageous.
[0262] The computer instructions include computer program code, which may be in the form of source code, object code, executable file, or some intermediate form. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording media, USB flash drive, portable hard drive, magnetic disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electrical carrier signals, telecommunication signals, and software distribution media, etc.
[0263] It should be noted that, for the sake of simplicity, the foregoing method embodiments are all described as a series of actions. However, those skilled in the art should understand that the embodiments in this specification are not limited to the described order of actions, because according to the embodiments in this specification, some steps can be performed in other orders or simultaneously. Furthermore, those skilled in the art should also understand that the embodiments described in this specification are all preferred embodiments, and the actions and modules involved are not necessarily essential to the embodiments in this specification.
[0264] In the above embodiments, the descriptions of each embodiment have different focuses. For parts not described in detail in a certain embodiment, please refer to the relevant descriptions of other embodiments.
[0265] The preferred embodiments disclosed above are merely illustrative of this specification. The optional embodiments do not exhaustively describe all details, nor do they limit the invention to the specific implementations described. Clearly, many modifications and variations can be made based on the embodiments described herein. These embodiments are selected and specifically described in this specification to better explain the principles and practical applications of the embodiments, thereby enabling those skilled in the art to better understand and utilize this specification. This specification is limited only by the claims and their full scope and equivalents.
Claims
1. A code conversion method, comprising: Obtain the source code; Identify component tags from the source code; Based on the component tags, a component object library is created, and based on the component object library, code import information is generated; The source code and the code import information are assembled to obtain the assembled code; Based on the component objects in the component object library, a target component is determined, a preset code parser is invoked, and a target component generation function is executed to parse the assembled code, obtaining the target component tag and target attribute parameters corresponding to the target component. Based on the target component tag and target attribute parameters, target code in a specific format is generated. The method for determining the target component generation function includes: obtaining an initial component generation function, which includes a component generation subfunction that generates a visual component from the source code; and replacing the component generation subfunction with a preset code generation subfunction to obtain the target component generation function.
2. The method according to claim 1, wherein creating a component object library based on the component tag includes: Set each component tag to the attribute parameter of the corresponding component to obtain multiple component objects; Based on the multiple component objects, construct a component object library.
3. The method according to claim 1, wherein generating code import information based on the component object library includes: Based on the component object library, code import information is generated using a pre-set code import information template.
4. The method according to claim 1, wherein generating target code in a specific format based on the target component tag and the target attribute parameter comprises: Based on the target component tag and the target attribute parameters, target code in a specific format is generated using the code template corresponding to the component object library.
5. The method according to claim 1, wherein identifying component tags from the source code comprises: Using regular expressions, the source code is searched to obtain component tags in the source code.
6. The method according to claim 1, before calling the preset code parser and executing the target component generation function to parse the assembled code and generate target code in a specific format, further includes: Obtain a code converter, wherein the code converter includes an object conversion module; Modify the parameters of the object conversion module to obtain the code parser.
7. A code conversion apparatus, comprising: The source code retrieval module is configured to retrieve source code. A component label recognition module is configured to recognize component labels from the source code; The code import information generation module is configured to create a component object library based on the component tags, and generate code import information based on the component object library; The assembly module is configured to assemble the source code and the code import information to obtain the assembled code; The target code generation module is configured to determine a target component based on component objects in the component object library, call a preset code parser, execute a target component generation function to parse the assembled code, obtain the target component tag and target attribute parameters corresponding to the target component, and generate target code in a specific format based on the target component tag and target attribute parameters. The determination of the target component generation function includes: obtaining an initial component generation function, which includes a component generation sub-function that generates a visual component from the source code; and replacing the component generation sub-function with a preset code generation sub-function to obtain the target component generation function.
8. A computing device, comprising: Memory and processor; The memory is used to store computer-executable instructions, and the processor is used to execute the computer-executable instructions, which, when executed by the processor, implement the steps of the code conversion method according to any one of claims 1 to 6.
9. A computer-readable storage medium storing computer-executable instructions that, when executed by a processor, implement the steps of the code conversion method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Automatic code generation method of architecture analysis and design language (AADL) model
CN101739258A
Program code conversion method and device, and electronic equipment
CN111880785A
Object conversion method and device, electronic equipment and storage medium
CN113961238A
