package fmlib

  1. Overview
  2. Docs

Source file error.ml

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
(*
    A parse error is either a list of syntax error or on semantic error.

    We need a list of syntax errors for the following reason:

    A syntax error signifies that something has been expected and has not been
    encountered. Since we have alternatives which can all fail without consuming
    tokens we can have a list of failed expectations.
*)
open Fmlib_std
open Interfaces


module Make (Expect: ANY) (Semantic: ANY) =
struct
    type t =
        | Syntax of Expect.t list
        | Semantic of Semantic.t


    let to_string
            (e: t)
            (f: Expect.t -> string)
            (g: Semantic.t -> string)
        : string =
        match e with
        | Syntax lst ->
            "["
            ^ String.concat
                ", "
                (List.rev_map f lst)
            ^ "]"
        | Semantic sem ->
            g sem


    let init: t =
        Syntax []


    let clear_last (e: t): t =
        match e with
        | Syntax (_ :: tail) ->
            Syntax tail
        | _ ->
            e


    let add_expected (exp: Expect.t) (e: t): t =
        match e with
        | Syntax lst ->
            Syntax (exp :: lst)
        | _ ->
            Syntax [exp]


    let make_semantic (sem: Semantic.t): t =
        Semantic sem


    let make_expectations (lst: Expect.t list): t =
        Syntax (List.rev lst)


    let is_semantic (e: t): bool =
        match e with
        | Syntax _ -> false
        | _        -> true


    let is_syntax (e: t): bool =
        not (is_semantic e)



    let semantic (e: t): Semantic.t =
        match e with
        | Syntax _ ->
            assert false (* Illegal call! *)
        | Semantic sem ->
            sem


    let expectations (e: t): Expect.t list =
        match e with
        | Syntax es ->
            List.rev es
        | Semantic _ ->
            assert false (* Illegal call! *)
end
OCaml

Innovation. Community. Security.