summaryrefslogtreecommitdiff
path: root/lib/configuration/parser.ml
blob: bf962d96f62bfcae14aaea0ed32d6322038e14f2 (plain)
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
open StdLabels

module ExpressionParser : sig
  val of_string : string -> (string list Model.Dsl.t, string) result
end = struct
  module MI = Expression_parser.MenhirInterpreter
  module E = MenhirLib.ErrorReports
  module L = MenhirLib.LexerUtil

  type error = {
    message : string;
    start_line : int;
    start_pos : int;
    end_pos : int;
  }

  let range_message start_pos end_pos message =
    let start_c = start_pos.Lexing.pos_cnum - start_pos.Lexing.pos_bol
    and end_c = end_pos.Lexing.pos_cnum - end_pos.Lexing.pos_bol in
    {
      message;
      start_line = start_pos.Lexing.pos_bol;
      start_pos = start_c;
      end_pos = end_c;
    }

  (** Extract the line in error from the whole expression, and print some
      characters just under the faulty part *)
  let get_line_error : error -> string -> string =
   fun error content ->
    let sub_text =
      try
        let end_pos = String.index_from content error.start_line '\n' in

        String.sub content ~pos:error.start_line
          ~len:(end_pos - error.start_line)
      with Not_found ->
        (* There is no new line, extract the ending part *)
        let len = String.length content - error.start_line in
        String.sub content ~pos:error.start_line ~len
    in
    (* I’m not sure how to produce it, but the error may be over two lines.
       This line is here to prevent the underline to overflow. *)
    let stop_pos = min error.end_pos (String.length sub_text) in
    let error_length = stop_pos - error.start_pos in
    String.concat ~sep:""
      [
        sub_text;
        "\n";
        String.make error.start_pos ' ';
        String.make error_length '^';
      ]

  let get_parse_error default_position env : error =
    match MI.stack env with
    | (lazy Nil) ->
        range_message default_position.Lexing.lex_start_p
          default_position.Lexing.lex_curr_p "Invalid syntax\n"
    | (lazy (Cons (MI.Element (state, _, start_pos, end_pos), _))) ->
        let message =
          try Expression_parser_messages.message (MI.number state)
          with Not_found ->
            "Invalid syntax (no specific message for this eror)\n"
        in
        range_message start_pos end_pos message

  let rec _parse lexbuf (checkpoint : 'a Model.Dsl.t MI.checkpoint) =
    match checkpoint with
    | MI.InputNeeded _env ->
        let token = Expression_lexer.token lexbuf in
        let startp = lexbuf.lex_start_p and endp = lexbuf.lex_curr_p in
        let checkpoint = MI.offer checkpoint (token, startp, endp) in
        _parse lexbuf checkpoint
    | MI.Shifting _ | MI.AboutToReduce _ ->
        let checkpoint = MI.resume checkpoint in
        _parse lexbuf checkpoint
    | MI.HandlingError _env ->
        let err = get_parse_error lexbuf _env in
        Error err
    | MI.Accepted v -> Ok v
    | MI.Rejected ->
        let err =
          range_message lexbuf.lex_start_p lexbuf.lex_curr_p
            "invalid syntax (parser rejected the input)"
        in
        Error err

  let of_string : string -> ('a Model.Dsl.t, string) result =
   fun str_expression ->
    let f = Expression_parser.Incremental.path_expr in
    try
      let lexbuf = Lexing.from_string str_expression in
      let init = f lexbuf.lex_curr_p in
      match _parse lexbuf init with
      | Ok res -> Ok res
      | Error e ->
          let message =
            String.concat ~sep:"\n"
              [ e.message; get_line_error e str_expression ]
          in
          Error message
    with
    | Expression_lexer.UnclosedQuote { line; content } ->
        let message =
          Printf.sprintf "Unclosed quote at line %d : \"%s\"" line content
        in
        Error message
    | Model.Dsl.UnknowFunction name ->
        let message =
          Printf.sprintf
            "Unknown function or wrong number of arguments for '%s'" name
        in

        Error message
    | e ->
        let message = Printexc.to_string e in
        Error message
end