Skip to content

Commit 22e9a72

Browse files
committed
Fix glsl attribute parsing error
1 parent 14b40e5 commit 22e9a72

2 files changed

Lines changed: 99 additions & 11 deletions

File tree

src/main/java/net/vulkanmod/vulkan/shader/converter/GLSLParser.java

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,14 @@ private void parseUniformBlock() {
326326
private void parseAttribute() {
327327
this.state = State.ATTRIBUTE;
328328

329+
Node prevNode = this.prevNode(true);
330+
331+
// Check if we are not inside a function declaration
332+
if (prevNode != null && (prevNode.type.equals(Token.TokenType.LEFT_PARENTHESIS.name()) || prevNode.type.equals(Token.TokenType.COMMA.name())))
333+
{
334+
return;
335+
}
336+
329337
String ioType = this.currentToken.value;
330338

331339
advanceToken(true);
@@ -445,19 +453,75 @@ private void advanceToken(boolean skipSpace) {
445453
}
446454
}
447455

448-
private Token nextToken(int i) {
449-
return this.tokens.get(this.currentTokenIdx + i);
456+
private Token prevToken(boolean skipSpace) {
457+
int tokenIdx = this.currentTokenIdx - 1;
458+
Token token;
459+
460+
if (tokenIdx == 0) {
461+
return null;
462+
}
463+
464+
tokenIdx--;
465+
token = this.tokens.get(tokenIdx);
466+
467+
while (skipSpace && tokenIdx != 0 &&
468+
(token.type == Token.TokenType.SPACING || token.type == Token.TokenType.PREPROCESSOR || token.type == Token.TokenType.COMMENT))
469+
{
470+
tokenIdx--;
471+
token = this.tokens.get(tokenIdx);
472+
}
473+
474+
if (skipSpace && (token.type == Token.TokenType.SPACING || token.type == Token.TokenType.COMMENT || token.type == Token.TokenType.PREPROCESSOR)) {
475+
return null;
476+
}
477+
478+
return token;
479+
}
480+
481+
private Node prevNode(boolean skipSpace) {
482+
var nodes = getNodeStream();
483+
int idx = nodes.size() - 1;
484+
String type;
485+
486+
if (idx == 0) {
487+
return null;
488+
}
489+
490+
idx--;
491+
Node node;
492+
node = nodes.get(idx);
493+
type = node.type;
494+
495+
while (skipSpace && idx != 0 &&
496+
(type.equals(Token.TokenType.SPACING.name()) || type.equals(Token.TokenType.PREPROCESSOR.name()) || type.equals(Token.TokenType.COMMENT.name())))
497+
{
498+
idx--;
499+
node = nodes.get(idx);
500+
type = node.type;
501+
}
502+
503+
if (skipSpace &&
504+
(type.equals(Token.TokenType.SPACING.name()) || type.equals(Token.TokenType.PREPROCESSOR.name()) || type.equals(Token.TokenType.COMMENT.name())))
505+
{
506+
return null;
507+
}
508+
509+
return node;
450510
}
451511

452512
private void appendToken(Token token) {
453513
this.appendNode(Node.fromToken(token));
454514
}
455515

456516
private void appendNode(Node node) {
457-
switch (this.stage) {
458-
case VERTEX -> this.vsStream.add(node);
459-
case FRAGMENT -> this.fsStream.add(node);
460-
}
517+
this.getNodeStream().add(node);
518+
}
519+
520+
private LinkedList<Node> getNodeStream() {
521+
return switch (this.stage) {
522+
case VERTEX -> this.vsStream;
523+
case FRAGMENT -> this.fsStream;
524+
};
461525
}
462526

463527
public String getOutput(Stage stage) {
@@ -565,7 +629,7 @@ public Node(String type, String value) {
565629
}
566630

567631
public static Node fromToken(Token token) {
568-
return new Node("token:%s".formatted(token.type), token.value);
632+
return new Node(token.type.name(), token.value);
569633
}
570634

571635
@Override

src/main/java/net/vulkanmod/vulkan/shader/converter/Lexer.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,13 @@ public Token nextToken() {
6666

6767
// Comment
6868
if (currentChar == '/') {
69-
if (peek() == '/') {
70-
advance(2);
71-
return this.comment();
69+
switch (peek()) {
70+
case '/' -> {
71+
return this.lineComment();
72+
}
73+
case '*' -> {
74+
return this.multiLineComment();
75+
}
7276
}
7377
}
7478

@@ -168,9 +172,11 @@ public Token nextToken() {
168172
return token;
169173
}
170174

171-
private Token comment() {
175+
private Token lineComment() {
172176
StringBuilder sb = new StringBuilder();
173177
sb.append("//");
178+
this.advance(2);
179+
174180
while (checkEOF() && currentChar != '\n') {
175181
sb.append(currentChar);
176182
advance();
@@ -182,6 +188,24 @@ private Token comment() {
182188
return new Token(Token.TokenType.COMMENT, value);
183189
}
184190

191+
private Token multiLineComment() {
192+
StringBuilder sb = new StringBuilder();
193+
sb.append("/*");
194+
this.advance(2);
195+
196+
while (checkEOF() && currentChar != '*' && this.peek() != '/') {
197+
sb.append(currentChar);
198+
advance();
199+
}
200+
sb.append(currentChar);
201+
advance();
202+
sb.append(currentChar);
203+
advance();
204+
205+
String value = sb.toString();
206+
return new Token(Token.TokenType.COMMENT, value);
207+
}
208+
185209
private Token identifier() {
186210
StringBuilder sb = new StringBuilder();
187211
while (checkEOF() && Character.isJavaIdentifierPart(currentChar)) {

0 commit comments

Comments
 (0)