summaryrefslogtreecommitdiffstats
path: root/parser.sml
blob: a907edf729d6b551bf4786e241cfe2f62e01a7f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
infix 4 <$> <$
infix 1 >>
infixr 1 <|>
infix 0 <?>

structure Parser =
struct
  (* vector of length 10, holding the left and right associative infix operators for each precedence level. *)
  type infixTable = (string list * string list) vector
  datatype userState = UserState of string list * infixTable
  datatype sourceLoc = SourceLoc of string * int * int  (* file * row * column *)
  datatype state = State of TextIO.StreamIO.instream * sourceLoc * userState
  datatype response = Consumed | Empty
  datatype message = Unexpected of string | Expected of string
  datatype parseError = ParseError of sourceLoc * message list
  datatype hints = Hints of string list
  type 'a parser = state -> response * (parseError, 'a * state * hints) Either.either

  val reservedWords =
    [ "abstype", "and", "andalso", "as", "case", "datatype", "do", "else"
    , "end", "exception", "fn", "fun", "handle", "if", "in", "infix"
    , "infixr", "let", "local", "nonfix", "of", "op", "open", "orelse"
    , "raise", "rec", "then", "type", "val", "with", "withtype", "while"
    , "(", ")", "[", "]", "{", "}", ",", ":", ";", "...", "_", "|", "=", "=>", "->", "#"
    ]

  val defaultInfixOperators =
    Vector.fromList
      [ (["before"], [])
      , ([], [])
      , ([], [])
      , ([":=", "o"], [])
      , (["=", "<>", ">", ">=", "<", "<="], [])
      , (["@@"], ["::", "@"])
      , (["+", "-", "^"], [])
      , (["*", "/", "div", "mod"], [])
      , ([], [])
      , ([], [])
      ]

  fun printSourceLoc (SourceLoc (fileName, row, col)) : string =
    fileName ^ ":" ^ Int.toString row ^ "." ^ Int.toString col

  fun printError (ParseError (loc, msgs)) : string =
    let
      val unexpect = List.mapPartial (fn Unexpected x => SOME x | _ => NONE) msgs
      val showUnexpect = case unexpect of
                           [] => ""
                         | s :: _ => "unexpected " ^ s ^ ";\n"
      val expect = List.mapPartial (fn Expected s => SOME s | _ => NONE) msgs
    in
        printSourceLoc loc ^ " Syntax error:\n"
      ^ showUnexpect
      ^ "expecting " ^ String.concatWith ", " expect
    end

  fun stateStream (State (stream, _, _)) : TextIO.StreamIO.instream = stream

  fun stateLoc (State (_, loc, _)) : sourceLoc = loc

  fun unpackParserResponse (_ : response, Either.Left err : (parseError, 'a * state * hints) Either.either) : (string, 'a) Either.either =
        Either.Left (printError err)
    | unpackParserResponse (_, Either.Right (a, st, _)) =
        if TextIO.StreamIO.endOfStream (stateStream st)
        then Either.Right a
        else Either.Left (printSourceLoc (stateLoc st) ^ " Syntax error: trailing characters")

  fun newLoc (fileName : string) : sourceLoc = SourceLoc (fileName, 1, 1)

  fun collectInfixOperators (opTable : (string list * string list) vector) : string list =
    Vector.foldl (fn ((a, b), acc) => a @ b @ acc) [] opTable

  fun makeUserState (opTable : infixTable) : userState =
    UserState (Vector.foldl (fn ((a, b), acc) => a @ b @ acc) [] opTable, opTable)

  fun newState (fileName : string) (fileStream : TextIO.instream) : state =
    State (TextIO.getInstream fileStream, newLoc fileName, makeUserState defaultInfixOperators)

  fun updateUserState (f : userState -> userState) : userState parser =
    fn State (stream, loc, us) =>
      let val st' = f us
      in (Empty, Either.Right (st', State (stream, loc, st'), Hints []))
      end

  val getUserState : userState parser = updateUserState (fn x => x)

  fun runParser (p : 'a parser) (fileName : string) : (string, 'a) Either.either =
    unpackParserResponse (p (newState fileName (TextIO.openIn fileName)))

  fun testParser (p : 'a parser) (s : string) : (string, 'a) Either.either =
    unpackParserResponse (p (newState "STRING" (TextIO.openString s)))

  fun mergeHints (Hints a) (Hints b) : hints = Hints (a @ b)

  fun withHints (Hints hints) (ParseError (loc, msgs)) : parseError = ParseError (loc, map Expected hints @ msgs)

  fun errToHints (ParseError (_, msgs)) = Hints (List.mapPartial (fn Expected s => SOME s | _ => NONE) msgs)

  fun compareLoc (SourceLoc (_, r1, c1), SourceLoc (_, r2, c2)) : order =
    case Int.compare (r1, r2) of
      EQUAL => Int.compare (c1, c2)
    | ord => ord

  fun mergeError (e1 as ParseError (loc1, msgs1)) (e2 as ParseError (loc2, msgs2)) : parseError =
    (* pick the longest match *)
    case compareLoc (loc1, loc2) of
      EQUAL => ParseError (loc1, msgs1 @ msgs2)
    | GREATER => e1
    | LESS => e2

  fun bind (p : 'a parser) (f : 'a -> 'b parser) : 'b parser =
    fn st =>
      case p st of
        (consumed1, Either.Right (a, st', hints)) =>
          (case (f a) st' of
            (Consumed, Either.Right success) => (Consumed, Either.Right success)
          | (Empty, Either.Right (b, st'', hints')) => (consumed1, Either.Right (b, st'', mergeHints hints hints'))
          | (Consumed, Either.Left err) => (Consumed, Either.Left (withHints hints err))
          | (Empty, Either.Left err) => (consumed1, Either.Left (withHints hints err)))
      | (consumed, Either.Left err) => (consumed, Either.Left err)

  fun (p1 : 'a parser) >> (p2 : 'b parser) : 'b parser = bind p1 (fn _ => p2)

  fun (p : 'a parser) <?> (msg : string) : 'a parser =
    fn st =>
      case p st of
        (consumed, Either.Right (a, st', _)) => (consumed, Either.Right (a, st', Hints [msg]))
      | (consumed, Either.Left (ParseError (loc, _))) => (consumed, Either.Left (ParseError (loc, [Expected msg])))

  fun (p1 : 'a parser) <|> (p2 : 'a parser) : 'a parser =
    fn st =>
      case p1 st of
        (Empty, Either.Left err) =>
          (case p2 st of
            (Empty, Either.Right (a, st', hints)) => (Empty, Either.Right (a, st', mergeHints (errToHints err) hints))
          | (Empty, Either.Left err') => (Empty, Either.Left (mergeError err err'))
          | res => res)
      | res => res

  fun const (x : 'a) (st : state) = (Empty, Either.Right (x, st, Hints []))

  fun (f : 'a -> 'b) <$> (p : 'a parser) : 'b parser = bind p (const o f)

  fun (p : 'a parser) <$ (x : 'b) : 'b parser = (fn _ => x) <$> p

  fun try (p : 'a parser) : 'a parser =
    fn st =>
      case p st of
        (Consumed, Either.Left err) => (Empty, Either.Left err)
      | res => res

  fun updatePosChar (SourceLoc (file, row, column)) (c : char) : sourceLoc =
    case c of
      #"\n" => SourceLoc (file, row + 1, 1)
    | #"\t" => SourceLoc (file, row, column + 8 - (column - 1) mod 8)
    | _ => SourceLoc (file, row, column + 1)

  fun satisfy (pred : char -> bool) : char parser =
    fn State (stream, loc, us) =>
      case TextIO.StreamIO.input1 stream of
        NONE => (Empty, Either.Left (ParseError (loc, [Expected "UNKNOWN"])))
      | SOME (c, stream') =>
          if pred c
          then (Consumed, Either.Right (c, State (stream', updatePosChar loc c, us), Hints []))
          else (Empty, Either.Left (ParseError (loc, [Expected "UNKNOWN"])))

  fun parseChar (c : char) : char parser =
    satisfy (fn c' => c = c') <?> str c

  fun parseString (s : string) : string parser =
    case explode s of
      [] => const ""
    | c1 :: cs => foldl (fn (c, p) => p >> parseChar c) (parseChar c1) cs <$ s <?> s

  fun manyErr () = raise Fail "many is applied to a parser that accepts an empty string"

  fun many (p : 'a parser) : 'a list parser =
    fn st =>
      let fun walk xs s' =
            case p s' of
              (Consumed, Either.Right (x, s'', _)) => walk (x :: xs) s''
            | (Consumed, Either.Left err) => (Consumed, Either.Left err)
            | (Empty, Either.Right _) => manyErr ()
            | (Empty, Either.Left err) => (Consumed, Either.Right (rev xs, s', errToHints err))
      in
        case p st of
          (Consumed, Either.Right (x, s', _)) => walk [x] s'
        | (Consumed, Either.Left err) => (Consumed, Either.Left err)
        | (Empty, Either.Right _) => manyErr ()
        | (Empty, Either.Left err) => (Empty, Either.Right ([], st, errToHints err))
      end

  fun many1 (p : 'a parser) : 'a list parser =
    bind p (fn x =>
    bind (many p) (fn xs =>
    const (x :: xs)))

  val space : char parser = satisfy Char.isSpace <?> "space"

  val spaces : unit parser = many space <$ () <?> "white space"

  fun unexpected (s : string) : 'a parser =
    fn st => (Empty, Either.Left (ParseError (stateLoc st, [Unexpected s])))

  val letter : char parser = satisfy Char.isAlpha <?> "letter"

  val alphaNum : char parser = satisfy Char.isAlphaNum <?> "letter or digit"

  fun oneOf ([] : char list) : char parser = raise Fail "oneOf empty"
    | oneOf (x :: xs) = foldl (fn (c, p) => p <|> parseChar c) (parseChar x) xs

  (* some day, whiteSpace will support comments *)
  val whiteSpace = spaces

  fun lexeme (p : 'a parser) : 'a parser =
    bind p (fn x =>
    whiteSpace >>
    const x)

  val alphaNumIdentifierLetter : char parser =
    alphaNum <|> oneOf [#"'", #"_"]

  val symbolicIdentifierLetters : char list =
    [ #"!", #"%", #"&", #"$", #"#", #"+", #"-", #"/", #":", #"<"
    , #"=", #">", #"?", #"@", #"\\", #"~", #"`", #"^", #"|", #"*"
    ]

  val symbolicIdentifierLetter : char parser = oneOf symbolicIdentifierLetters

  val alphaNumIdentifier : string parser =
    lexeme
      (bind (letter <|> parseChar #"'") (fn firstLetter =>
      bind (many alphaNumIdentifierLetter) (fn rest =>
      const (implode (firstLetter :: rest)))))

  val symbolicIdentifier : string parser =
    lexeme
      (implode <$> many1 symbolicIdentifierLetter)

  val identifier : string parser =
    try
      (bind (alphaNumIdentifier <|> symbolicIdentifier <?> "identifier") (fn identName =>
      bind getUserState (fn UserState (ops, _) =>
      if List.exists (fn n => n = identName) reservedWords orelse List.exists (fn n => n = identName) ops
      then unexpected identName
      else const identName)))

  fun notFollowedBy (p : char parser) : unit parser =
    bind (try p) (fn c => unexpected (str c))
    <|> const ()

  fun reserved (s : string) : string parser =
    let
      val start =
        if s = ""
        then raise Fail "reserved was called on an empty string"
        else String.sub (s, 0)
      val isSymbolic =
        List.exists (fn c => c = start) symbolicIdentifierLetters
    in
      lexeme
        (try (parseString s) >>
        notFollowedBy (if isSymbolic then symbolicIdentifierLetter else alphaNumIdentifierLetter) >>
        const s)
      <?> s
    end

  val digit : char parser = satisfy Char.isDigit <?> "digit"

  val integer : int parser =
    lexeme
      (bind (many1 digit) (fn digits =>
      const (valOf (Int.fromString (implode digits)))))

  val stringInternalChar : char parser =
    (parseString "\\" >> (parseChar #"a" <$ #"\a"
                         <|> parseChar #"b" <$ #"\b"
                         <|> parseChar #"t" <$ #"\t"
                         <|> parseChar #"n" <$ #"\n"
                         <|> parseChar #"v" <$ #"\v"
                         <|> parseChar #"f" <$ #"\f"
                         <|> parseChar #"r" <$ #"\r"
                         <|> parseChar #"\""
                         <|> parseChar #"\\") <?> "string escape")
    <|> (satisfy (fn c => c <> #"\"" andalso c <> #"\\") <?> "string character")

  val stringConstant : string parser =
    lexeme
      (parseString "\"" >>
      bind (implode <$> many stringInternalChar) (fn stringContent =>
      parseString "\"" >>
      const stringContent))

  fun symbol (s : string) : string parser = lexeme (parseString s)

  fun sepBy1 (p : 'a parser) (sep : 'b parser) : 'a list parser =
    bind p (fn x =>
    bind (many (sep >> p)) (fn xs =>
    const (x :: xs)))

  fun leftOp (i : int) : Syntax.expr parser =
    bind getUserState (fn UserState (_, opTable) =>
    let val (leftOps, _) = Vector.sub (opTable, i)
    in
      case (map reserved leftOps) of
        [] => unexpected "left-associative operator"
      | op1 :: ops => Syntax.EIdent <$> foldl (op <|>) op1 ops
    end)

  fun rightOp (i : int) : Syntax.expr parser =
    bind getUserState (fn UserState (_, opTable) =>
    let val (_, rightOps) = Vector.sub (opTable, i)
    in
      case (map reserved rightOps) of
        [] => unexpected "right-associative operator"
      | op1 :: ops => Syntax.EIdent <$> foldl (op <|>) op1 ops
    end)

  (* Values cannot be recursive, so we write each expression parser as a lambda. *)
  val rec atom : Syntax.expr parser =
    fn st =>
      (Syntax.EInt <$> integer
      <|> Syntax.EStr <$> stringConstant
      <|> Syntax.EIdent <$> identifier
      <|> (symbol "(" >>
           bind (sepBy1 expr (symbol ",")) (fn exprs =>
           symbol ")" >>
           const
             (case exprs of
               [x] => x
             | _ => Syntax.ETuple exprs)))) st
  and expr0 : Syntax.expr parser =
    fn st =>
      bind atom (fn e0 =>
      foldl (fn (x, acc) => Syntax.EApp (acc, x)) e0 <$> many atom) st
  and expr : Syntax.expr parser =
    fn st =>
      foldl
        (fn (i, exprLower) =>
          let
            fun exprLeft expr1 =
              bind (leftOp i) (fn opEx =>
              bind exprLower (fn expr2 =>
              let val app = Syntax.EApp (opEx, Syntax.ETuple [expr1, expr2])
              in exprLeft app <|> const app
              end))
            fun exprRight expr1 =
              bind (rightOp i) (fn opEx =>
              bind exprLower (fn expr2 =>
              bind (exprRight expr2 <|> const expr2) (fn rest =>
              const (Syntax.EApp (opEx, Syntax.ETuple [expr1, rest])))))
          in
            bind exprLower (fn expr1 =>
            exprLeft expr1 <|> exprRight expr1 <|> const expr1)
          end)
        expr0
        (List.tabulate (10, fn i => 9 - i)) st
end