Method and device for modifying syntax tree

By storing the syntax tree on disk and creating target nodes in memory for incremental replacement, the problem of excessive memory consumption in abstract syntax tree operations is solved, resulting in reduced memory consumption and improved processing efficiency, ensuring real-time code updates and a better user experience.

CN121909446APending Publication Date: 2026-04-21HUAWEI CLOUD COMPUTING TECHNOLOGIES CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
HUAWEI CLOUD COMPUTING TECHNOLOGIES CO LTD
Filing Date
2024-02-02
Publication Date
2026-04-21

AI Technical Summary

Technical Problem

In an integrated development environment, operations on abstract syntax trees consume a lot of memory, especially when dealing with large projects or large batches of files, leading to severe memory consumption.

Method used

By storing the syntax tree on disk and creating target nodes in memory for incremental replacement, the memory requirement is reduced. The red-green tree data structure and lossless red-green tree algorithm are used for modification to achieve incremental modification.

Benefits of technology

It reduces memory consumption, improves processing efficiency, and ensures real-time code updates and a better user experience.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121909446A_ABST
    Figure CN121909446A_ABST
Patent Text Reader

Abstract

The invention provides a method and device for modifying a syntax tree, and the method comprises the steps: obtaining an input first operation, the first operation indicates the content of modifying a first syntax tree corresponding to a first code text, and the first syntax tree is stored in a disk; searching a to-be-processed node in the first syntax tree according to the first operation, wherein the to-be-processed node comprises at least one of the following types: a node of a to-be-deleted character, a node of a to-be-inserted character or a node of a to-be-modified character; and creating a target node in the memory according to the first operation, and replacing the to-be-processed node by using the target node to obtain a second syntax tree. According to the scheme, the memory consumption during AST operation can be reduced.
Need to check novelty before this filing date? Find Prior Art

Description

[0001] This application claims priority to Russian Patent Application No. 2023126133, filed on October 12, 2023, entitled "Method and System for Modifying Abstract Syntax Trees", the entire contents of which are incorporated herein by reference. Technical Field

[0002] This application relates to the field of information technology, and more specifically, to a method and apparatus for modifying a syntax tree. Background Technology

[0003] An abstract syntax tree (AST) is a tree-like representation of the abstract syntactic structure of source code, where each node in the tree represents a structure in the source code. ASTs have a wide range of applications, such as code syntax checking, code highlighting, code error reporting, code auto-completion, modifying code structure, and code transformation.

[0004] In development tools such as integrated development environments (IDEs), the Abstract Syntax Tree (AST) and code files are correlated. Synchronization between code files and the AST is essential for complex software development processes. Specifically, modifications to the code files will result in corresponding modifications to the AST. Modifications to the AST made through code refactoring or quick fixes will also be reflected in the code files.

[0005] However, operations on ASTs can consume a lot of memory, especially when the IDE is processing large projects or a large number of files. ASTs can consume a lot of memory, causing serious memory consumption. Summary of the Invention

[0006] This application provides a method and apparatus for modifying a syntax tree, which helps to reduce the memory consumption caused by operations on the syntax tree.

[0007] In a first aspect, a method for modifying a syntax tree is provided, comprising: obtaining a first operation as input, the first operation indicating the content to be modified in a first syntax tree corresponding to a first code text, the first syntax tree being stored in a disk; searching for a node to be processed in the first syntax tree according to the first operation, the node to be processed including at least one of the following types: a node of a character to be deleted, a node of a character to be inserted, or a node of a character to be modified; creating a target node in memory according to the first operation, and using the target node to replace the node to be processed to obtain a second syntax tree.

[0008] In the solution of this application embodiment, when the syntax tree needs to be modified, a new node is created in memory, and the new node is used to incrementally replace the first syntax tree. The first syntax tree is stored on disk and does not need to be loaded into memory. Only the new nodes are created in memory. Modifying the syntax tree by incremental replacement does not require rebuilding the entire syntax tree, reducing the number of new nodes that need to be created, thereby reducing memory consumption and improving performance.

[0009] Meanwhile, in the solution of this application embodiment, the first syntax tree is modified by incremental replacement, which can achieve incremental modification and improve processing efficiency.

[0010] For example, the first operation may include a code refactoring operation.

[0011] In conjunction with the first aspect, in some implementations of the first aspect, the method further includes: obtaining the modified content of the code text based on the difference between the second syntax tree and the first syntax tree; modifying the first code text based on the modified content of the code text to obtain the second code text.

[0012] In the solution of this application embodiment, the first syntax tree is modified by incremental replacement, which can realize incremental modification, improve processing efficiency, and thus help ensure real-time code refresh.

[0013] In conjunction with the first aspect, in some implementations of the first aspect, the first syntax tree is stored in an index on disk.

[0014] For example, nodes in the first syntax tree are stored on disk as columns.

[0015] In conjunction with the first aspect, in some implementations of the first aspect, the first syntax tree can be compressed and stored in the index.

[0016] The first syntax tree is stored in the index, meaning that information about the first syntax tree or its nodes can be obtained through the index.

[0017] In conjunction with the first aspect, in some implementations of the first aspect, the first syntax tree adopts a red-green tree data structure, the first syntax tree includes nodes corresponding to all characters in the first code text, and when the first operation instruction inserts a node corresponding to a non-code character into the first syntax tree, the target node includes the node corresponding to the non-code character.

[0018] Modifications to the first syntax tree can be achieved using a lossless red-green tree incremental replacement algorithm.

[0019] The first syntax tree stored on disk is the green tree. A red tree is built when modifications to the first syntax tree are needed. Specifically, the search for nodes to be processed in the first syntax tree starts from the root node, and nodes in the red tree are built from top to bottom during the search until the search is complete. Every node in the red tree has a corresponding node in the green tree. Searching for nodes to be processed in the first syntax tree can also be understood as determining the nodes to be processed in the red tree. Target nodes are created in memory. Target nodes include green target nodes and red target nodes. There is a one-to-one correspondence between green and red target nodes. The nodes to be processed in the first syntax tree are replaced using the replacement nodes corresponding to the nodes to be processed in the target nodes.

[0020] In this embodiment, incremental replacement can be performed based on a lossless red-green tree, enabling a complete representation of the code text. In scenarios where the AST is directly modified (e.g., code refactoring), synchronizing the modifications in the AST to the code text helps to restore a more accurate code text and improves the user experience.

[0021] In conjunction with the first aspect, in some implementations of the first aspect, the node to be processed includes a first node to be processed, the first operation instruction deletes the first node from the child nodes of the first node to be processed, the target node includes a first target node, the first target node is used to replace the first node to be processed, and the child nodes referenced by the first target node include other child nodes of the first node to be processed besides the first node.

[0022] In this embodiment, a first target node is created in memory to replace the corresponding node in the first syntax tree, i.e., the first node to be processed. The first target node no longer references the node to be deleted, i.e., the first node. Except for the content to be modified (the first node), the first target node still references other child nodes of the first node to be processed. This eliminates the need to rebuild all nodes, achieving incremental modification. Moreover, all other child nodes are stored on the disk, which helps to reduce memory consumption and improve performance.

[0023] In conjunction with the first aspect, in some implementations of the first aspect, the node to be processed includes a second node to be processed, the first operation instruction adds a second node to the child nodes of the second node to be processed, the target node includes a second target node, the second target node is used to replace the second node to be processed, and the child nodes referenced by the second target node include all the child nodes of the second node to be processed and the second node.

[0024] In this embodiment, a second target node is created in memory to replace the corresponding node in the first syntax tree, i.e., the second node to be processed. In addition to referencing the content to be modified (the second node), the first target node also references all the child nodes of the second node to be processed. This eliminates the need to rebuild all nodes, enabling incremental modification. Moreover, the child nodes of the second node to be processed are all stored on the disk, which helps to reduce memory consumption and improve performance.

[0025] In conjunction with the first aspect, in some implementations of the first aspect, the second node is a child node of the third node in the first syntax tree.

