From e447e3a52b22fa35e60339d75a1ebf19fdbd8422 Mon Sep 17 00:00:00 2001 From: Andrew Ray Date: Tue, 1 Jan 2019 15:18:30 -0800 Subject: [PATCH] Don't mark funciton argument struct names as declarations Fixes - Issue #3 - https://github.com/glslify/glsl-token-descope/issues/3 - https://github.com/stackgl/glsl-parser/issues/24 --- index.js | 13 +++++++++++++ test/arguments.js | 6 ++++++ 2 files changed, 19 insertions(+) diff --git a/index.js b/index.js index c7ae273..5968525 100644 --- a/index.js +++ b/index.js @@ -64,6 +64,13 @@ function assigns(tokens) { idx++ skipWhitespace(+1) if (tokens[idx].type !== 'ident') continue + + // Handle the case of a parameter like "in StructName myArg", where if + // there's one ident after another, assume the one we're on is the + // StructName, identified in the higher scope, and not a declaration + var nextNonWhitespace = peekPastWhitespace() + if (nextNonWhitespace && nextNonWhitespace.type === 'ident') continue + tokens[idx++].declaration = true skipWhitespace(+1) skipArrayDimensions() @@ -143,6 +150,12 @@ function assigns(tokens) { return tokens + function peekPastWhitespace() { + var peekIdx = idx + 1; + while (tokens[peekIdx] && tokens[peekIdx].type === 'whitespace') peekIdx++ + return tokens[peekIdx]; + } + function skipWhitespace(n) { while (tokens[idx] && tokens[idx].type === 'whitespace') idx++ } diff --git a/test/arguments.js b/test/arguments.js index c318301..a3c8ad3 100644 --- a/test/arguments.js +++ b/test/arguments.js @@ -7,6 +7,10 @@ float aFunction(vec2 a, vec3 b, float c) { return z; } +float sFunction(UserStruct structArg); +float sFunction(const UserStruct structArg); +float sFunction(const in UserStruct structArg); + Thing xFunction(vec2 d, Thing e[2], Thing f, Thing g[2][5]); Thing xFunction(vec2 d, Thing e[2], Thing f, Thing g[2][5]) { Thing y = Thing(2.0); @@ -34,5 +38,7 @@ Another yFunction( , i: [true] , j: [true] , k: [true] + , UserStruct: [false, false, false] + , structArg: [true, true, true] , Thing: new Array(10).map(() => false) }))