Skip to content

Commit

Permalink
Fixing bug in defined with comment
Browse files Browse the repository at this point in the history
  • Loading branch information
06wj committed May 7, 2024
1 parent 15b399f commit 7401ded
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
12 changes: 10 additions & 2 deletions src/preprocessor/preprocessor-grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }); }
Expand Down Expand Up @@ -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);
}

Expand Down
30 changes: 11 additions & 19 deletions src/preprocessor/preprocessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
preprocessComments,
preprocessAst,
PreprocessorProgram,
visitPreprocessedAst,
} from './preprocessor.js';
import generate from './generator.js';
import { GlslSyntaxError } from '../error.js';
Expand Down Expand Up @@ -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);
});

Expand Down

0 comments on commit 7401ded

Please sign in to comment.