progress adding scanner

This commit is contained in:
2026-05-05 08:43:07 +01:00
parent c2c854a937
commit 14e8a0b363
2 changed files with 40 additions and 9 deletions

View File

@@ -1,28 +1,58 @@
structure Scanner = structure Scanner =
struct struct
structure Dfa = CaseSesitiveDfa structure Dfa = CaseSensitiveDfa
val intDfa = Dfa.fromString "[0-9]+" val intDfa = Dfa.fromString "[0-9]+"
val wordDfa = Dfa.fromString "0w[0-9]+" val wordDfa = Dfa.fromString "0w[0-9]+"
val realDfa = "[0-9]+.[0-9]+" val realDfa = "[0-9]+.[0-9]+"
val idDfa = Dfa.fromString "[a-zA-Z][a-zA-Z0-9_']*" val idDfa = Dfa.fromString "[a-zA-Z][a-zA-Z0-9_']*"
fun skipFormattingChars (str, pos) =
if pos = String.size str then
raise Fail "unterminated formatting char sequence"
else
let
val chr = String.sub (str, pos)
in
if Char.isSpace chr then skipFormattingChars (str, pos + 1)
else if chr = #"\\" then pos + 1
else raise Fail "encountered non-space char in formatting sequence"
end
fun getEscapeChr (str, pos, acc) =
case String.sub (str, pos) of
#"a" => (pos + 1, acc ^ "\a")
| #"b" => (pos + 1, acc ^ "\b")
| #"t" => (pos + 1, acc ^ "\t")
| #"n" => (pos + 1, acc ^ "\n")
| #"v" => (pos + 1, acc ^ "\v")
| #"f" => (pos + 1, acc ^ "\f")
| #"r" => (pos + 1, acc ^ "\r")
| #"\"" => (pos + 1, acc ^ "\"")
| #"\\" => (pos + 1, acc ^ "\\")
| chr =>
if Char.isSpace chr then
(* formatting characters ahead *)
let val nextPos = skipFormattingChars (str, pos + 1)
in (nextPos, acc)
end
else
raise Fail "invalid escape char"
fun helpExtractString (pos, str, acc) = fun helpExtractString (pos, str, acc) =
if pos >= String.size str then if pos >= String.size str then
raise Fail ("unterminated string: [" ^ acc ^ "]\n") raise Fail ("unterminated string: [" ^ acc ^ "]\n")
else else
case String.sub (str, pos) of case String.sub (str, pos) of
#"\"" => (pos + 1, acc) #"\"" => (pos + 1, acc)
| #"\\" => | #"\\" =>
if pos + 1 >= String.size str then if pos + 1 >= String.size str then
raise Fail ("unterminated string: [" ^ acc ^ "\\]\n") raise Fail ("unterminated string: [" ^ acc ^ "\\]\n")
else else
( let val (nextPos, acc) = getEscapeChr (str, pos + 1, acc)
case String.sub (str, pos + 1) of in helpExtractString (nextPos, str, acc)
end
| chr => helpExtractString (pos + 1, str, acc ^ String.implode [chr])
) fun extractString (pos, str) = helpExtractString (pos, str, "")
| chr =>
helpExtractString (pos + 1, str, acc ^ String.implode [chr])
fun extractString (pos, str) = helpExtractString (pos. str, "")
end end

View File

@@ -1 +1,2 @@
use "../dfa-gen/dfa-gen.sml"; use "../dfa-gen/dfa-gen.sml";
use "scanner.sml"