refactor: decompose macro parser into dedicated parsing stages #3
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
ae2f/Preproc!3
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "watta0/Preproc:main"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Refactor the macro parser by decomposing the monolithic parsing logic into dedicated parsing stages and introducing a clearer state management model.
This change significantly improves readability, maintainability, and future extensibility while preserving the original behavior of the parser.
Motivation
The previous implementation contained most parsing logic inside
main()and relied heavily on labels and cross-scope jumps. While functional, the control flow was difficult to follow and made future modifications risky.This refactor aims to:
main()Changes
Parser decomposition
Split the parser into dedicated functions:
s_parse_book()ae2f_MAC(sequence.s_parse_tparam()s_parse_fnname()s_parse_params()s_parse_body()s_parse_include()#includedirectives.s_parse_string()s_parse_else()State management cleanup
Replaced anonymous state values with explicit enums:
This makes parser states and error conditions self-documenting and removes magic numbers from the implementation.
Naming improvements
Renamed several macros and constants to follow a more descriptive naming convention.
Examples:
SZPARAM→PARAM_LENSZTPARAM→TPARAM_LENisAlph()→IS_ALPHA()isNumber()→IS_NUMBER()isVarName()→IS_VARNAME()isNewLine()→IS_NEWLINE()Flag operations were also renamed:
isTrue()→COMPARE_FLAG()setTrue()→SET_FLAG_TRUE()setFalse()→SET_FLAG_FALSE()Unified error handling
Introduced helper macros:
Benefits:
Buffer and constant cleanup
Introduced clearer length-related constants:
and removed several ad-hoc size calculations from parsing logic.
Improved separation of concerns
main()now acts primarily as a dispatcher:This significantly reduces the amount of logic embedded in the main execution loop.
Benefits
Notes
This refactor is intended to be behavior-preserving. No functional changes to the generated macro output are expected.
@ -332,4 +334,0 @@return 1;casenewlines:ENDL[2] = (char) ch;ENDL is used only here and modified. Declare this variable as under
s_parse_body.got it!
@ -1,22 +1,21 @@#define _CRT_SECURE_NO_WARNINGS 1#include <.main.h>#include <assert.h>That header is used nowhere.
Order received!
@ -18,2 +16,2 @@#define BOOK (ae2f_MAC_KEYWORD "(")#define STRLEN(a) ((int)(sizeof(a) - 1))#define BOOK (ae2f_MAC_KEYWORD "(")#define BOOK_LEN ((size_t)(sizeof(BOOK) - (size_t)1))Duplicated. Would you utilise STRLEN to make BOOK_LEN?
Got it!
@ -66,0 +46,4 @@#define __RET_IF_FAILED(ret_var, expr) \do { \(ret_var) = (expr); \if ((ret_var) != RET_STATE_OK) \same as #3/files (comment)
Got it
@ -66,0 +53,4 @@#define __RET_IF_EOF(expr) \do { \if ((expr) == EOF) \return RET_STATE_OUTFAILED; \enum definition is below this macro. move that please.
Got it
@ -66,0 +80,4 @@static enum RET_STATE_ s_parse_fnname (flags_t *rdwr_flags);static enum RET_STATE_ s_parse_params (flags_t *rdwr_flags);static enum RET_STATE_ s_parse_body (flags_t *rdwr_flags);static enum RET_STATE_ s_parse_else (int ch);This name does not explain what is this else of.
I suggest
s_parse_fallbackofs_parse_defaultGot it
@ -94,1 +99,3 @@return STATE_OUTFAILED;switch (ch) {case '\"':__RET_IF_FAILED(ret, s_parse_string(ch));You know what
chis. Using literal would be helpful.Order received
@ -95,0 +101,4 @@__RET_IF_FAILED(ret, s_parse_string(ch));break;case '#':__RET_IF_FAILED(ret, s_parse_include(ch));Same as #3/files (comment)
Got it
@ -289,2 +308,2 @@PARAMS_BUF[prms_len] = (char) ch;PARAMS_BUF[prms_len + 1] = 0;buf[buf_len] = (char)ch;buf[buf_len + 1] = '\0';That needs former length test
got it
@ -303,2 +315,2 @@PARAMS_BUF[prms_len + 4] = 0;}if (ch == ')') {buf[buf_len] = ' ';That is unnecessary as I see now.
This was my mistake and you seem to be shipping it as is.
Putting
putsas is and printing this statement later would be more stable.got it
It is not working.