[0026] In this embodiment, the second node in the first syntax tree can be directly reused without additional creation, further reducing memory usage.

[0027] In conjunction with the first aspect, in some implementations of the first aspect, the second node does not belong to the first syntax tree.

[0028] In conjunction with the first aspect, some implementations of the first aspect also include: storing the target node on disk.

[0029] Secondly, an apparatus for modifying a syntax tree is provided, the apparatus comprising: an acquisition module for acquiring an input first operation, the first operation indicating the content to be modified in a first syntax tree corresponding to a first code text, the first syntax tree being stored in a disk; and a syntax tree modification module for: searching for a node to be processed in the first syntax tree according to the first operation, the node to be processed including at least one of the following types: a node of a character to be deleted, a node of a character to be inserted, or a node of a character to be modified; creating a target node in memory according to the first operation; and replacing the node to be processed with the target node to obtain a second syntax tree.

[0030] In conjunction with the second aspect, in some implementations of the second aspect, the apparatus further includes: a code modification module, used to: obtain the modified content of the code text based on the difference between the second syntax tree and the first syntax tree; and modify the first code text based on the modified content of the code text to obtain the second code text.

[0031] In conjunction with the second aspect, in some implementations of the second aspect, the first syntax tree is stored in an index on disk.

[0032] In conjunction with the second aspect, in some implementations of the second aspect, the first syntax tree adopts a red-green tree data structure. The first syntax tree includes nodes corresponding to all characters in the first code text. When the first operation instruction inserts a node corresponding to a non-code character into the first syntax tree, the target node includes nodes corresponding to non-code characters.

[0033] In conjunction with the second aspect, in some implementations of the second aspect, the node to be processed includes a first node to be processed, a first operation instruction deletes the first node from the child nodes of the first node to be processed, the target node includes a first target node, the first target node is used to replace the first node to be processed, and the child nodes referenced by the first target node include other child nodes of the first node to be processed besides the first node.

[0034] In conjunction with the second aspect, in some implementations of the second aspect, the node to be processed includes a second node to be processed, the first operation instruction adds a second node to the child nodes of the second node to be processed, the target node includes a second target node, the second target node is used to replace the second node to be processed, and the child nodes referenced by the second target node include all the child nodes of the second node to be processed and the second node.

[0035] In conjunction with the second aspect, in some implementations of the second aspect, the second node is a child node of the third node in the first syntax tree.

[0036] It should be understood that the extensions, limitations, explanations and descriptions of the relevant content in the first aspect above also apply to the same content in the second aspect.

[0037] Thirdly, a computing device cluster is provided, comprising at least one computing device, each computing device including a processor and memory. The processor of the at least one computing device is configured to execute instructions stored in the memory of the at least one computing device, such that the computing device cluster performs the methods of the first aspect and any implementation thereof.

[0038] Fourthly, a computer-readable medium is provided, including computer program instructions that, when executed by a cluster of computing devices, execute the methods of the first aspect and any implementation thereof.

[0039] Fifthly, a computer program product containing instructions is provided, which, when run by a cluster of computing devices, cause the cluster of computing devices to perform the methods described in the first aspect and any implementation thereof. Attached Figure Description

[0040] Figure 1 It is a diagram illustrating the modification process of code text and abstract syntax tree; Figure 2 This is a schematic flowchart illustrating a method for modifying a syntax tree according to an embodiment of this application; Figure 3 This is a schematic diagram illustrating an example of syntax tree modification according to an embodiment of this application; Figure 4This is a schematic flowchart illustrating a method for modifying a syntax tree in a code refactoring scenario according to an embodiment of this application; Figure 5 This is a schematic diagram of a code text refresh process according to an embodiment of this application; Figure 6 This is a schematic diagram of a code refactoring process according to an embodiment of this application; Figure 7 This is a schematic block diagram of an apparatus for modifying a syntax tree according to an embodiment of this application; Figure 8 This is a schematic block diagram of a computing device according to an embodiment of this application; Figure 9 This is a schematic block diagram of a computing device cluster according to an embodiment of this application; Figure 10 This is a schematic block diagram of another computing device cluster according to an embodiment of this application. Detailed Implementation

[0041] The technical solutions in this application will now be described with reference to the accompanying drawings.

[0042] The terminology used in the following embodiments is for the purpose of describing specific embodiments only and is not intended to be limiting of this application. As used in the specification and appended claims of this application, the singular expressions “a,” “an,” and “the” are intended to include expressions such as “one or more,” unless the context clearly indicates otherwise. It should also be understood that in the following embodiments of this application, “at least one,” “at least one,” and “one or more” refer to one, two, or more than two. “First,” “second,” and various numerical designations are merely distinctions for descriptive convenience and are not intended to limit the scope of the embodiments of this application. “And / or” is used to describe the correspondence between corresponding objects, indicating that three relationships can exist. For example, “A and / or B” can represent: only A exists, only B exists, and both A and B exist simultaneously, where A and B can be singular or plural. The character “ / ” generally indicates that the preceding and following related objects are in an “or” relationship. The order of the process numbers below does not imply the order of execution. The execution order of each process should be determined by its function and internal logic and should not constitute any limitation on the implementation process of the embodiments of this application. For example, in the embodiments of this application, the words "301", "401", "501" etc. are merely identifiers made for the convenience of description and do not limit the order of execution steps.

[0043] References to "one embodiment" or "some embodiments" as described in this specification mean that one or more embodiments of this application include a specific feature, structure, or characteristic described in connection with that embodiment. In this application, the words "exemplary" or "for example" are used to indicate that something is illustrative, exemplary, or descriptive. Any embodiment or design described as "exemplary" or "for example" in this application should not be construed as being more preferred or advantageous than other embodiments or designs. Specifically, the use of the words "exemplary" or "for example" is intended to present the relevant concepts in a concrete manner. The terms "comprising," "including," "having," and variations thereof all mean "including but not limited to," unless otherwise specifically emphasized. In the embodiments of this application, descriptions such as "when," "in the case of," "if," and "if" all refer to the fact that the device will perform a corresponding processing under certain objective circumstances, and are not a limitation on time, nor do they require the device to perform a judgment action during implementation, nor do they imply any other limitations.

[0044] In this application, "for indicating" can include both direct and indirect indication. When describing an indication message as indicating A, it can include whether the indication message directly indicates A or indirectly indicates A, but does not necessarily mean that the indication message carries A.

[0045] To help those skilled in the art better understand the technical solutions of this application, some terms that may be involved in the embodiments of this application will be explained below.

[0046] 1. Integrated Development Environment (IDE): An IDE is an application that provides a program development environment. It typically includes tools such as a code editor, compiler, debugger, and graphical user interface. It is a suite of integrated development software services that combines code writing, analysis, compilation, and debugging functions.

[0047] An IDE can include a local IDE and a web IDE.

[0048] A local IDE, also known as a desktop IDE, is installed in the user's local development environment. For example, the local development environment can be a terminal device, such as a desktop computer, laptop, or mobile phone.

[0049] A Web IDE refers to an online IDE service, including both the IDE front-end and back-end. The IDE back-end runs in a remote environment. For example, the remote environment could be a cloud server, meaning the IDE is provided to users as a cloud service. Users can purchase cloud services by accessing a webpage, and the cloud service provider creates an IDE instance. Users can then develop programs based on this IDE instance.

[0050] In an IDE, a code index is a separate, physical storage structure that sorts the values ​​of all code and its dependent components (code) in the current project under a certain mapping relationship. It represents a set of one or more columns of values ​​in a data table under a certain mapping relationship, along with a corresponding list of logical pointers pointing to the data in the table that physically identifies these values. Through this list of logical pointers, the IDE can quickly find specific information, such as the code index of all class methods of the current type.

[0051] Code indexing is fundamental for an IDE's understanding of code; all core editing capabilities of an IDE rely on it. For example, features such as definition and reference lookup depend on the indexes of indexable objects within the code file. Exemplarily, indexable objects can be identifiers within the code file, such as variables, classes, constants, or functions. The index can be the metadata of the indexable objects. Metadata is data that describes data, specifically data that describes the indexable objects.

