From bc935fa9a13635c0077e8033ea08c428124cdf4e Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Wed, 4 Sep 2024 14:03:20 +0800 Subject: [PATCH] fsxc: always deliver a LineKind for each line There were still some issues with line numbers when reporting errors so I discovered that in some cases we were swallowing some lines instead of generating LineKind.Normal or .Commented for them. --- fsxc/Fsxc.fs | 112 ++++++++++++++++++++++++--------------------------- 1 file changed, 52 insertions(+), 60 deletions(-) diff --git a/fsxc/Fsxc.fs b/fsxc/Fsxc.fs index fbea64b..50e9f63 100644 --- a/fsxc/Fsxc.fs +++ b/fsxc/Fsxc.fs @@ -315,7 +315,7 @@ module Program = let rec readLines (lines: seq) - (readState: ReadState) + (currentReadState: ReadState) (acc: List) : List = @@ -324,74 +324,66 @@ module Program = let rest = Seq.tail lines let newAcc, newState = - let noStateChange() = - let lineKind = - match readPreprocessorLine line with - | Some(PreProcessorElement.Action action) -> - LineKind.PreProcessorAction action - | None -> LineKind.Normal - | _ -> LineKind.Commented - - let newAcc = - { - Line = line - LineKind = lineKind - } - :: acc - - newAcc, readState - - match readState with - | IgnoreLinesUntilNextPreProcessorConditional -> - let newAcc, newState = - if line.Trim() = "#else" then - acc, + let lineKind, newState = + match readPreprocessorLine line with + | Some(PreProcessorElement.Action action) -> + LineKind.PreProcessorAction action, currentReadState + | Some(PreProcessorElement.Conditional cond) -> + match cond, currentReadState with + | PreProcessorConditional.Else, + IgnoreLinesUntilNextPreProcessorConditional -> + LineKind.Commented, DeliverLinesUntilNextPreProcessorConditional - elif line.Trim() = "#endif" then - acc, NormalOperation - else - { - Line = line - LineKind = LineKind.Commented - } - :: acc, - readState - - newAcc, newState - | DeliverLinesUntilNextPreProcessorConditional -> - if line.Trim() = "#else" then - acc, IgnoreLinesUntilNextPreProcessorConditional - elif line.Trim() = "#endif" then - acc, NormalOperation - else - noStateChange() - | NormalOperation -> - let trimmedLine = line.Trim() - - if trimmedLine.StartsWith "#if" - && line.Contains "LEGACY_FRAMEWORK" then - let newState = - match trimmedLine with - | "#if LEGACY_FRAMEWORK" -> + | PreProcessorConditional.Else, + DeliverLinesUntilNextPreProcessorConditional -> + LineKind.Commented, + IgnoreLinesUntilNextPreProcessorConditional + | PreProcessorConditional.EndIf, _ -> + LineKind.Commented, NormalOperation + | PreProcessorConditional.If, _ -> + if not(line.Contains "LEGACY_FRAMEWORK") then + match currentReadState with + | IgnoreLinesUntilNextPreProcessorConditional -> + LineKind.Commented, currentReadState + | _ -> LineKind.Normal, currentReadState + else + let newState = + match line.Trim() with + | "#if LEGACY_FRAMEWORK" -> #if LEGACY_FRAMEWORK - DeliverLinesUntilNextPreProcessorConditional + DeliverLinesUntilNextPreProcessorConditional #else - IgnoreLinesUntilNextPreProcessorConditional + IgnoreLinesUntilNextPreProcessorConditional #endif - | "#if !LEGACY_FRAMEWORK" -> + | "#if !LEGACY_FRAMEWORK" -> #if LEGACY_FRAMEWORK - IgnoreLinesUntilNextPreProcessorConditional + IgnoreLinesUntilNextPreProcessorConditional #else - DeliverLinesUntilNextPreProcessorConditional + DeliverLinesUntilNextPreProcessorConditional #endif - | _ -> - failwith - "Only simple ifdef statements are supported for the LEGACY_FRAMEWORK define" + | _ -> + failwith + "Only simple ifdef statements are supported for the LEGACY_FRAMEWORK define" - acc, newState - else - noStateChange() + LineKind.Commented, newState + | _, NormalOperation -> + LineKind.Normal, currentReadState + + | None -> + match currentReadState with + | IgnoreLinesUntilNextPreProcessorConditional -> + LineKind.Commented, currentReadState + | _ -> LineKind.Normal, currentReadState + + let newAcc = + { + Line = line + LineKind = lineKind + } + :: acc + + newAcc, newState readLines rest newState newAcc | None -> acc