lexer.mll 8.42 KB
Newer Older
1 2
(**************************************************************************)
(*                                                                        *)
Jean-Christophe Filliâtre's avatar
headers  
Jean-Christophe Filliâtre committed
3
(*  Copyright (C) 2010-                                                   *)
MARCHE Claude's avatar
MARCHE Claude committed
4 5 6
(*    François Bobot                                                     *)
(*    Jean-Christophe Filliâtre                                          *)
(*    Claude Marché                                                      *)
Jean-Christophe Filliâtre's avatar
headers  
Jean-Christophe Filliâtre committed
7
(*    Andrei Paskevich                                                    *)
8 9 10 11 12 13 14 15 16 17 18
(*                                                                        *)
(*  This software is free software; you can redistribute it and/or        *)
(*  modify it under the terms of the GNU Library General Public           *)
(*  License version 2.1, with the special exception on linking            *)
(*  described in file LICENSE.                                            *)
(*                                                                        *)
(*  This software is distributed in the hope that it will be useful,      *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  *)
(*                                                                        *)
(**************************************************************************)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
19 20

{
21
  open Format
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
22
  open Lexing
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
23
  open Term
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
24 25 26
  open Ptree
  open Parser

27 28
  (* lexical errors *)

29 30 31
  exception IllegalCharacter of char
  exception UnterminatedComment
  exception UnterminatedString
32
  exception AmbiguousPath of string * string
33

34
  let () = Exn_printer.register (fun fmt e -> match e with
35 36 37
    | IllegalCharacter c -> fprintf fmt "illegal character %c" c
    | UnterminatedComment -> fprintf fmt "unterminated comment"
    | UnterminatedString -> fprintf fmt "unterminated string"
38 39
    | AmbiguousPath (f1, f2) ->
        fprintf fmt "ambiguous path:@ both `%s'@ and `%s'@ match" f1 f2
40
    | _ -> raise e)
41

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
42
  let keywords = Hashtbl.create 97
43 44
  let () =
    List.iter
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
45
      (fun (x,y) -> Hashtbl.add keywords x y)
46
      [
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
47 48 49
	"and", AND;
	"as", AS;
	"axiom", AXIOM;
50
	"clone", CLONE;
51
	"else", ELSE;
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
52
	"end", END;
Andrei Paskevich's avatar
Andrei Paskevich committed
53
	"epsilon", EPSILON;
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
54
	"exists", EXISTS;
55
	"export", EXPORT;
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
56 57 58 59
        "false", FALSE;
	"forall", FORALL;
	"goal", GOAL;
	"if", IF;
60
	"import", IMPORT;
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
61 62
	"in", IN;
	"inductive", INDUCTIVE;
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
63
	"lemma", LEMMA;
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
64 65 66
	"let", LET;
	"logic", LOGIC;
	"match", MATCH;
67
	"meta", META;
68
	"namespace", NAMESPACE;
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
69 70
	"not", NOT;
	"or", OR;
71
        "prop", PROP;
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
72
	"then", THEN;
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
73
	"theory", THEORY;
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
74 75
	"true", TRUE;
	"type", TYPE;
76
	"use", USE;
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
77
	"with", WITH;
78
      ]
79

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
80 81
  let newline lexbuf =
    let pos = lexbuf.lex_curr_p in
82
    lexbuf.lex_curr_p <-
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
83 84
      { pos with pos_lnum = pos.pos_lnum + 1; pos_bol = pos.pos_cnum }

85
  let string_start_loc = ref Loc.dummy_position
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
86 87
  let string_buf = Buffer.create 1024

88 89
  let comment_start_loc = ref Loc.dummy_position

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
90 91 92 93 94 95 96 97
  let char_for_backslash = function
    | 'n' -> '\n'
    | 't' -> '\t'
    | c -> c

  let update_loc lexbuf file line chars =
    let pos = lexbuf.lex_curr_p in
    let new_file = match file with None -> pos.pos_fname | Some s -> s in
98
    lexbuf.lex_curr_p <-
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
99 100 101 102 103 104 105
      { pos with
	  pos_fname = new_file;
	  pos_lnum = int_of_string line;
	  pos_bol = pos.pos_cnum - int_of_string chars;
      }

  let remove_leading_plus s =
106
    let n = String.length s in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
107 108
    if n > 0 && s.[0] = '+' then String.sub s 1 (n-1) else s

109 110
  let loc lb = (lexeme_start_p lb, lexeme_end_p lb)

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
111 112 113 114
}

