Skip to content

Commit e3b3b11

Browse files
authored
fix: Separate preprocessor macro signature from expansion (#35)
1 parent e62d99b commit e3b3b11

File tree

2 files changed

+162
-1
lines changed

2 files changed

+162
-1
lines changed

src/parser.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,24 @@ function parsePreprocessor(tokens: Token[]): PreprocessorStatement {
626626
name = consume(tokens).value
627627

628628
if (name === 'define') {
629-
const left = parseExpression(tokens)
629+
const lhs = consume(tokens)
630+
let left: Expression = { type: 'Identifier', name: lhs.value }
631+
const next = tokens[0]
632+
633+
// Macro definition: #define foo(a, b, c) ...
634+
if (next && next.value === '(') {
635+
consume(tokens)
636+
637+
const args: Expression[] = []
638+
while (tokens[0]?.value !== ')') {
639+
args.push(parseExpression(tokens, 0))
640+
if (tokens[0]?.value !== ')') consume(tokens, ',')
641+
}
642+
consume(tokens, ')')
643+
644+
left = { type: 'CallExpression', callee: left, arguments: args }
645+
}
646+
630647
if (tokens[0]?.value === '\\') value = [left]
631648
else value = [left, parseExpression(tokens)]
632649
} else if (name === 'extension') {

tests/parser.test.ts

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,5 +1005,149 @@ describe('parser', () => {
10051005
value: null,
10061006
},
10071007
])
1008+
1009+
expect(parse('#define union3(a, b, c) min(a, min(b, c))\n').body).toStrictEqual<[PreprocessorStatement]>([
1010+
{
1011+
name: 'define',
1012+
type: 'PreprocessorStatement',
1013+
value: [
1014+
{
1015+
arguments: [
1016+
{
1017+
name: 'a',
1018+
type: 'Identifier',
1019+
},
1020+
{
1021+
name: 'b',
1022+
type: 'Identifier',
1023+
},
1024+
{
1025+
name: 'c',
1026+
type: 'Identifier',
1027+
},
1028+
],
1029+
callee: {
1030+
name: 'union3',
1031+
type: 'Identifier',
1032+
},
1033+
type: 'CallExpression',
1034+
},
1035+
{
1036+
arguments: [
1037+
{
1038+
name: 'a',
1039+
type: 'Identifier',
1040+
},
1041+
{
1042+
arguments: [
1043+
{
1044+
name: 'b',
1045+
type: 'Identifier',
1046+
},
1047+
{
1048+
name: 'c',
1049+
type: 'Identifier',
1050+
},
1051+
],
1052+
callee: {
1053+
name: 'min',
1054+
type: 'Identifier',
1055+
},
1056+
type: 'CallExpression',
1057+
},
1058+
],
1059+
callee: {
1060+
name: 'min',
1061+
type: 'Identifier',
1062+
},
1063+
type: 'CallExpression',
1064+
},
1065+
],
1066+
},
1067+
])
1068+
1069+
expect(parse('#define whiteComplement( a ) ( 1.0 - saturate( a ) )\n').body).toStrictEqual<[PreprocessorStatement]>(
1070+
[
1071+
{
1072+
name: 'define',
1073+
type: 'PreprocessorStatement',
1074+
value: [
1075+
{
1076+
arguments: [
1077+
{
1078+
name: 'a',
1079+
type: 'Identifier',
1080+
},
1081+
],
1082+
callee: {
1083+
name: 'whiteComplement',
1084+
type: 'Identifier',
1085+
},
1086+
type: 'CallExpression',
1087+
},
1088+
{
1089+
left: {
1090+
type: 'Literal',
1091+
value: '1.0',
1092+
},
1093+
operator: '-',
1094+
right: {
1095+
arguments: [
1096+
{
1097+
name: 'a',
1098+
type: 'Identifier',
1099+
},
1100+
],
1101+
callee: {
1102+
name: 'saturate',
1103+
type: 'Identifier',
1104+
},
1105+
type: 'CallExpression',
1106+
},
1107+
type: 'BinaryExpression',
1108+
},
1109+
],
1110+
},
1111+
],
1112+
)
1113+
1114+
expect(parse('#if defined( USE_UV ) || defined( USE_ANISOTROPY )\n').body).toStrictEqual<[PreprocessorStatement]>([
1115+
{
1116+
name: 'if',
1117+
type: 'PreprocessorStatement',
1118+
value: [
1119+
{
1120+
left: {
1121+
arguments: [
1122+
{
1123+
name: 'USE_UV',
1124+
type: 'Identifier',
1125+
},
1126+
],
1127+
callee: {
1128+
name: 'defined',
1129+
type: 'Identifier',
1130+
},
1131+
type: 'CallExpression',
1132+
},
1133+
operator: '||',
1134+
right: {
1135+
arguments: [
1136+
{
1137+
name: 'USE_ANISOTROPY',
1138+
type: 'Identifier',
1139+
},
1140+
],
1141+
callee: {
1142+
name: 'defined',
1143+
type: 'Identifier',
1144+
},
1145+
type: 'CallExpression',
1146+
},
1147+
type: 'LogicalExpression',
1148+
},
1149+
],
1150+
},
1151+
])
10081152
})
10091153
})

0 commit comments

Comments
 (0)