[0052] Indexes to indexable objects in a code file can be stored in an index file. This index file can also be called a code index file. For example, an index may include at least one of an identifier's declaration and its location. For instance, an identifier's declaration may specifically be at least one of a function declaration, variable declaration, class declaration, or constant declaration. For instance, the identifier's location may be characterized based on its line and column number in the code file.

[0053] For example, code indexes can be stored in the system at the object level. One code file can correspond to one code index. A code index is an object. An object is stored as a file (i.e., a code index file). Multiple code index files correspond one-to-one with multiple code files, with each code index file storing the code index of the corresponding code file.

[0054] 2. AST An AST, or syntax tree for short, is an abstract representation of the syntactic structure of source code. An AST represents the syntactic structure of source code in a tree-like structure, where each node represents a structure within the source code, such as a variable declaration, expression, function call, or control structure. The root node typically represents the entire source code file, while child nodes represent specific syntactic elements and their relationships.

[0055] The reason why the syntax is described as "abstract" is that the syntax here does not represent every detail that appears in real syntax. For example, nested parentheses are implicit in the tree structure and are not presented as nodes; while conditional jump statements like if-condition-then can be represented using nodes with three branches.

[0056] ASTs have a wide range of applications, such as code syntax checking, code highlighting, code error checking, code completion, code optimization, code repair, code generation, and code refactoring.

[0057] In development tools such as IDEs, the Abstract Syntax Tree (AST) and code files are correlated. Synchronization services between code files and ASTs are essential for complex software development processes. For example... Figure 1 As shown, specifically, modifications to the code files will result in corresponding modifications to the AST. Modifications to the AST made through code refactoring or quick fixes will also be reflected in the code files.

[0058] In relevant schemes, modifications to the AST mainly fall into two categories. One is an AST modification system based on a mutable AST. In this system, nodes of the syntax tree can be deleted, added, or changed. The other is an AST modification system based on an immutable AST. In this method, once the syntax tree is created, it cannot be changed. The syntax tree itself is not writable. All transformations on the tree are delegated to a separate object called a "rewriter." This scheme does not require thread synchronization. Interaction with intermediate and final versions of the tree is not possible during the modification process.

[0059] In the above schemes, operations on the AST will consume a lot of memory, especially when the IDE is processing large projects or a large number of files, the AST will consume a lot of memory and cause serious memory consumption.

[0060] In view of this, embodiments of this application provide a method for modifying the syntax tree, which helps to reduce memory consumption.

[0061] Figure 2This application illustrates a method for modifying a syntax tree according to an embodiment of the present application. Figure 2 The method 100 shown can be performed by a device that modifies the syntax tree.

[0062] For example, the apparatus for modifying the syntax tree can be a development tool or a functional module of a development tool. For instance, the development tool can be an IDE. That is, method 100 can be applied to an IDE.

[0063] An IDE can be a desktop IDE or a web IDE. The backend of a web IDE runs in a remote environment. For example, the remote environment could be a cloud server.

[0064] The above are merely examples. The solutions in this application embodiment can also be executed by other devices capable of modifying the AST, and this application embodiment does not limit this.

[0065] In the embodiments of this application, the syntax tree can also be referred to as the Abstract Syntax Tree (AST). For example, the method of modifying the syntax tree can also be referred to as the method of modifying the AST.

[0066] like Figure 2 As shown, method 100 includes the following steps.

[0067] 110. Obtain the first operation of the input. The first operation indicates the content to be modified in the first syntax tree corresponding to the first code text. The first syntax tree is stored on disk.

[0068] 120. Search for the node to be processed in the first syntax tree according to the first operation.

[0069] The node to be processed can include at least one of the following types: a node with a character to be deleted, a node with a character to be inserted, or a node with a character to be modified.

[0070] 130. Based on the first operation, create the target node in memory, and use the target node to replace the node to be processed to obtain the second syntax tree.

[0071] In step 110, the first operation input by the user can be obtained. The specific type of the first operation is related to the application scenario of this application.

[0072] The solution proposed in this application can be applied to any scenario where the AST needs to be modified.

[0073] As a possible example, the solution of this application embodiment can be applied to scenarios where the AST is directly modified. For example, the solution of this application embodiment can be applied to scenarios such as code refactoring, code inspection, or quick fix.

[0074] Taking code refactoring as an example, the first operation can be code refactoring.

[0075] For example, if a user is prompted to refactor a code segment in the interactive interface, and the user confirms to refactor the code segment, the user's confirmation operation can be regarded as the first operation in step 110.

[0076] Alternatively, the user interface can be prompted with one or more candidate refactoring methods for a code segment. If the user selects one of the refactoring methods, the user's selection can be considered as the first operation in step 110. In this case, obtaining the first operation can also be understood as obtaining the user's selection result.

[0077] Taking code rearrangement in code refactoring as an example, the candidate refactoring methods for a code segment can include candidate positions of the code within that segment. For instance, the interactive interface can display available or recommended candidate positions for code adjustment. The user can then select the adjusted position of the code. This selection operation is the first operation. Step 110 can also be understood as obtaining the adjusted position of the code selected by the user.

[0078] Suppose the original code text is as follows.

[0079] Code text #1:

