Source file syntax_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
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
open Fmlib_pretty

let plural_s (i: int) (s: string): string =
    if i = 1 then
        s
    else
        s ^ "s"


let header (col: int) (e: Indent.expectation): Pretty.t =
    let open Indent in
    let open Pretty in
    match e with
    | Indent i ->
        if i <= col then
            Printf.printf
                "syntax_error.ml: line %d, col %d, i %d\n"
                __LINE__
                col i;
        [
            wrap_words "indented at least";
            text (string_of_int (i - col));
            plural_s (i - col) "column" |> text;
            text "more"
        ]
        |> separated_by (group space)

    | Align i ->
        if i = col then
            Printf.printf
                "syntax_error.ml: line %d, col %d, i %d\n"
                __LINE__
                col i;
        let delta, more_less =
            if col < i then
                i - col, "more"
            else
                col - i, "less"
        in
        [
            wrap_words "indented exactly";
            text (string_of_int delta);
            plural_s delta "column" |> text;
            text more_less
        ]
        |> separated_by (group space)

    | Align_between (i, j) ->
        let delta_i, delta_j, more_less =
            if col < i then
                i - col, j - col, "more"
            else
                col - i, col - j, "less"
        in
        [
            wrap_words "indented between";
            text (string_of_int delta_i);
            text "and";
            text (string_of_int delta_j);
            text "columns";
            text more_less
        ]
        |> separated_by (group space)


let one_group
        (col: int)
        ((e, lst): Indent.expectation option * string list)
    : Pretty.t list
    =
    let open Pretty
    in
    let lst =
        List.map
            (fun str ->
                 text "- "
                 <+> (wrap_words str |> nest 2)
                 <+> cut)
            lst
    in
    match e with
    | None ->
        lst
    | Some e ->
        [
            text "- "
            <+> (header col e |> nest 2)
            <+> cut <+> cut
            <+> (paragraphs lst |> nest 4)
        ]



let document
        (col: int)
        (es: (string * Indent.expectation option) list)
    : Pretty.t
    =
    let one_or_more: string =
        match es with
        | [_] ->
            ""
        | _ ->
            " one of"
    in
    let open Pretty
    in
    let ps =
        Indent.group es
        |> List.map (one_group col)
        |> List.concat
    in
    wrap_words
        ("I have encountered something unexpected. I was expecting"
             ^ one_or_more)
    <+> cut <+> cut
    <+>
    (paragraphs ps |> nest 4)
    <+> cut