Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShaderLab support variable list declaration. #2465

Open
wants to merge 8 commits into
base: dev/1.4
Choose a base branch
from

Conversation

Sway007
Copy link
Member

@Sway007 Sway007 commented Dec 16, 2024

Please check if the PR fulfills these requirements

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

Support syntax of variable list declaration for global uniform and local variables:

vec2 weights[2], offsets[2];

// currently you can achieve the same result by 
vec2 weights[2];
vec2 offsets[2];

Summary by CodeRabbit

  • New Features

    • Introduced new token type SEMICOLON for enhanced parsing.
    • Added support for multiple variable declarations in a single statement.
    • Introduced new built-in functions texture2DLodEXT and textureCubeLodEXT for advanced texture sampling.
  • Bug Fixes

    • Improved comment handling in the scanner to prevent potential errors and infinite loops.
  • Refactor

    • Enhanced grammar rules for variable declarations to allow more flexibility and structure.

@Sway007 Sway007 added bug Something isn't working Rendering Rendering related functions shader Shader related functions high priority High priority issue labels Dec 16, 2024
@Sway007 Sway007 self-assigned this Dec 16, 2024
Copy link

coderabbitai bot commented Dec 16, 2024

Walkthrough

This pull request introduces enhancements to the shader language parser, focusing on variable declaration syntax and token handling. The changes span multiple files within the shader-lab package, including modifications to the context-free grammar, abstract syntax tree (AST), and parser rules. The primary objectives include adding a new semicolon token, refining variable declaration parsing, and introducing more flexible declaration list handling. These modifications improve the shader language's parsing capabilities and provide more structured approach to variable declarations.

Changes

File Change Summary
packages/shader-lab/src/common/types.ts Added SEMICOLON token type to ETokenType enum
packages/shader-lab/src/lalr/CFG.ts Updated grammar production rules for variable declarations, added new functions createGrammar() and addTranslationRule()
packages/shader-lab/src/parser/AST.ts Added type property to VariableDeclaration, introduced new VariableDeclarationList class, enhanced semantic analysis for variable declarations
packages/shader-lab/src/parser/GrammarSymbol.ts Added variable_declaration_list and variable_declaration_statement to NoneTerminal enum
packages/shader-lab/src/parser/TargetParser.y Modified grammar rules for variable declarations, added new rules for declaration lists and statements
packages/shader-lab/src/parser/builtin/functions.ts Added texture2DLodEXT and textureCubeLodEXT built-in functions
packages/shader-lab/src/common/BaseScanner.ts Improved comment skipping logic with additional end-of-source checks

Sequence Diagram

sequenceDiagram
    participant Parser
    participant AST
    participant Grammar
    participant Scanner

    Parser->>Grammar: Define variable declaration rules
    Grammar->>Parser: Confirm rule structure
    Parser->>Scanner: Tokenize source code
    Scanner->>Parser: Return tokens
    Parser->>AST: Create Abstract Syntax Tree
    AST->>Parser: Validate variable declarations
Loading

Possibly related PRs

Suggested Labels

enhancement, medium priority

Suggested Reviewers

  • GuoLei1990
  • zhuxudong

Poem

🐰 Semicolons dance, tokens align,
In shader's realm, new rules entwine.
Parsing magic, grammar's delight,
Code flows smooth, syntax burning bright!
A rabbit's parser, crisp and clean. 🌟

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Dec 16, 2024

Codecov Report

Attention: Patch coverage is 95.83333% with 3 lines in your changes missing coverage. Please review.

Project coverage is 68.47%. Comparing base (90c3228) to head (8b0de12).

Files with missing lines Patch % Lines
packages/shader-lab/src/parser/AST.ts 93.75% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           dev/1.4    #2465      +/-   ##
===========================================
+ Coverage    68.43%   68.47%   +0.04%     
===========================================
  Files          922      922              
  Lines        95661    95716      +55     
  Branches      8121     8131      +10     
===========================================
+ Hits         65466    65545      +79     
+ Misses       29942    29918      -24     
  Partials       253      253              
