improve parsing code slightly, with successful parsing when saved .dsc file has inconsistent spacing

This commit is contained in:
2024-08-29 11:18:17 +01:00
parent 24bc45142d
commit da2a652506
3 changed files with 27 additions and 13 deletions

BIN
dotscape

Binary file not shown.

View File

@@ -29,27 +29,41 @@ struct
SOME num => COORD num SOME num => COORD num
| NONE => UNKNOWN str | NONE => UNKNOWN str
fun helpParseLine (line, pos, acc, lastSpacePos) = fun helpParseLine (line, pos, acc, wordStartPos) =
if pos = String.size line then if pos = String.size line then
List.rev acc List.rev acc
else else
let let
val chr = String.sub (line, pos) val chr = String.sub (line, pos)
in in
if chr = #" " orelse chr = #"\n" then if Char.isSpace chr then
if pos > 0 andalso Char.isSpace (String.sub (line, pos - 1)) then
(* if previous character is space, just proceed to next character *)
helpParseLine (line, pos + 1, acc, wordStartPos)
else
let let
val strToken = String.substring (* current character is space, but previous character is not,
(line, lastSpacePos + 1, pos - (lastSpacePos + 1)) * which means we have some text to substring and tokenise
* before proceeding to next character *)
val strToken =
String.substring (line, wordStartPos, pos - wordStartPos)
val token = tokeniseString strToken val token = tokeniseString strToken
in in
helpParseLine (line, pos + 1, token :: acc, pos) helpParseLine (line, pos + 1, token :: acc, pos)
end end
else if pos > 0 andalso Char.isSpace (String.sub (line, pos - 1)) then
(* previous character was space but current character is not,
* meaning that we have hit the start of a new word *)
helpParseLine (line, pos + 1, acc, pos)
else else
helpParseLine (line, pos + 1, acc, lastSpacePos) (* just proceed to next character *)
helpParseLine (line, pos + 1, acc, wordStartPos)
end end
fun parseLine line = fun parseLine line =
let val lst = helpParseLine (line, 0, [], ~1) let
in extractTriangle lst val lst = helpParseLine (line, 0, [], 0)
in
extractTriangle lst
end end
end end