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

2
a.dsc
View File

@@ -1,2 +1,2 @@
x ~0.0499999858439 y 0.400000035763 x ~0.400000035763 y 0.350000023842 x ~0.100000038743 y 0.100000038743
x ~0.0499999858439 y 0.400000035763 x ~0.400000035763 y 0.350000023842 x ~0.100000038743 y 0.100000038743
x 0.25 y ~0.449999958277 x 0.599999964237 y ~0.199999943376 x 0.0499999858439 y 0.0499999858439

BIN
dotscape

Binary file not shown.

View File

@@ -29,27 +29,41 @@ struct
SOME num => COORD num
| NONE => UNKNOWN str
fun helpParseLine (line, pos, acc, lastSpacePos) =
fun helpParseLine (line, pos, acc, wordStartPos) =
if pos = String.size line then
List.rev acc
else
let
val chr = String.sub (line, pos)
in
if chr = #" " orelse chr = #"\n" then
let
val strToken = String.substring
(line, lastSpacePos + 1, pos - (lastSpacePos + 1))
val token = tokeniseString strToken
in
helpParseLine (line, pos + 1, token :: acc, pos)
end
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
(* current character is space, but previous character is not,
* 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
in
helpParseLine (line, pos + 1, token :: acc, pos)
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
helpParseLine (line, pos + 1, acc, lastSpacePos)
(* just proceed to next character *)
helpParseLine (line, pos + 1, acc, wordStartPos)
end
fun parseLine line =
let val lst = helpParseLine (line, 0, [], ~1)
in extractTriangle lst
let
val lst = helpParseLine (line, 0, [], 0)
in
extractTriangle lst
end
end