Skip to content

[Compiler] Preprocessor modes for the IDE and a lexer hook in the preprocessor - #2209

Open
hpetriffer wants to merge 2 commits into
X-Sharp:CompilerChangesfrom
hpetriffer:hpetriffer/pp-ide-modes-lexer-hook
Open

hpetriffer wants to merge 2 commits into
X-Sharp:CompilerChangesfrom
hpetriffer:hpetriffer/pp-ide-modes-lexer-hook

Conversation

@hpetriffer

Copy link
Copy Markdown
Contributor

What

The second PR of the series for the new IDE parser: preprocessor modes for the IDE and a lexer hook in the preprocessor. Everything is additive and opt-in: the compile path does not enter the new modes and keeps using its XSharpLexer, so xsc output does not change.

This PR targets CompilerChanges and builds on #2207 (preprocessor keyword table per dialect, include cache), which is merged there.

Lexer hook (IPPLexerServices)

Today the preprocessor reads several things from its XSharpLexer: HasPPIfdefs, ReclassifyTokens for rule results, and a new XSharpLexer for every include file. The new interface IPPLexerServices names exactly these. New constructors take the services instead of a lexer. The constructors that take a lexer wrap it in XSharpLexerServices, so the preprocessor behaves as before.

  • The include cache gets a partition per kind of lexer services. Include tokens that another lexer produced are never reused. The lexer's own path keeps the plain file name as its key.
  • XSharpLexer.KeywordTables(options) and a static ReclassifyTokens(tokens, tables) give the keyword tables and the reclassification without a lexer object. KwIds keeps a reference to its shared table, so a lexer no longer takes the lock once per identifier.

The same hook is what lets the macro compiler share the compiler's preprocessor source (the "linked items" proposal); that will be a PR of its own.

Preprocessor modes for the IDE (PPMode, PPState)

  • Conditionals only: evaluates #define, #undef, #if, #ifdef, #ifndef, #else and #endif (also through include files) and marks inactive code. Commands, translates and macros are not applied. It has its own include cache for the define state an include produces, keyed by the include, its write time, the options that affect #if and the define state before it; the compiler's PPIncludeFile cache is not involved.
  • Rules only: collects the #command / #translate rules of a file and its includes (the UDC rule set of the IDE), without rewriting the code.
  • Marking outside the VSPARSER build: the marking of inactive code that XSharpVSParser does today can be switched on in XSharp.CodeAnalysis too (markInactiveCode).
  • Read-only questions to the rules: PPRule.Matches returns the matched tokens, FirstWordMatches, ResultStartsText, the rule ordinal, PPRuleDictionary.HasKeyFor / Keys / Clone. The preprocessor itself does not call the new ones.

The IDE code is in partial files of its own: XSharpPreprocessor.Ide.cs (the modes, their constructors and the conditionals-only include cache) and XSharpPPRule.Ide.cs (the read-only questions to PPRule and PPRuleDictionary). XSharpPreprocessor.cs, XSharpPPRule.cs and XSharpPPHelpers.cs keep the hook and the small checks of the mode in the existing methods, so the part the compiler shares with the macro compiler stays easy to compare.

Files: XSharpPreprocessor.cs, XSharpPreprocessor.Ide.cs (new), XSharpPPRule.cs, XSharpPPRule.Ide.cs (new), XSharpPPHelpers.cs, XSharpPPState.cs (new types), XSharpLexerCode.cs. The macro compiler's preprocessor copy is not touched; nothing here is used on a macro path.

Tests

The unit tests for the hook and the modes (marking, conditionals-only include cache, rules-only, UDC matching) live in the test project of the XSharp.Syntax library, which comes with the next PR of the series. dev has no unit test project for XSharp.CodeAnalysis to put them in now.

Before and after this change, on the same machine (before = #2207):

Check #2207 this PR
compiler.slnx Release (xsc, XSCompiler, XSharp.CodeAnalysis, XSharpVSParser) builds builds
Compiler tests (src/CompilerTests/runtests.cmd) 390 of 392 runtime tests, 0 unexpected warnings, 0 compiler problems the same
LogBroken.Log identical line by line
LogFixed.Log identical apart from the memory addresses that some runtime tests print and the worktree path

The two failing runtime tests fail on dev as well: they need a .NET 10 runtime path that this machine lacks.

🤖 Generated with Claude Code

hpetriffer and others added 2 commits December 6, 2026 18:08
…processor

- IPPLexerServices: what the preprocessor takes from its lexer
  (HasPPIfdefs, ReclassifyTokens, lexing an include file). New
  constructors take the services; the constructors that take an
  XSharpLexer wrap it and behave as before. The include cache is
  partitioned per kind of services; the lexer's own path keeps its keys.
- XSharpLexer.KeywordTables and a static ReclassifyTokens give the
  keyword tables and the reclassification without a lexer object;
  KwIds keeps its shared table per lexer.
- PPMode/PPState: a conditionals-only mode (defines and #if, inactive
  code marked, own include cache) and a rules-only mode (the UDC rule
  set, nothing substituted); marking of inactive code outside the
  VSPARSER build; read-only questions to PPRule and PPRuleDictionary.
- The IDE modes are in their own partial files (XSharpPreprocessor.Ide.cs,
  XSharpPPRule.Ide.cs), so the part the compiler shares with the macro
  compiler stays easy to compare.
- Additive and opt-in: the compile path enters none of the new modes.

Compiler tests give the same results as before (LogBroken.Log
identical, LogFixed.Log identical apart from memory addresses and the
worktree path, 390/392 runtime tests in both runs).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… and the lexer

The private helper methods that the previous commit added follow the
naming rule of src/Compiler/.editorconfig (members in PascalCase).
The private worker beside XSharpLexer.ReclassifyTokens is now
ReclassifyTokensCore, and the static method that reads the include
cache partition is GetIncludeCachePartition (IPPLexerServices has a
property IncludeCachePartition). Only names change.

Compiler tests give the same results as before (390/392 runtime tests,
LogBroken.Log identical, LogFixed.Log identical apart from memory
addresses, the date and the freed PSZ that test C629 prints).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant