[/] [trunk/] [src/] [cmdlang/] [cmdlang.sml] - Blame information for rev 55

Line No. Rev Author Line
1 4 tbourke
(* $Id: cmdlang.sml 55 2008-07-25 01:42:07Z 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 55 tbourke
  exception Failure;
15
 
16 4 tbourke
  fun parse rdr (env, ostrm) = let
17 17 tbourke
      val prError   = FilePos.error (Settings.progName^":")
18 4 tbourke
 
19
      val strm = ref ostrm
20
      fun read _ = case rdr (!strm) of
21
                     NONE => ""
22
                   | SOME (s, strm') => s before strm := strm'
23
 
24
      val lexstream = Parser.makeLexer read (FilePos.newstate ())
25
 
26
      val tokenEOF = LrVals.Tokens.EOF (FilePos.zero, FilePos.zero)
27
      val tokenEOL = LrVals.Tokens.SEMICOLON (FilePos.zero, FilePos.zero)
28
 
29
      fun skipLine strm = let
30
            val (next, strm') = Parser.Stream.get strm
31
          in
32
            if Parser.sameToken (next, tokenEOF)
33
               orelse Parser.sameToken (next, tokenEOL)
34
            then strm' else skipLine strm'
35
          end
36
 
37 55 tbourke
 
38
      fun doLoop (env, strm) = let
39 4 tbourke
            val (next, strm') = Parser.Stream.get strm
40
          in
41
            if Parser.sameToken (next, tokenEOF) then SOME env
42
            else if Parser.sameToken (next, tokenEOL)
43
                 then loop ((CmdLoop.Continue, env), strm')
44
                 else loop (Parser.parse (0, strm, prError, env)
45
                         handle Parser.ParseError =>
46
                            ((CmdLoop.Continue, env), skipLine strm))
47
          end
48
 
49 55 tbourke
      and loop ((CmdLoop.Stop, env), _) = SOME env
50
        | loop ((CmdLoop.Abort, _), _) = NONE
51
        | loop ((CmdLoop.Continue, env), strm) = doLoop (env, strm)
52
        | loop ((CmdLoop.Fail, env), strm) = if Settings.exitOnFail ()
53
                                             then raise Failure
54
                                             else doLoop (env, strm)
55
 
56 4 tbourke
    in loop ((CmdLoop.Continue, env), lexstream) end
57
 
58
end
59