Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Diff of /smlnj-lib/trunk/SExp/sexp-parser.sml
ViewVC logotype

Diff of /smlnj-lib/trunk/SExp/sexp-parser.sml

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3859, Sun Nov 17 14:05:50 2013 UTC revision 3860, Thu Nov 21 03:48:49 2013 UTC
# Line 8  Line 8 
8    
9  structure SExpParser : sig  structure SExpParser : sig
10    
11      val parse : TextIO.instream -> SExp.value      val parse : TextIO.instream -> SExp.value list
12    
13      val parseFile : string -> SExp.value      val parseFile : string -> SExp.value list
14    
15    end = struct    end = struct
16    
# Line 30  Line 30 
30        * nextStrm and strm is that tok is the next token in strm        * nextStrm and strm is that tok is the next token in strm
31        * whereas the token _after_ tok is the next up in nextStrm.        * whereas the token _after_ tok is the next up in nextStrm.
32        *        *
33        * In other words, strm has had all leading whitespace consumed. *)           * In other words, strm has had all leading whitespace consumed.
34        fun lex_nonwhite (strm : Lex.strm) =           *)
35          case lexer strm            fun lexNWS (strm : Lex.strm) = (case lexer strm
36            of (T.WHITE, _, strm) => lex_nonwhite strm                   of (T.WHITE, _, strm) => lexNWS strm
37             | (tok, pos, nextStrm) => (tok, pos, nextStrm, strm)             | (tok, pos, nextStrm) => (tok, pos, nextStrm, strm)
38                    (* end case *))
39    
40            fun parseValue (strm : Lex.strm) = let            fun parseValue (strm : Lex.strm) = let
41                  val (tok, pos, strm) = lexer strm                  val (tok, pos, strm) = lexer strm
# Line 50  Line 51 
51                      | _ => error (pos, "parsing value", tok)                      | _ => error (pos, "parsing value", tok)
52                    (* end case *)                    (* end case *)
53                  end                  end
54            (* parse a list, where delim is the type of delimiter enclosing the list *)
55            and parseList (delim : T.delim_type, strm : Lex.strm) = let            and parseList (delim : T.delim_type, strm : Lex.strm) = let
   
56          fun matchDelim (T.DELIM(dType, T.CLOSE)) = (dType = delim)          fun matchDelim (T.DELIM(dType, T.CLOSE)) = (dType = delim)
57            | matchDelim tok = false            | matchDelim tok = false
58                    val (tok, _, nextStrm, strm) = lexNWS strm
59          val (tok, _, nextStrm, strm) = lex_nonwhite strm                  in
60                      if matchDelim tok
         in if matchDelim tok  
61            then (nextStrm, S.LIST [])            then (nextStrm, S.LIST [])
62            else let            else let
63              fun loop (strm, items) = let              fun loop (strm, items) = let
64                val (strm, v) = parseValue strm                val (strm, v) = parseValue strm
65              (* expect either a separator (whitespace) or a delimiter *)              (* expect either a separator (whitespace) or a delimiter *)
66                val (tok, pos, nextStrm, strm) = lex_nonwhite strm                              val (tok, pos, nextStrm, strm) = lexNWS strm
67                in                in
68                  if matchDelim tok                  if matchDelim tok
69                  then (nextStrm, v::items)                  then (nextStrm, v::items)
# Line 76  Line 76 
76                (strm, S.LIST (List.rev items))                (strm, S.LIST (List.rev items))
77              end              end
78          end          end
79            (* parse top-level s-expressions until EOF *)
80              fun parseSExps (strm, sexps) = let
81                    val (strm, sexp) = parseValue strm
82                    val (tok, pos, nextStrm, strm) = lexNWS strm
83                    in
84                      case tok
85                       of T.EOF => List.rev(sexp::sexps)
86                        | _ => parseSExps (strm, sexp::sexps)
87                      (* end case *)
88                    end
89            in            in
90              #2 (parseValue (Lex.streamifyInstream inStrm))              parseSExps (Lex.streamifyInstream inStrm, [])
91            end            end
92    
93      fun parse inStrm = parse' (AntlrStreamPos.mkSourcemap (), inStrm)      fun parse inStrm = parse' (AntlrStreamPos.mkSourcemap (), inStrm)

Legend:
Removed from v.3859  
changed lines
  Added in v.3860

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0