Why doesn't the preprocessor substitude a macro as a prefix of an event's name?

The place to post your WML questions and answers.

Moderator: Forum Moderators

Forum rules
  • Please use [code] BBCode tags in your posts for embedding WML snippets.
  • To keep your code readable so that others can easily help you, make sure to indent it following our conventions.
Post Reply
dwarftough
Posts: 582
Joined: August 4th, 2019, 5:27 pm
Contact:

Why doesn't the preprocessor substitude a macro as a prefix of an event's name?

Post by dwarftough »

Hi everyone! Got a question about how the preprocessor works.

Let's say I have the following code

Code: Select all

#DOESN'T WORK FOR SOME REASON
#define TEST
Test
#enddef

[event]
name={TEST} my event
...
[/event]
I expected that {TEST} will be macrosubstituted and I'll get an event with name "Test my event", instead I got an error during the preprocessing stage saying "Unexpected characters after variable name (expected , or =)" at the line with "name={TEST} my event".

It only works if I make {TEST} the suffix, so

Code: Select all

#works
#define TEST
Test
#enddef

[event]
name=my event {TEST}
...
[/event]
this works.

But if I put my event inside a macro with PREFIX being a macro-parameter instead of a full-fledged macro, it works in the prefix form too

Code: Select all

#somehow works too
#define MY_EVENTS PREFIX
[event]
name={PREFIX} first_event
...
[/event]
#enddef

{MY_EVENTS Test}
this works too.

I don't really understand why the first snippet doesn't work. I expected the preprocessor to substitude the macro independently of where it is in the line
Co-founder and current maintainer of IsarFoundation, Afterlife Rated and overall Wesnoth Autohost Project
MP versions of classical mainline campaigns: UtBS, TRoW, SotA
Developer and maintainer of my fork of World Conquest, Invincibles Conquest II
User avatar
Spannerbag
Posts: 761
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: Why doesn't the preprocessor substitude a macro as a prefix of an event's name?

Post by Spannerbag »

dwarftough wrote: August 22nd, 2024, 11:00 am Hi everyone! Got a question about how the preprocessor works.

Let's say I have the following code

Code: Select all

#DOESN'T WORK FOR SOME REASON
#define TEST
Test
#enddef
I think you're embedding a trailing carriage return in the macro.

Code: Select all

#DOESN'T WORK FOR SOME REASON
#define TEST
Test#enddef
Should work as you want.

Cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.18, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
dwarftough
Posts: 582
Joined: August 4th, 2019, 5:27 pm
Contact:

Re: Why doesn't the preprocessor substitude a macro as a prefix of an event's name?

Post by dwarftough »

Spannerbag wrote: August 22nd, 2024, 11:39 am

Code: Select all

#DOESN'T WORK FOR SOME REASON
#define TEST
Test#enddef
Should work as you want.
gnombat wrote: August 22nd, 2024, 12:35 pm This is documented here:

PreprocessorRef#Whitespace_in_Macros
Indeed, that works! Thanks a lot!
Co-founder and current maintainer of IsarFoundation, Afterlife Rated and overall Wesnoth Autohost Project
MP versions of classical mainline campaigns: UtBS, TRoW, SotA
Developer and maintainer of my fork of World Conquest, Invincibles Conquest II
Post Reply