Repository navigation
Keep a compiler directive that comes before the unit keyword - #6
Merged
Merged
Conversation
A switch directive before `unit` - {$WARN UNIT_PLATFORM OFF}, {$D+},
{$APPTYPE GUI}, all accepted by Delphi - failed the whole parse with
EListError "Unbalanced stack or queue operation".
The parser reads the first token before UnitFile pushes the root node, so
CompilerDirective ran with an empty stack and its FStack.Peek raised.
Conditional directives never reach CompilerDirective, which is why
{$IFNDEF X} unit Y; {$ENDIF} was fine.
Hold such a directive until the root exists. PushRootNode, now shared by
UnitFile, ProgramFile, LibraryFile and PackageFile, creates the root and
adopts them, so they sit first under it in source order - where a
directive right after `unit X;` already goes, since GetMainSection returns
the root for a node without a parent. Run frees any left over from a parse
that failed before reaching the keyword.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AST.DirectiveBeforeUnit parses a unit that starts with {$WARN ...} and
{$D+} and checks that both end up first under the ntUnit root, in source
order, and that the rest of the unit still parses. It fails on main and
passes with the fix.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug. A switch directive before the
unitkeyword fails the whole parse:raises
EListError: Unbalanced stack or queue operation. Delphi accepts it, and real code has it: in the Dev-C++ 4.9.9.2 sources, 9 units start with{$WARN ...},{$D+},{$OPTIMIZATION off},{$APPTYPE GUI}or similar. The same directive afterunit T;parses fine, and so does a conditional ({$IFNDEF X} unit Y; {$ENDIF}), because conditionals never reachCompilerDirective.Cause. The parser reads the first token before
UnitFilepushes the root node, soCompilerDirectiveruns with an empty stack andFStack.Peekraises.Fix. Hold such a directive in
FLeadingDirectivesuntil the root exists.PushRootNode, now shared byUnitFile,ProgramFile,LibraryFileandPackageFile, creates the root and adopts them, so they sit first under it in source order. That is where a directive right afterunit X;already goes, sinceGetMainSectionreturns the root for a node without a parent.Runfrees any left over from a parse that failed before reaching the keyword.Test.
AST.DirectiveBeforeUnitis added toTest/UnitTests. It fails onmainand passes with this change. All 9 Dev-C++ units parse with it.Serialization.BinaryRoundTripfails with and without this change under Delphi 10.4, as noted in #5.🤖 Generated with Claude Code