Flag Coverage Δ
unittests 68.47% <95.83%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🔭 Outside diff range comments (1)
packages/shader-lab/src/codeGen/GLES300.ts (1)

gl_FragColor translation appears incomplete

The verification reveals potential issues with the gl_FragColor translation:

  • While GS_glFragColor is defined as a constant, there's no evidence of it being properly declared as an output variable in fragment shaders
  • The codebase shows gl_FragColor is registered as a builtin variable, but the complete translation mechanism to GLSL ES 3.0's required output declaration is not visible
  • No out vec4 declarations were found that would indicate proper fragment shader output handling

Points requiring attention:

  • Need to ensure GS_glFragColor is properly declared as out vec4 at the top of fragment shaders
  • The translation mechanism should include both variable renaming and output declaration handling
🔗 Analysis chain

Line range hint 35-52: Handle gl_FragColor translation appropriately.

The updated visitVariableIdentifier method now correctly handles gl_FragColor by mapping it to GS_glFragColor and adding it to the _referencedVaryingList when in the fragment shader stage. This ensures compatibility with GLSL ES 3.0 standards.

Run the following script to ensure that gl_FragColor is consistently handled across the codebase:


Let me gather more context about how gl_FragColor is handled in the codebase.


Let me check one more aspect to see how the fragment shader output is handled in GLES300.


Let me try one final verification with corrected commands.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that `gl_FragColor` is properly mapped and used.

# Test: Search for usage of `gl_FragColor` and `GS_glFragColor`.
rg 'gl_FragColor|GS_glFragColor' packages/shader-lab/src/

Length of output: 425


Script:

#!/bin/bash
# Check the implementation of visitVariableIdentifier in GLES300.ts
ast-grep --pattern 'visitVariableIdentifier($_) {
  $$$
}'

# Check if GS_glFragColor is properly declared as an output variable
rg -A 5 'GS_glFragColor' packages/shader-lab/src/

Length of output: 560


Script:

#!/bin/bash
# Check for fragment shader output handling
ast-grep --pattern 'override visitVariableIdentifier(node: $_) {
  $$$
}'

# Check for any output declarations in fragment shaders
rg -B 2 -A 5 'precision.*?;\s*\n.*?out\s+vec4' packages/shader-lab/src/

Length of output: 341


Script:

#!/bin/bash
# Check for fragment shader output handling
ast-grep --pattern 'override visitVariableIdentifier(node: $_) {
  $$$
}' packages/shader-lab/src/codeGen/GLES300.ts

# Check for output declarations in fragment shaders
rg -U 'out\s+vec4' packages/shader-lab/src/

# Check how gl_FragColor is initialized
rg -B 2 -A 2 'GS_glFragColor' packages/shader-lab/src/codeGen/

Length of output: 578

🧹 Nitpick comments (2)
packages/shader-lab/src/parser/AST.ts (1)

237-253: Fix the typo in variable name variableDeclation.

In the semanticAnalyze method of VariableDeclarationList, the variable variableDeclation is misspelled. The correct spelling should be variableDeclaration. This typo may lead to confusion and could affect code readability and maintainability.

Apply this diff to correct the typo:

-    const variableDeclation = children[0] as VariableDeclaration;
+    const variableDeclaration = children[0] as VariableDeclaration;
packages/shader-lab/src/lalr/CFG.ts (1)

33-45: LGTM! Consider adding grammar documentation.

The new rules for variable_declaration_list and variable_declaration_statement are well-structured and correctly implement the list declaration syntax. They properly handle both simple variables and array declarations.

Consider adding comments to document the grammar rules, especially the new list declaration syntax. For example:

+// Grammar rule for comma-separated variable declarations
 ...GrammarUtils.createProductionWithOptions(
   ENonTerminal.variable_declaration_list,
   [
     [ENonTerminal.variable_declaration],
     [ENonTerminal.variable_declaration_list, ETokenType.COMMA, ETokenType.ID],
     [ENonTerminal.variable_declaration_list, ETokenType.COMMA, ETokenType.ID, ENonTerminal.array_specifier]
   ],
   ASTNode.VariableDeclarationList.pool
 ),
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1405f8b and b27c4bd.

