Skip to content

Commit f775ee5

Browse files
committed
feat: code opt
1 parent 6cd03dd commit f775ee5

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

packages/shader-lab/src/common/BaseScanner.ts

+13-18
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export type OnToken = (token: BaseToken, scanner: BaseScanner) => void;
1111
* @internal
1212
*/
1313
export default class BaseScanner {
14+
private static _spaceCharsWithBreak = [" ", "\t", "\n", "\r"];
1415
private static _spaceChars = [" ", "\t"];
1516
private static _checkIsIn(checked: string, chars: string[]): boolean {
1617
for (let i = 0; i < chars.length; i++) {
@@ -101,27 +102,17 @@ export default class BaseScanner {
101102
}
102103

103104
skipSpace(includeLineBreak: boolean): void {
104-
let curChar: string;
105-
106-
while (includeLineBreak) {
107-
const chars = this.peek(2);
108-
curChar = chars[0];
109-
110-
if (chars === "\r\n") {
111-
this.advance(2);
112-
} else if (curChar === "\n" || curChar === "\r") {
113-
this.advance(1);
114-
} else {
115-
break;
116-
}
117-
}
118-
119-
curChar = this.getCurChar();
120-
const spaceChars = BaseScanner._spaceChars;
105+
const spaceChars = includeLineBreak ? BaseScanner._spaceCharsWithBreak : BaseScanner._spaceChars;
106+
let curChar = this.getCurChar();
121107

122108
while (BaseScanner._checkIsIn(curChar, spaceChars)) {
123109
this._advance();
124110
curChar = this.getCurChar();
111+
// Compatible with windows line break CRLF.
112+
if (includeLineBreak && curChar === "\n") {
113+
this._advance();
114+
curChar = this.getCurChar();
115+
}
125116
}
126117
}
127118

@@ -131,7 +122,11 @@ export default class BaseScanner {
131122
const start = this.getCurPosition();
132123
this.advance(2);
133124
// single line comments
134-
while (this.getCurChar() !== "\n" && !this.isEnd()) this._advance();
125+
let curChar = this.getCurChar();
126+
while (curChar !== "\n" && curChar !== "\r" && !this.isEnd()) {
127+
this._advance();
128+
curChar = this.getCurChar();
129+
}
135130
this.skipCommentsAndSpace();
136131
return ShaderLab.createRange(start, this.getCurPosition());
137132
} else if (this.peek(2) === "/*") {

packages/shader-lab/src/parser/AST.ts

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ export abstract class TreeNode implements IPoolElement {
5151
return visitor.defaultCodeGen(this.children);
5252
}
5353

54+
/**
55+
* Do semantic analyze right after the ast node is generated.
56+
*/
5457
semanticAnalyze(sa: SematicAnalyzer) {}
5558
}
5659

0 commit comments

Comments
 (0)