Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle preprocessor directives #13

Closed
11 of 12 tasks
kymckay opened this issue May 2, 2021 · 4 comments
Closed
11 of 12 tasks

Handle preprocessor directives #13

kymckay opened this issue May 2, 2021 · 4 comments
Labels
enhancement New feature or request
Milestone

Comments

@kymckay
Copy link
Owner

kymckay commented May 2, 2021

I probably need to introduce a lexical preprocessor to truly handle these since they'd be resolved before the sqf lexer takes over

  • #include
  • #define MACRO value
  • #define MACRO_FUNC(ARG1, ARG2, ...)
  • # (stringification)
  • ## (token concatenation)
  • \ (exactly before a newline, multi-line definition)
  • #undef
  • #if
  • #ifdef
  • #ifndef
  • #else
  • #endif
@kymckay kymckay added the enhancement New feature or request label May 2, 2021
@kymckay kymckay added this to To do in Language Support May 8, 2021
@kymckay
Copy link
Owner Author

kymckay commented May 10, 2021

One idea:

  1. Implement the preprocessor as an input stream wrapper (the lexer can then consume a preprocessor or just a bare stream as it currently dose)
  2. As the lexer consumes the preprocessor it does its thing (e.g. resolve macros to an internal buffer) and then returns the next character post-preprocessing

@kymckay
Copy link
Owner Author

kymckay commented May 12, 2021

On further consideration it may make sense to just extend the lexer itself to be "preprocessor aware" and perform tokenisation as-if preprocessing happens first.

One consideration for preprocessing is the position of reported errors. Some inspiration may be taken from the C preprocessor

kymckay added a commit that referenced this issue May 13, 2021
Decided to introduce a new class for better functional encapsulation and
clear distribution of duties. First step is to handle comments in the
preprocessor since that's where they are actually stripped out which
results in some slightly different behaviour of tokenisation (added
tests to capture this).

On the road to #13
@kymckay
Copy link
Owner Author

kymckay commented May 16, 2021

My current idea for #define:

  • Use regex to capture the head, arguments and body of a macro, replacing the arguments with standardised variation like $1 corresponding to their index
  • Store in a multi map (macro can be defined multiple times for different argument counts) where the head is the key:
    • The number of arguments
    • The position of the head token
    • The body of the macro
  • Whenever an alpha character or _ is encountered, read the full ID and check if it is a macro
    • If it is (matches an entry in the multimap) check for arguments
      • if the number of arguments matches an entry in the multimap then regex replace them by index using previously injected $1 tokens, then resolve further macros within the string
      • if the number of arguments doesn't match TBD (test real behaviour)
    • If it isn't just store the string in the buffer and return the first PosChar as expected

kymckay added a commit that referenced this issue May 17, 2021
Still need to handle the actual macro replacement when calling `get`

Another step towards #13
@kymckay kymckay added this to the 1.0.0 milestone Jul 18, 2021
kymckay added a commit that referenced this issue Oct 10, 2021
Decided to introduce a new class for better functional encapsulation and
clear distribution of duties. First step is to handle comments in the
preprocessor since that's where they are actually stripped out which
results in some slightly different behaviour of tokenisation (added
tests to capture this).

On the road to #13
kymckay added a commit that referenced this issue Oct 10, 2021
Still need to handle the actual macro replacement when calling `get`

Another step towards #13
@kymckay
Copy link
Owner Author

kymckay commented May 10, 2022

With da91d20 the bulk of directives are now handled in some form. The remaining #include directive has a separate issue open.

@kymckay kymckay closed this as completed May 10, 2022
Language Support automation moved this from To do to Done May 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

No branches or pull requests

1 participant