From 7401ded5e063f4f9e28dd008bdb79746cb3ac293 Mon Sep 17 00:00:00 2001 From: 06wj <06wj@163.com> Date: Tue, 7 May 2024 11:30:07 +0800 Subject: [PATCH] Fixing bug in defined with comment --- src/preprocessor/preprocessor-grammar.pegjs | 12 +++++++-- src/preprocessor/preprocessor.test.ts | 30 ++++++++------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/preprocessor/preprocessor-grammar.pegjs b/src/preprocessor/preprocessor-grammar.pegjs index 610abeb..fca70f3 100644 --- a/src/preprocessor/preprocessor-grammar.pegjs +++ b/src/preprocessor/preprocessor-grammar.pegjs @@ -77,7 +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 }); } +DEFINED_WITH_END_WS = wsStart:_? token:"defined" wsEnd:__ { 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 }); } @@ -327,10 +327,18 @@ logical_or_expression "logical or expression" // I added this as a maybe entry point to expressions constant_expression "constant expression" = logical_or_expression +// Must have a space or a comment +__ "whitespace or comment" = w:whitespace rest:(comment whitespace?)* { + return collapse(w, rest); +} +/ c:comment rest:(whitespace comment?)* { + return collapse(c, rest); +} + // The whitespace is optional so that we can put comments immediately after // terminals, like void/* comment */ // The ending whitespace is so that linebreaks can happen after comments -_ "whitespace or comment" = w:whitespace? rest:(comment whitespace?)* { +_ "whitespace or comment or null" = w:whitespace? rest:(comment whitespace?)* { return collapse(w, rest); } diff --git a/src/preprocessor/preprocessor.test.ts b/src/preprocessor/preprocessor.test.ts index 0765cf5..910f98c 100644 --- a/src/preprocessor/preprocessor.test.ts +++ b/src/preprocessor/preprocessor.test.ts @@ -5,7 +5,6 @@ import { preprocessComments, preprocessAst, PreprocessorProgram, - visitPreprocessedAst, } from './preprocessor.js'; import generate from './generator.js'; import { GlslSyntaxError } from '../error.js'; @@ -479,28 +478,21 @@ test('generate #ifdef & #ifndef & #else', () => { }); -test('parse defined', () => { - expectParsedProgram(` -#if defined AAA && defined(BBB)&& definedCCC -#endif -`); -}); - -test('parse definedXXX', () => { +test('parse defined && defined() && definedXXX', () => { const program = ` -#if definedXXX +#if defined AAA && defined/**/BBB && defined/**/ CCC && definedXXX && defined(DDD) #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'); - } - } - } - }); + const astStr = JSON.stringify(ast); + expect(astStr.includes('"identifier":"definedXXX"')).toBeTruthy(); + expect(astStr.includes('"identifier":"AAA"')).toBeTruthy(); + expect(astStr.includes('"identifier":"BBB"')).toBeTruthy(); + expect(astStr.includes('"identifier":"CCC"')).toBeTruthy(); + expect(astStr.includes('"identifier":"DDD"')).toBeTruthy(); + expect(astStr.includes('"identifier":"XXX"')).toBeFalsy(); + expect(astStr.match(/unary_defined/g)?.length).toBe(4); + expectParsedProgram(program); });