[0080] When the user moves the mouse to the code "a=3" at line 0, the adjustable position of that code will be displayed on the interactive interface. The user wants to move "a=3" inside the `f()` function; for example, the user wants the modified code text (i.e., code text #2) to look like the code shown below.

[0081] Code text #2:

[0082] In this way, users can select the desired location from the candidate locations on the interactive interface, and the location is inside f().

[0083] It should be noted that in the context of code refactoring, code text #2 is an example of the code text that the user expects after refactoring, and not the code text that the user obtains after editing code text #1 (such as copying and pasting code).

[0084] As a possible example, the solution of this application embodiment can be applied to scenarios where the AST is modified by modifying the source code. For example, the solution of this application embodiment can be applied to a scenario of AST synchronization after code file modification. In this case, the first operation may include editing the first code file.

[0085] The first syntax tree is the syntax tree corresponding to the first code file.

[0086] Step 120 can be understood as: searching for the position of the node to be processed in the first syntax tree based on the first operation. Or, in other words, obtaining the node to be processed based on the first operation.

[0087] Step 130 can be understood as incrementally modifying the first syntax tree using the target node to obtain the second syntax tree. The target node is stored in memory. The target node is indicated by the first operation.

[0088] Incremental modification can be understood as adjustments to the node to be processed and the nodes related to the node to be processed. During incremental modification, nodes unrelated to the node to be processed remain unchanged.

[0089] The first syntax tree can be understood as the original syntax tree, i.e., the old AST. The target node can be understood as a node in the new syntax tree.

[0090] The node to be processed may include one node or multiple nodes. That is, the node in the first syntax tree that needs to be adjusted may be one or more. This application embodiment does not limit the number of nodes to be processed.

[0091] The target node can include one node or multiple nodes. That is, the new node created in memory can be one or more. This application does not limit the number of target nodes.

[0092] The number of target nodes and the number of nodes to be processed can be the same or different.

[0093] In the solution of this application embodiment, when the syntax tree needs to be modified, new nodes are created in memory, and the original syntax tree (i.e., the first syntax tree) is incrementally replaced using the new nodes. The original syntax tree (i.e., the first syntax tree) is stored on disk and does not need to be loaded into memory. Only the new nodes are created in memory. The syntax tree is modified by incremental replacement, without rebuilding the entire syntax tree. This reduces the number of new nodes that need to be created, thereby reducing memory consumption and improving performance.

[0094] Meanwhile, in the solutions of this application embodiment, incremental replacement is used to modify the original syntax tree, enabling incremental modification, improving processing efficiency, and facilitating real-time code updates. For example, when minor modifications are made to the source code file, the solution of this application embodiment allows for incremental modification of the syntax tree without re-parsing the entire source code file, thus improving processing efficiency. Furthermore, when minor modifications to the source code file are required, such as during code refactoring, the solution of this application embodiment allows for incremental modification of the syntax tree, which improves processing efficiency. The modified syntax tree allows for rapid code updates, thereby ensuring real-time code updates.

[0095] Optionally, the first syntax tree is stored in an index.

[0096] For example, nodes in the first syntax tree are stored on disk as columns.

[0097] The first syntax tree is stored in the index, meaning that the first syntax tree is stored on disk using an index-based storage method. In other words, information about the first syntax tree or its nodes can be retrieved through the index.

[0098] In the embodiments of this application, the index can also be referred to as a code index.

[0099] Furthermore, the first syntax tree can be compressed and stored in the index. That is, the nodes of the first syntax tree are stored in compressed form in the index. Or, in other words, the nodes of the first syntax tree are stored in a compressed code index.

[0100] For example, incremental modifications to the first syntax tree can be implemented using the `rewriter` class. The `rewriter` class is typically a global object in the program. Rewriting is achieved through `rewriter`, i.e., `rewrite()`.

[0101] As mentioned above, the type of node to be processed may include at least one of the following: a node to which a character is to be inserted, a node to which a character is to be deleted, or a node to which a character is to be modified.

[0102] The node to which the character is to be inserted can also be called the node to which the child node is to be inserted. This child node is the node corresponding to the character to be inserted.

[0103] The character to be inserted can be one character or multiple characters. The node corresponding to the character to be inserted can be one or multiple nodes.

[0104] The node containing the character to be deleted can also be called the node containing the child node to be deleted. This child node is the node corresponding to the character to be deleted.

[0105] The character to be deleted can be one character or multiple characters. The node corresponding to the character to be deleted can be one or multiple nodes.

[0106] The node containing the character to be modified can also be called the node containing the child node to be modified. This child node is the node corresponding to the character to be modified.

[0107] The character to be modified can be one character or multiple characters. The node corresponding to the character to be modified can be one or multiple nodes.

[0108] The target node may include the replacement node corresponding to the node to be processed. Step 130 may include: replacing the node to be processed with the replacement node corresponding to the node to be processed. Except for the content to be modified, the replacement node corresponding to the node to be processed and the node to be processed have the same reference relationship. The content to be modified is at least one of the following: characters to be inserted, characters to be deleted, or characters to be modified.

[0109] Node references can be used to indicate a node's child nodes. Two nodes with the same reference relationship can refer to the same child nodes. Among the child nodes of the node to be processed, except for those that need to be inserted, deleted, or modified, the replacement node retains references to the other child nodes of the node to be processed, enabling the reuse of other child nodes and thus achieving incremental modification.

[0110] If a node is not the root node, it still has a parent node. Node references can also be used to indicate the node's parent node. Two nodes having the same parent node reference can be understood as both nodes being referenced by the same parent node.

[0111] Optionally, method 130 may include: creating a parent node of the replacement node corresponding to the node to be processed, and using the parent node of the replacement node to replace the parent node of the node to be processed. Except for the node to be processed and its corresponding replacement node, the parent nodes of the node to be processed and the parent nodes of the replacement node have the same reference relationship.

[0112] In other words, the parent node of the node to be processed is processed in the same way as the node to be processed, that is, a new node is created to replace the parent node. The new node no longer references the node to be processed, but instead references the corresponding replacement node of the node to be processed; that is, the new node becomes the parent node of the replacement node. Except for the node to be processed and its corresponding replacement node, the reference relationship of the parent node of the node to be processed is the same as the reference relationship of the parent node of the replacement node. This process continues until all parent nodes have been replaced.

[0113] Furthermore, the target node may also include the node corresponding to the character to be inserted.

[0114] The following is an example illustrating the incremental modification process.

[0115] In one possible implementation, the node to be processed includes a first node to be processed, and the target node includes a first target node. The first node is a child node of the first node to be processed. The child nodes of the first target node do not include the first node; the first target node replaces the first node to be processed. Except for the reference relationships related to the first node, the other reference relationships between the first target node and the first node to be processed are the same.

[0116] Optionally, the node to be processed includes a first node to be processed, the first operation instruction deletes the first node from the child nodes of the first node to be processed, and the target node includes a first target node. The first target node is used to replace the first node to be processed. The child nodes referenced by the first target node include other child nodes of the first node to be processed besides the first node.

[0117] The first node to be processed is the node containing the character to be deleted. The position of the character to be deleted is the next level down from the first node to be processed; that is, the node corresponding to that character needs to be deleted from the child nodes of the first node to be processed.

[0118] When a node needs to be deleted, a new node can be created to replace the corresponding node in the original syntax tree. The new node no longer references the node to be deleted. Except for the content to be modified (the deleted child nodes), the new node retains the reference relationship of the corresponding node in the original syntax tree. For example, the new node still references the child nodes of the corresponding node in the original syntax tree. There is no need to rebuild all nodes, thus achieving incremental modification.

[0119] For example, if the first operation determines that node C2 (an example of the first node) needs to be deleted from the first syntax tree, then node C2 is the node corresponding to the character to be deleted. Node C2 is located one level below node D2 (an example of the first node to be processed) in the first syntax tree; in other words, the child node C2 of node D2 needs to be deleted. Accordingly, node D2 is the node to be processed. Node D1 (an example of the first target node) is constructed, and node D1 no longer references node C2. Node D1 is the target node. Node D2 is replaced with node D1. Node D1 retains all references to node D2 except for the reference related to node C2.

[0120] Figure 3 An example of incremental modification is given below. Figure 3 An example is provided to illustrate the incremental modification process.

[0121] Suppose that in a code refactoring scenario, oldStmt contains semicolons, such as Figure 3 As shown, the `oldStmt` node references the `semicolon` node. Based on the user-input code refactoring operation, it was determined that `semicolon` needs to be deleted. The `oldStmt` node is the node containing the character to be deleted. Figure 3 As shown, a new node, newStmt (the target node), is created to replace oldStmt. newStmt no longer references the semicolon node, but it still references other child nodes of oldStmt. The replacement of oldStmt by newStmt is achieved through rewriter, i.e., the replace instruction in the diagram: replace(oldStmt, newStmt).

[0122] Furthermore, such as Figure 3 As shown, a new node, the New Root node, is created to replace the Root node. The New Root node no longer references the oldStmt node, but instead references the replaced newStmt node. Furthermore, the New Root node still references the other child nodes of the Root.

[0123] exist Figure 3 In this approach, only the two newly created nodes are stored in memory, while the remaining nodes can be stored in a compressed index, for example, as columns on disk. This reduces memory usage and improves performance.

[0124] As another possible implementation, the node to be processed includes a second node to be processed, and the target node includes a second target node. The child nodes of the second node to be processed do not include the second node. The child nodes of the second target node include the second node. The second node to be processed is replaced by the second target node. Except for the reference relationship with the second node, the other reference relationships between the second target node and the second node to be processed are the same.

[0125] Optionally, the node to be processed includes a second node to be processed, the first operation instruction is to add a second node among the child nodes of the second node to be processed, the target node includes a second target node, the second target node is used to replace the second node to be processed, and the child nodes referenced by the second target node include all the child nodes and the second node of the second node to be processed.

[0126] For example, the second node does not belong to the first syntax tree. In this case, the target node may also include the second node, which is the node corresponding to the character to be inserted. The position of the character to be inserted is the next level down from the second node to be processed. That is, the node corresponding to the character to be inserted (i.e., the second node) needs to become a child node of the second node to be processed.

[0127] When characters need to be inserted, new nodes can be created to replace the corresponding nodes in the original syntax tree. Apart from the content to be modified (the added child nodes), the new nodes retain the reference relationships of the corresponding nodes in the original syntax tree. For example, the new nodes still reference the child nodes of the corresponding nodes in the original syntax tree. There is no need to rebuild all nodes, thus achieving incremental modification.

[0128] For example, if the first operation determines that node A1 (an example of a second node) needs to be added to the first syntax tree, then node A1 is the node corresponding to the character to be inserted. Node A1 needs to be inserted into the first syntax tree at the next level below node B2 (an example of a second node to be processed), or in other words, node A1 needs to be inserted at the next level below node B2 as a child node of node B2. Accordingly, node B2 is the node to be processed. Nodes A1 and B1 (an example of a second target node) are constructed, with node B1 referencing node A1. Nodes A1 and B1 are the target nodes. Node B2 is replaced using node B1. Node B1 retains all references to node B2 except for the reference related to node A1. Specifically, in addition to node A1, the child nodes of node B1 also include all the child nodes of node B2. If node B2 has a parent node in the first syntax tree, then its parent node also needs to be replaced. Specifically, a new node corresponding to the parent node of node B2 is created, and the parent node of node B2 is replaced by this new node. The new node corresponding to the parent node of node B2 no longer references node B2, but instead references node B1, becoming the parent node of node B1. The parent node of node B1 also references the other child nodes of the parent node of node B2, excluding node B2. For the remaining parent nodes, similar steps are repeated, and so on, until all parent nodes have been replaced.

[0129] Optionally, the second node is a child node of the third node in the first syntax tree.

[0130] In this case, the second node belongs to the first syntax tree.

[0131] In this way, the second node in the first syntax tree can be reused directly without creating it separately, further reducing memory usage.

[0132] Optionally, the node to be processed includes a third node to be processed. The child nodes of the third node to be processed include the second node. The target node includes a third target node. The third target node is used to replace the third node to be processed. The child nodes referenced by the third target node include all child nodes of the third node to be processed except for the second node.

[0133] In other words, the nodes to be processed include the second and third nodes to be processed in the first syntax tree. The child nodes of the second node to be processed do not include the second node, and the child nodes of the third node to be processed include the second node. The target nodes include the second and third target nodes. The child nodes of the third target node do not include the second node, and the child nodes of the second target node include the second node. Replace the second node to be processed with the second target node. Replace the third node to be processed with the third target node. Except for the reference relationships related to the second node, the second target node and the second node to be processed have the same other reference relationships. Except for the reference relationships related to the second node, the third target node and the third node to be processed have the same other reference relationships.

[0134] The second node is the node corresponding to the character whose position needs to be adjusted. In other words, the second node is the node to be moved. It needs to be moved from the level below the third node to the level below the second node, becoming a child node of the second node.

[0135] When a node needs to be moved, a new node can be created to replace the corresponding node in the original syntax tree. Aside from the content to be modified (the moved child nodes), the new node retains the reference relationships of the corresponding node in the original syntax tree. For example, the new node still references the child nodes of the corresponding node in the original syntax tree, eliminating the need to rebuild all nodes and enabling incremental modifications. Furthermore, the node to be moved can be reused as a child node of the replacement node without creating new child nodes, further reducing memory usage.

[0136] For example, based on the first operation, it is determined that node E2 (an example of the second node) in the first syntax tree needs to be moved from the level below node F2 (an example of the third node to be processed) to the level below node G2 (an example of the second node to be processed). Node E2 can be considered simultaneously as the node corresponding to the character to be deleted and the node corresponding to the character to be inserted. Accordingly, nodes F2 and G2 are the nodes to be processed, and nodes F1 (an example of the third target node) and G1 (an example of the second target node) are constructed. Node F1 no longer references node E2, and node G1 references node E2. Nodes F1 and G1 are the target nodes. Nodes F2 and G2 are replaced with nodes F1 and G1 respectively. Node F1 retains the references of node F2 to node E2. Node G1 retains the references of node G2 to node E2.

[0137] In addition, for characters that need to be modified, you can refer to the methods for deleting and adding characters mentioned earlier. For example, take the character before modification as the character to be deleted and the character after modification as the character to be inserted, and then perform incremental replacement as described above.

[0138] In the scheme of this application embodiment, the new node used to replace the node in the first syntax tree can reuse the node in the first syntax tree. For example, apart from the content that needs to be modified, the new node still references the child nodes of the corresponding node in the first syntax tree, without having to rebuild all nodes, thus achieving incremental modification. The target node is located in memory, while the subtree it references continues to be stored on disk, which helps to reduce memory consumption and improve performance.

[0139] Optionally, the first syntax tree may adopt a red-green tree data structure, and step 130 may include: implementing incremental replacement based on the red-green tree.

[0140] Red-green trees are a data structure that combines two trees into one. Nodes in the tree cannot be modified, but branches can be pruned.

[0141] The green tree represents the complete AST corresponding to the code text, and the red tree is the appearance built around the green tree. The red tree is only built when needed. The colors of the red tree and green tree do not have any other limiting effect.

[0142] The complete AST (i.e., the first syntax tree) corresponding to the first code text is a green tree. A red tree can be built when incremental replacement is needed. That is, the red tree can be built during the execution of step 130.

[0143] Furthermore, the first syntax tree can adopt a lossless red-green tree data structure, and in step 130, incremental replacement can be implemented based on the lossless red-green tree.

[0144] Lossless means that, in addition to code characters, non-code characters such as spaces are also stored in this data structure. In other words, all characters in the code text are stored in a lossless red-green tree data structure. This allows for a complete representation of the code text.

[0145] Specifically, the first syntax tree includes nodes corresponding to all characters in the first code text. When the first operation instructs the insertion of a node corresponding to a non-code character into the first syntax tree, the target node includes the node corresponding to the non-code character.

[0146] In other words, all characters in the first code text are represented in the first syntax tree, or stored in the red-green tree data structure of the first syntax tree. In this case, the red-green tree data structure is a lossless red-green tree data structure. When constructing a new node, corresponding nodes also need to be created for any non-code characters that need to be inserted.

[0147] For example, non-code characters may include formatting characters, such as space characters or newline characters.

[0148] For example, in a code refactoring scenario, if you want to insert a code segment between two lines of code as a separate line, the inserted line of code should be followed by a newline character. For instance, non-code characters can include the newline character following the inserted code segment. When creating a new node, you need to create the node corresponding to that newline character. That is, the target node can include the node corresponding to that newline character.

[0149] In this embodiment, incremental replacement can be performed based on a lossless red-green tree, enabling a complete representation of the code text. In scenarios where the AST is directly modified (e.g., code refactoring), synchronizing the modifications in the AST to the code text helps to restore a more accurate code text and improves the user experience.

[0150] The incremental replacement process for lossless red-green trees is described below. The incremental replacement process may include the following steps.

[0151] 131. Starting from the root node, search for the position of the node to be processed in the first syntax tree. During the search, construct the nodes in the initial red tree from top to bottom until the search is completed.

[0152] In other words, during the search process, the nodes in the initial redwood are constructed from top to bottom, starting from the root node. Each node in the initial redwood has a corresponding node in the green tree. The nodes in the initial redwood constructed during the search process represent the appearance of their corresponding nodes in the first syntax tree. Searching for the node to be processed in the first syntax tree can also be understood as determining the node to be processed in the initial redwood.

[0153] Since there is a one-to-one correspondence between the nodes in the mangrove tree and the nodes in the green tree, the node to be processed in this embodiment can be understood as a node to be processed in the mangrove tree or a node to be processed in the green tree. The target node can include the target node in the mangrove tree or the target node in the green tree.

[0154] The nodes in the initial mangrove tree are created in memory. For ease of description, in this embodiment, nodes in the mangrove tree are simply referred to as red nodes. Nodes in the green tree are simply referred to as green nodes.

[0155] 132, Construct the target node.

[0156] Target nodes can include red target nodes and green target nodes.

[0157] 133. Replace the node to be processed in the first syntax tree with the replacement node corresponding to the node to be processed in the target node.

[0158] For a detailed description, please refer to the examples below; they will not be elaborated upon here.

[0159] Furthermore, in scenarios where the AST is directly modified, the modifications to the AST can be synchronized to the code text.

[0160] Optionally, method 100 may also include steps 140 and 150 (not shown in the figure).

[0161] 140. Obtain the modified content of the code text based on the difference between the first syntax tree and the second syntax tree.

[0162] The changes to the code text are the changes to the code text corresponding to the changes in the second syntax tree relative to the first syntax tree.

[0163] For example, by comparing the first and second syntax trees using the tree differ algorithm, the changes to the code text can be obtained.

[0164] This is merely an example; other algorithms can also be used to determine the modifications to the code text, but this application does not limit this approach.

[0165] For example, modifications to the code text can include modification operations. Processing the first code text according to these modification operations yields the second code text.

[0166] 150. Modify the first code text according to the changes made to the code text to obtain the second code text.

[0167] The second code text corresponds to the second syntax tree.

[0168] In step 150, the modifications to the code text can be applied to the code editor in the IDE to refresh the code text in the code editor, that is, refresh the code text in the code editor to the second code text.

[0169] This allows users to see changes applied to code files in real time.

[0170] Furthermore, method 100 may also include step 160 (not shown in the figure).

[0171] 160. Store the target node on disk.

[0172] Optionally, the target node can be stored in an index.

[0173] For example, the target node is compressed and stored in the index.

[0174] In other words, newly added AST nodes in memory are compressed and stored in the index.

[0175] The triggering method for step 160 can be set as needed.

[0176] For example, step 160 can be triggered by changes to the code file. For instance, when the code file is changed, an index reconcile process can be triggered to compress and store newly added AST nodes in memory into the index.

[0177] Alternatively, step 160 can also be triggered by the user. For example, receiving relevant instructions from the user and then compressing and storing the newly added AST nodes in memory into the index.

[0178] The above are merely examples and do not constitute a limitation on the solutions implemented in this application.

[0179] The solutions of this application embodiment are illustrated below with reference to application scenarios. For ease of description, this application embodiment mainly uses a code refactoring scenario as an example.

[0180] Figure 4 A schematic flowchart illustrating the application of the solution of this application in a code refactoring scenario is shown. Figure 4 It can be regarded as Figure 2 This is one possible implementation of method 100. A detailed description can be found in method 100; to avoid repetition, further details are omitted in the description. Figure 4 The process shown is described with appropriate omissions.

[0181] like Figure 4 As shown, method 300 includes the following steps.

[0182] 310, receives code refactoring operations from users.

[0183] Figure 4 The code refactoring operation in the code is the first operation in method 100.

[0184] 320. The old AST is incrementally modified based on the lossless red-green tree incremental replacement algorithm to obtain the new AST (i.e. the second syntax tree in method 100).

[0185] Specifically, based on the code refactoring operation, the location of the node to be processed is searched in the lossless red-green tree of the old AST (i.e., the first syntax tree in method 100). A new AST node (i.e., the target node in method 100) is constructed based on the code refactoring operation. The old AST is then incrementally modified using the new AST node.

[0186] 330. Obtain the modified code text based on the differences between the new AST and the old AST.

[0187] Compare the new AST and the old AST to obtain the changes made to the code text.

[0188] 340. Refreshes the code text in the code editor based on changes made to the code text.

[0189] Apply changes to the code text to the code editor to refresh the code text in the code editor.

[0190] Figure 5 A schematic diagram of a code text refresh process is shown. Figure 5 The old AST and new AST in the text are... Figure 3 The syntax tree before and after the modification.

[0191] For example, such as Figure 5 As shown, the change in code text corresponding to the change in the syntax tree before and after modification is obtained by using tree differ. Figure 5 The diagram illustrates the changes made to the code text. Applying these changes to the code text will refresh the code text in the IDE's code editor.

[0192] The following example illustrates the code refactoring process using the proposed solution. Assume the refactoring scenario involves rearranging code. The original code text is code text #1 from the previous text. The user-inputted refactoring instruction moves the statement "a=3" between the lines "def f()" and "b=4". "a=3" represents the character to be moved.

[0193] Figure 6 (a) shows the lossless red-green tree of the old AST corresponding to code text #1. The lossless red-green tree of the old AST is the lossless red-green tree in the initial state.

[0194] like Figure 6 As shown in (a), the node corresponding to "a=3" is located one level below the pyFile node. "b=4" and "c=5" are located one level below the function->statementList node.

[0195] The code refactoring process may include the following steps.

[0196] S1, search for the location of the node to be processed in the old AST. During the search, nodes in the mangrove are lazily constructed.

[0197] Specifically, starting from the root node, search for a suitable node to move in the old AST, that is, the node corresponding to "a=3".

[0198] In step S1, the nodes in the mangrove tree are constructed from top to bottom based on the structure of the old AST until the search process is completed. The red node is the appearance of the corresponding green node.

[0199] Figure 6 (b) shows a schematic diagram of the lossless red-green tree construction process at the red node. Figure 6 As shown in (b), two new red nodes are constructed: AssignStmt1 and Function. These two new red nodes correspond to the assignment statement and function statement in code text #1, respectively.

[0200] Figure 6 (c) is a schematic diagram of the lossless red-green tree after the search is completed. The nodes to be processed include the node containing the statement "a=3" to be inserted; the node found to be processed is StatementList (the red node). During the search for this node, the red tree has been dynamically constructed, as shown below. Figure 6 As shown in (c), the red and green nodes `StatementList` can be considered examples of the second node to be processed. In this case, the node corresponding to "a=3" can be considered an example of the second node.

[0201] S2 modifies the lossless red-green tree.

[0202] S2 may include the following steps.

[0203] S2.1, Delete the node corresponding to "a=3" in the PyFile tree.

[0204] Figure 6 (d) shows a schematic diagram of the deletion process of the node corresponding to "a=3".

[0205] Specifically, a new green node pyFileV1 is constructed. The child nodes of pyFileV1 no longer include the green node assignStmt1; that is, the green node corresponding to "a=3" is deleted. Figure 6 As shown in (d), a new red node PyFileV1 is constructed, which corresponds to the new green node pyFileV1. The child nodes of PyFileV1 no longer include the red node AssignStmt1, as shown in (d). Figure 6 As shown in (d). In this case, the node corresponding to "a=3" can be considered an example of the first node. The green node pyFileV1 and the red node PyFileV1 can be considered examples of the first target node. The red node PyFile and the green node pyFile can be considered examples of the first node to be processed.

[0206] from Figure 6As can be seen in (d), when deleting a node, both the new green node pyFileV1 and the new red node PyFileV1 reference the child nodes of the previous version, thus achieving incremental modification.

[0207] The new green node pyFileV1 is created in memory, but the nodes of the subtrees it references are stored in the local code index in a compressed form, which helps to reduce memory consumption and improve performance.

[0208] S2.2, insert the node corresponding to “a=3” into the PyFileV1 tree.

[0209] like Figure 6 As shown in (e), a green node "\n___" corresponding to a newline character is created. The green node assignStmt1 corresponding to "a=3" and the green node "\n___" corresponding to the newline character are inserted into the next level of the green node statementListV1. This newline character is used to maintain the format of the code text. A new green node statementListV1 is created. For example, the above insertion operation of assignStmt1 can be achieved by calling the function "insertintobeginning(assignstmt1)".

[0210] Create a new red node StatementListV1, which corresponds to a new green node statementListV1. In this case, the green node assignStmt1 corresponding to "a=3" and the green node "\n___" corresponding to the newline character can be considered as the second node. The red node StatementListV1 and the green node statementListV1 can be considered as an example of the second target node.

[0211] exist Figure 6 In the green nodes shown in (e), the new green node statementListV1 and the green node corresponding to the new newline character are created in memory, while other green nodes remain compressed in the index. The green node corresponding to "a=3" reuses the old green node and is still stored in the local code index in compressed form.

[0212] S2.3, replace StatementList with StatementListV1.

[0213] Specifically, such as Figure 6 As shown in (f), the green node statementList is replaced with the green node statementListV1. The red node StatementList is replaced with StatementListV1.

[0214] The StatementList is replaced, and correspondingly, its parent node Function is also replaced.

[0215] like Figure 6 As shown in (f), a new green node `functionV1` is created, which references a new green node `statementListV1`. The green node `function` is replaced with the green node `functionV1`. A new red node `FunctionV1` is created, which references the new red node `StatementListV1`. The red node `Function` is replaced with the red node `FunctionV1`.

[0216] The Function was replaced, and correspondingly, its parent node PyFileV1 was also replaced.

[0217] like Figure 6 As shown in (f), a new green node pyFileV2 is created, which references a new green node functionV1. The green node pyFileV1 is replaced by the green node pyFileV2. A new red node PyFileV2 is created, which references a new red node FunctionV1. The red node PyFileV1 is replaced by the red node PyFileV2.

[0218] from Figure 6 (f) and Figure 6 As can be seen from the comparison in (a), for the green nodes, pyFile, function, and statementList are replaced, and assignStmt1 is moved to the required position. Apart from the newly created nodes pyFileV2, functionV1, statementListV1, and "\n___" stored in memory, most other nodes are still compressed and stored in the code index. The solution adopted in this application's embodiment helps reduce memory usage and improve performance.

[0219] S3 retrieves the modified code text corresponding to the changes in the modified AST compared to the old AST.

[0220] By comparing the syntax trees of the two versions before and after modification, the changes to the code text are obtained. These changes are then applied to the code editor, allowing users to see the changes applied to the code file in real time.

[0221] For example, modifications to the code text can include code text modification operations, as shown below: REPLACE [0, 5] with "def f():\na=3\nb=4\nc=5" DELETE "\n" in [5, 6] DELETE "def f():\nb=4\nc=5" in [6, 34] This means replacing the content in row 0, column 5 with "def f():\na=3\nb=4\nc=5", deleting "\n" in row 5, column 6, and deleting "def f():\nb=4\nc=5" in row 6, column 34.

[0222] Furthermore, refreshing the code file can trigger the reconcile process of the index, compressing and storing newly added nodes in memory into the code index.

[0223] In the solution of this application embodiment, incremental modification can be achieved through pruning without modifying the nodes of the first syntax tree. In AST modification systems based on mutable ASTs, updating the offsets and lengths of AST nodes is difficult because the syntax tree changes dynamically. Furthermore, code synchronization is challenging when multiple threads modify the AST simultaneously. However, in the solution of this application embodiment, the nodes of the syntax tree do not change dynamically, which facilitates updating the offsets and lengths of AST nodes and allows multiple threads to modify the AST simultaneously without affecting code synchronization.

[0224] The following is combined with Figures 7 to 10 The apparatus of the embodiments of this application will be described below. It should be understood that the apparatus described below is capable of performing the methods of the foregoing embodiments of this application. To avoid unnecessary repetition, repeated descriptions will be appropriately omitted when describing the apparatus of the embodiments of this application below.

[0225] Figure 7 This is a schematic block diagram of an apparatus for modifying a syntax tree according to an embodiment of this application. Figure 7 The device 2000 shown can be used to perform Figure 2 The method is shown. The apparatus 2000 includes an acquisition module 2010 and a syntax tree modification module 2020.

[0226] In one possible implementation, the device 2000 can be used to perform the method shown in 1.

[0227] The acquisition module 2010 is used to acquire the first operation of the input. The first operation indicates the content to be modified on the first syntax tree corresponding to the first code text. The first syntax tree is stored on the disk.

[0228] The syntax tree modification module 2020 is used to: search for a node to be processed in the first syntax tree according to a first operation, the node to be processed including at least one of the following types: a node of a character to be deleted, a node of a character to be inserted, or a node of a character to be modified; create a target node in memory according to the first operation, and use the target node to replace the node to be processed to obtain a second syntax tree.

[0229] Optionally, the apparatus further includes: a code modification module (not shown in the figure), used to: obtain the modified content of the code text based on the difference between the second syntax tree and the first syntax tree; and modify the first code text based on the modified content of the code text to obtain the second code text.

[0230] Optionally, the first syntax tree is stored in an index on disk.

[0231] Optionally, the first syntax tree adopts a red-green tree data structure. The first syntax tree includes nodes corresponding to all characters in the first code text. When the first operation instruction inserts a node corresponding to a non-code character into the first syntax tree, the target node includes the node corresponding to the non-code character.

[0232] Optionally, the node to be processed includes a first node to be processed, the first operation instruction deletes the first node from the child nodes of the first node to be processed, the target node includes a first target node, the first target node is used to replace the first node to be processed, and the child nodes referenced by the first target node include other child nodes of the first node to be processed besides the first node.

[0233] Optionally, the node to be processed includes a second node to be processed, the first operation instruction is to add a second node among the child nodes of the second node to be processed, the target node includes a second target node, the second target node is used to replace the second node to be processed, and the child nodes referenced by the second target node include all the child nodes and the second node of the second node to be processed.

[0234] Optionally, the second node is a child node of the third node in the first syntax tree.

[0235] Each module in device 2000 can be implemented in software or hardware. For example, the implementation of the syntax tree modification module 2020 will be described below. Similarly, the implementation of other modules can refer to the implementation of the syntax tree modification module 2020.

[0236] As an example of a software functional unit, the syntax tree modification module 2020 may include code running on compute instances. A compute instance may include at least one of a physical host (computing device), a virtual machine, or a container. Furthermore, the aforementioned compute instance may be one or more. For example, the syntax tree modification module 2020 may include code running on multiple hosts / virtual machines / containers. It should be noted that the multiple hosts / virtual machines / containers used to run the code may be distributed within the same region or in different regions. Further, the multiple hosts / virtual machines / containers used to run the code may be distributed within the same availability zone (AZ) or in different AZs, each AZ comprising one or more geographically proximate data centers. Typically, a region may include multiple AZs.

[0237] Similarly, multiple hosts / virtual machines / containers used to run this code can be distributed within the same Virtual Private Cloud (VPC) or across multiple VPCs. Typically, a VPC is set up within a region. Communication between two VPCs within the same region, as well as between VPCs in different regions, requires a communication gateway to be set up within each VPC to enable interconnection between VPCs.

[0238] As an example of a hardware functional unit, the syntax tree modification module 2020 may include at least one computing device, such as a server. Alternatively, the syntax tree modification module 2020 may also be a device implemented using an application-specific integrated circuit (ASIC) or a programmable logic device (PLD). The PLD may be implemented using a complex programmable logical device (CPLD), a field-programmable gate array (FPGA), generic array logic (GAL), or any combination thereof.

[0239] The syntax tree modification module 2020 includes multiple computing devices that can be distributed within the same region or in different regions. Similarly, the multiple computing devices included in the syntax tree modification module 2020 can be distributed within the same Availability Zone (AZ) or in different AZs. Likewise, the multiple computing devices included in the syntax tree modification module 2020 can be distributed within the same Virtual Private Cloud (VPC) or in multiple VPCs. These multiple computing devices can be any combination of computing devices such as servers, ASICs, PLDs, CPLDs, FPGAs, and GALs.

[0240] It should be noted that, in other embodiments, the syntax tree modification module 2020 can be used to execute any step in the method of modifying the syntax tree, and the acquisition module 2010 can be used to execute any step in the method of modifying the syntax tree. The steps that each module is responsible for implementing can be specified as needed. By implementing different steps in the method of modifying the syntax tree through each module, all functions of the device 2000 can be realized.

[0241] This application also provides a computing device 1000. For example... Figure 8 As shown, the computing device 1000 includes a bus 1002, a processor 1004, a memory 1006, and a communication interface 1008. The processor 1004, the memory 1006, and the communication interface 1008 communicate with each other via the bus 1002. The computing device 1000 can be a server or a terminal device. It should be understood that this application does not limit the number of processors and memories in the computing device 1000.

[0242] Bus 1002 can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. Buses can be categorized as address buses, data buses, control buses, etc. For ease of representation, Figure 8 The bus 1002 may be represented by a single line, but this does not mean that there is only one bus or one type of bus. The bus 1002 may include a path for transmitting information between various components of the computing device 1000 (e.g., memory 1006, processor 1004, communication interface 1008).

[0243] The processor 1004 may include any one or more processors such as a central processing unit (CPU), a graphics processing unit (GPU), a microprocessor (MP), or a digital signal processor (DSP).

[0244] The memory 1006 may include volatile memory, such as random access memory (RAM). The processor 1004 may also include non-volatile memory, such as read-only memory (ROM), flash memory, hard disk drive (HDD), or solid state drive (SSD).

[0245] The memory 1006 stores executable program code, which the processor 1004 executes to implement the functions of the aforementioned acquisition module 2010 and syntax tree modification module 2020, thereby implementing the method for modifying the syntax tree. In other words, the memory 1006 stores instructions for executing the method of modifying the syntax tree.

[0246] The communication interface 1008 uses transceiver modules such as, but not limited to, network interface cards and transceivers to enable communication between the computing device 1000 and other devices or communication networks.

[0247] This application also provides a computing device cluster. The computing device cluster includes at least one computing device. The computing device can be a server, such as a central server, an edge server, or a local server in a local data center. In some embodiments, the computing device can also be a terminal device such as a desktop computer, a laptop computer, or a smartphone.

[0248] like Figure 9 As shown, the computing device cluster includes at least one computing device 1000. The memory 1006 of one or more computing devices 1000 in the computing device cluster may store the same instructions for executing methods to modify the syntax tree.

[0249] In some possible implementations, the memory 1006 of one or more computing devices 1000 in the computing device cluster may also store partial instructions for executing methods to modify the syntax tree. In other words, a combination of one or more computing devices 1000 can jointly execute instructions for executing methods to modify the syntax tree.

[0250] It should be noted that the memory 1006 in different computing devices 1000 within the computing device cluster can store different instructions, each used to execute a portion of the functions of the syntax tree modification device. That is, the instructions stored in the memory 1006 of different computing devices 1000 can implement the functions of one or more modules in the fetch module 2010 and the syntax tree modification module 2020.

[0251] In some possible implementations, one or more computing devices in a computing device cluster can be connected via a network. This network can be a wide area network (WAN) or a local area network (LAN), etc. Figure 10 One possible implementation is shown. For example... Figure 10 As shown, two computing devices 1000A and 1000B are connected via a network. Specifically, they are connected to the network through communication interfaces in each computing device. In this possible implementation, the memory 1006 in computing device 1000A stores instructions for executing the functions of the acquisition module 2010. Simultaneously, the memory 1006 in computing device 1000B stores instructions for executing the functions of the syntax tree modification module 2020.

[0252] Figure 10 The connection method between the computing device clusters shown can be such that, considering the method of modifying the syntax tree provided in this application may require data storage, the function implemented by the syntax tree modification module 2020 is delegated to the computing device 1000B for execution.

[0253] It should be understood that Figure 10 The functions of computing device 1000A shown can also be performed by multiple computing devices 1000. Similarly, the functions of computing device 1000B can also be performed by multiple computing devices 1000.

[0254] This application also provides a computer program product containing instructions. The computer program product may be a software or program product containing instructions, capable of running on a computing device or stored on any usable medium. When the computer program product is run on at least one computing device, it causes the at least one computing device to perform a method for modifying the syntax tree.

[0255] This application also provides a computer-readable storage medium. The computer-readable storage medium can be any available medium that a computing device can store, or a data storage device such as a data center containing one or more available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., solid-state drive). The computer-readable storage medium includes instructions that instruct the computing device to perform a method for modifying the syntax tree.

[0256] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the protection scope of the technical solutions of the embodiments of this application.

[0257] Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.

[0258] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.

[0259] In the several embodiments provided in this application, it should be understood that the disclosed systems, apparatuses, and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between apparatuses or units may be electrical, mechanical, or other forms.

[0260] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.

[0261] In addition, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.

[0262] If the aforementioned functions are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0263] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.

Claims

1. A method for modifying a syntax tree, characterized in that, The method is applied to an integrated development environment (IDE), and the method includes: The first operation of obtaining input indicates the content to be modified in the first syntax tree corresponding to the first code text, and the first syntax tree is stored on disk; According to the first operation, the node to be processed in the first syntax tree is searched. The node to be processed includes at least one of the following types: a node of a character to be deleted, a node of a character to be inserted, or a node of a character to be modified. According to the first operation, a target node is created in memory, and the target node is used to replace the node to be processed to obtain a second syntax tree.

2. The method according to claim 1, characterized in that, The method further includes: The modifications to the code text are obtained based on the differences between the second syntax tree and the first syntax tree; The first code text is modified based on the changes made to the code text to obtain the second code text.

3. The method according to claim 1 or 2, characterized in that, The first syntax tree is stored in an index on disk.

4. The method according to any one of claims 1 to 3, characterized in that, The first syntax tree adopts a red-green tree data structure. The first syntax tree includes nodes corresponding to all characters in the first code text. When the first operation instructs to insert a node corresponding to a non-code character in the first syntax tree, the target node includes the node corresponding to the non-code character.

