diff --git a/compiler/scanner.sml b/compiler/scanner.sml index 1eff882..7266d25 100644 --- a/compiler/scanner.sml +++ b/compiler/scanner.sml @@ -1,28 +1,58 @@ structure Scanner = struct - structure Dfa = CaseSesitiveDfa + structure Dfa = CaseSensitiveDfa 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 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) = 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 + let val (nextPos, acc) = getEscapeChr (str, pos + 1, acc) + in helpExtractString (nextPos, str, acc) + end + | chr => helpExtractString (pos + 1, str, acc ^ String.implode [chr]) - ) - | chr => - helpExtractString (pos + 1, str, acc ^ String.implode [chr]) - - fun extractString (pos, str) = helpExtractString (pos. str, "") + fun extractString (pos, str) = helpExtractString (pos, str, "") end diff --git a/compiler/uses.sml b/compiler/uses.sml index ea7a5e2..f9de816 100644 --- a/compiler/uses.sml +++ b/compiler/uses.sml @@ -1 +1,2 @@ use "../dfa-gen/dfa-gen.sml"; +use "scanner.sml"