Skip to content

Commit

Permalink
fsxc: always deliver a LineKind for each line
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
knocte committed Sep 4, 2024
1 parent 88c79a1 commit bc935fa
Showing 1 changed file with 52 additions and 60 deletions.
112 changes: 52 additions & 60 deletions fsxc/Fsxc.fs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ module Program =

let rec readLines
(lines: seq<string>)
(readState: ReadState)
(currentReadState: ReadState)
(acc: List<LineAction>)
: List<LineAction> =

Expand All @@ -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
Expand Down

0 comments on commit bc935fa

Please sign in to comment.