5. The method according to any one of claims 1 to 4, characterized in that, The node to be processed includes a first node to be processed, the first operation instruction deletes the first node from the child nodes of the first node to be processed, the target node includes a first target node, the first target node is used to replace the first node to be processed, and the child nodes referenced by the first target node include other child nodes of the first node to be processed besides the first node.

6. The method according to any one of claims 1 to 5, characterized in that, The node to be processed includes a second node to be processed. The first operation instruction adds a second node to the child nodes of the second node to be processed. The target node includes a second target node, which is used to replace the second node to be processed. The child nodes referenced by the second target node include all the child nodes of the second node to be processed and the second node.

7. The method according to claim 6, characterized in that, The second node is a child node of the third node in the first syntax tree.

8. An apparatus for modifying a syntax tree, characterized in that, The device is used in an integrated development environment (IDE), and the device includes: The acquisition module is used to acquire the first operation of the input, the first operation indicating the content to be modified on the first syntax tree corresponding to the first code text, the first syntax tree being stored on the disk; The syntax tree modification module is used for: According to the first operation, a node to be processed is searched in the first syntax tree. The node to be processed includes at least one of the following types: a node of a character to be deleted, a node of a character to be inserted, or a node of a character to be modified. According to the first operation, a target node is created in memory, and the target node is used to replace the node to be processed to obtain a second syntax tree.

