A code parsing method and device
By partially parsing target code snippets in the IDE, the memory consumption problem caused by parsing large-scale code files is solved, improving system performance and development efficiency, and ensuring code quality and stability.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- HUAWEI TECH CO LTD
- Filing Date
- 2024-12-27
- Publication Date
- 2026-06-30
AI Technical Summary
When processing large-scale code files, the editors of integrated development environments (IDEs) consume excessive memory and slow down the system by parsing the entire abstract syntax tree, which affects the user experience of developers.
The first type of nodes in the target code segment of the code file, including functions and/or methods, are parsed locally. Only important content is parsed, saving memory usage and improving system performance and stability.
By parsing target code snippets in a partial manner, memory usage is reduced, operating system performance and stability are improved, and development efficiency and user experience are enhanced.
Smart Images

Figure CN122309319A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to a code parsing method and device. Background Technology
[0002] Currently, integrated development environment (IDE) editors all support real-time code inspection to check for errors in the code and improve the writing speed of programmers.
[0003] Specifically, the editor can parse the code file into an abstract syntax tree (AST), where each node represents a structure in the source code. Then, the editor parses each node of the AST in conjunction with its context to determine if any errors exist.
[0004] Therefore, especially when the code file is large, using this parsing method will result in high memory consumption, slow system operation, and negatively impact the developer's user experience. Summary of the Invention
[0005] This application provides a code parsing method and apparatus. By parsing the first type of nodes that represent target code fragments, it can not only parse important content in code files but also save a significant amount of memory. This improves operating system performance and system stability.
[0006] To achieve the above objectives, this application adopts the following technical solution:
[0007] Firstly, this application provides a code parsing method applied to an electronic device. The method includes: the electronic device can display the code project file in response to an open operation on the code project file. The electronic device can also obtain the abstract syntax tree (AST) of each code file within the code project file, where nodes in the AST correspond to fragment information of code segments within the code files. The electronic device can parse code from a first type of node in the AST of the code file, where the target code segment corresponding to the first type of node in the code file supports export. The electronic device displays the first file in response to an open operation on the first file within the code project file.
[0008] In this embodiment of the application, when the code project file is open but the first file within the code project file is not open, the electronic device only needs to perform partial node parsing on the code file, such as parsing the first type of nodes. The target code fragment corresponding to the first type of node in the code file can be exported.
[0009] Thus, in scenarios with large file sizes, the target code snippets are of significant importance and numerous within the code file. This eliminates the need for electronic devices to perform full code parsing. By parsing the first type of nodes representing the target code snippets, not only can important content within the code file be analyzed, but significant memory usage can also be saved. This, in turn, improves operating system performance and system stability.
[0010] In one feasible approach, the target code snippet includes functions and / or methods that declare a return type.
[0011] Thus, by parsing the first type of nodes representing functions and / or methods in this embodiment, not only can important content in the code file be parsed, but significant memory usage can also be saved. This improves operating system performance and system stability.
[0012] In one possible implementation, the electronic device can obtain a first type of node from the abstract syntax tree of the second file within the code file. This first type of node includes a first node and a second node, where the first and second nodes represent code segments with different segment names. The electronic device can also display a reference list in response to a reference operation to the second file; wherein the reference list includes a first code segment and a second code segment from the second file, with the first code segment corresponding to the first node and the second code segment corresponding to the second node.
[0013] An electronic device can, in response to a triggering operation on the first code snippet in the reference list, insert the first code snippet into the target location in the first file.
[0014] Thus, in this embodiment of the application, after parsing the first type of nodes in the second file, the code snippets corresponding to the first type of nodes in the second file can be directly referenced in the first file. By performing local node parsing on the second file, sudden increases in memory usage during the parsing process are avoided. Furthermore, the parsing results can be directly used in subsequent referencing scenarios, improving development efficiency and the user experience for developers.
[0015] In one feasible approach, the electronic device can acquire a first type of node in the abstract syntax tree of the second file. This first type of node includes a first node and a second node. The first node represents a first code segment in the second file, and the second node represents a second code segment in the second file. The first and second code segments have different segment names. The electronic device can also, in response to a reference operation to the first code segment in the second file, insert the first code segment into a target location within the first file.
[0016] Thus, in this embodiment of the application, after parsing the first type of nodes in the second file, the specific code snippets corresponding to the first type of nodes in the second file can be directly referenced in the first file. By performing local node parsing on the second file, sudden increases in memory usage during the parsing process are avoided. Furthermore, the parsing results can be directly used in subsequent reference scenarios, improving development efficiency and the user experience for developers.
[0017] In one feasible approach, when an electronic device displays a first file and the first type of nodes in the abstract syntax tree of the first file have completed code parsing, it performs code parsing on the second type of nodes in the abstract syntax tree of the first file. The second type of nodes are nodes in the abstract syntax tree of the first file other than the first type of nodes.
[0018] Thus, in this embodiment of the application, when the first file is displayed and the first type of nodes in the abstract syntax tree of the first file have completed code parsing, the unparsed nodes can then be parsed. This results in a more comprehensive code inspection of the first file, ensuring its code quality. Simultaneously, parsing only the second type of nodes saves memory space and reduces the parsing time.
[0019] In one feasible approach, the electronic device can parse the code of each node according to the order of the abstract syntax tree nodes of the first file after displaying the first file.
[0020] Thus, this embodiment of the application can perform partial node parsing on the first type of nodes in the abstract syntax tree of the first file when the first file is not opened. When the first file is opened, a full parsing is performed on each node in the abstract syntax tree of the first file. This not only avoids the large memory consumption of full parsing when the first file is not opened, but also allows for a clear understanding of all aspects of the code in the first file, from the overall architecture to the details, when the first file is opened. Therefore, a more comprehensive code inspection of the first file is performed, ensuring the code quality of the first file.
[0021] In one feasible approach, the electronic device can stop parsing the first type of nodes in the abstract syntax tree of the first file if the first file is displayed and the first type of nodes in the abstract syntax tree of the first file have not been completely parsed. Alternatively, the electronic device can parse the code of each node according to the order of the nodes in the abstract syntax tree of the first file.
[0022] Therefore, in the case where the first file is displayed and the parsing of the first type of nodes in the abstract syntax tree of the first file has not been completed, the parsing of the first type of nodes can be stopped before the full code parsing process is executed. Thus, while ensuring the code quality of the first file, stopping the parsing of the first type of nodes avoids repeated parsing of these nodes. This saves significant parsing time and computational resources, improving the overall operating system efficiency.
[0023] In one feasible approach, the electronic device does not parse the second type of nodes in the abstract syntax tree of the code file before displaying the first file. The second type of nodes are the nodes in the abstract syntax tree of the first file that are not the first type of nodes.
[0024] Thus, this embodiment only needs to focus on the first type of nodes, namely the signature information corresponding to the target code fragment, to complete the code inspection process simply and directly. It can skip analyzing the specific statements in the method body and / or function body, and does not require reading the second type of nodes, thereby reducing memory usage and saving significant time and computational resources.
[0025] In one feasible approach, the electronic device can obtain second-type nodes from the abstract syntax tree of the code file, which are nodes other than the first-type nodes, before displaying the first file and after the first type of nodes in the code file's abstract syntax tree have been parsed. The electronic device can then parse these second-type nodes.
[0026] Thus, this embodiment of the application can parse the second type of nodes after the first type of nodes in the abstract syntax tree of the code file has been parsed. On the one hand, this improves the completeness of the parsing process. On the other hand, this embodiment of the application can read and parse another part of the nodes after parsing a portion of the nodes, avoiding a sudden increase in memory usage in a short period of time.
[0027] In one feasible approach, the electronic device can obtain the signature information of the target code fragment, which includes the return type of the function and / or the return type of the method. Based on the signature information of the target code fragment, the electronic device can search for a first-class node in the abstract syntax tree of the code file and parse the code of that first-class node.
[0028] Thus, this embodiment of the application utilizes the signature information of the target code fragment to locate and parse the first type of node, facilitating subsequent code export and code referencing functions. This increases code reusability, eliminating the need to repeatedly write the same code, and also makes the program structure clearer. It also simplifies development and maintenance.
[0029] In one feasible approach, the electronic device can obtain the correspondence between the first type of nodes and other nodes in the abstract syntax tree of the code file, and perform semantic and syntactic analysis on the code fragment information corresponding to the first type of nodes to obtain the analysis results. Based on the correspondence and / or the analysis results, the electronic device can determine whether the target code fragment corresponding to the first type of node contains errors.
[0030] Thus, by parsing the first type of nodes, and based on the relationship between the first type of nodes and other nodes, and / or the analysis results of semantic and syntactic analysis, it is possible to determine whether there are errors in the target code segment corresponding to the first type of nodes. This improves the code instructions in the code file and ensures the code's standardization and security. Furthermore, by parsing only the first type of nodes, this embodiment of the application does not consume too much memory space, thus improving system stability.
[0031] In one feasible approach, the electronic device can output a first prompt message, which indicates that there is a code error in the target code segment corresponding to the first type of node.
[0032] Thus, this embodiment of the application can also output a first prompt message to alert developers to the target code segment containing a code error, thereby improving the developer's user experience.
[0033] In one possible implementation, the electronic device can output a second prompt message to indicate that there is a code error in the code file.
[0034] Thus, this embodiment of the application can also output a second prompt message to alert developers to code files containing errors, thereby improving the user experience for developers.
[0035] In a second aspect, this application also provides an electronic device, comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, the instructions being executed by the at least one processor to cause the at least one processor to perform the code parsing method as described in the first aspect above.
[0036] Thirdly, this application also provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the code parsing method described in the first aspect above.
[0037] Fourthly, this application also provides a computer program product that, when run on a computer, causes the computer to execute the code parsing method described in the first aspect above. Attached Figure Description
[0038] Figure 1 A schematic diagram of the hardware structure of a laptop computer provided in an embodiment of this application;
[0039] Figure 2 A schematic diagram of the software structure of a laptop computer provided in an embodiment of this application;
[0040] Figure 3 A flowchart illustrating a code parsing method provided in an embodiment of this application;
[0041] Figure 4 A schematic diagram of an application scenario provided in this application embodiment;
[0042] Figure 5 A schematic diagram of an interface for opening project file A provided in an embodiment of this application;
[0043] Figure 6 This is a schematic diagram of an interface for providing a first prompt message, as provided in an embodiment of this application.
[0044] Figure 7 This is a schematic diagram of an interface for a second prompt message provided in an embodiment of this application;
[0045] Figure 8 This application provides a schematic diagram of a first file parsing process as an embodiment of the present application.
[0046] Figure 9 A schematic diagram of an interface referencing a second document provided for embodiments of this application. Figure 1 ;
[0047] Figure 10 A schematic diagram of an interface referencing a second document provided for embodiments of this application. Figure 2 ;
[0048] Figure 11 This is a schematic diagram of the structure of a laptop computer provided in an embodiment of this application. Detailed Implementation
[0049] The technical solutions of the embodiments of this application will be described below with reference to the accompanying drawings. In the description of this application, unless otherwise stated, " / " indicates that the objects before and after are in an "or" relationship. For example, A / B can represent A or B. "And / or" in this application is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A alone, A and B simultaneously, and B alone, where A and B can be singular or plural. Furthermore, in the description of this application, unless otherwise stated, "multiple" refers to two or more. "At least one of the following" or similar expressions refer to any combination of these items, including any combination of single or plural items. For example, at least one of a, b, or c can represent: a, b, c, ab, ac, bc, or abc, where a, b, and c can be single or multiple. In addition, in order to clearly describe the technical solutions of the embodiments of this application, the terms "first" and "second" are used in the embodiments of this application to distinguish the same or similar items with basically the same function and effect.
[0050] Those skilled in the art will understand that the terms "first," "second," etc., do not limit the quantity or order of execution, and that "first," "second," etc., are not necessarily different. Furthermore, in some embodiments of this application, words such as "exemplary" or "for example" are used to indicate that something is being described as an example, illustration, or description. Any embodiment or design scheme described as "exemplary" or "for example" in the embodiments of this application should not be construed as being more preferred or advantageous than other embodiments or design schemes. Specifically, the use of words such as "exemplary" or "for example" is intended to present the relevant concepts in a concrete manner for ease of understanding.
[0051] Furthermore, the device architecture and business scenarios described in the embodiments of this application are for the purpose of more clearly illustrating the technical solutions of the embodiments of this application, and do not constitute a limitation on the technical solutions provided in the embodiments of this application. As those skilled in the art will know, with the evolution of device architecture and the emergence of new business scenarios, the technical solutions provided in the embodiments of this application are also applicable to similar technical problems.
[0052] To facilitate understanding, the relevant technical terms involved in this application will be explained first.
[0053] An Integrated Development Environment (IDE) is an application that provides a program development environment, integrating functions such as code writing, analysis, compilation, and debugging. An IDE typically includes tools such as an editor, compiler, debugger, and graphical developer interface.
[0054] An abstract syntax tree (AST) is an abstract representation of the syntactic structure of code in a code file. An AST can represent the syntactic structure of a code file in a tree-like format, and each node in the AST can represent a specific structure within the code file.
[0055] Parsing involves performing lexical and syntactic analysis on the code content of a code file. Lexical analysis involves scanning the strings that make up the code file and identifying all the strings that constitute it. Syntactic analysis, based on lexical analysis, involves combining the strings in the code file into syntactic phrases and determining whether the code file is structurally correct.
[0056] The current process by which IDE editors inspect code files is as follows: the editor parses the entire code file into an abstract syntax tree, then traverses all nodes of the abstract syntax tree from top to bottom, and inspects each node by combining the contextual semantics of each node, thus obtaining the full inspection result of the code file.
[0057] Typically, developers compile a project at a time during the compilation process, and each project can correspond to a set of code project files. The developers will compile the entire code project file.
[0058] Taking C / C++ as an example, a code project file typically includes source files (.c, .cpp), which contain the actual code logic. The code project file can also include header files (.h), which are used to declare functions, classes, variables, etc., to help source files share information. Of course, the code project file can also include a project configuration file (.vcxproj), which defines the compilation options for the project, including compiler settings, linking information, etc., so that the compiler can use it during project build.
[0059] Therefore, before a developer compiles a file within a code project, the editor automatically triggers a check process on the code files within the code project after it is launched. For example, a developer might open a code project file through the editor, which may contain multiple code files. The editor will automatically trigger a check process on all of these code files.
[0060] Therefore, especially when the code files are large, performing a full traversal of each code file for inspection will result in excessive memory consumption, slow system operation, and negatively impact the developer's user experience.
[0061] Based on the above, this application provides a code parsing method applied to an electronic device. The method includes: the electronic device can display the code project file in response to an open operation on the code project file. The electronic device can also obtain the abstract syntax tree (AST) of each code file within the code project file, where nodes in the AST correspond to fragment information of code segments within the code files. The electronic device can parse the code of the first type of nodes in the AST of the code files, and the target code fragment corresponding to the first type of node in the code file can be exported. The electronic device displays the first file in response to an open operation on the first file within the code project file.
[0062] In this embodiment of the application, when the code project file is open but the first file within the code project file is not open, the electronic device only needs to perform partial node parsing on the code file, such as parsing the first type of nodes. The target code fragment corresponding to the first type of node in the code file can be exported.
[0063] Understandably, code snippets that support export functionality are important in many situations. For example, exportable code snippets facilitate subsequent sharing with other developers, enabling data dissemination and sharing. Furthermore, exportable code snippets can be used for subsequent analysis operations, i.e., in scenarios involving data processing and data interaction.
[0064] Thus, in scenarios with large file sizes, the target code snippets are of significant importance and numerous within the code file. This eliminates the need for electronic devices to perform full code parsing. By parsing the first type of nodes representing the target code snippets, not only can the important content of the code file be analyzed, but significant memory usage can also be saved. This, in turn, improves operating system performance and system stability.
[0065] The electronic devices provided in this application embodiment may include at least one of the following: laptop computer, desktop computer, mobile phone, foldable electronic device, tablet computer, desktop computer, laptop computer, handheld computer, Ultra-Mobile Personal Computer (UMPC), netbook, cellular phone, Personal Digital Assistant (PDA), Augmented Reality (AR) device, Virtual Reality (VR) device, Artificial Intelligence (AI) device, wearable device, in-vehicle device, or smart city device. This application embodiment does not impose any special limitation on the specific type of the electronic device.
[0066] Furthermore, the operating system installed on the electronic device provided in this application embodiment includes, but is not limited to, the following: Or other operating systems. This application does not limit the specific type of electronic device or the type of operating system installed thereon.
[0067] For example, let's take a laptop computer as an example. Figure 1 A schematic diagram of the hardware structure of a laptop computer 100 is shown.
[0068] like Figure 1 As shown, the laptop 100 may include: a processor 120, an external memory interface 130, an internal memory 131, a universal serial bus (USB) interface 140, a charging management module 150, a power management module 151, a battery 152, an antenna, a wireless communication module 160, an audio module 170, a speaker 170A, a receiver 170B, a microphone 170C, a headphone jack 170D, a sensor module 180, buttons 190, a motor 191, an indicator 192, a camera 193, and a display screen 194, etc.
[0069] It is understood that the structures illustrated in the embodiments of this application do not constitute a specific limitation on the laptop computer 100. In other embodiments of this application, the laptop computer 100 may include more or fewer components than illustrated, or combine some components, or split some components, or have different component arrangements. The illustrated components may be implemented in hardware, software, or a combination of software and hardware.
[0070] Processor 120 may include one or more processing units, such as application processors (APs), modem processors, graphics processing units (GPUs), image signal processors (ISPs), controllers, memory, video codecs, digital signal processors (DSPs), baseband processors, and / or neural network processing units (NPUs). These different processing units may be independent devices or integrated into one or more processors.
[0071] The controller can serve as the central nervous system and command center of the laptop computer 100. Based on the instruction opcode and timing signals, the controller generates operation control signals to control the fetching and execution of instructions.
[0072] The processor 120 may also include a memory for storing instructions and data. In some embodiments, the memory in the processor 120 is a cache memory. This memory can store instructions or data that the processor 120 has just used or that are used repeatedly. If the processor 120 needs to use the instruction or data again, it can retrieve it directly from the memory. This avoids repeated accesses, reduces the waiting time of the processor 120, and thus improves the efficiency of the system.
[0073] In some embodiments, the processor 120 may include one or more interfaces. Interfaces may include an inter-integrated circuit (I2C) interface, an inter-integrated circuit sound (I2S) interface, a pulse code modulation (PCM) interface, a universal asynchronous receiver / transmitter (UART) interface, a mobile industry processor interface (MIPI), a general-purpose input / output (GPIO) interface, a subscriber identity module (SIM) interface, and / or a universal serial bus (USB) interface, etc.
[0074] The wireless communication function of the laptop 100 can be achieved through an antenna, a wireless communication module 160, a modem processor, and a baseband processor.
[0075] Antennas are used to transmit and receive electromagnetic wave signals. Each antenna in the laptop 100 can be used to cover one or more communication frequency bands. Different antennas can also be reused to improve antenna utilization.
[0076] In other embodiments, the antenna can be used in conjunction with a tuning switch.
[0077] The wireless communication module 160 can provide solutions for wireless communication applications on the laptop 100, including wireless local area networks (WLAN) (such as wireless fidelity (Wi-Fi) networks), Bluetooth (BT), global navigation satellite system (GNSS), frequency modulation (FM), near field communication (NFC), and infrared (IR) technologies. The wireless communication module 160 can be one or more devices integrating at least one communication processing module. The wireless communication module 160 receives electromagnetic waves via an antenna, performs frequency modulation and filtering of the electromagnetic wave signals, and sends the processed signal to the processor 120. The wireless communication module 160 can also receive signals to be transmitted from the processor 120, perform frequency modulation and amplification, and convert them into electromagnetic waves for radiation via the antenna.
[0078] The laptop computer 100 implements display functions through a GPU, a display screen 194, and an application processor. The GPU is a microprocessor for image processing, connecting the display screen 194 and the application processor. The GPU is used to perform mathematical and geometric calculations for graphics rendering. The processor 120 may include one or more GPUs, which execute program instructions to generate or modify display information.
[0079] Display screen 194 is used to display user interfaces, etc. Developers can open code files in the code project file through display screen 194, and the laptop 100 responds to the action of opening the code file by controlling display screen 197 to display the corresponding code file.
[0080] The display screen 194 includes a display panel. The display panel may be a liquid crystal display (LCD), an organic light-emitting diode (OLED), an active-matrix organic light-emitting diode (AMOLED), a flexible light-emitting diode (FLED), a minimized display, a microLED, a micro-OLED, a quantum dot light-emitting diode (QLED), etc. In some embodiments, the laptop computer 100 may include one or N displays 194, where N is a positive integer greater than 1.
[0081] The laptop computer 100 can achieve shooting functions through ISP, camera 193, video codec, GPU, display 194 and application processor.
[0082] The ISP (Image Signal Processor) is used to process data fed back from the camera 193. For example, when taking a picture, the shutter is opened, and light is transmitted through the lens to the camera's photosensitive element. The light signal is converted into an electrical signal, and the camera's photosensitive element transmits the electrical signal to the ISP for processing, transforming it into an image visible to the naked eye. The ISP can also perform algorithmic optimization of image noise, brightness, and skin tone. The ISP can also optimize parameters such as exposure and color temperature of the shooting scene. In some embodiments, the ISP can be set in the camera 193.
[0083] The laptop computer 100 can perform audio functions, such as music playback and recording, through an audio module 170, speakers 170A, receiver 170B, microphone 170C, headphone jack 170D, and application processor.
[0084] The electronic device provided in this application embodiment can run an operating system (OS). This operating system can be various operating systems used in industry, such as an operating system developed based on OpenHarmony, for example... Or other operating systems, such as The iOS mobile operating system; it can also be various open-source operating systems or their derivatives, such as Linux. This includes other embedded operating systems; it can also refer to future new operating systems, such as AI operating systems based on artificial intelligence. An operating system is a set of interconnected system software programs that manage and control the operation of electronic devices, utilize and run hardware and software resources, and provide public services to organize user interaction. In electronic devices, the operating system connects downwards to the physical devices at the hardware layer and upwards to provide a runtime environment for application software.
[0085] An operating system typically includes a kernel layer, a middleware layer, and an application layer. The application layer includes applications, which can include system applications and third-party applications. The middleware layer includes a suite of software providing various services to application developers, or frameworks providing services such as databases, multimedia, and graphics, or capabilities such as distributed scheduling and system scaling. For example, the middleware layer may include a framework layer and / or a system service layer. The framework layer provides application programming interfaces (APIs) and programming frameworks for applications in the application layer. The system service layer includes the system's core capabilities, providing services to applications through the framework layer. The kernel layer is the layer between hardware and software. The kernel layer may include hardware drivers and the operating system kernel. In addition to providing hardware drivers, the kernel layer also supports functions such as memory management and system process management.
[0086] The electronic devices we use in our daily lives come in various types and forms, and are applied in a wide range of scenarios. Therefore, based on the different forms and functions of electronic devices, different application scenarios, and different user needs, the operating systems used in these devices may also differ. The basic functions implemented by the electronic device provided in this application can be implemented using a general-purpose operating system or a dedicated operating system. To more clearly illustrate the implementation of the embodiments of this application under a specific operating system, the following shows... Based on the architecture, those skilled in the art can deduce the implementation of the embodiments of this application under other specific operating systems, such as... Implementation under operating systems, etc.
[0087] Figure 2 This is a schematic diagram of the software structure of a laptop computer 100 according to an embodiment of this application.
[0088] The software architecture of the laptop 100 can be divided into several layers. In some embodiments, from bottom to top, these layers are: kernel layer, system service layer, framework layer, and application layer. Layers communicate with each other through software interfaces. System functions can be tailored, added, or combined at the subsystem level depending on the deployment scenario of different device forms. Each subsystem can also be tailored, added, or combined at the functional level.
[0089] The kernel layer includes the kernel abstraction layer, the kernel subsystem, and the driver subsystem.
[0090] The Kernel Abstraction Layer (KAL) provides basic kernel capabilities to upper layers by shielding the differences between multiple kernels, including but not limited to process / thread management, memory management, file system, network management, and peripheral device management.
[0091] Kernel Subsystem: Supports the selection of a suitable OS kernel for different resource-constrained devices, including but not limited to Linux kernel, HarmonyOS kernel, LiteOS (Lite Operating System), etc.
[0092] Driver Subsystem: The driver framework is the foundation for the open system hardware ecosystem, providing unified peripheral access capabilities and a framework for driver development and management. The driver framework includes: display drivers, camera drivers, audio drivers, Bluetooth drivers, sensor drivers, etc.
[0093] The system service layer comprises the core capabilities of the system, providing services to applications through the framework layer. This layer includes, but is not limited to, the following subsystems:
[0094] The system's basic capability subsystem set provides fundamental capabilities for the operation, scheduling, and migration of distributed applications across multiple devices. This set may include distributed soft bus, distributed data management, distributed task scheduling, and Ark multi-language runtime; it may also include multi-modal input subsystem, graphics subsystem, security subsystem, and AI business subsystem.
[0095] The graphics subsystem mainly includes modules such as UI components, layout, animation, fonts, input events, window management, and rendering. The graphics service provides graphics rendering and display output functions, and internally, through the rational utilization of system hardware resources, it provides a smooth and efficient display experience.
[0096] In some examples, the graphics subsystem can render the decoded video data and display the rendering results.
[0097] Basic software service subsystem set: provides public and general software services; the basic software service subsystem set may include event notification subsystem, multimedia subsystem, etc.
[0098] Enhanced software service subsystem suite: Provides differentiated enhanced software services for different devices; the enhanced software service subsystem suite may include smart screen proprietary business subsystem, wearable proprietary business subsystem, IoT proprietary business subsystem, etc.
[0099] Hardware service subsystem set: Provides hardware services; the hardware service subsystem set may include location service subsystem, user IAM (Identity and Access Management) subsystem, wearable proprietary hardware service subsystem, biometric identification subsystem, IoT proprietary hardware service subsystem, etc.
[0100] Distributed task scheduling enables distributed service management (discovery, synchronization, registration, and invocation), supporting remote startup, remote invocation, remote connection, and migration of applications across devices.
[0101] Distributed data management enables data synchronization, data storage, data sharing, and data access across all scenarios and devices.
[0102] The distributed soft bus provides communication-related capabilities for seamless interconnection between multiple devices, including: WLAN service capabilities, Bluetooth service capabilities, soft bus, inter-process communication RPC (Remote Procedure Call), and StarFlash communication capabilities.
[0103] Ark Multilingual Runtime is a unified compilation runtime platform designed to support the joint compilation and execution of multiple programming languages and multiple chip platforms.
[0104] The framework layer provides application programming interfaces (APIs) and programming frameworks for applications in the application layer. The framework layer includes: the ArkUI framework (which provides a complete infrastructure for UI development of system applications, including UI functions such as components, layouts, animations, and interactive events, as well as a real-time interface preview tool), the user application framework, and the Ability framework (an Ability is a lightweight application; the Ability framework schedules and manages the operation and lifecycle of Abilities). Different devices may have different operating systems, and the APIs they support may also differ.
[0105] The HarmonyOS API is designed to support... HarmonyOS API provides a range of open capabilities for application development. It can be configured at the framework layer or independently of it. The HarmonyOS API includes the Audio API, Push API, and Account API, among others.
[0106] Applications can include system apps and extended / third-party apps. System apps can include the desktop, control bar, compiler apps, and settings, while extended / third-party apps can include social apps, travel apps, etc.
[0107] In some examples, compiler applications are used by developers to compile code files, and compiler applications can also parse code files.
[0108] The following will use a laptop computer as an example to specifically explain the code parsing method provided in the embodiments of this application.
[0109] Figure 3 This is a flowchart illustrating a code parsing method according to an embodiment of this application, such as... Figure 3 As shown, the method may include the following steps S301-S305.
[0110] Step S301: The laptop detects an open operation for the target application and launches the target application.
[0111] In some embodiments of this application, users, such as developers, can use a target application on a laptop computer to compile code files. The target application may include a compiler application, a programming application including code inspection and code referencing functions, and a software development application including code inspection and code referencing functions, etc. Developers can use the target application to implement code file inspection and referencing functions. Of course, the laptop computer can also use other methods besides the target application to achieve this, and this application does not specifically limit this.
[0112] Specifically, developers can instruct the laptop to launch the corresponding application while compiling code files and using the relevant functions. There are several ways for the laptop to launch the target application.
[0113] Typically, developers can select the icon corresponding to the target application on the laptop's home screen to launch the application. This launch action can include clicking the application's icon or using voice commands to launch the target file.
[0114] For example, Figure 4 This is a schematic diagram of an interface illustrating an application scenario in an embodiment of this application. For example... Figure 4 As shown in (A), a developer can click the target application icon on the laptop's home screen, and the laptop will launch the target application in response to the developer's click. Figure 4As shown in (B), the laptop responds to the developer's click and displays the target application's details page. At this point, the target application is launched. Alternatively, the laptop can also launch the target application in response to the developer's voice command. The developer can then use the target application to compile code, such as performing code inspections and referencing code within code files.
[0115] See also Figure 4 In section (B), the target application's details page includes a function bar, a shortcut bar, and a display area located below the shortcut bar. The display area shows file information for recently used code project files. The display area includes display controls for the corresponding code project files. Developers can click on the display controls to input display commands for any code project file, triggering the display of the corresponding code project file.
[0116] Step S302: The laptop computer responds to the open operation for the code project file by displaying the code project file.
[0117] In real-world scenarios, developers typically compile a project as a whole, with each project corresponding to a set of code files. Developers will compile the entire code project. Of course, a code project can include at least one code file. Developers will compile each code file individually or for a few code files.
[0118] Laptops store numerous data files, such as one or more code project files. When a developer wants to compile a code project file, they need to first open that file using the target application on the laptop. This allows the target application to perform subsequent checks and references on the code files within the project file. Of course, developers can also open remote code project files using their laptops. This application does not limit the specific storage location of the code project files or the method by which the developer opens them.
[0119] In some embodiments of this application, the above-described open operation can be used to instruct a laptop computer to open a code project file, such as... Figure 4 In project file A (B), for example, a triggering action could include a developer's click action on project file A; a triggering action could also include a developer's voice action on project file A.
[0120] For example, Figure 5 This is a schematic diagram of the interface for opening project file A, as shown in an embodiment of this application. Figure 5As shown in (A), when the developer clicks the display control corresponding to "Project File A", the laptop responds to the developer's operation on the display control corresponding to "Project File A" and triggers the display of Project File A.
[0121] The code files in the code project file can include files with extensions such as .java, .py, .cpp, and .c. The target application can open these files to perform subsequent code checks and reference operations. This application does not limit the file type corresponding to the code files.
[0122] Step S303: The laptop computer obtains the abstract syntax tree of each code file in the code project file.
[0123] In this context, the nodes in the abstract syntax tree of the code file correspond to the fragment information of the code segments in the code file.
[0124] In some embodiments of this application, a laptop computer can parse each code file in a code project file to obtain the abstract syntax tree (AST) of each code file. It is understood that each code file corresponds to an AST.
[0125] Specifically, the target application in the laptop can use a syntax parser to parse each code file and generate an abstract syntax tree (AST) for each code file.
[0126] The abstract syntax tree contains the syntax information of each code file, and the syntax parser can be any of the following: @babel / parser parser, uglify-js parser, Esprima parser, acorn parser, espree parser, TypeScript parser, superse parser, swc parser, AST Explorer parser, or jscodeshift parser.
[0127] In some embodiments of this application, an abstract syntax tree (API) is an abstract representation of the code syntax structure. It represents the syntax structure of a programming language in a tree-like form, where each node in the tree represents fragment information of a specific code segment in the code file. In other words, an API is a tree-structured representation of the fragment information of code segments. The fragment information of a code segment may include syntax information, such as the signature information of the code segment.
[0128] In some embodiments of this application, nodes in the abstract syntax tree correspond to fragment information of code snippets in a code file. It is understood that the code snippets in the code file are specific code content written in a programming language, while the abstract syntax tree is an abstract representation of the syntax structure corresponding to the specific code content.
[0129] For example, the code snippets on lines 19 and 20 of the first file include:
[0130] 19. function add(a,b)
[0131] 20{return a+b}
[0132] The code snippets in lines 19 and 20 above are function code. The abstract syntax tree corresponding to the first file can include function nodes and parameter nodes. Function nodes represent function definitions and can correspond to the code for the function definition, addition operation, and return statement mentioned above. Parameter nodes can correspond to the function parameters (such as a and b). Parameter nodes can be child nodes of function nodes. It should be noted that the above code snippets are merely examples, and this application does not specifically limit the implementation process of obtaining the abstract syntax tree or the code snippets representing nodes in the abstract syntax tree.
[0133] As can be seen, this embodiment of the application parses the code project files to obtain the abstract syntax tree of each code file. The abstract syntax tree includes the syntax information of the code file. It can use the abstract syntax tree to abstractly represent the information in the code file, including both the semantics of the code file itself and optimizing the data structure, priority, and hierarchy of the information in the code file according to the hierarchical arrangement of nodes. This enables the extraction of syntax information and the display of the data distribution structure of the code file, thereby achieving accurate and rapid acquisition of relevant information regarding the specific execution process of each code segment in the code file.
[0134] Step S304: The laptop performs code parsing on the first type of node in the abstract syntax tree of the code file.
[0135] Among them, the target code snippets corresponding to the first type of nodes in the code file can be exported.
[0136] In some embodiments of this application, the target code snippets corresponding to the first type of node in the code file support export. The target code snippets may include functions, methods, classes, interfaces, constants, and variables, etc. It should be noted that the code snippets that can be exported differ in different programming languages, and this application does not limit this.
[0137] Understandably, the exported target code snippets mentioned above can also be referenced subsequently. For example, the first file can reference the exported target code snippets from the second file.
[0138] Furthermore, target code snippets can include functions and / or methods, and these snippets can be declared with a return type. Declaring a return type means that the target code snippet explicitly specifies the data it will return after execution. Understandably, in a code file, declaring a return type for a target code snippet makes the type of the result immediately clear, improving readability. At the same time, target code snippets also help developers quickly locate problems, facilitating code maintenance and debugging. Furthermore, these target code snippets can often be combined; for example, the return value of one target code snippet can be used as input to another piece of code.
[0139] Thus, by parsing the first type of nodes representing functions and / or methods in this embodiment, not only can important content in the code file be parsed, but significant memory usage can also be saved. This improves operating system performance and system stability.
[0140] Typically, when a target application on a laptop starts the code project file, it can automatically trigger the parsing process of the code project file, that is, the process of checking each code file in the code project file. Of course, developers can also manually trigger the parsing process of the code project file. This application does not specifically limit this.
[0141] In some embodiments of this application, the target application in the laptop needs to first obtain the first type of nodes in the abstract syntax tree of the code file, and then parse the first type of nodes in the abstract syntax tree of the code file. Specifically, the target application can obtain the first type of nodes in the abstract syntax tree of the code file based on the signature information of the target code fragment.
[0142] The signature information of a target code snippet typically defines that snippet clearly and can be understood as an important identifier describing the code characteristics within it. Based on this, the target application can use the signature information of the target code snippet to obtain the first type of node in the abstract syntax tree of the code file.
[0143] In some examples, the target application on the laptop can obtain the signature information of the target code snippet, which includes the return type of the function and / or the return type of the method. The target application can also use the signature information of the target code snippet to find the first type of node in the abstract syntax tree of the code file and perform code parsing on the first type of node.
[0144] For example, the object code snippet in lines 28-30 of the first file includes:
[0145] 28function sayHello():string{
[0146] 29let age:number = 1
[0147] 30}
[0148] The target code snippet in lines 28-30 above is function code. The signature of the target code snippet declares that the return type of the function "sayHello" is "string". The target application can use "string" to find the first type of node corresponding to the function "sayHello".
[0149] For another example, the object code snippet in lines 38-40 of the first file includes:
[0150] 38function sayHello(){
[0151] 39let age:number=1
[0152] 40}
[0153] The target code snippet in lines 38-40 above is function code, and the signature information of the target code snippet does not declare the return type of the function "sayHello". The absence of a declared return type can be interpreted as the function returning an empty value.
[0154] Thus, this embodiment of the application utilizes the signature information of the target code fragment to locate and parse the first type of node, facilitating subsequent code export and code referencing functions. This increases code reusability, eliminating the need to repeatedly write the same code, and also makes the program structure clearer. It also simplifies development and maintenance.
[0155] In some embodiments of this application, the signature information of the target code fragment includes the return type, function name, and at least one item from the function parameter list for the corresponding function, and / or the return type, method name, and at least one item from the method parameter list for the corresponding method. The parameter list may include the number, type, and order of parameters, etc.
[0156] In one possible implementation, embodiments of this application can obtain the first type of node in the abstract syntax tree of the code file based on the return value type and function name of the function, or the return value type and method name of the method.
[0157] In another possible implementation, embodiments of this application can obtain the first type of node in the abstract syntax tree of the code file based on the return type of the function and the list of function parameters, or the return type of the method and the list of method parameters.
[0158] It should be noted that the embodiments of this application do not limit the specific implementation of the above-mentioned signature information and the acquisition of the first type of node.
[0159] In some embodiments of this application, during the code parsing of the first type of nodes by the laptop computer, the target application can obtain the correspondence between the first type of nodes and other nodes in the abstract syntax tree of the code file. The target application can also perform semantic and syntactic analysis on the code fragment information corresponding to the first type of nodes to obtain analysis results. Based on the correspondence and / or analysis results, the target application can determine whether there are errors in the target code fragment corresponding to the first type of nodes.
[0160] In other words, after obtaining the first type of nodes, the target application in the laptop can traverse these nodes, identify their node types, and extract their node information. Based on this information, the target application can determine the correspondence between the first type of nodes and other nodes. Furthermore, the target application can process the child nodes within the first type of nodes, parsing them according to their node types, semantics, and syntax rules to obtain the analysis results of the code snippets corresponding to the first type of nodes. Thus, the parsing of the first type of nodes is completed. It can be understood that the parsing process of nodes in the abstract syntax tree can also be called the inspection process of nodes in the abstract syntax tree, or the inspection process of code snippets in a code file.
[0161] In some examples, the target application can also perform code style checks, such as indentation and whitespace checks, and naming convention checks, using the first type of node. This application's embodiments do not limit the parsing process corresponding to the first type of node.
[0162] Thus, by parsing the first type of nodes, and based on the relationship between the first type of nodes and other nodes, and / or the analysis results of semantic and syntactic analysis, it is possible to determine whether there are errors in the target code segment corresponding to the first type of nodes. This improves the code instructions in the code file and ensures the code's standardization and security. Furthermore, by parsing only the first type of nodes, this embodiment of the application does not consume too much memory space, thus improving system stability.
[0163] In some embodiments of this application, the laptop computer may also output a first prompt message, which is used to indicate that there is a code error in the target code segment corresponding to the first type of node.
[0164] For example, after parsing the first type of node, the target application can output the first description information. This first prompt information is used to alert the developer that there is a code error in the target code snippet corresponding to the first type of node. This allows the developer to modify the code file containing the error.
[0165] For example, the first suggestion message can be provided for each code file. See also Figure 6 Project file A includes a first file and a second file. If a code error exists in the target code segment corresponding to a first type of node in the first file, the first warning message may include "A code error exists in the target code segment of the first file." For example, if code errors exist in the target code segments corresponding to the first type of nodes in both the first and second files, the first warning message may include "A code error exists in the target code segment of both the first and second files."
[0166] Thus, this embodiment of the application can also output a first prompt message to alert developers to the target code segment containing a code error, thereby improving the developer's user experience.
[0167] In some embodiments of this application, after the laptop displays the code project file (i.e., before the first file is displayed), it does not parse the second type of nodes in the abstract syntax tree of the code file. The second type of nodes are the nodes in the abstract syntax tree of the first file other than the first type of nodes. In other words, the target application only parses the first type of nodes in each code file and does not parse the second type of nodes.
[0168] For example, after displaying project file A, the laptop may only parse the first type of nodes in the first file and the second file, and not parse any nodes other than the first type of nodes.
[0169] In some embodiments, the second type of node can be used to represent the code snippets corresponding to method bodies and / or function bodies in a code file. The code snippet corresponding to the method body defines the code block inside the method declaration, containing the specific statements and logic that implement the method's functionality. The function body, like the method body, is a code container used to hold the code logic that implements a specific function. Whether it's a function or a method, the actual operations they perform are completed within the method body or function body. Typically, in scenarios where functions or methods implement complex functionalities, the function body or method body usually contains a large amount of code.
[0170] In some examples, to verify the effectiveness of the above partial node parsing process, with 500,000 code actions, the memory usage for partial node parsing was 0.58G, and the memory usage for full node parsing was 0.8G. With 1,500,000 code actions, the memory usage for partial node parsing was 2.9G, and the memory usage for full node parsing was 3.9G.
[0171] Thus, this embodiment only needs to focus on the first type of nodes, namely the signature information corresponding to the target code fragment, to complete the code inspection process simply and directly. It can skip analyzing the specific statements in the method body and / or function body, and does not require reading the second type of nodes, thereby reducing memory usage and saving significant time and computational resources.
[0172] Furthermore, examining only the external characteristics of the target code snippet, such as the return type, without resolving the method body, can protect the code logic implemented inside the method to a certain extent, thus protecting code privacy.
[0173] In some embodiments of this application, the laptop computer can also parse the second type of nodes after parsing the first type of nodes. Specifically, after the target application displays the code project file, i.e., before the first file is displayed, it can determine whether the first type of nodes in the abstract syntax tree of the code file have been parsed. If the first type of nodes in the abstract syntax tree of the code file have been parsed before the first file is displayed, the second type of nodes in the abstract syntax tree of the code file can be obtained. The second type of nodes are the nodes in the abstract syntax tree of the code file other than the first type of nodes. The target application can also parse the second type of nodes.
[0174] For example, during the parsing of the second type of nodes, the target application can use rate limiting to restrict the parsing speed. As another example, during the parsing of the second type of nodes, the target application can limit the amount of data parsed to avoid sudden increases in memory usage within a short period.
[0175] Thus, this embodiment of the application can parse the second type of nodes after the first type of nodes in the abstract syntax tree of the code file has been parsed. On the one hand, this improves the completeness of the parsing process. On the other hand, this embodiment of the application can read and parse another part of the nodes after parsing a portion of the nodes, avoiding a sudden increase in memory usage in a short period of time.
[0176] In some embodiments of this application, after the laptop computer has finished parsing each code file, it can output a second prompt message, which is used to indicate that there is a code error in the code file.
[0177] For example, after parsing the first type of nodes, or after parsing both the first and second types of nodes, the target application can output a second prompt message. This second prompt message is used to alert developers that there are code errors in the code files. This allows developers to modify the code files containing the errors.
[0178] For example, the second suggestion message can be provided for individual code files. See also Figure 7 Project file A includes a first file and a second file. If a code error exists in the first file, the second error message could include "A code error exists in the first file." Alternatively, if both the first and second files contain code errors, the second error message could include "A code error exists in both the first and second files."
[0179] Thus, this embodiment of the application can also output a second prompt message to alert developers to code files containing errors, thereby improving the user experience for developers.
[0180] Step S305: The laptop computer responds to the open operation of the first file in the code file and displays the first file.
[0181] In some embodiments of this application, the laptop computer can display the first file in response to an open operation on the first file in the code file (such as a click operation on the display control of "first file" in the details page corresponding to project file A).
[0182] For example, see Figure 5 As shown in (B), the details page for project file A includes a first file, a second file, and an open control. Developers can click the file icon corresponding to the code file to trigger the opening of that code file. For example, if a developer clicks the file icon corresponding to "first file" and then clicks the open control, the laptop responds to the developer's actions on the file icon and open control, triggering the display of the first file. As another example, if a developer double-clicks the file icon corresponding to "first file," the laptop responds to the developer's actions on the file icon, triggering the display of the first file. In this way, developers can perform subsequent compilation operations on the first file.
[0183] In some embodiments of this application, after the laptop displays the first file, i.e., after opening the first file, the process of checking the first file can be automatically triggered. Of course, developers can also manually trigger the parsing process of the first file. This application does not impose specific limitations on this.
[0184] In one feasible approach, after displaying the first file, the target application can parse the code of each node according to the order of the abstract syntax tree nodes in the first file. In other words, after displaying the first file, the target application can parse all nodes in the abstract syntax tree of the first file.
[0185] For example, see Figure 8 The target application may include a processing module (such as a Parser module). When the code project files are open, this processing module can traverse the first file. If the first file is not open, it can be understood as not being in the list of open files. Thus, the processing module in the target application can parse the first type of nodes in the abstract syntax tree of the first file, but not the second type of nodes. After opening the first file, the processing module triggers a full code parsing of each node according to the order of the abstract syntax tree nodes in the first file; that is, the processing module parses both the first and second type of nodes.
[0186] Thus, this embodiment of the application can perform partial node parsing on the first type of nodes in the abstract syntax tree of the first file when the first file is not opened. When the first file is opened, a full parsing is performed on each node in the abstract syntax tree of the first file. This not only avoids the large memory consumption of full parsing when the first file is not opened, but also allows for a clear understanding of all aspects of the code in the first file, from the overall architecture to the details, when the first file is opened. Therefore, a more comprehensive code inspection of the first file is performed, ensuring the code quality of the first file.
[0187] In another possible implementation, if the target application displays a first file and the first type of nodes in the abstract syntax tree of the first file have completed code parsing, it can perform code parsing on the second type of nodes in the abstract syntax tree of the first file. The second type of nodes are the nodes in the abstract syntax tree of the first file other than the first type of nodes.
[0188] For example, if the target application is displaying a first file and the nodes representing functions with return value types in the abstract syntax tree of the first file have been resolved, the nodes representing method bodies in the abstract syntax tree of the first file can be resolved.
[0189] Thus, in this embodiment of the application, when the first file is displayed and the first type of nodes in the abstract syntax tree of the first file have completed code parsing, the unparsed nodes can then be parsed. This results in a more comprehensive code inspection of the first file, ensuring its code quality. Simultaneously, parsing only the second type of nodes saves memory space and reduces the parsing time.
[0190] In another possible implementation, the target application stops parsing the first type of nodes in the first file's abstract syntax tree (API) if the first file is displayed and the first type of nodes in the API are not yet parsed. The target application can then parse the code of each node in the order of the API nodes in the first file.
[0191] In other words, if a developer opens the first file quickly, there might be a scenario where the target application hasn't finished parsing the first type of nodes in the first file's abstract syntax tree (API). This allows the target application to determine whether the first type of nodes in the API have been completely parsed. If the first file is opened before the first type of nodes in its API have been completely parsed, the parsing of the first type of nodes needs to be stopped before triggering full code parsing.
[0192] Therefore, in the case where the first file is displayed and the parsing of the first type of nodes in the abstract syntax tree of the first file has not been completed, the parsing of the first type of nodes can be stopped before the full code parsing process is executed. Thus, while ensuring the code quality of the first file, stopping the parsing of the first type of nodes avoids repeated parsing of these nodes. This saves significant parsing time and computational resources, improving the overall operating system efficiency.
[0193] In some embodiments of this application, the target code snippets corresponding to the first type of nodes in the code file support export. It is understood that the exportable target code snippets can be referenced subsequently. Therefore, the target code snippets corresponding to the first type of nodes can be applied in the context of code application. For example, developers can reference target code snippets from a second file in an opened first file.
[0194] In one feasible approach, the target application can obtain a first-class node in the abstract syntax tree of the second file in the code file. The first-class node includes a first node and a second node, and the first node and the second node represent code segments with different segment names.
[0195] The target application can display a reference list in response to a reference operation on the second file; the reference list includes a first code snippet and a second code snippet from the second file, the first code snippet corresponding to a first node and the second code snippet corresponding to a second node. The target application can also insert the first code snippet into the target location in the first file in response to a trigger operation on the first code snippet in the reference list.
[0196] For example, a developer can reference a target code snippet from a second file at a target location in the first file. See also Figure 9In (A), developers can trigger the use of code references by typing "import" at the target location on line 31 of the first file. The target application, in response to a reference operation to the second file (such as typing "import"), displays a list of references. See also Figure 9 In section (B), the reference list includes a first code snippet (e.g., "main") and a second code snippet (e.g., "name") from the second file. Specifically, the target application can obtain a first type of node in the abstract syntax tree of the second file. This first type of node includes a first node and a second node, where the first node corresponds to the first code snippet and the second node corresponds to the second code snippet. Thus, the target application can display the reference list based on the first and second nodes. The target application can respond to a triggering operation on the first code snippet in the reference list, such as a click on "main," to insert "main" into the target position at line 31 of the first file.
[0197] It is understood that the first type of node in the second file may include multiple different nodes, each representing a different function name and / or method name. Furthermore, in the case of multiple files in the code project file, the first file may also reference other files besides the second file. The reference list may also include object code snippets from files other than the second file. This application does not limit this aspect.
[0198] Thus, in this embodiment of the application, after parsing the first type of nodes in the second file, the code snippets corresponding to the first type of nodes in the second file can be directly referenced in the first file. By performing local node parsing on the second file, sudden increases in memory usage during the parsing process are avoided. Furthermore, the parsing results can be directly used in subsequent referencing scenarios, improving development efficiency and the user experience for developers.
[0199] In another possible implementation, the target application can obtain a first type of node in the abstract syntax tree of the second file. This first type of node includes a first node and a second node. The first node represents a first code segment in the second file, and the second node represents a second code segment in the second file. The first and second code segments have different names. The target application can also, in response to a reference operation to the first code segment in the second file, insert the first code segment into a target location within the first file.
[0200] For example, a developer can directly reference the first code snippet in the second file from a target location in the first file. See also Figure 10In (A), the developer can trigger the code import functionality for "main" by typing "import(main)" at the target location on line 31 of the first file. The target application can then directly insert "main" into the target location on line 31 of the first file in response to the import operation on the first code snippet in the second file.
[0201] Or see Figure 10 In (B) the target application can, in response to a reference operation to the first code snippet in the second file, display a reference list, which includes the first code snippet in the second file (e.g., "main"). The target application can, in response to a triggering operation on the first code snippet in the reference list, such as a click on "main", insert "main" at the target position on line 31 of the first file.
[0202] Thus, in this embodiment of the application, after parsing the first type of nodes in the second file, the specific code snippets corresponding to the first type of nodes in the second file can be directly referenced in the first file. By performing local node parsing on the second file, sudden increases in memory usage during the parsing process are avoided. Furthermore, the parsing results can be directly used in subsequent reference scenarios, improving development efficiency and the user experience for developers.
[0203] In some solutions, multiple embodiments of this application can be combined, and the combined solution can be implemented. Optionally, some operations in the process of each method embodiment may be combined, and / or the order of some operations may be changed. Furthermore, the execution order between the steps of each process is merely exemplary and does not constitute a limitation on the execution order between steps; other execution orders are also possible. It is not intended to indicate that the execution order is the only possible order in which these operations can be performed.
[0204] Those skilled in the art will conceive of various ways to reorder the operations described in the embodiments of this application. Furthermore, it should be noted that process details involved in one embodiment of this application are similarly applicable to other embodiments, or different embodiments can be combined.
[0205] Furthermore, some steps in the method embodiments can be equivalently replaced with other possible steps. Alternatively, some steps in the method embodiments may be optional and can be deleted in certain use cases. Or, other possible steps may be added to the method embodiments.
[0206] Furthermore, the various method embodiments can be implemented individually or in combination.
[0207] This application also provides an electronic device, such as the aforementioned laptop computer, etc. Figure 11As shown, the laptop computer may include one or more processors 1110, memory 1120 and communication interfaces 1130.
[0208] The memory 1120, communication interface 1130, and processor 1110 are coupled together. For example, the memory 1120, communication interface 1130, and processor 1110 can be coupled together via bus 1140.
[0209] The communication interface 1130 is used for data transmission with other devices. The memory 1120 stores computer program code. The computer program code includes computer instructions, which, when executed by the processor 1110, cause the electronic device to perform the relevant method steps in the embodiments of this application.
[0210] Processor 1110 may be a processor or controller, such as a central processing unit (CPU), a general-purpose processor, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, transistor logic devices, hardware components, or any combination thereof. It may implement or execute the various exemplary logic blocks, modules, and circuits described in conjunction with this disclosure. The processor may also be a combination that implements computational functions, such as a combination of one or more microprocessors, a combination of a DSP and a microprocessor, etc.
[0211] Bus 1140 can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The aforementioned bus 1140 can be divided into address bus, data bus, control bus, etc. For ease of representation, Figure 11 The bus is represented by a single thick line, but this does not mean that there is only one bus or one type of bus.
[0212] This application also provides an electronic device, which includes a memory and one or more processors; the memory is coupled to the processors; wherein the memory stores computer program code, which includes computer instructions, and when the computer instructions are executed by the processor, the electronic device performs the relevant method steps in the above method embodiments.
[0213] This application also provides a communication device, which includes a memory and one or more processors; the memory is coupled to the processors; wherein the memory stores computer program code, which includes computer instructions, and when the computer instructions are executed by the processor, the communication device performs the relevant method steps in the above method embodiments.
[0214] This application also provides a computer-readable storage medium storing computer program code. When the processor executes the computer program code, the electronic device executes the relevant method steps in the above method embodiments.
[0215] This application also provides a computer program product containing instructions that, when executed on a computer or processor, cause the computer or processor to perform the relevant method steps as described in the above method embodiments.
[0216] This application also provides a chip system, including: a processor coupled to a memory, the memory being used to store programs or instructions, and when the program or instructions are executed by the processor, the chip system enables the methods in any of the above method embodiments.
[0217] Optionally, the chip system may include one or more processors. These processors can be implemented in hardware or software. When implemented in hardware, the processor can be a logic circuit, an integrated circuit, etc. When implemented in software, the processor can be a general-purpose processor, implemented by reading software code stored in memory.
[0218] Optionally, the chip system may contain one or more memories. The memory may be integrated with the processor or disposed separately from it; this application embodiment does not limit this. For example, the memory may be a non-transient processor, such as a read-only memory (ROM), which may be integrated with the processor on the same chip or disposed separately on different chips. This application embodiment does not specifically limit the type of memory or the arrangement of the memory and processor.
[0219] For example, the chip system can be a field programmable gate array (FPGA), an application-specific integrated circuit (ASIC), a system-on-chip (SoC), a central processing unit (CPU), a network processor (NP), a digital signal processor (DSP), a micro controller unit (MCU), a programmable logic device (PLD), or other integrated chips.
[0220] The electronic devices, computer storage media, or computer program products provided in this application are all used to execute the corresponding methods provided above. Therefore, the beneficial effects they can achieve can be referred to the beneficial effects in the corresponding methods provided above, and will not be repeated here.
[0221] Through the above description of the embodiments, those skilled in the art can clearly understand that, for the sake of convenience and brevity, only the division of the above functional modules is used as an example. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above.
[0222] In the several embodiments provided in this application, it should be understood that the disclosed apparatus and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of modules or units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another apparatus, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between apparatuses or units may be electrical, mechanical, or other forms.
[0223] The units described as separate components may or may not be physically separate. A component shown as a unit can be one or more physical units, located in one place or distributed in multiple different locations. Some or all of the units can be selected to achieve the purpose of this embodiment, depending on actual needs.
[0224] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0225] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a readable storage medium. Based on this understanding, the technical solutions of the embodiments of this application, in essence, or the contributing parts, or all or part of the technical solutions, can be embodied in the form of a software product. This software product is stored in a storage medium and includes several instructions to cause a device (which may be a microcontroller, chip, etc.) or processor to execute all or part of the steps of the methods of the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0226] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions within the technical scope disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
1. A code parsing method characterized by, The method includes: In response to an open operation on a code project file, the code project file is displayed; Obtain the abstract syntax tree of each code file in the code project file, wherein the nodes in the abstract syntax tree of the code file correspond to the fragment information of the code fragments in the code file; Code parsing is performed on the first type of nodes in the abstract syntax tree of the code file, and the target code fragments corresponding to the first type of nodes in the code file can be exported. In response to an open operation on the first file in the code file, the first file is displayed.
2. The method of claim 1, wherein, The target code snippet includes functions and / or methods, and the target code snippet declares a return value type.
3. The method according to claim 1 or 2, characterized in that, The method further includes: Obtain the first type of node in the abstract syntax tree of the second file in the code file. The first type of node includes a first node and a second node. The first node and the second node represent code segments with different segment names. In response to a reference operation on the second file, a reference list is displayed; wherein the reference list includes a first code snippet and a second code snippet from the second file, the first code snippet corresponding to the first node and the second code snippet corresponding to the second node; In response to a triggering operation on the first code snippet in the reference list, the first code snippet is inserted into the target location in the first file.
4. The method according to claim 1 or 2, characterized in that, The method further includes: Obtain the first type of node in the abstract syntax tree of the second file. The first type of node includes a first node and a second node. The first node is used to represent a first code segment in the second file, and the second node is used to represent a second code segment in the second file. The first code segment and the second code segment have different segment names. In response to a reference operation to the first code snippet in the second file, the first code snippet is inserted into the target location in the first file.
5. The method according to any one of claims 1 to 4, characterized in that, The method further includes: When the first file is displayed and the first type of nodes in the abstract syntax tree of the first file have completed code parsing, the second type of nodes in the abstract syntax tree of the first file are parsed. The second type of nodes are the nodes in the abstract syntax tree of the first file other than the first type of nodes.
6. The method according to any one of claims 1 to 4, characterized in that, The method further includes: After displaying the first file, the code of each node is parsed according to the order of the abstract syntax tree nodes of the first file.
7. The method according to any one of claims 1-6, characterized in that, The method further includes: If the first file is displayed and the first type of node in the abstract syntax tree of the first file has not been parsed, stop parsing the first type of node in the abstract syntax tree of the first file; The code is parsed for each node according to the order of the abstract syntax tree nodes in the first file.
8. The method according to any one of claims 1 to 7, characterized in that, The method further includes: Before displaying the first file, the second type of nodes in the abstract syntax tree of the code file are not parsed. The second type of nodes are the nodes in the abstract syntax tree of the first file other than the first type of nodes.
9. The method according to any one of claims 1-7, characterized in that, The method further includes: Before displaying the first file and after the first type of nodes in the abstract syntax tree of the code file have been parsed, obtain the second type of nodes in the abstract syntax tree of the code file. The second type of nodes are the nodes in the abstract syntax tree of the code file other than the first type of nodes. The second type of node is parsed.
10. The method according to any one of claims 1-9, characterized in that, The code parsing of the first type of nodes in the abstract syntax tree of the code file includes: Obtain the signature information of the target code fragment, wherein the signature information of the target code fragment includes the return type of the function and / or the return type of the method; Based on the signature information of the target code fragment, the first type of node is searched in the abstract syntax tree of the code file; The code of the first type of node is parsed.
11. The method according to claim 10, characterized in that, The code parsing of the first type of node includes: Obtain the correspondence between the first type of nodes and other nodes in the abstract syntax tree of the code file; Semantic and syntactic analysis is performed on the code fragment information corresponding to the first type of node to obtain the analysis results; Based on the correspondence and / or the analysis results, determine whether there are errors in the target code segment corresponding to the first type of node.
12. The method according to any one of claims 1-11, characterized in that, The method further includes: Output a first prompt message, which is used to indicate that there is a code error in the target code segment corresponding to the first type of node.
13. The method according to claim 5 or 6, characterized in that, The method further includes: Output a second prompt message, which is used to indicate that there is a code error in the code file.
14. An electronic device, characterized in that, include: At least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, the instructions being executed by the at least one processor to cause the at least one processor to perform the code parsing method as described in any one of claims 1-13.
15. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements the code parsing method as described in any one of claims 1-13.
16. A computer program product, characterized in that, When the computer program product is run on a computer, the computer performs the code parsing method as described in any one of claims 1-13.