Skip to content

Commit

Permalink
Fixing defined bug
Browse files Browse the repository at this point in the history
  • Loading branch information
06wj committed May 6, 2024
1 parent 4306978 commit 15b399f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/preprocessor/preprocessor-grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }); }
Expand Down Expand Up @@ -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 {
Expand Down
24 changes: 22 additions & 2 deletions src/preprocessor/preprocessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
preprocessComments,
preprocessAst,
PreprocessorProgram,
visitPreprocessedAst,
} from './preprocessor.js';
import generate from './generator.js';
import { GlslSyntaxError } from '../error.js';
Expand Down Expand Up @@ -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 = `
Expand Down

0 comments on commit 15b399f

Please sign in to comment.