Code interception method and code range determination method
By screening the target interception node set in the code syntax tree and determining the code interception range, the problem of incomplete code fragments when processing large language models is solved, and the integrity of code interception and model performance are improved.
Patent Information
- Application Number
- CN202410585370.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-11
- Publication Date
- 2024-10-01
Smart Images

Figure CN118733109A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present specification relate to the field of computer technology, and in particular to a code interception method and a code range determination method. Background Art
[0002] Code completion and generation is an important area in current software development. Recently, with the development of large language models, code completion and generation tasks have gradually been processed based on large language models. However, when using large language models to process code, the number of characters that can be received and processed is limited. Therefore, when the number of characters in a code file exceeds the character processing capacity of a large language model, the code file needs to be intercepted.
[0003] In current practical applications, intercepting code files may result in incomplete code snippets and loss of context information, which can mislead the model's understanding of the code snippets, reduce the quality of model-generated code, and affect model performance. Therefore, a method is urgently needed to solve the technical problem of incomplete code snippets. Summary of the invention
[0004] In view of this, the embodiments of this specification provide a code interception method and a code range determination method. One or more embodiments of this specification also relate to a code interception device, a code range determination device, a computing device, a computer-readable storage medium and a computer program product to solve the technical defects existing in the prior art.
[0005] According to a first aspect of an embodiment of this specification, a code interception method is provided, comprising:
[0006] Obtaining a code syntax tree corresponding to the source code file, wherein the code syntax tree is used to represent the code structure of the source code file, the code syntax tree includes at least one node, and the node information of each node is used to determine the number of code characters corresponding to each node;
[0007] According to a preset character threshold, screening a target interception node set in the code syntax tree;
[0008] Determining a code interception range based on the target interception node set;
[0009] According to the code interception range, a target code segment is intercepted in the source code file.
[0010] Optionally, screening a target interception node set in the code syntax tree according to a preset character threshold includes:
[0011] Determining an initial interception node in the code syntax tree, and determining the number of initial code characters corresponding to the initial interception node;
[0012] When the number of initial code characters does not reach the preset character threshold, traverse the peer nodes of the initial interception node, screen a reserved node set in each peer node, and obtain the number of reserved code characters corresponding to the reserved node set;
[0013] Determining the number of candidate code characters of the source code file according to the number of initial code characters and the number of reserved code characters;
[0014] When the number of candidate code characters does not reach the preset character threshold, a new initial interception node is determined in the code syntax tree, and the step of determining the number of initial code characters corresponding to the initial interception node is continued until the number of candidate code characters reaches the preset character threshold, thereby obtaining a target interception node set.
[0015] Optionally, determining an initial interception node in the code syntax tree includes:
[0016] Determining a code interception starting position in the code syntax tree;
[0017] An initial interception node is determined according to the code interception starting position and a preset interception node type.
[0018] Optionally, determining the number of initial code characters corresponding to the initial interception node includes:
[0019] Obtaining node information of the initial intercept node, and determining a code start position and a code end position of the initial intercept node in the source code file from the node information of the initial intercept node;
[0020] The number of initial code characters corresponding to the initial interception node is determined according to the code start position and the code end position.
[0021] Optionally, traversing the peer nodes of the initial interception node and screening the reserved node set in each peer node includes:
[0022] Traversing the predecessor nodes of the initial interception node in each node at the same level, and screening a reserved predecessor node set in each predecessor node;
[0023] Obtaining the number of reserved prefix code characters corresponding to the reserved prefix node set;
[0024] When the number of the initial code characters and the number of the reserved preceding code characters do not reach the preset character threshold, traverse the post nodes of the initial intercepted node in each node at the same level, and select the reserved post node set in each post node, and generate the reserved node set based on the reserved preceding node set and the reserved post node set;
[0025] In a case where the number of the initial code characters and the number of the reserved preceding code characters reach the preset character threshold, the reserved preceding node set is determined as a reserved node set.
[0026] Optionally, traversing the predecessor nodes of the initial interception node in each node at the same level, and screening a reserved predecessor node set in each predecessor node, includes:
[0027] Determine a first preceding node of the initial interception node, and obtain the number of first preceding code characters corresponding to the first preceding node;
[0028] In the case that the number of characters of the initial code and the number of characters of the first preceding code do not reach the preset character threshold, determining a second preceding node of the initial interception node, and returning to execute the step of obtaining the number of characters of the first preceding code corresponding to the first preceding node, until the number of characters of the initial code and the number of characters of the preceding code reach the preset character threshold, or the traversal of each preceding node is completed, and a set of reserved preceding nodes is obtained;
[0029] When the number of characters of the initial code and the number of characters of the first preceding code reach the preset character threshold, it is determined that there is no reserved preceding node for the initial interception node.
[0030] Optionally, traversing the subsequent nodes of the initial intercepted node in each node at the same level, and screening a reserved subsequent node set in each subsequent node, including:
[0031] Determine a first post-node of the initial interception node, and obtain the number of first post-code characters corresponding to the first post-node;
[0032] In the case that the number of the initial code characters, the number of the reserved pre-code characters, and the number of the first post-code characters do not reach the preset character threshold, determining a second post-node of the initial interception node, and returning to execute the step of obtaining the number of the first post-code characters corresponding to the first post-code node, until the number of the initial code characters, the number of the reserved pre-code characters, and the number of post-code characters reach the preset character threshold, or the traversal of each post-code node is completed, and a set of reserved post-code nodes is obtained;
[0033] When the number of the initial code characters, the number of the reserved pre-code characters, and the number of the first post-code characters reach the preset character threshold, it is determined that there is no reserved post-code node for the initial interception node.
[0034] Optionally, determining a new initial interception node in the code syntax tree includes:
[0035] Obtaining a parent node of a currently intercepted node, wherein the currently intercepted node is determined according to a traversal order of each reserved node in the reserved node set;
[0036] The parent node of the current intercepted node is determined as a new initial intercepted node.
[0037] Optionally, screening a target interception node set in the code syntax tree according to a preset character threshold includes:
[0038] Determining an initial interception node in the code syntax tree, and determining the number of initial code characters corresponding to the initial interception node;
[0039] Determining a first candidate code character number of the source code file according to the initial code character number;
[0040] When the number of the first candidate code characters reaches the preset character threshold, traverse the peer nodes of the initial interception node, screen a set of nodes to be pruned in each peer node, and obtain the number of pruned code characters corresponding to the set of nodes to be pruned;
[0041] Determining the number of second candidate code characters of the source code file according to the number of pruned code characters and the number of the first candidate code characters;
[0042] When the number of characters in the second candidate code reaches the preset character threshold, a new initial interception node is determined in the code syntax tree, and the step of determining the number of initial code characters corresponding to the initial interception node is continued until the number of candidate code characters does not reach the preset character threshold, thereby obtaining a target interception node set.
[0043] Optionally, determining an initial interception node in the code syntax tree includes:
[0044] Determining a code interception starting position in the code syntax tree;
[0045] Determine a code interception path according to the code interception starting position and the root node of the code syntax tree;
[0046] In the code interception path, determining a path node to be processed, wherein the path node to be processed is determined according to the hierarchical structure of the syntax tree;
[0047] According to the node distance between the peer node corresponding to the path node to be processed and the path node to be processed, an initial interception node is determined in each peer node, wherein the node distance between the initial interception node and the path node to be processed is greater than the node distance between other peer nodes and the path node to be processed.
[0048] Optionally, determining the first candidate code character number of the source code file according to the initial code character number includes:
[0049] Obtain the number of source code characters of the source code file;
[0050] The first candidate code character quantity is determined according to the source code character quantity and the initial code character quantity.
[0051] Optionally, traversing the sibling nodes of the initial interception node and selecting a set of nodes to be pruned from the sibling nodes includes:
[0052] Traversing each node at the same level according to the node distance between each node at the same level and the path node to be processed;
[0053] Determine the number of target sibling node code characters corresponding to the target sibling node, wherein the target sibling node is any one of the sibling nodes;
[0054] Determining the target candidate code character quantity of the source code file according to the target same-level node code character quantity;
[0055] When the number of characters of the target candidate code reaches the preset character threshold, a reference peer node of the target peer node is determined as a node to be pruned, and a set of nodes to be pruned is generated based on each node to be pruned.
[0056] Optionally, determining a new initial interception node in the code syntax tree includes:
[0057] Get the code interception path;
[0058] Determine the to-be-processed path node corresponding to the initial interception node, and the to-be-processed node layer corresponding to the to-be-processed path node;
[0059] Determining an update node layer of the node layer to be processed, wherein the update node layer is a new node layer to be processed;
[0060] Determine a node in the update node layer located on the code interception path as an update path node, wherein the update path node is a new path node to be processed;
[0061] According to the node distance between the node at the same level corresponding to the update path node and the update path node, a new initial intercept node is determined in each node at the same level.
[0062] Optionally, determining a code interception range based on the target interception node set includes:
[0063] Determine a to-be-determined code start position and a to-be-determined code end position of a to-be-determined interception node in the source code file, wherein the to-be-determined interception node is any target interception node in the target interception node set;
[0064] Determine the to-be-determined code interception range corresponding to the to-be-determined interception node according to the to-be-determined code starting position and the to-be-determined code ending position;
[0065] According to the target code interception range corresponding to each target interception node in the target interception node set, the code interception range corresponding to the target interception node set is determined.
[0066] According to a second aspect of the embodiments of this specification, a code interception device is provided, including:
[0067] An acquisition module is configured to acquire a code syntax tree corresponding to a source code file, wherein the code syntax tree is used to represent a code structure of the source code file, the code syntax tree includes at least one node, and node information of each node is used to determine the number of code characters corresponding to each node;
[0068] A screening module is configured to screen a target interception node set in the code syntax tree according to a preset character threshold;
[0069] A determination module is configured to determine a code interception range based on the target interception node set;
[0070] The interception module is configured to intercept a target code segment in the source code file according to the code interception range.
[0071] According to a third aspect of an embodiment of this specification, a method for determining a code range is provided, including:
[0072] Receiving a code interception instruction for a source code file, wherein the code interception instruction carries a preset character threshold;
[0073] Obtaining a code syntax tree corresponding to the source code file, wherein the code syntax tree is used to represent the code structure of the source code file, the code syntax tree includes at least one node, and the node information of each node is used to determine the number of code characters corresponding to each node;
[0074] According to the preset character threshold, screening a target interception node set in the code syntax tree;
[0075] Based on the target interception node set, a code interception range of the source code file is determined, and the code interception range is sent to a target terminal.
[0076] According to a fourth aspect of the embodiments of this specification, a device for determining a code range is provided, including:
[0077] A receiving module is configured to receive a code interception instruction for a source code file, wherein the code interception instruction carries a preset character threshold;
[0078] A syntax tree acquisition module is configured to acquire a code syntax tree corresponding to the source code file, wherein the code syntax tree is used to represent the code structure of the source code file, the code syntax tree includes at least one node, and the node information of each node is used to determine the number of code characters corresponding to each node;
[0079] A set screening module, configured to screen a target interception node set in the code syntax tree according to the preset character threshold;
[0080] The sending module is configured to determine a code interception range of the source code file based on the target interception node set, and send the code interception range to a target terminal.
[0081] According to a fifth aspect of an embodiment of this specification, a computing device is provided, including:
[0082] Memory and processor;
[0083] The memory is used to store computer programs / instructions, and the processor is used to execute the computer programs / instructions. When the computer programs / instructions are executed by the processor, the steps of the above method are implemented.
[0084] According to a sixth aspect of the embodiments of this specification, a computer-readable storage medium is provided, which stores a computer program / instruction, and the steps of the above method are implemented when the computer program / instruction is executed by a processor.
[0085] According to a seventh aspect of the embodiments of this specification, a computer program product is provided, comprising a computer program / instruction, which implements the steps of the above method when executed by a processor.
[0086] An embodiment of the present specification realizes that, through the syntax tree corresponding to the source code file and the preset character threshold, the target interception node set is screened in the code syntax tree, so as to intercept the source code file based on the code interception range formed by the target interception node set, and obtain the target code fragment. Since the code syntax tree can be used to characterize the code structure of the source code source file, each target interception node in the target interception node set screened in the code syntax tree will not destroy the code structure of the source code file, and then combined with the preset character threshold, the target interception node set is screened in the code syntax tree, and the code fragment to be intercepted is accurately intercepted on the basis of retaining the code structure and context information of the source code file, thereby improving the integrity of code interception of the source code file. BRIEF DESCRIPTION OF THE DRAWINGS
[0087] Figure 1 is a flow chart of a code interception method provided by an embodiment of this specification;
[0088] Figure 2 is a schematic diagram of a screening target interception node set provided by an embodiment of this specification;
[0089] Figure 3 is a schematic diagram of another method of screening a target interception node set provided by an embodiment of the present specification;
[0090] Figure 4 It is a flowchart of a method of screening a target interception node set provided by an embodiment of this specification;
[0091] Figure 5 is another flowchart of screening a target interception node set provided by an embodiment of this specification;
[0092] Figure 6 is a process flow chart of a code interception method provided by an embodiment of this specification;
[0093] Figure 7 is a process flow chart of another code interception method provided by an embodiment of this specification;
[0094] Figure 8 It is a structural schematic diagram of a code interception device provided by an embodiment of this specification;
[0095] Fig. 9 is a flow chart of a code range determination method provided by an embodiment of this specification;
[0096] Fig.10 It is a structural schematic diagram of a code range determination device provided by an embodiment of this specification;
[0097] Fig.11It is a structural block diagram of a computing device provided by an embodiment of this specification. DETAILED DESCRIPTION
[0098] Many specific details are described in the following description to facilitate a full understanding of this specification. However, this specification can be implemented in many other ways than those described herein, and those skilled in the art can make similar generalizations without violating the connotation of this specification, so this specification is not limited to the specific implementation disclosed below.
[0099] The terms used in one or more embodiments of this specification are only for the purpose of describing specific embodiments, and are not intended to limit one or more embodiments of this specification. The singular forms of "a", "said" and "the" used in one or more embodiments of this specification and the appended claims are also intended to include plural forms, unless the context clearly indicates other meanings. It should also be understood that the term "and / or" used in one or more embodiments of this specification refers to and includes any or all possible combinations of one or more associated listed items.
[0100] It should be understood that although the terms first, second, etc. may be used to describe various information in one or more embodiments of this specification, this information should not be limited to these terms. These terms are only used to distinguish the same type of information from each other. For example, without departing from the scope of one or more embodiments of this specification, the first may also be referred to as the second, and similarly, the second may also be referred to as the first. Depending on the context, the word "if" as used herein may be interpreted as "at the time of" or "when" or "in response to determining".
[0101] In addition, it should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in one or more embodiments of this specification are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of the relevant countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.
[0102] First, the terms involved in one or more embodiments of this specification are explained.
[0103] PSI (Program Structure Interface): is an intermediate representation used to represent the structure of a programming language. In many integrated development environments (such as InterJ IDEA), PSI is used to provide code completion, code navigation, refactoring, static analysis and other functions.
[0104] Code completion and generation is an important area in current software development. Recently, with the development of large language models, code completion and generation tasks have gradually been processed based on large language models. However, when using large language models to process code, the number of characters that can be received and processed is limited. Therefore, when the number of characters in a code file exceeds the character processing capacity of a large language model, the code file needs to be intercepted. In current practical applications, intercepting code files may result in incomplete intercepted code snippets and loss of contextual information, thereby misleading the model's understanding of the code snippet, reducing the quality of the model-generated code, and affecting the model performance.
[0105] In this specification, a code interception method and a code range determination method are provided. This specification also relates to a code interception device, a code range determination device, a computing device, a computer-readable storage medium and a computer program product, which are described in detail one by one in the following embodiments.
[0106] See also Figure 1 , Figure 1 A flowchart of a code interception method provided according to an embodiment of the present specification is shown, which specifically includes the following steps.
[0107] Step 102: Obtain a code syntax tree corresponding to the source code file.
[0108] In practical applications, if the code file is too long, intercepting the code file may damage the integrity of the code file and make the code file unusable. Based on this, the code file can be intercepted by building a code syntax tree. Based on the code structure of the code file, the code file can be intercepted to preserve the integrity of the code file as much as possible and retain the context information of the code file.
[0109] Among them, the source code file refers to a code file that needs to be code intercepted. In actual applications, the source code file can be a code file that has not been code processed, or it can be a code file that still needs to be code intercepted again after code interception. The code syntax tree refers to the code syntax tree of the source code file constructed based on the code structure of the source code file, which is a tree representation of the syntax structure of the source code file. The code syntax tree can specifically be a PSI syntax tree, or an AST syntax tree (abstract syntax code, abstract syntax tree), etc. The code syntax tree is used to characterize the code structure of the source code file. The code syntax tree includes at least one node, and the node information of each node is used to determine the number of code characters corresponding to each node.
[0110] In practical applications, the code syntax tree may be constructed in advance, or may be constructed before code interception is performed on the source code file. This specification does not limit the construction of the code syntax tree.
[0111] Specifically, after obtaining the source code file, it can be determined whether the code syntax tree of the source code file already exists. If it does, the code syntax tree can be directly obtained for subsequent code interception. If it does not exist, the code syntax tree of the source code file can be constructed first, and after the code syntax tree is constructed, the constructed code syntax tree can be used for subsequent code interception.
[0112] Since the code syntax tree is a tree representation of the grammatical structure of the source code file, the subsequent code interception of the source code file is performed through the code syntax tree of the source code file. The code interception is based on the grammatical code structure of the source code file. The code structure and semantic information of the source code file can be retained during the code interception of the source code file.
[0113] Step 104: Filter a target interception node set in the code syntax tree according to a preset character threshold.
[0114] Since code interception is to obtain code snippets with a certain number of code characters, before code interception is performed on the source code file, it is necessary to perform code interception on the source code file according to the upper limit of the number of code characters that need to be intercepted. Among them, the preset character threshold refers to the upper limit of the number of code characters for code interception of the source code file. For example, when using a large model to process a code file, since the code characters that the large model can receive and process are limited, it is necessary to perform code interception on the code file so that the large model can receive and process the code file after code interception. Therefore, the number of code characters that the large model can receive and process is the preset character threshold.
[0115] In one or more embodiments provided in this specification, after obtaining the code syntax tree corresponding to the source code file, the source code file can be code intercepted according to the code syntax tree according to a preset character threshold, specifically, a target interception node set is screened from multiple nodes of the code syntax tree. Among them, the target interception node set refers to a set for storing target interception nodes, including multiple target interception nodes; the target interception node refers to a node that needs to be retained in the code syntax tree, and the target interception node is used to determine the code interception range of the source code file. The code interception range refers to the code range of the target code fragment that needs to be intercepted and retained in the source code file.
[0116] Specifically, for the implementation method of screening the target interception node set in the code syntax tree according to the preset character threshold, this specification specifically provides the implementation method of the bottom-up search interception strategy and the implementation method of the top-down pruning interception strategy. The bottom-up search interception strategy refers to a strategy of traversing each node from the bottom to the top in the code syntax tree to search for the target interception node set. The top-down pruning interception strategy refers to a strategy of traversing each node from the top to the bottom in the code syntax tree to prune the non-target interception node set and obtain the target interception node set. The following explains the implementation methods of screening the target interception node set based on the bottom-up search interception strategy and the top-down pruning interception strategy.
[0117] See also Figure 2 , Figure 2 FIG. 1 shows a schematic diagram of a method for screening a target interception node set according to an embodiment of the present specification. Figure 2 The figure shows the process of selecting the target interception node set in the code syntax tree corresponding to the source code file based on the bottom-up search interception strategy. In actual applications, the user needs to input the node, which can be determined according to the user's mouse cursor position. According to the node input by the user, the node representing the complete code block or expression is searched upwards, for example, Figure 2 The Block node, Class node, etc. shown in the figure. In the node's peer nodes, follow the steps below: Figure 2 The same-level traversal is performed in the direction of the arrow of the same-level traversal shown in . After the traversal of the same-level nodes of the node is completed, the traversal continues upward until the number of code characters corresponding to the determined target interception node set reaches the preset character threshold, and the traversal is completed.
[0118] See also Figure 3 , Figure 3 FIG. 2 shows another schematic diagram of a method for screening target interception node sets according to an embodiment of the present specification. Figure 3 The figure shows the process of selecting the target interception node set in the code syntax tree corresponding to the source code file based on the top-down pruning interception strategy. Specifically, after determining the node where the user input is located, a code interception path is determined according to the node where the user input is located and the root node of the code syntax tree. According to the determined code interception path, the code syntax tree is traversed from top to bottom. In each node of the code interception path, for each node, in the node's peer nodes, according to the following Figure 2 The same-level traversal is performed in the direction of the arrow of the same-level traversal shown in . After the traversal of this node is completed, the traversal continues downward until the number of code characters corresponding to the determined target interception node set reaches a preset character threshold, and the traversal is completed.
[0119] This specification uses a bottom-up search interception strategy or a top-down pruning interception strategy to screen a target interception node set in a code syntax tree of a source code file, thereby determining a code interception range of the source code file based on the target interception node set to implement code interception of the source code file.
[0120] The following specifically describes the implementation process of screening the target interception node set based on the bottom-up search interception strategy.
[0121] In a specific implementation provided in this specification, according to a preset character threshold, screening a target interception node set in the code syntax tree includes:
[0122] Determining an initial interception node in the code syntax tree, and determining the number of initial code characters corresponding to the initial interception node;
[0123] When the number of initial code characters does not reach the preset character threshold, traverse the peer nodes of the initial interception node, screen a reserved node set in each peer node, and obtain the number of reserved code characters corresponding to the reserved node set;
[0124] Determining the number of candidate code characters of the source code file according to the number of initial code characters and the number of reserved code characters;
[0125] When the number of candidate code characters does not reach the preset character threshold, a new initial interception node is determined in the code syntax tree, and the step of determining the number of initial code characters corresponding to the initial interception node is continued until the number of candidate code characters reaches the preset character threshold, thereby obtaining a target interception node set.
[0126] Among them, the initial interception node refers to the first node that needs to be retained or pruned in any node layer of the code syntax tree. In the bottom-up search interception strategy, the initial interception node is specifically the first node that needs to be retained in any node layer of the code syntax tree. The initial number of code characters refers to the number of code characters of the initial interception node, or the number of code characters of the initial interception node and its corresponding child nodes. Since there are often multiple parent nodes and child nodes in the code syntax tree, the determined initial interception node may be a child node or a parent node. When the initial interception node is a child node, the initial number of code characters is the number of code characters of the initial interception node. When the initial interception node is a parent node, the initial number of code characters is the number of code characters of the initial interception node and its corresponding child nodes.
[0127] Specifically, an initial interception node is determined in the code syntax tree, and the number of initial code characters corresponding to the initial interception node is determined, so as to determine whether it is necessary to continue traversing the code syntax tree according to the number of initial code characters and a preset character threshold.
[0128] In a specific implementation manner provided in this specification, determining an initial interception node in the code syntax tree includes:
[0129] Determining a code interception starting position in the code syntax tree;
[0130] An initial interception node is determined according to the code interception starting position and a preset interception node type.
[0131] The code interception starting position refers to the starting position in the code syntax tree for code interception of the source code file. The code interception starting position can specifically be a node determined according to the user input, that is, the node where the user input is located, which can be specifically determined according to the user's mouse cursor position. The preset interception node type refers to a pre-set node type that represents a complete code block or expression, such as a Block node type, a Statement node type, a Method node type, a Class node type, etc.
[0132] Specifically, according to the user's mouse cursor position, the code interception starting position is determined in the code syntax tree, and according to the code interception starting position, the first node whose node type is a preset interception node type is searched upward, and the node is determined as the initial interception node.
[0133] For example, after determining the code interception start position in the code syntax tree, if the first node type of the preset interception node type found upward is a Statement node, then the Statement node is the initial interception node.
[0134] After the initial interception node is determined in the code syntax tree, it is necessary to obtain the number of initial code characters corresponding to the initial interception node to determine whether the obtained number of initial code characters corresponding to the initial interception node reaches a preset character threshold.
[0135] In a specific implementation provided in this specification, determining the number of initial code characters corresponding to the initial interception node includes:
[0136] Obtaining node information of the initial intercept node, and determining a code start position and a code end position of the initial intercept node in the source code file from the node information of the initial intercept node;
[0137] The number of initial code characters corresponding to the initial interception node is determined according to the code start position and the code end position.
[0138] The node information is used to store information such as the node type of each node in the code syntax tree, the code start position of the node in the source code file, the code end position of the node in the source code file, the parent node of the node, and the child node of the node.
[0139] Specifically, after determining the initial interception node, the node information of the initial interception node is obtained, and the code starting position and code ending position of the initial interception node in the source code file are determined in the node information of the initial interception node, and the number of initial code characters corresponding to the initial interception node is determined based on the code starting position and code ending position of the initial interception node in the source code file.
[0140] After determining the number of initial code characters corresponding to the initial intercept node, the number of initial code characters is compared with the preset number of characters to determine whether the number of initial code characters reaches the preset number of characters. If the number of initial code characters does not reach the preset number of characters, the peer nodes of the initial intercept node are traversed, and the reserved node set is screened in each peer node of the initial intercept node.
[0141] Among them, the sibling nodes of the initial intercepted node are nodes located at the same node layer as the initial intercepted node, that is, the brother nodes of the initial intercepted node. The reserved node set refers to a set used to store reserved nodes, including multiple reserved nodes; the reserved nodes refer to the nodes that need to be reserved among the sibling nodes of the initial intercepted node, and the reserved nodes specifically include reserved predecessor nodes and reserved successor nodes. The reserved predecessor node refers to the predecessor node that needs to be reserved among the sibling nodes of the initial intercepted node; the reserved successor node refers to the successor node that needs to be reserved among the sibling nodes of the initial node. The number of reserved code characters is the number of code characters corresponding to each reserved node in the reserved node set. The method for determining the number of reserved code characters is the same as the method for determining the number of initial code characters. The method for determining the number of reserved code characters can refer to the above-mentioned method for determining the number of initial code characters, and this specification will not repeat it here.
[0142] In a specific implementation provided in this specification, traversing the peer nodes of the initial interception node and screening the reserved node set in each peer node includes:
[0143] Traversing the predecessor nodes of the initial interception node in each node at the same level, and screening a reserved predecessor node set in each predecessor node;
[0144] Obtaining the number of reserved prefix code characters corresponding to the reserved prefix node set;
[0145] When the number of the initial code characters and the number of the reserved preceding code characters do not reach the preset character threshold, traverse the post nodes of the initial intercepted node in each node at the same level, and select the reserved post node set in each post node, and generate the reserved node set based on the reserved preceding node set and the reserved post node set;
[0146] In a case where the number of the initial code characters and the number of the reserved preceding code characters reach the preset character threshold, the reserved preceding node set is determined as a reserved node set.
[0147] In practical applications, the determined initial intercept node may have a preceding node and a succeeding node. Therefore, in the process of traversing the same-level nodes of the initial intercept node, it is necessary to traverse both the preceding node and the succeeding node of the initial intercept node.
[0148] The number of reserved leading code characters refers to the number of code characters reserved for leading nodes; the number of reserved trailing code characters refers to the number of code characters reserved for trailing nodes.
[0149] Specifically, in the process of traversing the nodes at the same level of the initial intercept node, the preceding nodes of the initial intercept node may be traversed first, and then the succeeding nodes of the initial intercept node may be traversed. The preceding nodes of the initial intercept node are traversed, a reserved preceding node set is screened in the preceding nodes of the initial intercept node, and the number of reserved preceding code characters corresponding to the reserved preceding node set is obtained. The number of initial code characters and the number of reserved preceding code characters are summed, and the character sum result of the number of initial code characters and the number of reserved preceding code characters is determined. The character sum result is compared with a preset character threshold to determine whether the character sum result exceeds the preset character threshold. If the character sum result does not exceed the preset character threshold, the succeeding nodes of the initial intercept node are continued to be traversed, a reserved succeeding node set is screened in the succeeding nodes of the initial intercept node, and a reserved node set is generated based on the reserved preceding node set and the reserved succeeding node set. When the sum of characters exceeds the preset character threshold, there is no need to traverse the subsequent nodes of the initial intercepted node, so that the reserved preceding node set is determined to be the reserved node set.
[0150] Furthermore, the specific implementation process of traversing the preceding nodes and succeeding nodes of the initial intercepted node is as follows:
[0151] In a specific implementation provided in this specification, traversing the predecessor nodes of the initial interception node in each node at the same level, and screening the reserved predecessor node set in each predecessor node, includes:
[0152] Determine a first preceding node of the initial interception node, and obtain the number of first preceding code characters corresponding to the first preceding node;
[0153] In the case that the number of characters of the initial code and the number of characters of the first preceding code do not reach the preset character threshold, determining a second preceding node of the initial interception node, and returning to execute the step of obtaining the number of characters of the first preceding code corresponding to the first preceding node, until the number of characters of the initial code and the number of characters of the preceding code reach the preset character threshold, or the traversal of each preceding node is completed, and a set of reserved preceding nodes is obtained;
[0154] When the number of characters of the initial code and the number of characters of the first preceding code reach the preset character threshold, it is determined that there is no reserved preceding node for the initial interception node.
[0155] The first predecessor node refers to any predecessor node of the same level node of the initial interception node. The first number of predecessor code characters refers to the number of code characters corresponding to the first predecessor node. The second predecessor node refers to the predecessor node of the first predecessor node, and the node distance between the second predecessor node and the first predecessor node is less than the node distance between each predecessor node of the first predecessor node and the first predecessor node.
[0156] Specifically, the first preceding node of the initial interception node is determined, and based on the above-mentioned method of obtaining the number of initial code characters, the first preceding code character number of the first preceding node is obtained. The initial code character number and the first preceding code character number are summed to obtain a character sum result. The character sum result is compared with a preset character threshold. If the character sum result does not reach the preset character threshold, the second preceding node of the initial interception node is determined, that is, the preceding node of the first preceding node is determined, and the step of obtaining the first preceding code character number of the first preceding node is returned to execute, that is, the second preceding code character number of the second preceding node is continued to be obtained until the character sum result of the initial code character number and the character sum of each preceding code character number reaches the preset character threshold, or each preceding node is traversed, and a reserved preceding node set can be obtained. If the character sum result reaches the preset character threshold, it means that there is no reserved preceding node for the initial interception node, so there is no need to continue to traverse the preceding nodes of the initial interception node. At this time, the execution process based on the bottom-up search interception strategy ends.
[0157] In a specific implementation provided in this specification, traversing the subsequent nodes of the initial intercepted node in each node at the same level, and screening a reserved subsequent node set in each subsequent node, includes:
[0158] Determine a first post-node of the initial interception node, and obtain the number of first post-code characters corresponding to the first post-node;
[0159] In the case that the number of the initial code characters, the number of the reserved pre-code characters, and the number of the first post-code characters do not reach the preset character threshold, determining a second post-node of the initial interception node, and returning to execute the step of obtaining the number of the first post-code characters corresponding to the first post-code node, until the number of the initial code characters, the number of the reserved pre-code characters, and the number of post-code characters reach the preset character threshold, or the traversal of each post-code node is completed, and a set of reserved post-code nodes is obtained;
[0160] When the number of the initial code characters, the number of the reserved pre-code characters, and the number of the first post-code characters reach the preset character threshold, it is determined that there is no reserved post-code node for the initial interception node.
[0161] The first post-positioned node refers to any post-positioned node of the same level node as the initial intercepted node. The first post-positioned code character quantity refers to the code character quantity corresponding to the first post-positioned node. The second post-positioned node refers to the post-positioned node of the first post-positioned node, and the node distance between the second post-positioned node and the first post-positioned node is less than the node distance between each post-positioned node of the first post-positioned node and the first post-positioned node.
[0162] Specifically, after the traversal of the preceding node of the initial intercepted node is completed and the number of code characters obtained has not reached the preset character threshold, the traversal of the following nodes of the initial intercepted node is continued. The first following node of the initial intercepted node is determined, and based on the above-mentioned method of obtaining the number of initial code characters, the number of first following code characters of the first following node is obtained. The number of initial code characters, the number of reserved preceding code characters and the number of first following code characters are summed to obtain a character sum result. The character sum result is compared with the preset character threshold. If the character sum result does not reach the preset character threshold, the second following node of the initial intercepted node is determined, that is, the following node of the first following node is determined, and the step of obtaining the number of first following code characters of the first following node is returned to execute, that is, the number of second following code characters of the second following node is continued to be obtained until the character sum result of the initial code character number, the number of reserved preceding code characters and the number of each following code character reaches the preset character threshold, or the traversal of each following node is completed, and the reserved following node set can be obtained. When the character sum reaches the preset character threshold, it means that there is no reserved subsequent node for the initial interception node, so there is no need to continue traversing the subsequent nodes of the initial interception node. At this time, the execution process based on the bottom-up search interception strategy ends.
[0163] Furthermore, after obtaining the reserved preceding node set and the reserved succeeding node set, the reserved node set is obtained, and the number of reserved code characters corresponding to the reserved node set is obtained. According to the number of initial code characters and the number of reserved code characters, the number of candidate code characters of the source code file is determined, so that it can be determined whether to continue traversing the code syntax tree according to the number of candidate code characters and the preset character threshold. Among them, the number of candidate code characters refers to the sum of the number of characters of the initial code characters and the number of reserved code characters. That is, after obtaining the reserved node set, the number of code characters that can be used for code interception of the source code file.
[0164] After determining the number of candidate code characters in the source code file, determine whether the number of candidate code characters reaches the preset character threshold. If the number of candidate code characters does not reach the preset character threshold, you can continue to traverse the previous layer of the node layer where the initial interception node is located. At this time, it is necessary to determine a new initial interception node in the code syntax tree, and continue to execute the step of determining the number of initial code characters corresponding to the initial interception node until the new number of candidate code characters reaches the preset character threshold, and then obtain the target interception node set. If the number of candidate code characters reaches the preset character threshold, there is no need to continue traversing the code syntax tree. At this time, the execution process based on the bottom-up search interception strategy ends. Specifically, the implementation method of determining a new initial interception node in the code syntax tree is as follows:
[0165] In a specific implementation manner provided in this specification, determining a new initial interception node in the code syntax tree includes:
[0166] Obtaining a parent node of a currently intercepted node, wherein the currently intercepted node is determined according to a traversal order of each reserved node in the reserved node set;
[0167] The parent node of the current intercepted node is determined as a new initial intercepted node.
[0168] Since each parent node of the code syntax tree includes the content of its corresponding child node, in order to improve the efficiency of traversing the code syntax tree, the parent node can be preferentially selected as the new initial interception node.
[0169] The current intercepted node refers to a node in the reserved node set whose traversal order is later than the traversal order of other reserved nodes. The current intercepted node can be a preceding node or a succeeding node. For example, the reserved node set includes reserved node 1, reserved node 2, and reserved node 3, and the traversal order of these three reserved nodes is reserved node 2, reserved node 1, and reserved node 3, respectively. Since the traversal order of reserved node 3 is later than that of reserved node 1 and reserved node 2, reserved node 3 is determined to be the current intercepted node.
[0170] Specifically, the parent node of the current intercepted node is obtained, and the parent node of the current intercepted node is determined as a new initial intercepted node. By determining the parent node of the current intercepted node as a new initial intercepted node, the efficiency of traversing the code syntax tree is improved.
[0171] See also Figure 4 , Figure 4 FIG. 2 shows a flow chart of a method for screening a target interception node set according to an embodiment of the present specification. Figure 4 As shown, determine the initial intercept node, determine the number of initial code characters corresponding to the initial intercept node, determine whether the number of initial code characters reaches the preset character threshold, if reached, the execution process based on the bottom-up search interception strategy ends. If not reached, determine the predecessor node forward based on the initial intercept node, and obtain the predecessor node range and the number of predecessor code characters of the predecessor node. Determine whether the number of initial code characters and the number of predecessor code characters reach the preset character threshold, if reached, the execution process based on the bottom-up search interception strategy ends. If not reached, determine whether there is still a predecessor node, if so, return to the step of determining the predecessor node forward, if not, determine the rear node backward. Get the number of rear code characters of the rear node, and determine whether the number of initial code characters, the number of predecessor code characters, and the number of rear code characters reach the preset character threshold, if reached, the execution process based on the bottom-up search interception strategy ends. If not reached, determine whether there is any subsequent node. If so, return to execute the step of determining the subsequent node backward. If not, obtain the parent node upward and determine the parent node as the new initial interception node until the number of code characters reaches the preset character threshold and obtains the target interception node set.
[0172] The bottom-up search and interception strategy provided in this specification uses the same-level traversal and upward traversal methods to ensure that the target code fragments subsequently obtained based on the target interception node set are structurally complete; by judging whether the number of code characters reaches the preset character threshold node by node, the number of characters in the code fragments that need to be intercepted can be flexibly managed.
[0173] The following specifically describes the implementation process of screening the target interception node set based on the top-down pruning interception strategy.
[0174] In a specific implementation provided in this specification, according to a preset character threshold, screening a target interception node set in the code syntax tree includes:
[0175] Determining an initial interception node in the code syntax tree, and determining the number of initial code characters corresponding to the initial interception node;
[0176] Determining a first candidate code character number of the source code file according to the initial code character number;
[0177] When the number of the first candidate code characters reaches the preset character threshold, traverse the peer nodes of the initial interception node, screen a set of nodes to be pruned in each peer node, and obtain the number of pruned code characters corresponding to the set of nodes to be pruned;
[0178] Determining the number of second candidate code characters of the source code file according to the number of pruned code characters and the number of the first candidate code characters;
[0179] When the number of characters in the second candidate code reaches the preset character threshold, a new initial interception node is determined in the code syntax tree, and the step of determining the number of initial code characters corresponding to the initial interception node is continued until the number of candidate code characters does not reach the preset character threshold, thereby obtaining a target interception node set.
[0180] Among them, in the top-down pruning strategy, the initial truncation node is specifically the first node to be pruned determined in any node layer of the code syntax tree. The first candidate code character number refers to the number of remaining code characters after pruning the initial code character number corresponding to the initial truncation node in the source code file. The set of nodes to be pruned refers to a set for storing nodes to be pruned, including multiple nodes to be pruned; the node to be pruned refers to a node that needs to be pruned in the code syntax tree. The number of pruned code characters refers to the number of code characters corresponding to the node to be pruned. The second candidate code character number refers to the number of code characters obtained after pruning the number of pruned code characters in the first candidate code character number.
[0181] Specifically, in the top-down pruning and interception strategy, the initial interception node of the code syntax tree is determined, and the number of initial code characters corresponding to the initial interception node is determined. According to the number of initial code characters, the number of first candidate code characters obtained after pruning the number of initial code characters in the source code file is determined. It is judged whether the number of first candidate code characters reaches the preset character threshold. When the number of first candidate code characters reaches the preset character threshold, the nodes at the same level of the initial interception node are traversed, and the set of nodes to be pruned is screened in each node at the same level, and the number of pruned code characters corresponding to the set of nodes to be pruned is obtained. According to the number of pruned code characters and the number of first candidate code characters, the number of second candidate code characters of the source code file is determined. It is judged whether the number of second candidate code characters reaches the preset character threshold. When the number of second candidate code characters reaches the preset character threshold, a new initial interception node is determined in the code syntax tree, and the code syntax tree is continued to be traversed until the number of candidate code characters does not reach the preset character threshold, and the target interception node set is obtained.
[0182] In a specific implementation manner provided in this specification, determining an initial interception node in the code syntax tree includes:
[0183] Determining a code interception starting position in the code syntax tree;
[0184] Determine a code interception path according to the code interception starting position and the root node of the code syntax tree;
[0185] In the code interception path, determining a path node to be processed, wherein the path node to be processed is determined according to the hierarchical structure of the syntax tree;
[0186] According to the node distance between the peer node corresponding to the path node to be processed and the path node to be processed, an initial interception node is determined in each peer node, wherein the node distance between the initial interception node and the path node to be processed is greater than the node distance between other peer nodes and the path node to be processed.
[0187] Since the code syntax tree has only one root node, a node path consisting of multiple nodes can be determined based on the root node of the code syntax tree and the starting position of the code interception. This node path is the code interception path. The code interception path is the basis for node pruning of the code syntax tree, and the nodes on the code interception path will not be pruned. The path node to be processed refers to any path node determined from the root node from top to bottom.
[0188] Specifically, the code interception starting position is determined in the code syntax tree, and a code interception path is determined according to the code interception starting position and the root node of the code syntax tree. In the code interception path, any path node to be processed is determined from the root node according to the top-down strategy. The node distance between the sibling node of the path node to be processed and the path node to be processed is obtained, and the sibling node with the largest node distance among the node distances is determined as the initial interception node.
[0189] After determining the initial truncation node, the initial number of code characters corresponding to the initial truncation node is obtained. However, since the top-down pruning strategy obtains the target truncation node by pruning each node in the code syntax tree, after obtaining the initial number of code characters, it is necessary to further determine whether the number of code characters remaining in the source code file after pruning the initial truncation node reaches the preset character threshold.
[0190] Based on this, in a specific implementation manner provided in this specification, determining the first candidate code character number of the source code file according to the initial code character number includes:
[0191] Obtain the number of source code characters of the source code file;
[0192] The first candidate code character quantity is determined according to the source code character quantity and the initial code character quantity.
[0193] The number of source code characters refers to the number of original code characters in the source code file without code interception.
[0194] Specifically, after determining the number of initial code characters corresponding to the initial cut node, the number of source code characters of the source code file is obtained, and based on the number of source code characters and the number of initial code characters, the number of remaining code characters after pruning the initial number of code characters corresponding to the initial cut node in the source code file is calculated, that is, the number of first candidate code characters.
[0195] Furthermore, after determining the number of remaining code characters after pruning the initial number of code characters corresponding to the initial interception node, that is, after determining the number of first candidate code characters, it is possible to determine whether the number of first candidate code characters reaches a preset character threshold, and then determine whether it is necessary to continue traversing and pruning the code syntax tree. In the case where the number of first candidate code characters reaches the preset character threshold, traverse the same-level nodes of the initial interception node, screen the set of nodes to be pruned in each of the same-level nodes, and obtain the number of pruned code characters corresponding to the set of nodes to be pruned. The specific implementation method for obtaining the number of pruned code characters can be found in the above-mentioned implementation method for obtaining the number of initial code characters, and this specification will not be repeated here.
[0196] In a specific implementation provided in this specification, traversing the peer nodes of the initial interception node and selecting the set of nodes to be pruned in each peer node includes:
[0197] Traversing each node at the same level according to the node distance between each node at the same level and the path node to be processed;
[0198] Determine the number of target sibling node code characters corresponding to the target sibling node, wherein the target sibling node is any one of the sibling nodes;
[0199] Determining the target candidate code character quantity of the source code file according to the target same-level node code character quantity;
[0200] When the number of characters of the target candidate code reaches the preset character threshold, a reference peer node of the target peer node is determined as a node to be pruned, and a set of nodes to be pruned is generated based on each node to be pruned.
[0201] Among them, the target sibling node refers to any one of the sibling nodes of the path node to be processed, and the target sibling node is the node to be pruned. The number of target sibling node code characters refers to the number of code characters corresponding to the target sibling node. The number of target candidate code characters refers to the number of remaining code characters after pruning based on the number of target sibling node code characters corresponding to the target sibling node. The reference sibling node refers to the adjacent sibling node of the target sibling node, and the node distance between the reference sibling node and the path node to be processed is less than the node distance between the target sibling node and the path node to be processed.
[0202] Specifically, according to the node distance between each sibling node and the path node to be processed, each sibling node is traversed according to the strategy of node distance from far to near. Taking the target sibling node as an example, the number of target sibling node code characters corresponding to the target sibling node is determined, and the number of target candidate code characters of the source code file is determined according to the number of target sibling node code characters and the number of code characters before the source code file is pruned based on the number of target sibling node code characters. It is judged whether the number of target candidate code characters reaches the preset character threshold. When the number of target candidate code characters reaches the preset character threshold, the reference sibling node of the target sibling node is determined as the node to be pruned, thereby generating a set of nodes to be pruned based on each node to be pruned.
[0203] After traversing to obtain the set of nodes to be pruned and obtaining the number of pruned code characters corresponding to the set of nodes to be pruned, calculate the number of second candidate code characters obtained after pruning the number of pruned code characters from the number of characters at the first candidate point based on the number of pruned code characters and the number of first candidate code characters. Then, determine whether the number of characters at the second candidate code reaches the preset character threshold. If the number of characters at the second candidate code reaches the preset character threshold, it is necessary to continue traversing downward along the code syntax tree. At this time, it is necessary to determine a new initial interception node downward in the code syntax tree, and continue to perform the step of determining the number of initial code characters corresponding to the initial interception node until the number of candidate code characters does not reach the preset character threshold, so as to obtain the target interception node set. If the number of characters at the second candidate point does not reach the preset character threshold, there is no need to continue traversing the code syntax tree. At this time, the execution process based on the top-down pruning and interception strategy ends. Specifically, the implementation method of determining a new initial interception node in the code syntax tree is as follows:
[0204] In a specific implementation manner provided in this specification, determining a new initial interception node in the code syntax tree includes:
[0205] Get the code interception path;
[0206] Determine the to-be-processed path node corresponding to the initial interception node, and the to-be-processed node layer corresponding to the to-be-processed path node;
[0207] Determining an update node layer of the node layer to be processed, wherein the update node layer is a new node layer to be processed;
[0208] Determine a node in the update node layer located on the code interception path as an update path node, wherein the update path node is a new path node to be processed;
[0209] According to the node distance between the node at the same level corresponding to the update path node and the update path node, a new initial intercept node is determined in each node at the same level.
[0210] The node layer to be processed refers to the node layer where the path node to be processed is located. The update node layer refers to the updated node layer to be processed, specifically the next node layer of the node layer to be processed. The update path node refers to the updated path node to be processed, specifically the next path node to be processed in the code interception path, and the update path node is located on the update node layer.
[0211] Specifically, a code interception path is obtained, and in the code interception path, a to-be-processed path node corresponding to the initial interception node and a to-be-processed node layer where the to-be-processed path node is located are determined. The next node layer of the to-be-processed node layer is determined to be an update node layer, and the nodes in the code interception path in the update node layer are determined to be update path nodes. Among the same-level nodes of the update path node, according to the node distance between each of the same-level nodes and the update path node, the node with the largest node distance is determined as a new initial interception node.
[0212] For example, the update node layer includes node 1, node 2, node 3 and node 4, among which node 3 is the update path node, and the nodes between node 1, node 2, node 4 and node 3 are arranged from far to near, therefore, node 1 is determined as the new initial intercept node.
[0213] See also Figure 5 , Figure 5 FIG. 2 shows another flow chart of screening target interception node sets according to an embodiment of the present specification. Figure 5As shown, the code interception path is determined according to the code interception starting position and the root node of the code syntax tree. On the code interception path, according to the top-down traversal strategy, the path node to be processed is determined, and according to the node distance between the peer node of the path node to be processed and the path node to be processed, the initial interception node is determined according to the traversal strategy from far to near, and the number of initial code characters corresponding to the initial interception node is determined. According to the number of initial code characters and the number of source code characters of the source code file, the number of candidate code characters of the source code file is determined. It is judged whether the number of candidate code characters reaches the preset character threshold. If not, there is no need to traverse and prune the code syntax tree. At this time, the execution process based on the top-down pruning interception strategy ends. If it is reached, it is necessary to determine whether there is a node to be pruned in the peer node of the initial interception node. If so, the node to be pruned is determined, and the number of pruned code characters of the node to be pruned is obtained. According to the number of pruned code characters and the number of candidate code characters, the number of new candidate code characters is determined, and it is judged whether the number of new candidate code characters reaches the preset character threshold. If it is not reached, there is no need to traverse and prune the code syntax tree. At this time, the execution process based on the top-down pruning and interception strategy ends. If it is reached, return to the step of determining whether there is a node to be pruned. If there is no node to be pruned in the same-level node of the initial interception node, traverse to the next node layer of the node layer to be processed where the path node to be processed is located, determine the update path node in the next node layer, and determine the updated path node as the new path node to be processed. According to the new path node to be processed (i.e., the updated path node), determine the initial interception node (new interception node) corresponding to the new path node to be processed, until the number of candidate code characters does not reach the preset character threshold, and obtain the target interception node set.
[0214] The top-down pruning and interception strategy provided in this specification uses the same-level traversal and downward traversal methods to ensure that the target code fragments subsequently obtained based on the target interception node set are structurally complete; by judging whether the number of code characters reaches the preset character threshold node by node, dynamic adjustment of the target interception node set is achieved.
[0215] Step 106: Determine a code interception range based on the target interception node set.
[0216] After traversing the code syntax tree, a target interception node set can be obtained, so that a code interception range for point interception of the source code file can be determined according to the target interception node set.
[0217] The code capture range refers to a code interval or code offset used to capture code snippets in a source code file.
[0218] In a specific implementation provided in this specification, determining a code interception range based on the target interception node set includes:
[0219] Determine a to-be-determined code start position and a to-be-determined code end position of a to-be-determined interception node in the source code file, wherein the to-be-determined interception node is any target interception node in the target interception node set;
[0220] Determine the to-be-determined code interception range corresponding to the to-be-determined interception node according to the to-be-determined code starting position and the to-be-determined code ending position;
[0221] According to the target code interception range corresponding to each target interception node in the target interception node set, the code interception range corresponding to the target interception node set is determined.
[0222] The to-be-determined interception node refers to any target interception node in the target interception node set.
[0223] Specifically, the starting position of the code to be determined and the ending position of the code to be determined of the interception node to be determined in the source code file are determined. According to the starting position of the code to be determined and the ending position of the code to be determined of the interception node to be determined in the source code file, the interception range of the code to be determined corresponding to the interception node to be determined is determined, so that based on the same method, the target code interception range of each target interception node in the target interception node set can be obtained to further determine the code interception range corresponding to the target interception node set.
[0224] After the target interception node set is obtained according to the preset character threshold, the code interception range of the source code file is determined based on the target interception node set, so as to realize dynamic adjustment of the code interception range based on the target interception node set.
[0225] Step 108: According to the code interception range, intercept a target code segment in the source code file.
[0226] The target code snippet refers to a code snippet intercepted based on the code interception range.
[0227] After the code interception range is determined, code interception can be performed in the source code file based on the code interception range to obtain the target code fragment.
[0228] The code interception method provided in the present specification includes: obtaining a code syntax tree corresponding to a source code file, wherein the code syntax tree is used to represent the code structure of the source code file, the code syntax tree includes at least one node, and the node information of each node is used to determine the number of code characters corresponding to each node; according to a preset character threshold, screening a target interception node set in the code syntax tree; based on the target interception node set, determining a code interception range; according to the code interception range, intercepting a target code fragment in the source code file.
[0229] An embodiment of the present specification realizes that, through the syntax tree corresponding to the source code file and the preset character threshold, the target interception node set is screened in the code syntax tree, so as to intercept the source code file based on the code interception range formed by the target interception node set, and obtain the target code fragment. Since the code syntax tree can be used to characterize the code structure of the source code source file, each target interception node in the target interception node set screened in the code syntax tree will not destroy the code structure of the source code file, and then combined with the preset character threshold, the target interception node set is screened in the code syntax tree, and the code fragment to be intercepted is accurately intercepted on the basis of retaining the code structure and context information of the source code file, thereby improving the integrity of code interception of the source code file.
[0230] The following combination Figure 6 , the code interception method is further described. Figure 6 FIG. 1 shows a process flow chart of a code interception method provided by an embodiment of the present specification. Figure 6As shown, in the execution process based on the bottom-up search interception strategy, the code syntax tree of the source code file is obtained, the node where the user input is located is determined, the code interception starting position is determined in the code syntax tree, and according to the code interception starting position, the first node type is upwardly searched for a node of a preset interception node type, and the node is determined as the initial interception node. Further, the number of initial code characters corresponding to the initial interception node is determined, and it is determined whether the number of initial code characters reaches the preset character threshold. If it is reached, the execution process based on the bottom-up search interception strategy ends. If it is not reached, the preceding node is determined forward based on the initial interception node, and the preceding node range and the number of preceding code characters of the preceding node are obtained. It is determined whether the number of initial code characters and the number of preceding code characters reach the preset character threshold. If it is reached, the execution process based on the bottom-up search interception strategy ends. If it is not reached, it is determined whether there is still a preceding node. If it exists, it returns to the step of determining the preceding node forward, and if it does not exist, it determines the following node backward. The number of post-code characters of the post-node is obtained, and it is determined whether the number of initial code characters, the number of pre-code characters, and the number of post-code characters reach a preset character threshold. If so, the execution process based on the bottom-up search interception strategy ends. If not, it is determined whether there is still a post-node. If so, the execution returns to the step of determining the post-node backward. If not, the parent node is obtained upward, and the parent node is determined as the new initial interception node until the number of code characters reaches the preset character threshold, and the target interception node set is obtained. Further, the target code interception range of each target interception node in the target interception node set in the source code file is determined, thereby, according to each target code interception range, the code interception range for intercepting the source code file is determined, and the corresponding target code fragment is intercepted in the source file code based on the code interception range.
[0231] The following combination Figure 7 , the code interception method is further described. Among them, Figure 7 FIG. 2 shows a process flow chart of another code interception method provided by an embodiment of the present specification. Figure 7As shown, in the execution process based on the top-down pruning and interception strategy, the code syntax tree of the source code file is obtained, the node where the user input is located is determined, the code interception starting position is determined in the code syntax tree, and a code interception path is determined according to the code interception starting position and the root node of the code syntax tree. In the code interception path, according to the top-down traversal strategy, the path node to be processed is determined from the root node. The node distance between the node of the same level of the path node to be processed and the path node to be processed is obtained, and the initial interception node is determined according to the traversal strategy from far to near according to the distance of each node, and the initial code character number corresponding to the initial interception node is determined. According to the number of initial code characters and the number of source code characters of the source code file, the number of candidate code characters of the source code file is determined. It is judged whether the number of candidate code characters reaches the preset character threshold. If not, there is no need to traverse and prune the code syntax tree. At this time, the execution process based on the top-down pruning and interception strategy ends. If it is reached, it is necessary to determine whether there is a node to be pruned in the same-level node of the initial interception node. If there is, the node to be pruned is determined, and the number of pruned code characters of the node to be pruned is obtained. According to the number of pruned code characters and the number of candidate code characters, the number of new candidate code characters is determined, and it is judged whether the number of new candidate code characters reaches the preset character threshold. If it is not reached, there is no need to traverse and prune the code syntax tree. At this time, the execution process based on the top-down pruning interception strategy ends. If it is reached, return to the step of determining whether there is a node to be pruned. If there is no node to be pruned in the same-level node of the initial interception node, traverse to the next node layer of the node layer to be processed where the path node to be processed is located, determine the update path node in the next node layer, and determine the update path node as the new path node to be processed. According to the new path node to be processed (i.e., the updated path node), determine the initial intercept node (new intercept node) corresponding to the new path node to be processed, until the number of candidate code characters does not reach the preset character threshold, and obtain the target intercept node set. Furthermore, the target code interception range of each target interception node in the target interception node set in the source code file is determined, thereby determining the code interception range for intercepting the source code file according to each target code interception range, and intercepting the corresponding target code fragment in the source file code based on the code interception range.
[0232] An embodiment of the present specification realizes traversing the code syntax tree by a bottom-up search interception strategy and a top-down pruning interception strategy, respectively, and screening a target interception node set in the code syntax tree according to a preset character threshold, so as to intercept the source code file based on the code interception range formed by the target interception node set, and obtain the target code fragment. Since the code syntax tree can be used to characterize the code structure of the source code source file, each target interception node in the target interception node set screened out in the code syntax tree will not destroy the code structure of the source code file, and then combined with the preset character threshold, the target interception node set is screened in the code syntax tree, and the code fragment to be intercepted is accurately intercepted on the basis of retaining the code structure and context information of the source code file, thereby improving the integrity of code interception of the source code file.
[0233] Corresponding to the above method embodiment, this specification also provides a code interception device embodiment, Figure 8 FIG. 1 shows a schematic diagram of the structure of a code interception device provided by an embodiment of the present specification. Figure 8 As shown, the device comprises:
[0234] The acquisition module 802 is configured to acquire a code syntax tree corresponding to the source code file, wherein the code syntax tree is used to represent the code structure of the source code file, the code syntax tree includes at least one node, and the node information of each node is used to determine the number of code characters corresponding to each node;
[0235] A screening module 804 is configured to screen a target interception node set in the code syntax tree according to a preset character threshold;
[0236] A determination module 806 is configured to determine a code interception range based on the target interception node set;
[0237] The interception module 808 is configured to intercept a target code segment in the source code file according to the code interception range.
[0238] Optionally, the screening module 804 is further configured to:
[0239] Determining an initial interception node in the code syntax tree, and determining the number of initial code characters corresponding to the initial interception node;
[0240] When the number of initial code characters does not reach the preset character threshold, traverse the peer nodes of the initial interception node, screen a reserved node set in each peer node, and obtain the number of reserved code characters corresponding to the reserved node set;
[0241] Determining the number of candidate code characters of the source code file according to the number of initial code characters and the number of reserved code characters;
[0242] When the number of candidate code characters does not reach the preset character threshold, a new initial interception node is determined in the code syntax tree, and the step of determining the number of initial code characters corresponding to the initial interception node is continued until the number of candidate code characters reaches the preset character threshold, thereby obtaining a target interception node set.
[0243] Optionally, the screening module 804 is further configured to:
[0244] Determining a code interception starting position in the code syntax tree;
[0245] An initial interception node is determined according to the code interception starting position and a preset interception node type.
[0246] Optionally, the screening module 804 is further configured to:
[0247] Obtaining node information of the initial intercept node, and determining a code start position and a code end position of the initial intercept node in the source code file from the node information of the initial intercept node;
[0248] The number of initial code characters corresponding to the initial interception node is determined according to the code start position and the code end position.
[0249] Optionally, the screening module 804 is further configured to:
[0250] Traversing the predecessor nodes of the initial interception node in each node at the same level, and screening a reserved predecessor node set in each predecessor node;
[0251] Obtaining the number of reserved prefix code characters corresponding to the reserved prefix node set;
[0252] When the number of the initial code characters and the number of the reserved preceding code characters do not reach the preset character threshold, traverse the post nodes of the initial intercepted node in each node at the same level, and select the reserved post node set in each post node, and generate the reserved node set based on the reserved preceding node set and the reserved post node set;
[0253] In a case where the number of the initial code characters and the number of the reserved preceding code characters reach the preset character threshold, the reserved preceding node set is determined as a reserved node set.
[0254] Optionally, the screening module 804 is further configured to:
[0255] Determine a first preceding node of the initial interception node, and obtain the number of first preceding code characters corresponding to the first preceding node;
[0256] In the case that the number of characters of the initial code and the number of characters of the first preceding code do not reach the preset character threshold, determining a second preceding node of the initial interception node, and returning to execute the step of obtaining the number of characters of the first preceding code corresponding to the first preceding node, until the number of characters of the initial code and the number of characters of the preceding code reach the preset character threshold, or the traversal of each preceding node is completed, and a set of reserved preceding nodes is obtained;
[0257] When the number of characters of the initial code and the number of characters of the first preceding code reach the preset character threshold, it is determined that there is no reserved preceding node for the initial interception node.
[0258] Optionally, the screening module 804 is further configured to:
[0259] Determine a first post-node of the initial interception node, and obtain the number of first post-code characters corresponding to the first post-node;
[0260] In the case that the number of the initial code characters, the number of the reserved pre-code characters, and the number of the first post-code characters do not reach the preset character threshold, determining a second post-node of the initial interception node, and returning to execute the step of obtaining the number of the first post-code characters corresponding to the first post-code node, until the number of the initial code characters, the number of the reserved pre-code characters, and the number of post-code characters reach the preset character threshold, or the traversal of each post-code node is completed, and a set of reserved post-code nodes is obtained;
[0261] When the number of the initial code characters, the number of the reserved pre-code characters, and the number of the first post-code characters reach the preset character threshold, it is determined that there is no reserved post-code node for the initial interception node.
[0262] Optionally, the screening module 804 is further configured to:
[0263] Obtaining a parent node of a currently intercepted node, wherein the currently intercepted node is determined according to a traversal order of each reserved node in the reserved node set;
[0264] The parent node of the current intercepted node is determined as a new initial intercepted node.
[0265] Optionally, the screening module 804 is further configured to:
[0266] Determining an initial interception node in the code syntax tree, and determining the number of initial code characters corresponding to the initial interception node;
[0267] Determining a first candidate code character number of the source code file according to the initial code character number;
[0268] When the number of the first candidate code characters reaches the preset character threshold, traverse the peer nodes of the initial interception node, screen a set of nodes to be pruned in each peer node, and obtain the number of pruned code characters corresponding to the set of nodes to be pruned;
[0269] Determining the number of second candidate code characters of the source code file according to the number of pruned code characters and the number of the first candidate code characters;
[0270] When the number of characters in the second candidate code reaches the preset character threshold, a new initial interception node is determined in the code syntax tree, and the step of determining the number of initial code characters corresponding to the initial interception node is continued until the number of candidate code characters does not reach the preset character threshold, thereby obtaining a target interception node set.
[0271] Optionally, the screening module 804 is further configured to:
[0272] Determining a code interception starting position in the code syntax tree;
[0273] Determine a code interception path according to the code interception starting position and the root node of the code syntax tree;
[0274] In the code interception path, determining a path node to be processed, wherein the path node to be processed is determined according to the hierarchical structure of the syntax tree;
[0275] According to the node distance between the peer node corresponding to the path node to be processed and the path node to be processed, an initial interception node is determined in each peer node, wherein the node distance between the initial interception node and the path node to be processed is greater than the node distance between other peer nodes and the path node to be processed.
[0276] Optionally, the screening module 804 is further configured to:
[0277] Obtain the number of source code characters of the source code file;
[0278] The first candidate code character quantity is determined according to the source code character quantity and the initial code character quantity.
[0279] Optionally, the screening module 804 is further configured to:
[0280] Traversing each node at the same level according to the node distance between each node at the same level and the path node to be processed;
[0281] Determine the number of target sibling node code characters corresponding to the target sibling node, wherein the target sibling node is any one of the sibling nodes;
[0282] Determining the target candidate code character quantity of the source code file according to the target same-level node code character quantity;
[0283] When the number of characters of the target candidate code reaches the preset character threshold, a reference peer node of the target peer node is determined as a node to be pruned, and a set of nodes to be pruned is generated based on each node to be pruned.
[0284] Optionally, the screening module 804 is further configured to:
[0285] Get the code interception path;
[0286] Determine the to-be-processed path node corresponding to the initial interception node, and the to-be-processed node layer corresponding to the to-be-processed path node;
[0287] Determining an update node layer of the node layer to be processed, wherein the update node layer is a new node layer to be processed;
[0288] Determine a node in the update node layer located on the code interception path as an update path node, wherein the update path node is a new path node to be processed;
[0289] According to the node distance between the node at the same level corresponding to the update path node and the update path node, a new initial intercept node is determined in each node at the same level.
[0290] Optionally, the determining module 806 is further configured to:
[0291] Determine a to-be-determined code start position and a to-be-determined code end position of a to-be-determined interception node in the source code file, wherein the to-be-determined interception node is any target interception node in the target interception node set;
[0292] Determine the to-be-determined code interception range corresponding to the to-be-determined interception node according to the to-be-determined code starting position and the to-be-determined code ending position;
[0293] According to the target code interception range corresponding to each target interception node in the target interception node set, the code interception range corresponding to the target interception node set is determined.
[0294] The code interception device provided in the present specification includes: an acquisition module, configured to acquire a code syntax tree corresponding to a source code file, wherein the code syntax tree is used to represent the code structure of the source code file, the code syntax tree includes at least one node, and the node information of each node is used to determine the number of code characters corresponding to each node; a screening module, configured to screen a target interception node set in the code syntax tree according to a preset character threshold; a determination module, configured to determine a code interception range based on the target interception node set; and an interception module, configured to intercept a target code fragment in the source code file according to the code interception range.
[0295] An embodiment of the present specification realizes that, through the syntax tree corresponding to the source code file and the preset character threshold, the target interception node set is screened in the code syntax tree, so as to intercept the source code file based on the code interception range formed by the target interception node set, and obtain the target code fragment. Since the code syntax tree can be used to characterize the code structure of the source code source file, each target interception node in the target interception node set screened in the code syntax tree will not destroy the code structure of the source code file, and then combined with the preset character threshold, the target interception node set is screened in the code syntax tree, and the code fragment to be intercepted is accurately intercepted on the basis of retaining the code structure and context information of the source code file, thereby improving the integrity of code interception of the source code file.
[0296] The above is a schematic scheme of a code interception device of this embodiment. It should be noted that the technical scheme of the code interception device and the technical scheme of the above code interception method belong to the same concept, and the details not described in detail in the technical scheme of the code interception device can be referred to the description of the technical scheme of the above code interception method.
[0297] See also Fig. 9 , Fig. 9 A flowchart of a code range determination method provided according to an embodiment of the present specification is shown, which specifically includes the following steps.
[0298] Step 902: Receive a code interception instruction for a source code file, wherein the code interception instruction carries a preset character threshold.
[0299] The code interception instruction refers to an instruction sent by a target terminal for intercepting code from a source code file. The target terminal is a terminal that needs to intercept code from a source code file.
[0300] Step 904: Obtain a code syntax tree corresponding to the source code file.
[0301] The code syntax tree is used to represent the code structure of the source code file. The code syntax tree includes at least one node, and the node information of each node is used to determine the number of code characters corresponding to each node.
[0302] Step 906: Filter a target interception node set in the code syntax tree according to the preset character threshold.
[0303] Step 908: Determine a code interception range of the source code file based on the target interception node set, and send the code interception range to the target terminal.
[0304] After determining the code interception range for code interception of the source code file based on the target interception node set, the code interception range is sent to the target terminal, so that the target terminal performs code interception on the source code file based on the code interception range. In actual applications, the code range determination method can further perform code interception on the source code file according to the determined point interception range, obtain the target code fragment, and send the target code fragment to the target terminal. The specific implementation method is determined according to the actual application situation, and this specification does not limit it here.
[0305] The implementation process of traversing the code syntax tree and determining the code interception range of the source code file in the code range determination method is the same as the above-mentioned code interception method. The specific implementation process of determining the code interception range of the source code file can be referred to the above-mentioned code interception method, and this specification will not repeat it here.
[0306] An embodiment of the present specification realizes that, after receiving a code interception instruction, a target interception node set is screened in a code syntax tree through a syntax tree corresponding to a source code file and a preset character threshold, so as to form a code interception range based on the target interception node set, thereby ensuring that a target code fragment subsequently obtained based on the code interception range is structurally complete; and dynamic adjustment of the code interception range is achieved by judging whether the number of code characters reaches the preset character threshold node by node.
[0307] Corresponding to the above method embodiment, this specification also provides a code range determination device embodiment, Fig.10 FIG. 2 shows a schematic diagram of a code range determination device provided by an embodiment of the present specification. Fig.10 As shown, the device comprises:
[0308] The receiving module 1002 is configured to receive a code interception instruction for a source code file, wherein the code interception instruction carries a preset character threshold;
[0309] The syntax tree acquisition module 1004 is configured to acquire a code syntax tree corresponding to the source code file, wherein the code syntax tree is used to represent the code structure of the source code file, the code syntax tree includes at least one node, and the node information of each node is used to determine the number of code characters corresponding to each node;
[0310] A set screening module 1006 is configured to screen a target interception node set in the code syntax tree according to the preset character threshold;
[0311] The sending module 1008 is configured to determine a code interception range of the source code file based on the target interception node set, and send the code interception range to a target terminal.
[0312] An embodiment of the present specification realizes that, after receiving a code interception instruction, a target interception node set is screened in a code syntax tree through a syntax tree corresponding to a source code file and a preset character threshold, so as to form a code interception range based on the target interception node set, thereby ensuring that a target code fragment subsequently obtained based on the code interception range is structurally complete; and dynamic adjustment of the code interception range is achieved by judging whether the number of code characters reaches the preset character threshold node by node.
[0313] The above is a schematic scheme of a code range determination device of this embodiment. It should be noted that the technical scheme of the code range determination device and the technical scheme of the above-mentioned code range determination method belong to the same concept, and the details not described in detail in the technical scheme of the code range determination device can be referred to the description of the technical scheme of the above-mentioned code range determination method.
[0314] Fig.11 The block diagram of a computing device 1100 according to one embodiment of the present specification is shown. The components of the computing device 1100 include but are not limited to a memory 1110 and a processor 1120. The processor 1120 is connected to the memory 1110 via a bus 1130, and a database 1150 is used to store data.
[0315] The computing device 1100 also includes an access device 1140 that enables the computing device 1100 to communicate via one or more networks 1160. Examples of these networks include a public switched telephone network (PSTN), a local area network (LAN), a wide area network (WAN), a personal area network (PAN), or a combination of communication networks such as the Internet. The access device 1140 may include one or more of any type of network interface, wired or wireless (e.g., a network interface card (NIC)), such as an IEEE 802.11 wireless local area network (WLAN) wireless interface, a World Wide Interoperability for Microwave Access (Wi-MAX) interface, an Ethernet interface, a Universal Serial Bus (USB) interface, a cellular network interface, a Bluetooth interface, and a Near Field Communication (NFC).
[0316] In one embodiment of the present specification, the above components of the computing device 1100 and Fig.11 Other components not shown in the figure may also be connected to each other, for example, via a bus. It should be understood that Fig.11 The computing device structure block diagram shown is only for the purpose of illustration, and is not intended to limit the scope of this specification. Those skilled in the art can add or replace other components as needed.
[0317] The computing device 1100 may be any type of stationary or mobile computing device, including a mobile computer or mobile computing device (e.g., a tablet computer, a personal digital assistant, a laptop computer, a notebook computer, a netbook, etc.), a mobile phone (e.g., a smart phone), a wearable computing device (e.g., a smart watch, smart glasses, etc.), or other types of mobile devices, or a stationary computing device such as a desktop computer or a personal computer (PC). The computing device 1100 may also be a mobile or stationary server.
[0318] The processor 1120 is used to execute the following computer program / instruction, which implements the steps of the above-mentioned code interception method or code range determination method when executed by the processor.
[0319] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the computing device embodiment, since it is basically similar to the code interception method or code range determination method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the code interception method or code range determination method embodiment.
[0320] An embodiment of the present specification further provides a computer-readable storage medium storing a computer program / instruction, which implements the steps of the above-mentioned code interception method or code range determination method when executed by a processor.
[0321] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the computer-readable storage medium embodiment, since it is basically similar to the code interception method or code range determination method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the code interception method or code range determination method embodiment.
[0322] An embodiment of the present specification further provides a computer program product, including a computer program / instruction, which implements the steps of the above-mentioned code interception method or code range determination method when executed by a processor.
[0323] The above is a schematic scheme of a computer program product of this embodiment. It should be noted that the technical scheme of the computer program product and the technical scheme of the above-mentioned code interception method or code range determination method belong to the same concept, and the details not described in detail in the technical scheme of the computer program product can be referred to the description of the technical scheme of the above-mentioned code interception method or code range determination method.
[0324] The above is a description of a specific embodiment of the specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0325] The computer instructions include computer program codes, which may be in source code form, object code form, executable files or some intermediate forms, etc. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, USB flash drive, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electric carrier signal, telecommunication signal and software distribution medium, etc. It should be noted that the content contained in the computer-readable medium may be appropriately increased or decreased according to the requirements of patent practice. For example, in some regions, according to patent practice, computer-readable media do not include electric carrier signals and telecommunication signals.
[0326] It should be noted that, for the above-mentioned method embodiments, for the sake of simplicity of description, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the embodiments of this specification are not limited by the order of the actions described, because according to the embodiments of this specification, some steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the embodiments of this specification.
[0327] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0328] The preferred embodiments of this specification disclosed above are only used to help explain this specification. The optional embodiments do not describe all the details in detail, nor do they limit the invention to only the specific implementation methods described. Obviously, many modifications and changes can be made according to the content of the embodiments of this specification. This specification selects and specifically describes these embodiments in order to better explain the principles and practical applications of the embodiments of this specification, so that technicians in the relevant technical field can well understand and use this specification. This specification is only limited by the claims and their full scope and equivalents.
Claims
1. A code interception method, comprising: Obtaining a code syntax tree corresponding to the source code file, wherein the code syntax tree is used to represent the code structure of the source code file, the code syntax tree includes at least one node, and the node information of each node is used to determine the number of code characters corresponding to each node; According to a preset character threshold, screening a target interception node set in the code syntax tree; Determining a code interception range based on the target interception node set; According to the code interception range, a target code segment is intercepted in the source code file.
2. The method according to claim 1, screening a target interception node set in the code syntax tree according to a preset character threshold, comprises: Determining an initial interception node in the code syntax tree, and determining the number of initial code characters corresponding to the initial interception node; When the number of initial code characters does not reach the preset character threshold, traverse the peer nodes of the initial interception node, screen a reserved node set in each peer node, and obtain the number of reserved code characters corresponding to the reserved node set; Determining the number of candidate code characters of the source code file according to the number of initial code characters and the number of reserved code characters; When the number of candidate code characters does not reach the preset character threshold, a new initial interception node is determined in the code syntax tree, and the step of determining the number of initial code characters corresponding to the initial interception node is continued until the number of candidate code characters reaches the preset character threshold, thereby obtaining a target interception node set.
3. The method of claim 2, wherein determining an initial interception node in the code syntax tree comprises: Determining a code interception starting position in the code syntax tree; An initial interception node is determined according to the code interception starting position and a preset interception node type.
4. The method of claim 2, wherein determining the number of initial code characters corresponding to the initial interception node comprises: Obtaining node information of the initial intercept node, and determining a code start position and a code end position of the initial intercept node in the source code file from the node information of the initial intercept node; The number of initial code characters corresponding to the initial interception node is determined according to the code start position and the code end position.
5. The method according to claim 2, traversing the peer nodes of the initial interception node and screening the reserved node set in each peer node, comprises: Traversing the predecessor nodes of the initial interception node in each node at the same level, and screening a reserved predecessor node set in each predecessor node; Obtaining the number of reserved prefix code characters corresponding to the reserved prefix node set; When the number of the initial code characters and the number of the reserved preceding code characters do not reach the preset character threshold, traverse the post nodes of the initial intercepted node in each node at the same level, and select the reserved post node set in each post node, and generate the reserved node set based on the reserved preceding node set and the reserved post node set; In a case where the number of the initial code characters and the number of the reserved preceding code characters reach the preset character threshold, the reserved preceding node set is determined as a reserved node set.
6. The method according to claim 5, traversing the predecessor nodes of the initial interception node in each node at the same level, and screening the reserved predecessor node set in each predecessor node, comprising: Determine a first preceding node of the initial interception node, and obtain the number of first preceding code characters corresponding to the first preceding node; In the case that the number of characters of the initial code and the number of characters of the first preceding code do not reach the preset character threshold, determining a second preceding node of the initial interception node, and returning to execute the step of obtaining the number of characters of the first preceding code corresponding to the first preceding node, until the number of characters of the initial code and the number of characters of the preceding code reach the preset character threshold, or the traversal of each preceding node is completed, and a set of reserved preceding nodes is obtained; When the number of characters of the initial code and the number of characters of the first preceding code reach the preset character threshold, it is determined that there is no reserved preceding node for the initial interception node.
7. A code range determination method, comprising: Receiving a code interception instruction for a source code file, wherein the code interception instruction carries a preset character threshold; Obtaining a code syntax tree corresponding to the source code file, wherein the code syntax tree is used to represent the code structure of the source code file, the code syntax tree includes at least one node, and the node information of each node is used to determine the number of code characters corresponding to each node; According to the preset character threshold, screening a target interception node set in the code syntax tree; Based on the target interception node set, a code interception range of the source code file is determined, and the code interception range is sent to a target terminal.
8. A computing device comprising: Memory and processor; The memory is used to store computer programs / instructions, and the processor is used to execute the computer programs / instructions. When the computer program / instructions are executed by the processor, the steps of the method described in any one of claims 1 to 7 are implemented.
9. A computer-readable storage medium storing a computer program / instruction, wherein the computer program / instruction, when executed by a processor, implements the steps of the method according to any one of claims 1 to 7.
10. A computer program product, comprising a computer program / instruction, which, when executed by a processor, implements the steps of the method according to any one of claims 1 to 7.
Citation Information
Cited By
Code recommendation method and device, equipment and medium
CN119248243A
Code block segmentation method and device, program product, electronic equipment and storage medium
CN121579546A
Code block segmentation method, apparatus, program product, electronic device, and storage medium
CN121579546B
