summaryrefslogtreecommitdiff
path: root/ocamltest/tsl_lexer.mll
blob: b643509eca5957c25e0dc06c0961799979e4dd40 (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
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
(**************************************************************************)
(*                                                                        *)
(*                                 OCaml                                  *)
(*                                                                        *)
(*             Sebastien Hinderer, projet Gallium, INRIA Paris            *)
(*                                                                        *)
(*   Copyright 2016 Institut National de Recherche en Informatique et     *)
(*     en Automatique.                                                    *)
(*                                                                        *)
(*   All rights reserved.  This file is distributed under the terms of    *)
(*   the GNU Lesser General Public License version 2.1, with the          *)
(*   special exception on linking described in the file LICENSE.          *)
(*                                                                        *)
(**************************************************************************)

(* Lexer definitions for the Tests Specification Language and for
   response files *)

{
open Tsl_parser

let comment_start_pos = ref []
let has_comments = ref false

let lexer_error message =
  failwith (Printf.sprintf "Tsl lexer: %s" message)
}

let newline = ('\013'* '\010')
let blank = [' ' '\009' '\012']
let identchar = ['A'-'Z' 'a'-'z' '_' '.' '-' '\'' '0'-'9']
let num = ['0'-'9']

rule is_test = parse
  | blank * { is_test lexbuf }
  | newline { Lexing.new_line lexbuf; is_test lexbuf }
  | "/*" blank* "TEST" { true }
  | "/*" blank* "TEST_BELOW" { true }
  | "(*" blank* "TEST" { true }
  | "(*" blank* "TEST_BELOW" { true }
  | _ { false }
  | eof { false }

and token = parse
  | blank * { token lexbuf }
  | newline { Lexing.new_line lexbuf; token lexbuf }
  | "/*" blank* "TEST" { TSL_BEGIN_C_STYLE `Above }
  | "/*" blank* "TEST_BELOW" _ * "/*" blank* "TEST" {
      let s = Lexing.lexeme lexbuf in
      String.iter (fun c -> if c = '\n' then Lexing.new_line lexbuf) s;
      TSL_BEGIN_C_STYLE `Below
    }
  | "*/" { TSL_END_C_STYLE }
  | "(*" blank* "TEST" { TSL_BEGIN_OCAML_STYLE `Above }
  | "(*" blank* "TEST_BELOW" _ * "(*" blank* "TEST" {
      let s = Lexing.lexeme lexbuf in
      String.iter (fun c -> if c = '\n' then Lexing.new_line lexbuf) s;
      TSL_BEGIN_OCAML_STYLE `Below
    }
  | "*)" { TSL_END_OCAML_STYLE }
  | "," { COMMA }
  | '*'+ { TEST_DEPTH (String.length (Lexing.lexeme lexbuf)) }
  | "*" (num+ as n) { TEST_DEPTH (int_of_string n)}
  | "+=" { PLUSEQUAL }
  | "=" { EQUAL }
  | identchar *
    { let s = Lexing.lexeme lexbuf in
      match s with
        | "include" -> INCLUDE
        | "set" -> SET
        | "unset" -> UNSET
        | "with" -> WITH
        | _ -> IDENTIFIER s
    }
  | "{" { LEFT_BRACE }
  | "}" { RIGHT_BRACE }
  | ";" { SEMI }
  | "(*"
    {
      comment_start_pos := [Lexing.lexeme_start_p lexbuf];
      has_comments := true;
      comment lexbuf
    }
  | '"'
    { STRING (string "" lexbuf) }
  | _
    {
      let pos = Lexing.lexeme_start_p lexbuf in
      let file = pos.Lexing.pos_fname in
      let line = pos.Lexing.pos_lnum in
      let column = pos.Lexing.pos_cnum - pos.Lexing.pos_bol in
      let message = Printf.sprintf "%s:%d:%d: unexpected character %s"
        file line column (Lexing.lexeme lexbuf) in
      lexer_error message
    }
  | eof
    { lexer_error "unexpected eof" }
(* Backslashes are ignored in strings except at the end of lines where they
   cause the newline to be ignored. After an escaped newline, any blank
   characters at the start of the line are ignored and optionally one blank
   character may be escaped with a backslash.

   In particular, this means that the following:
script = "some-directory\\
         \ foo"
   is interpreted as the OCaml string "some-directory\\ foo".
   *)
and string acc = parse
  | [^ '\\' '"' ]+
    { string (acc ^ Lexing.lexeme lexbuf) lexbuf }
  | '\\' newline blank* ('\\' (blank as blank))?
    { let space =
        match blank with None -> "" | Some blank -> String.make 1 blank
      in
      string (acc ^ space) lexbuf }
  | '\\'
    {string (acc ^ "\\") lexbuf}
  | '"'
    {acc}
and comment = parse
  | "(*"
    {
      comment_start_pos :=
        (Lexing.lexeme_start_p lexbuf) :: !comment_start_pos;
      comment lexbuf
    }
  | "*)"
    {
      comment_start_pos := List.tl !comment_start_pos;
      if !comment_start_pos = [] then token lexbuf else comment lexbuf
    }
  | eof
    {
      let pos = List.hd !comment_start_pos in
      let file = pos.Lexing.pos_fname in
      let line = pos.Lexing.pos_lnum in
      let column = pos.Lexing.pos_cnum - pos.Lexing.pos_bol in
      let message = Printf.sprintf "%s:%d:%d: unterminated comment"
        file line column in
      lexer_error message
    }
  | _
    {
      comment lexbuf
    }

(* Parse one line of a response file (for scripts and hooks) *)
and modifier = parse
  | '-' (identchar* as variable)
    { variable, `Remove }
  | (identchar* as variable) "=\"" (_* as str) '"'
    { variable, `Add str }
  | (identchar* as variable) "+=\"" (_* as str) '"'
    { variable, `Append str }
  | _
    { failwith "syntax error in script response file" }