Skip to content

Commit

Permalink
Fix editor conditional compilation error of ShaderLab (#2227)
Browse files Browse the repository at this point in the history
* fix: editor conditional compilation error of ShaderLab
  • Loading branch information
Sway007 authored Jul 16, 2024
1 parent 2ae6905 commit d15dcd3
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 63 deletions.
7 changes: 1 addition & 6 deletions packages/shader-lab/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
"debug": "src/index.ts",
"types": "types/index.d.ts",
"scripts": {
"b:types": "tsc",
"gen_diagram": "ts-node ./scripts/genDiagram.ts",
"gen_dts": "ts-node ./scripts/genDts.ts"
"b:types": "tsc"
},
"umd": {
"name": "Galacean.ShaderLab",
Expand All @@ -26,9 +24,6 @@
"dist/**/*",
"types/**/*"
],
"dependencies": {
"chevrotain": "^10.5.0"
},
"devDependencies": {
"@galacean/engine-design": "workspace:*",
"@galacean/engine": "workspace:*"
Expand Down
2 changes: 1 addition & 1 deletion packages/shader-lab/src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { EKeyword, ETokenType, GalaceanDataType, ShaderRange, ShaderPosition } f
import { TreeNode } from "./parser/AST";
// #if _EDITOR
import State from "./lalr/State";
import { Logger } from "@galacean/engine";
// #endif
import { Logger } from "@galacean/engine";

export class ParserUtils {
static unwrapNodeByType<T = TreeNode>(node: TreeNode, type: ENonTerminal): T | undefined {
Expand Down
3 changes: 1 addition & 2 deletions packages/shader-lab/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
export { ShaderLab } from "./ShaderLab";

// #if _EDITOR
import { Preprocessor } from "./preprocessor";
export { Preprocessor };
export { Preprocessor } from "./preprocessor";
// #endif

//@ts-ignore
Expand Down
4 changes: 1 addition & 3 deletions packages/shader-lab/src/preprocessor/PpParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,16 +605,14 @@ export default class PpParser {
}

private static _onToken(token: BaseToken, scanner: PpScanner) {
// #if !_EDITOR
this._skipEditorBlock(token, scanner);
// #endif
this._expandToken(token, scanner);
}

private static _skipEditorBlock(token: BaseToken, scanner: PpScanner) {
if (token.lexeme === "EditorProperties" || token.lexeme === "EditorMacros") {
const start = scanner.current - token.lexeme.length;
scanner.scanPairedBlock();
scanner.scanPairedBlock("{", "}");
const end = scanner.current;
const startPosition = ShaderLab.createPosition(start);
const endPosition = ShaderLab.createPosition(end);
Expand Down
24 changes: 15 additions & 9 deletions packages/shader-lab/src/preprocessor/PpScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default class PpScanner extends BaseScanner {
if (this.isEnd()) return EOF;

const start = this._currentIndex;
while (LexerUtils.isLetter(this.getCurChar())) {
while (LexerUtils.isLetter(this.getCurChar()) && !this.isEnd()) {
this.advance();
}
const end = this._currentIndex;
Expand Down Expand Up @@ -161,7 +161,8 @@ export default class PpScanner extends BaseScanner {
}

scanToChar(char: string) {
while (this.getCurChar() !== char && !this.isEnd()) {
const source = this._source;
while (source[this._currentIndex] !== char && !this.isEnd()) {
this.advance();
}
}
Expand Down Expand Up @@ -189,17 +190,22 @@ export default class PpScanner extends BaseScanner {
return { token, nextDirective: directive };
}

// #if !_EDITOR
scanPairedBlock(lc = "{", rc = "}") {
scanPairedBlock(lc: string, rc: string): void {
this.scanToChar(lc);
let lvl = 0;
let level = 0;
const source = this._source;

do {
if (this.getCurChar() === lc) lvl += 1;
else if (this.getCurChar() === rc) lvl -= 1;
const curChar = source[this._currentIndex];

if (curChar === lc) {
level++;
} else if (curChar === rc) {
level--;
}
this._advance();
} while (lvl > 0);
} while (level > 0);
}
// #endif

/**
* @returns end ShaderPosition
Expand Down
43 changes: 1 addition & 42 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d15dcd3

Please sign in to comment.