let newline = '\n'
let space = [' ' '\t' '\r']
115 116 117
let lalpha = ['a'-'z' '_']
let ualpha = ['A'-'Z']
let alpha = lalpha | ualpha
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
118
let digit = ['0'-'9']
119 120
let lident = lalpha (alpha | digit | '\'')*
let uident = ualpha (alpha | digit | '\'')*
121 122 123 124 125 126 127 128 129 130
let decimal_literal =
  ['0'-'9'] ['0'-'9' '_']*
let hex_literal =
  '0' ['x' 'X'] ['0'-'9' 'A'-'F' 'a'-'f']['0'-'9' 'A'-'F' 'a'-'f' '_']*
let oct_literal =
  '0' ['o' 'O'] ['0'-'7'] ['0'-'7' '_']*
let bin_literal =
  '0' ['b' 'B'] ['0'-'1'] ['0'-'1' '_']*
let int_literal =
  decimal_literal | hex_literal | oct_literal | bin_literal
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
131 132
let hexadigit = ['0'-'9' 'a'-'f' 'A'-'F']

133
let op_char_1 = ['=' '<' '>' '~']
134 135
let op_char_2 = ['+' '-']
let op_char_3 = ['*' '/' '%']
136
let op_char_4 = ['!' '$' '&' '?' '@' '^' '.' ':' '|' '#']
137 138 139 140
let op_char_34 = op_char_3 | op_char_4
let op_char_234 = op_char_2 | op_char_34
let op_char_1234 = op_char_1 | op_char_234

141 142
let op_char_pref = ['!' '?']

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
143 144 145 146
rule token = parse
  | "#" space* ("\"" ([^ '\010' '\013' '"' ]* as file) "\"")?
    space* (digit+ as line) space* (digit+ as char) space* "#"
      { update_loc lexbuf file line char; token lexbuf }
147
  | newline
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
148
      { newline lexbuf; token lexbuf }
149
  | space+
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
150
      { token lexbuf }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
151 152
  | '_'
      { UNDERSCORE }
153
  | lident as id
154
      { try Hashtbl.find keywords id with Not_found -> LIDENT id }
155
  | uident as id
156
      { UIDENT id }
157
  | int_literal as s
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
158 159 160 161
      { INTEGER s }
  | (digit+ as i) ("" as f) ['e' 'E'] (['-' '+']? digit+ as e)
  | (digit+ as i) '.' (digit* as f) (['e' 'E'] (['-' '+']? digit+ as e))?
  | (digit* as i) '.' (digit+ as f) (['e' 'E'] (['-' '+']? digit+ as e))?
162
      { FLOAT (RConstDecimal (i, f, Util.option_map remove_leading_plus e)) }
163
  | '0' ['x' 'X'] ((hexadigit* as i) '.' (hexadigit+ as f)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
164 165 166 167
                  |(hexadigit+ as i) '.' (hexadigit* as f)
		  |(hexadigit+ as i) ("" as f))
    ['p' 'P'] (['-' '+']? digit+ as e)
      { FLOAT (RConstHexa (i, f, remove_leading_plus e)) }
168 169
  | "(*)"
      { LEFTPAR_STAR_RIGHTPAR }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
170
  | "(*"
171
      { comment_start_loc := loc lexbuf; comment lexbuf; token lexbuf }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
172 173
  | "'"
      { QUOTE }
174 175
  | "`"
      { BACKQUOTE }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
176 177 178 179 180 181 182 183 184 185 186 187
  | ","
      { COMMA }
  | "("
      { LEFTPAR }
  | ")"
      { RIGHTPAR }
  | ":"
      { COLON }
  | "->"
      { ARROW }
  | "<->"
      { LRARROW }
