Skip to content

How do we deal with macros? #30

@nolanderc

Description

@nolanderc

Macros can appear almost anywhere. For example, in tests/glsl-samples/well-formed/glslang/tokenPaste.vert we have the following line:

float bothpaste(foo, 719);

At first this looks like a function definition, but it is actually macro expansion with:

#define bothpaste(a, b) a##b

The current implementation ignores macro expansion entirely, and just treats it as if it were normal code. This keeps both the tokenizer and parser simple, but has the added benefit of keeping the parse tree a 1:1 correspondence with the source code.

To properly implement macros we would ideally want to include both the pre-expanded tokens, as well as the expanded tokens in the same tree. One possible approach here is to add a special .macro syntax node, which contains the tokens before expansion (used in formatting and hover info, but ignored everywhere else), and then have the expanded tokens in the tree as usual, but with 0-width token spans pointing to the first character of the macro. Spans being 0-width ensures that tokens cannot be hovered, but still have a position for goto-definition.

The parse tree for the above snippet would then look something like the following:

file
  declaration
    identifier "float"@0..5
    macro
      identifier "bothpaste"@6..15
      ( @15..16
      identifier "foo" @16..19
      , @19..20
      number @21..24
      ) @24..25
    identifier "foo123"@6..6
    ; @25..26

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions