lexer.mll 7.14 KB
Newer Older
1 2
(**************************************************************************)
(*                                                                        *)
Jean-Christophe Filliâtre's avatar
headers  
Jean-Christophe Filliâtre committed
3 4 5 6 7
(*  Copyright (C) 2010-                                                   *)
(*    Francois Bobot                                                      *)
(*    Jean-Christophe Filliatre                                           *)
(*    Johannes Kanig                                                      *)
(*    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

33
  let () = Exn_printer.register (fun fmt e -> match e with
34 35 36
    | IllegalCharacter c -> fprintf fmt "illegal character %c" c
    | UnterminatedComment -> fprintf fmt "unterminated comment"
    | UnterminatedString -> fprintf fmt "unterminated string"
37
    | _ -> raise e)
38

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

82
  let string_start_loc = ref Loc.dummy_position
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
83 84
  let string_buf = Buffer.create 1024

85 86
  let comment_start_loc = ref Loc.dummy_position

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  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
    lexbuf.lex_curr_p <- 
      { 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 =
    let n = String.length s in 
    if n > 0 && s.[0] = '+' then String.sub s 1 (n-1) else s

106 107
  let loc lb = (lexeme_start_p lb, lexeme_end_p lb)

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
108 109 110 111
}

let newline = '\n'
let space = [' ' '\t' '\r']
112 113 114
let lalpha = ['a'-'z' '_']
let ualpha = ['A'-'Z']
let alpha = lalpha | ualpha
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
115
let digit = ['0'-'9']
116 117
let lident = lalpha (alpha | digit | '\'')*
let uident = ualpha (alpha | digit | '\'')*
118 119 120 121 122 123 124 125 126 127
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
128 129
let hexadigit = ['0'-'9' 'a'-'f' 'A'-'F']

130
let op_char_1 = ['=' '<' '>' '~']
131 132
let op_char_2 = ['+' '-']
let op_char_3 = ['*' '/' '%']
133
let op_char_4 = ['!' '$' '&' '?' '@' '^' '.' ':' '|' '#']
134 135 136 137
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

138 139
let op_char_pref = ['!' '?']

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

and comment = parse
  | "*)" 
      { () }
  | "(*" 
      { comment lexbuf; comment lexbuf }
  | newline 
      { newline lexbuf; comment lexbuf }
  | eof
224
      { raise (Loc.Located (!comment_start_loc,UnterminatedComment)) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
225 226 227 228 229
  | _ 
      { comment lexbuf }

and string = parse
  | "\""
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
230 231 232
      { let s = Buffer.contents string_buf in
	Buffer.clear string_buf; 
	s }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
233 234 235 236 237
  | "\\" (_ as c)
      { Buffer.add_char string_buf (char_for_backslash c); string lexbuf }
  | newline 
      { newline lexbuf; Buffer.add_char string_buf '\n'; string lexbuf }
  | eof
238
      { raise (Loc.Located (!string_start_loc, UnterminatedString)) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
239 240 241 242 243 244 245 246
  | _ as c
      { Buffer.add_char string_buf c; string lexbuf }



{

  let with_location f lb =
247 248 249 250 251
    try 
      f lb 
    with 
      | Loc.Located _ as e -> raise e
      | e -> raise (Loc.Located (loc lb, e))
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
252

253 254
  let parse_logic_file = with_location (logic_file token)

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
255
  let parse_lexpr = with_location (lexpr token)
256
  let parse_list0_decl = with_location (list0_decl token)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
257 258 259 260 261

}

(*
Local Variables: 
262
compile-command: "unset LANG; make -C ../.. test"
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
263 264 265
End: 
*)