188 189 190 191
  | "/\\"
      { ASYM_AND }
  | "\\/"
      { ASYM_OR }
Andrei Paskevich's avatar
Andrei Paskevich committed
192 193
  | "\\"
      { LAMBDA }
194 195 196 197
  | "\\?"
      { PRED }
  | "\\!"
      { FUNC }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
198 199
  | "."
      { DOT }
200 201 202 203
  | "|"
      { BAR }
  | "="
      { EQUAL }
204 205
  | "<>"
      { LTGT }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
206 207 208 209
  | "["
      { LEFTSQ }
  | "]"
      { RIGHTSQ }
210 211
  | op_char_pref op_char_4* as s
      { OPPREF s }
212 213 214 215 216 217 218 219
  | op_char_1234* op_char_1 op_char_1234* as s
      { OP1 s }
  | op_char_234*  op_char_2 op_char_234*  as s
      { OP2 s }
  | op_char_34*   op_char_3 op_char_34*  as s
      { OP3 s }
  | op_char_4+ as s
      { OP4 s }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
220
  | "\""
221
      { string_start_loc := loc lexbuf; STRING (string lexbuf) }
222
  | eof
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
223 224
      { EOF }
  | _ as c
225
      { raise (IllegalCharacter c) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
226 227

and comment = parse
228 229
  | "(*)"
      { comment lexbuf }
230
  | "*)"
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
231
      { () }
232
  | "(*"
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
233
      { comment lexbuf; comment lexbuf }
234
  | newline
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
235 236
      { newline lexbuf; comment lexbuf }
  | eof
237
      { raise (Loc.Located (!comment_start_loc, UnterminatedComment)) }
238
  | _
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
239 240 241 242
      { comment lexbuf }

and string = parse
  | "\""
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
243
      { let s = Buffer.contents string_buf in
244
	Buffer.clear string_buf;
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
245
	s }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
246 247
  | "\\" (_ as c)
      { Buffer.add_char string_buf (char_for_backslash c); string lexbuf }
248
  | newline
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
249 250
      { newline lexbuf; Buffer.add_char string_buf '\n'; string lexbuf }
  | eof
251
      { raise (Loc.Located (!string_start_loc, UnterminatedString)) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
252 253 254 255 256
  | _ as c
      { Buffer.add_char string_buf c; string lexbuf }

{
  let with_location f lb =
257 258
    if Debug.test_flag Debug.stack_trace then f lb else
    try f lb with
259 260
      | Loc.Located _ as e -> raise e
      | e -> raise (Loc.Located (loc lb, e))
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
261

262 263 264 265 266 267 268 269 270 271 272
  let parse_logic_file env = with_location (logic_file_eof env token)

  let parse_list0_decl env lenv uc =
    with_location (list0_decl_eof env lenv uc token)

  let parse_lexpr = with_location (lexpr_eof token)

  let read_channel env file c =
    let lb = Lexing.from_channel c in
    Loc.set_file file lb;
    parse_logic_file env lb
273

274 275
  (* searches file [f] in loadpath [lp] *)
  let locate_file lp f =
276
    let fl = List.map (fun dir -> Filename.concat dir f) lp in
277
    match List.filter Sys.file_exists fl with
278 279 280
      | [] -> raise Not_found
      | [file] -> file
      | file1 :: file2 :: _ -> raise (AmbiguousPath (file1, file2))
281 282 283 284

  let create_env lp =
    let ret_chan sl = 
      let f = List.fold_left Filename.concat "" sl in
285 286
      let file = locate_file lp f in
      file, open_in file
287 288 289 290 291 292 293 294 295 296 297
    in
    let retrieve env sl =
      let f = List.fold_left Filename.concat "" sl ^ ".why" in
      let file = locate_file lp f in
      let c = open_in file in
      try
	let tl = read_channel env file c in
	close_in c;
	tl
      with
	| e -> close_in c; raise e
298
    in
299
    Env.create_env ret_chan retrieve
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
300

301
  let () = Env.register_format "why" ["why"] read_channel
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
302 303 304
}

(*
305
Local Variables:
306
compile-command: "unset LANG; make -C ../.. test"
307
End:
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
308 309
*)