⛔ Files ignored due to path filters (1)
  • tests/src/shader-lab/shaders/demo.shader is excluded by !**/*.shader
📒 Files selected for processing (10)
  • packages/shader-lab/src/codeGen/CodeGenVisitor.ts (1 hunks)
  • packages/shader-lab/src/codeGen/GLES300.ts (1 hunks)
  • packages/shader-lab/src/common/types.ts (1 hunks)
  • packages/shader-lab/src/lalr/CFG.ts (1 hunks)
  • packages/shader-lab/src/lalr/Utils.ts (0 hunks)
  • packages/shader-lab/src/parser/AST.ts (3 hunks)
  • packages/shader-lab/src/parser/GrammarSymbol.ts (1 hunks)
  • packages/shader-lab/src/parser/SemanticAnalyzer.ts (2 hunks)
  • packages/shader-lab/src/parser/TargetParser.y (1 hunks)
  • packages/shader-lab/src/parser/builtin/functions.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • packages/shader-lab/src/lalr/Utils.ts
🧰 Additional context used
🪛 Biome (1.9.4)
packages/shader-lab/src/parser/SemanticAnalyzer.ts

[error] 73-73: This code is unreachable

... because this statement will return from the function beforehand

(lint/correctness/noUnreachable)

🔇 Additional comments (15)
packages/shader-lab/src/parser/AST.ts (3)

248-251: Confirm error handling for arrays of arrays.

In the error handling logic, the code checks for arrays of arrays and triggers an error if both arraySpecifier and this.arraySpecifier are present. Ensure that this logic correctly identifies and handles such cases, and verify that the error message is informative and aligns with user expectations.


390-392: Accessor method arraySpecifier appears correct.

The addition of the arraySpecifier getter in the TypeSpecifier class correctly returns the expected child node as an ArraySpecifier. This enhances code readability and maintainability.


1421-1423: Ensure consistent semicolon handling in code generation.

In the codeGen method of VariableDeclaration, a semicolon is appended to the result of visitor.visitVariableDeclaration(this). Verify that visitor.visitVariableDeclaration does not already include a semicolon to prevent double semicolons in the generated code.

Run the following script to check for duplicate semicolons in the code generation:

✅ Verification successful

Let me check the visitor implementation to see how it handles variable declarations.


Let me try a different approach to find the visitor implementation.


Let me check the implementation of visitVariableDeclaration in CodeGenVisitor.ts.


No double semicolon issue found in variable declaration code generation

The implementation shows that visitVariableDeclaration in CodeGenVisitor uses defaultCodeGen which simply concatenates the code from child nodes without adding any semicolons. The semicolon is only added once in the codeGen method of the VariableDeclaration AST node, so there's no risk of duplicate semicolons.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for instances where semicolons may be duplicated in variable declarations.

# Test: Look for ';;' patterns resulting from concatenation.
rg -A 2 -B 2 $'visitVariableDeclaration\(this\).+;'

Length of output: 368


Script:

#!/bin/bash
# Search for visitVariableDeclaration implementation in the codebase
ast-grep --pattern 'visitVariableDeclaration($_) {
  $$$
}'

Length of output: 61


Script:

#!/bin/bash
# Search for CodeGenVisitor class and its implementation
rg -A 10 "class.*CodeGenVisitor" 

# Also search for any file containing visitVariableDeclaration
fd -e ts -e js -x rg -l "visitVariableDeclaration"

Length of output: 2013


Script:

#!/bin/bash
# Search for visitVariableDeclaration implementation in CodeGenVisitor
rg -A 10 "visitVariableDeclaration" packages/shader-lab/src/codeGen/CodeGenVisitor.ts

Length of output: 537

packages/shader-lab/src/parser/SemanticAnalyzer.ts (2)

27-27: Update the errors property documentation and usage.

The errors property type has been updated from GSError[] to Error[]. Ensure that all references and handling of this property throughout the codebase are updated accordingly to reflect the new type.


73-73: Align with static analysis tool recommendations.

Static analysis indicates that the code at line 73 is unreachable. Review the conditional compilation directives to ensure that the code is reachable and functions as intended.

Run the following script to identify unreachable code within the file:

🧰 Tools
🪛 Biome (1.9.4)

[error] 73-73: This code is unreachable

... because this statement will return from the function beforehand

(lint/correctness/noUnreachable)

packages/shader-lab/src/parser/GrammarSymbol.ts (1)

13-14: New enum members are properly added to ENonTerminal.

The addition of variable_declaration_list and variable_declaration_statement to the ENonTerminal enum enhances the grammar's ability to handle variable declarations. Ensure that these new symbols are integrated consistently throughout the parser and that all related parsing logic is updated accordingly.

packages/shader-lab/src/codeGen/GLES300.ts (1)

4-4: Unused import ShaderPosition has been correctly removed.

The removal of the unused import statement for ShaderPosition cleans up the code and eliminates unnecessary dependencies, improving maintainability.

packages/shader-lab/src/common/types.ts (1)

135-135: LGTM! Addition of SEMICOLON token

The addition of the SEMICOLON token is well-documented and properly placed within the enum, supporting the new variable list declaration syntax.

packages/shader-lab/src/codeGen/CodeGenVisitor.ts (1)

Line range hint 128-133: LGTM! Method rename improves flexibility

The rename from visitGlobalVariableDeclaration to visitVariableDeclaration makes the method more generic while maintaining its functionality.

Let's verify that all callers have been updated to use the new method name:

✅ Verification successful

Method rename is correctly implemented and all references are updated

The verification shows that:

  1. No references to the old method name visitGlobalVariableDeclaration exist in the codebase
  2. The new method name visitVariableDeclaration is properly used in both:
    • The implementation file (CodeGenVisitor.ts)
    • The AST definition file (AST.ts) where it's being called
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any remaining references to the old method name
rg "visitGlobalVariableDeclaration" -l

# Search for references to the new method name to ensure it's being used
rg "visitVariableDeclaration" -A 2

Length of output: 613

packages/shader-lab/src/parser/TargetParser.y (2)

77-81: LGTM! Well-structured variable list declaration rule

The new variable_declaration_list rule correctly implements the comma-separated list syntax, supporting both simple identifiers and array declarations. This enables the target syntax: vec2 weights[2], offsets[2];


67-67: LGTM! Clean grammar restructuring

The grammar changes are well-organized:

  1. Separated core variable declaration from statement termination
  2. Made variable declarations reusable in lists
  3. Properly integrated with global declarations

Let's verify the grammar doesn't introduce any conflicts:

Also applies to: 72-74, 83-84

✅ Verification successful

The previous attempt failed because we couldn't locate the parser output file. Let's try to understand the grammar structure and potential conflicts by examining the grammar files directly. This will help us verify if the variable declaration changes are properly integrated.


The previous output showed multiple grammar files but we need to specifically examine the variable declaration rules and their integration with other productions to verify the grammar structure. Let's analyze the specific grammar rules and any potential conflict resolution directives.


Grammar structure verified - no conflicts detected

The grammar changes for variable declarations are well-structured and safe:

  • Variable declarations follow a clear hierarchical structure:
    • variable_declaration defines basic form (type + id + optional array)
    • variable_declaration_list allows comma-separated declarations
    • variable_declaration_statement properly terminates with semicolon
  • Integration points are clean:
    • Used in global_declaration without ambiguity
    • Separate from but compatible with struct declarations
    • Properly integrated with statement and declaration productions

The grammar restructuring maintains a clear separation of concerns and follows standard parser design patterns without introducing ambiguity in the productions.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any shift/reduce or reduce/reduce conflicts in the grammar
rg "conflict|warning" packages/shader-lab/src/parser/TargetParser.output

# Check for any error handling related to the new syntax
rg "error.*variable_declaration" -A 2

Length of output: 205


Script:

#!/bin/bash
# First, let's check if the parser output file exists in a different location
fd TargetParser.output

# Let's examine the build process to see where parser outputs might be generated
fd -e y -e yacc -e grammar --exec cat {} \; | grep -i "output"

# Let's check for any existing grammar files and their content
fd -e y -e yacc -e grammar --exec head -n 20 {} \;

Length of output: 1090


Script:

#!/bin/bash
# Let's examine the grammar file content more specifically around variable declarations
rg -A 5 -B 5 "variable_declaration" packages/shader-lab/src/parser/TargetParser.y

# Let's also check for any related productions and rules
rg "statement|declaration" packages/shader-lab/src/parser/TargetParser.y

# Check if there are any error productions or conflict resolution directives
rg "%prec|%left|%right|%nonassoc|error" packages/shader-lab/src/parser/TargetParser.y

Length of output: 2369

packages/shader-lab/src/parser/builtin/functions.ts (2)

295-295: LGTM! Function signature matches GLSL spec.

The textureCubeLodEXT function is correctly defined with appropriate parameter types (GSamplerCube, VEC3, FLOAT) and return value (GVec4).


289-290: LGTM! Verify WebGL extension availability.

The texture2DLodEXT functions are correctly defined with appropriate parameter types and return values. These functions are typically part of the GL_EXT_shader_texture_lod extension.

✅ Verification successful

Extension dependency is properly handled

The texture2DLodEXT functions are correctly supported as the GL_EXT_shader_texture_lod extension is already enabled in both the shader-lab and core packages:

  • packages/shader-lab/src/codeGen/GLES100.ts
  • packages/core/src/shaderlib/ShaderFactory.ts
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for extension enabling code
rg -A 2 "GL_EXT_shader_texture_lod|WEBGL_shader_texture_lod"

Length of output: 519

packages/shader-lab/src/lalr/CFG.ts (2)

19-19: LGTM! Grammar rule updated for variable list support.

The change from variable_declaration to variable_declaration_statement aligns with the PR objective to support variable list declarations.


27-28: LGTM! Variable declaration rule properly decomposed.

The variable_declaration rule has been correctly simplified to handle basic declaration components, allowing it to be used within a variable list. The semicolon is appropriately moved to the statement level.

packages/shader-lab/src/parser/SemanticAnalyzer.ts Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (3)
packages/shader-lab/src/parser/SemanticAnalyzer.ts (1)

28-28: Consider implications of broadening error type.

The change from GSError[] to Error[] makes the error collection more generic, but might lose shader-specific error information. Consider using a union type Array<GSError | Error> to maintain type safety while allowing flexibility.

packages/shader-lab/src/parser/AST.ts (2)

237-253: Enhance error message for array validation.

While the array validation is good, the error message could be more descriptive to help developers understand the limitations and alternatives.

- sa.error(arraySpecifier.location, "Array of array is not supported.");
+ sa.error(arraySpecifier.location, "Nested arrays (array of arrays) are not supported in ShaderLab. Consider using a single array with a larger size.");

1405-1422: Consider moving semicolon handling to the visitor.

The semicolon appending in codeGen might be better handled in the visitor to maintain consistent statement termination across all AST nodes.

- return visitor.visitVariableDeclaration(this) + ";";
+ return visitor.visitVariableDeclaration(this);

Then handle the semicolon in the visitor:

visitVariableDeclaration(node: VariableDeclaration): string {
  return this.generateVariableDeclaration(node) + ";";
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b27c4bd and 6f1fa10.

⛔ Files ignored due to path filters (1)
  • tests/src/shader-lab/shaders/demo.shader is excluded by !**/*.shader
