diff --git a/src/preprocessor/preprocessor-grammar.pegjs b/src/preprocessor/preprocessor-grammar.pegjs index c3f9094..610abeb 100644 --- a/src/preprocessor/preprocessor-grammar.pegjs +++ b/src/preprocessor/preprocessor-grammar.pegjs @@ -77,6 +77,7 @@ UNDEF = wsStart:_? token:"#undef" wsEnd:_? { return node('literal', { literal: t ERROR = wsStart:_? token:"#error" wsEnd:_? { return node('literal', { literal: token, wsStart, wsEnd }); } PRAGMA = wsStart:_? token:"#pragma" wsEnd:_? { return node('literal', { literal: token, wsStart, wsEnd }); } DEFINED = wsStart:_? token:"defined" wsEnd:_? { return node('literal', { literal: token, wsStart, wsEnd }); } +DEFINED_WITH_END_WS = wsStart:_? token:"defined" wsEnd:whitespace { return node('literal', { literal: token, wsStart, wsEnd }); } IF = wsStart:_? token:"#if" wsEnd:_? { return node('literal', { literal: token, wsStart, wsEnd }); } IFDEF = wsStart:_? token:"#ifdef" wsEnd:_? { return node('literal', { literal: token, wsStart, wsEnd }); } IFNDEF = wsStart:_? token:"#ifndef" wsEnd:_? { return node('literal', { literal: token, wsStart, wsEnd }); } @@ -224,8 +225,8 @@ unary_expression "unary expression" = operator:DEFINED lp:LEFT_PAREN identifier:IDENTIFIER rp:RIGHT_PAREN { return node('unary_defined', { operator, lp, identifier, rp, }); } - / operator:DEFINED identifier:IDENTIFIER { - return node('unary_defined', { operator, identifier, }); + / operator:DEFINED_WITH_END_WS identifier:IDENTIFIER { + return node('unary_defined', { operator, identifier}); } / operator:(PLUS / DASH / BANG / TILDE) expression:unary_expression { diff --git a/src/preprocessor/preprocessor.test.ts b/src/preprocessor/preprocessor.test.ts index 9bd62df..0765cf5 100644 --- a/src/preprocessor/preprocessor.test.ts +++ b/src/preprocessor/preprocessor.test.ts @@ -5,6 +5,7 @@ import { preprocessComments, preprocessAst, PreprocessorProgram, + visitPreprocessedAst, } from './preprocessor.js'; import generate from './generator.js'; import { GlslSyntaxError } from '../error.js'; @@ -477,13 +478,32 @@ test('generate #ifdef & #ifndef & #else', () => { `); }); -test('generate defined', () => { + +test('parse defined', () => { expectParsedProgram(` -#if defined AAA && defined(BBB) +#if defined AAA && defined(BBB)&& definedCCC #endif `); }); +test('parse definedXXX', () => { + const program = ` +#if definedXXX +#endif +`; + const ast = parse(program); + visitPreprocessedAst(ast, { + conditional: { + enter: function (path) { + if (path.node.ifPart.type === 'if') { + expect(path.node.ifPart.expression.type).toBe('identifier'); + } + } + } + }); + expectParsedProgram(program); +}); + /* test('debug', () => { const program = `