| 1 |
4 |
tbourke |
(* $Id: cmdlang.sml 17 2007-11-09 10:24:48Z tbourke $ *)
|
| 2 |
|
|
|
| 3 |
|
|
structure CmdLang : CMD_LANG = struct
|
| 4 |
|
|
|
| 5 |
|
|
structure LrVals = CmdLangLrValsFn (structure FilePos = FilePos
|
| 6 |
|
|
structure Token = LrParser.Token
|
| 7 |
|
|
structure CmdEnv = CmdEnv)
|
| 8 |
|
|
structure Lex = CmdLangLexFn (structure FilePos = FilePos
|
| 9 |
|
|
structure Tokens = LrVals.Tokens)
|
| 10 |
|
|
structure Parser = JoinWithArg(structure LrParser = LrParser
|
| 11 |
|
|
structure ParserData = LrVals.ParserData
|
| 12 |
|
|
structure Lex = Lex)
|
| 13 |
|
|
|
| 14 |
|
|
fun parse rdr (env, ostrm) = let
|
| 15 |
17 |
tbourke |
val prError = FilePos.error (Settings.progName^":")
|
| 16 |
4 |
tbourke |
|
| 17 |
|
|
val strm = ref ostrm
|
| 18 |
|
|
fun read _ = case rdr (!strm) of
|
| 19 |
|
|
NONE => ""
|
| 20 |
|
|
| SOME (s, strm') => s before strm := strm'
|
| 21 |
|
|
|
| 22 |
|
|
val lexstream = Parser.makeLexer read (FilePos.newstate ())
|
| 23 |
|
|
|
| 24 |
|
|
val tokenEOF = LrVals.Tokens.EOF (FilePos.zero, FilePos.zero)
|
| 25 |
|
|
val tokenEOL = LrVals.Tokens.SEMICOLON (FilePos.zero, FilePos.zero)
|
| 26 |
|
|
|
| 27 |
|
|
fun skipLine strm = let
|
| 28 |
|
|
val (next, strm') = Parser.Stream.get strm
|
| 29 |
|
|
in
|
| 30 |
|
|
if Parser.sameToken (next, tokenEOF)
|
| 31 |
|
|
orelse Parser.sameToken (next, tokenEOL)
|
| 32 |
|
|
then strm' else skipLine strm'
|
| 33 |
|
|
end
|
| 34 |
|
|
|
| 35 |
|
|
fun loop ((CmdLoop.Stop, env), _) = SOME env
|
| 36 |
|
|
| loop ((CmdLoop.Abort, _), _) = NONE
|
| 37 |
|
|
| loop ((CmdLoop.Continue, env), strm) = let
|
| 38 |
|
|
val (next, strm') = Parser.Stream.get strm
|
| 39 |
|
|
in
|
| 40 |
|
|
if Parser.sameToken (next, tokenEOF) then SOME env
|
| 41 |
|
|
else if Parser.sameToken (next, tokenEOL)
|
| 42 |
|
|
then loop ((CmdLoop.Continue, env), strm')
|
| 43 |
|
|
else loop (Parser.parse (0, strm, prError, env)
|
| 44 |
|
|
handle Parser.ParseError =>
|
| 45 |
|
|
((CmdLoop.Continue, env), skipLine strm))
|
| 46 |
|
|
end
|
| 47 |
|
|
|
| 48 |
|
|
in loop ((CmdLoop.Continue, env), lexstream) end
|
| 49 |
|
|
|
| 50 |
|
|
end
|
| 51 |
|
|
|