From 33ea0332e12933a4fd3c73ec330a439786a037cf 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 | 119 +++++++++++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 60 deletions(-) diff --git a/fsxc/Fsxc.fs b/fsxc/Fsxc.fs index fbea64b..87b6266 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,73 @@ 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, - DeliverLinesUntilNextPreProcessorConditional - elif line.Trim() = "#endif" then - acc, NormalOperation + let lineKind, newState = + match readPreprocessorLine line with + | Some(PreProcessorElement.Action action) -> + if currentReadState <> IgnoreLinesUntilNextPreProcessorConditional then + LineKind.PreProcessorAction action, currentReadState 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" -> + LineKind.Commented, currentReadState + | Some(PreProcessorElement.Conditional cond) -> + match cond, currentReadState with + | PreProcessorConditional.Else, + IgnoreLinesUntilNextPreProcessorConditional -> + LineKind.Commented, + DeliverLinesUntilNextPreProcessorConditional + | PreProcessorConditional.Else, + DeliverLinesUntilNextPreProcessorConditional -> + LineKind.Commented, + IgnoreLinesUntilNextPreProcessorConditional + | PreProcessorConditional.EndIf, IgnoreLinesUntilNextPreProcessorConditional -> + LineKind.Commented, NormalOperation + | PreProcessorConditional.EndIf, DeliverLinesUntilNextPreProcessorConditional -> + LineKind.Commented, NormalOperation + | PreProcessorConditional.EndIf, NormalOperation -> + LineKind.Normal, 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