9. The apparatus according to claim 8, characterized in that, The device further includes: The code modification module is used for: The modifications to the code text are obtained based on the differences between the second syntax tree and the first syntax tree; The first code text is modified based on the changes made to the code text to obtain the second code text.

10. The apparatus according to claim 8 or 9, characterized in that, The first syntax tree is stored in an index on disk.

11. The apparatus according to any one of claims 8 to 10, characterized in that, The first syntax tree adopts a red-green tree data structure. The first syntax tree includes nodes corresponding to all characters in the first code text. When the first operation instructs to insert a node corresponding to a non-code character in the first syntax tree, the target node includes the node corresponding to the non-code character.

12. The apparatus according to any one of claims 8 to 11, characterized in that, The node to be processed includes a first node to be processed, the first operation instruction deletes the first node from the child nodes of the first node to be processed, the target node includes a first target node, the first target node is used to replace the first node to be processed, and the child nodes referenced by the first target node include other child nodes of the first node to be processed besides the first node.

13. The apparatus according to any one of claims 8 to 12, characterized in that, The node to be processed includes a second node to be processed. The first operation instruction adds a second node to the child nodes of the second node to be processed. The target node includes a second target node, which is used to replace the second node to be processed. The child nodes referenced by the second target node include all the child nodes of the second node to be processed and the second node.

14. The apparatus according to claim 13, characterized in that, The second node is a child node of the third node in the first syntax tree.

15. A computing device cluster, characterized in that, It includes at least one computing device, each computing device including a processor and memory; The processor of the at least one computing device is configured to execute instructions stored in the memory of the at least one computing device to cause the cluster of computing devices to perform the method as described in any one of claims 1 to 7.

16. A computer-readable storage medium, characterized in that, It includes computer program instructions, which, when executed by a cluster of computing devices, perform the method as described in any one of claims 1 to 7.

17. A computer program product containing instructions, characterized in that, When the instruction is executed by the computing device cluster, the computing device cluster performs the method as described in any one of claims 1 to 7.