diff --git a/README.md b/README.md index d4b347e..37c3385 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,17 @@ npm run start ``` 9. Create a pull request on one of your repositories and watch the review agent submit a review! - - Make sure to create the pull request on a repository that your GitHub app has access to. - - Make sure the pull request has at least one changed file that is supported by the review agent. The following file extensions are ignored: ".pdf", ".png", ".jpg", ".jpeg", ".gif", ".mp4", ".mp3", ".md", ".json", ".env", ".toml", and ".svg". - - You will have to create new pull requests each time to test the review agent, as it will not work on the same pull request twice. + +- Make sure to create the pull request on a repository that your GitHub app has access to. +- Make sure the pull request has at least one changed file that is supported by the review agent. The following file extensions are ignored: ".pdf", ".png", ".jpg", ".jpeg", ".gif", ".mp4", ".mp3", ".md", ".json", ".env", ".toml", and ".svg". +- You will have to create new pull requests each time to test the review agent, as it will not work on the same pull request twice. + +## Resources + +- [Project Diagram](https://excalidraw.com/#json=Tyo1OFlu5AY5b7AiZyPJw,jIJfR3fdbbqsxecfalkkEw) +- [Abstract Syntax Trees (AST)](https://earthly.dev/blog/python-ast/) +- [ngrok](https://dashboard.ngrok.com/get-started/setup/windows) +- [Code Review Automation With LLMs](https://www.sciencedirect.com/science/article/pii/S0950584924001289) +- [AI-powered Code Review](https://arxiv.org/pdf/2404.18496) + +_Used NotebookLM to create highlight podcasts_ diff --git a/src/context/language/python-parser.ts b/src/context/language/python-parser.ts index 845e90b..d60d85a 100644 --- a/src/context/language/python-parser.ts +++ b/src/context/language/python-parser.ts @@ -1,15 +1,82 @@ import { AbstractParser, EnclosingContext } from "../../constants"; +import { Python3Parser } from "python-parser"; + +// Function to process each node and determine if it is the largest enclosing context +const processNode = ( + node: any, + lineStart: number, + lineEnd: number, + largestSize: number, + largestEnclosingContext: any | null +) => { + const { start, end } = node.loc; + if (start.line <= lineStart && lineEnd <= end.line) { + const size = end.line - start.line; + if (size > largestSize) { + largestSize = size; + largestEnclosingContext = node; + } + } + return { largestSize, largestEnclosingContext }; +}; + export class PythonParser implements AbstractParser { findEnclosingContext( file: string, lineStart: number, lineEnd: number ): EnclosingContext { - // TODO: Implement this method for Python - return null; + const parser = new Python3Parser(); // Create a new instance of Python3Parser + const ast = parser.parse(file); // Parse the Python file to get the AST + let largestEnclosingContext: any = null; // Initialize the largest enclosing context + let largestSize = 0; // Initialize the largest size + + // Custom listener class to traverse the AST + class CustomListener { + // What does the enterEveryRule method do? + // It makes the listener visit every rule node in the AST + // and process each node to determine the largest enclosing context + // The ctx parameter is the context object for the rule node + enterEveryRule(ctx: any): void { + // Process each rule node + ({ largestSize, largestEnclosingContext } = processNode( + ctx, + lineStart, + lineEnd, + largestSize, + largestEnclosingContext + )); + } + } + + const listener = new CustomListener(); + parser.listen(listener, ast); // Traverse the AST with the custom listener + + return { + enclosingContext: largestEnclosingContext, // Return the largest enclosing context + } as EnclosingContext; } dryRun(file: string): { valid: boolean; error: string } { - // TODO: Implement this method for Python - return { valid: false, error: "Not implemented yet" }; + try { + const parser = new Python3Parser(); // Create a new instance of Python3Parser + parser.parse(file); // Parse the Python file + return { + valid: true, + error: "", // No errors + }; + } catch (exc) { + return { + valid: false, + error: exc.message, // Return the error message + }; + } } } +/* + AST (Abstract Syntax Tree) is a tree representation of the source code + It represents the structure of the code in a hierarchical manner + The AST is used to analyze and manipulate the code programmatically + The Python3Parser class is used to parse Python code and generate the AST + --> As the code is parsed, the code is analyzed and broken down into parts + --> These parts are represented as nodes in the AST + */