Files
sml-projects/compiler/scanner.sml

29 lines
818 B
Standard ML
Raw Normal View History

2026-05-05 06:56:45 +01:00
structure Scanner =
struct
structure Dfa = CaseSesitiveDfa
val intDfa = Dfa.fromString "[0-9]+"
val wordDfa = Dfa.fromString "0w[0-9]+"
val realDfa = "[0-9]+.[0-9]+"
val idDfa = Dfa.fromString "[a-zA-Z][a-zA-Z0-9_']*"
fun helpExtractString (pos, str, acc) =
if pos >= String.size str then
raise Fail ("unterminated string: [" ^ acc ^ "]\n")
else
case String.sub (str, pos) of
#"\"" => (pos + 1, acc)
| #"\\" =>
if pos + 1 >= String.size str then
raise Fail ("unterminated string: [" ^ acc ^ "\\]\n")
else
(
case String.sub (str, pos + 1) of
)
| chr =>
helpExtractString (pos + 1, str, acc ^ String.implode [chr])
fun extractString (pos, str) = helpExtractString (pos. str, "")
end