Skip to content

Commit

Permalink
Merge pull request #22 from 06wj/main
Browse files Browse the repository at this point in the history
Adding export visitPreprocessedAst & Fixing preprocess defined without paren bug
  • Loading branch information
AndrewRayCode authored May 11, 2024
2 parents e4d4492 + 7401ded commit f9813f7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/preprocessor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
preprocessAst,
preprocessComments,
PreprocessorOptions,
visitPreprocessedAst,
} from './preprocessor.js';

// This index file is currently only for package publishing, where the whole
Expand All @@ -20,4 +21,4 @@ const preprocess = (src: string, options: PreprocessorOptions) =>

export default preprocess;

export { preprocessAst, preprocessComments, generate, preprocess, parser };
export { preprocessAst, preprocessComments, generate, preprocess, parser, visitPreprocessedAst };
14 changes: 13 additions & 1 deletion 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:__ { 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,6 +225,9 @@ unary_expression "unary expression"
= operator:DEFINED lp:LEFT_PAREN identifier:IDENTIFIER rp:RIGHT_PAREN {
return node('unary_defined', { operator, lp, identifier, rp, });
}
/ operator:DEFINED_WITH_END_WS identifier:IDENTIFIER {
return node('unary_defined', { operator, identifier});
}
/ operator:(PLUS / DASH / BANG / TILDE)
expression:unary_expression {
return node('unary', { operator, expression });
Expand Down Expand Up @@ -323,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
19 changes: 19 additions & 0 deletions src/preprocessor/preprocessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,25 @@ test('generate #ifdef & #ifndef & #else', () => {
`);
});


test('parse defined && defined() && definedXXX', () => {
const program = `
#if defined AAA && defined/**/BBB && defined/**/ CCC && definedXXX && defined(DDD)
#endif
`;
const ast = parse(program);
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);
});

/*
test('debug', () => {
const program = `
Expand Down
2 changes: 1 addition & 1 deletion src/preprocessor/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ type VisitorOverride = (
) => void;

// @ts-ignore
const visitPreprocessedAst = visit as VisitorOverride;
export const visitPreprocessedAst = visit as VisitorOverride;

type PathOverride<NodeType> = {
node: NodeType;
Expand Down

0 comments on commit f9813f7

Please sign in to comment.