📒 Files selected for processing (3)
  • packages/shader-lab/src/common/BaseScanner.ts (1 hunks)
  • packages/shader-lab/src/parser/AST.ts (6 hunks)
  • packages/shader-lab/src/parser/SemanticAnalyzer.ts (3 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
packages/shader-lab/src/parser/SemanticAnalyzer.ts

[error] 74-74: This code is unreachable

... because this statement will return from the function beforehand

(lint/correctness/noUnreachable)

🔇 Additional comments (2)
packages/shader-lab/src/parser/SemanticAnalyzer.ts (1)

68-76: ⚠️ Potential issue

Fix unreachable code in error handling.

The current implementation has unreachable code in non-verbose mode due to the conditional compilation structure.

Apply this diff to fix the issue:

  error(loc: ShaderRange, message: string) {
    // #if _VERBOSE
    const err = new GSError(GSErrorName.CompilationError, message, loc, ShaderLab._processingPassText);
    this.errors.push(err);
    return err;
    // #else
-   Logger.error(message);
+   const err = new Error(message);
+   Logger.error(message);
+   return err;
    // #endif
  }

Likely invalid or redundant comment.

🧰 Tools
🪛 Biome (1.9.4)

[error] 74-74: This code is unreachable

... because this statement will return from the function beforehand

(lint/correctness/noUnreachable)

packages/shader-lab/src/common/BaseScanner.ts (1)

120-120: LGTM! Improved safety in comment parsing.

The addition of !this.isEnd() check is a crucial safety improvement that prevents infinite loops when parsing comments at the end of the source string.

packages/shader-lab/src/parser/AST.ts Outdated Show resolved Hide resolved
@Sway007 Sway007 marked this pull request as draft December 18, 2024 03:00
@Sway007 Sway007 marked this pull request as ready for review December 26, 2024 01:48
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
packages/shader-lab/src/parser/AST.ts (2)

1126-1127: Consider moving semicolon handling to the visitor.

The semicolon is currently hardcoded in the codeGen method. Consider moving this responsibility to the CodeGenVisitor for better separation of concerns.

-  return visitor.visitGlobalVariableDeclaration(this) + ";";
+  return visitor.visitGlobalVariableDeclaration(this);

Then update the visitor to handle semicolons consistently.

Also applies to: 1139-1141


1143-1169: Well-structured implementation with good separation of concerns.

The implementation nicely separates:

  1. Grammar rules (in TargetParser.y)
  2. AST node definitions (in AST.ts)
  3. Semantic analysis (in node classes)
  4. Code generation (via visitor pattern)

This makes the code maintainable and extensible.

Consider documenting the design decisions and validation rules in the README or developer documentation.

Also applies to: 75-86

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 1163-1163: packages/shader-lab/src/parser/AST.ts#L1163
Added line #L1163 was not covered by tests

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6f1fa10 and 8b0de12.

📒 Files selected for processing (6)
  • packages/shader-lab/src/common/BaseScanner.ts (1 hunks)
  • packages/shader-lab/src/common/types.ts (1 hunks)
  • packages/shader-lab/src/lalr/CFG.ts (1 hunks)
  • packages/shader-lab/src/parser/AST.ts (3 hunks)
  • packages/shader-lab/src/parser/GrammarSymbol.ts (1 hunks)
  • packages/shader-lab/src/parser/TargetParser.y (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
  • packages/shader-lab/src/common/types.ts
  • packages/shader-lab/src/lalr/CFG.ts
  • packages/shader-lab/src/parser/GrammarSymbol.ts
  • packages/shader-lab/src/common/BaseScanner.ts
🧰 Additional context used
🪛 GitHub Check: codecov/patch
packages/shader-lab/src/parser/AST.ts

[warning] 200-201: packages/shader-lab/src/parser/AST.ts#L200-L201
Added lines #L200 - L201 were not covered by tests


[warning] 1163-1163: packages/shader-lab/src/parser/AST.ts#L1163
Added line #L1163 was not covered by tests

🔇 Additional comments (4)
packages/shader-lab/src/parser/TargetParser.y (1)

69-69: Grammar changes look good but need test coverage.

The grammar changes correctly implement variable list declarations with the following structure:

  • Modified global_declaration to use variable_declaration_statement
  • Added support for comma-separated declarations via variable_declaration_list
  • Properly handles array specifiers
  • Enforces semicolon termination

The implementation follows common shader language conventions, similar to GLSL.

Please ensure comprehensive test coverage for:

  1. Basic variable list declarations
  2. Array declarations in lists
  3. Error cases (e.g., mismatched types)

Also applies to: 75-76, 79-83, 85-86

✅ Verification successful

Let me check the test coverage in the identified files.


Let me try a different approach to check the test coverage.


Let me check the actual shader test files to see if they cover variable declarations.


Test coverage exists for variable declaration list syntax

Based on the analysis of the test files, particularly demo.shader, there is test coverage for the new variable declaration list syntax. The file includes:

  1. Basic variable list declarations:
vec2 v1, v2[2], v3[3];
  1. Array declarations in lists with initialization:
vec2 weights[2], offsets[2];
  1. Error cases are covered in compilation-error.shader which tests the parser's error handling capabilities.

The test files demonstrate that the grammar changes for variable declarations are being exercised through the existing test suite, including:

  • Comma-separated declarations
  • Array specifiers
  • Semicolon termination
  • Various variable types and contexts
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for existing test files
fd -e test.ts -e spec.ts . | grep -i "shader\|variable\|declaration"

Length of output: 181


Script:

#!/bin/bash
# Check test content for variable declaration tests
echo "=== Checking Shader.test.ts ==="
rg -A 5 "variable.*declaration|test.*variable" tests/src/core/Shader.test.ts

echo -e "\n=== Checking Preprocessor.test.ts ==="
rg -A 5 "variable.*declaration|test.*variable" tests/src/shader-lab/Preprocessor.test.ts

echo -e "\n=== Checking ShaderLab.test.ts ==="
rg -A 5 "variable.*declaration|test.*variable" tests/src/shader-lab/ShaderLab.test.ts

# Look for parser test files that might have been missed
echo -e "\n=== Checking for parser test files ==="
fd -e test.ts -e spec.ts . | grep -i "parser"

Length of output: 645


Script:

#!/bin/bash
# Search for test files specifically in shader-lab package
echo "=== Checking for parser tests in shader-lab ==="
fd -e test.ts -e spec.ts . packages/shader-lab/

# Check content of ShaderLab.test.ts for any parser-related tests
echo -e "\n=== Content of ShaderLab.test.ts ==="
cat tests/src/shader-lab/ShaderLab.test.ts

# Look for test files containing "parser" in their content
echo -e "\n=== Files containing parser tests ==="
rg -l "describe.*['\"]Parser|test.*['\"]Parser" -g "*.test.ts" -g "*.spec.ts"

Length of output: 10107


Script:

#!/bin/bash
# Check the content of test shader files for variable declarations
echo "=== Checking test shader files ==="
# First, find the shader files
fd -e shader . tests/src/shader-lab/shaders/

# Then check content of a few key shader files that might contain variable declarations
echo -e "\n=== Content of demo.shader ==="
cat tests/src/shader-lab/shaders/demo.shader

echo -e "\n=== Content of compilation-error.shader ==="
cat tests/src/shader-lab/shaders/compilation-error.shader

Length of output: 8260

packages/shader-lab/src/parser/AST.ts (3)

181-187: LGTM: SingleDeclaration changes handle array specifiers correctly.

The changes properly track and handle array specifiers in the type system.


199-201: Add test coverage for array validation.

The array validation logic is important but lacks test coverage according to static analysis.

Please add tests for the array validation logic, particularly the "Array of array is not supported" error case.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 200-201: packages/shader-lab/src/parser/AST.ts#L200-L201
Added lines #L200 - L201 were not covered by tests


1143-1169: Add type validation and improve test coverage.

The VariableDeclarationList implementation looks good but could be improved:

  1. Missing test coverage for variable list creation (line 1163)
  2. No type compatibility validation between variables in the list

Run the following to check existing test coverage:

#!/bin/bash
# Search for relevant test cases
rg -A 5 "VariableDeclarationList" . --type ts --type test
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 1163-1163: packages/shader-lab/src/parser/AST.ts#L1163
Added line #L1163 was not covered by tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working high priority High priority issue Rendering Rendering related functions shader Shader related functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant