driver_lexer.mll 3.64 KB
Newer Older
Andrei Paskevich's avatar
Andrei Paskevich committed
1 2 3
(********************************************************************)
(*                                                                  *)
(*  The Why3 Verification Platform   /   The Why3 Development Team  *)
4
(*  Copyright 2010-2013   --   INRIA - CNRS - Paris-Sud University  *)
Andrei Paskevich's avatar
Andrei Paskevich committed
5 6 7 8 9 10
(*                                                                  *)
(*  This software is distributed under the terms of the GNU Lesser  *)
(*  General Public License version 2.1, with the special exception  *)
(*  on linking described in file LICENSE.                           *)
(*                                                                  *)
(********************************************************************)
11 12

{
Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
13
  open Format
14 15
  open Lexing
  open Driver_parser
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
16
  open Lexer
Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
17

18 19 20 21 22 23
  exception IllegalCharacter of char

  let () = Exn_printer.register (fun fmt e -> match e with
    | IllegalCharacter c -> fprintf fmt "illegal character %c" c
    | _ -> raise e)

Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
24
  let keywords = Hashtbl.create 97
25 26
  let () =
    List.iter
Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
27 28
      (fun (x,y) -> Hashtbl.add keywords x y)
      [ "theory", THEORY;
29 30 31 32 33 34 35 36
        "end", END;
        "syntax", SYNTAX;
        "remove", REMOVE;
        "meta", META;
        "prelude", PRELUDE;
        "printer", PRINTER;
        "valid", VALID;
        "invalid", INVALID;
37
        "timeout", TIMEOUT;
38
        "outofmemory", OUTOFMEMORY;
39
        "time",    TIME;
40 41
        "unknown", UNKNOWN;
        "fail", FAIL;
42
        "constant", CONSTANT;
43 44
        "function", FUNCTION;
        "predicate", PREDICATE;
45 46
        "type", TYPE;
        "prop", PROP;
47
        "filename", FILENAME;
48
        "transformation", TRANSFORM;
49 50 51 52 53 54
        "plugin", PLUGIN;
        "blacklist", BLACKLIST;
        (* WhyML *)
        "module", MODULE;
        "exception", EXCEPTION;
        "val", VAL;
Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
55 56
      ]

57 58
}

Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
59 60 61 62 63
let space = [' ' '\t' '\r']
let alpha = ['a'-'z' 'A'-'Z' '_']
let digit = ['0'-'9']
let ident = alpha (alpha | digit | '\'')*

64 65 66
let op_char = ['=' '<' '>' '~' '+' '-' '*' '/' '%'
               '!' '$' '&' '?' '@' '^' '.' ':' '|' '#']

67
rule token = parse
Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
68 69
  | '\n'
      { newline lexbuf; token lexbuf }
70
  | space+
Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
71
      { token lexbuf }
72 73
  | "(*)"
      { LEFTPAR_STAR_RIGHTPAR }
Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
74 75
  | "(*"
      { comment lexbuf; token lexbuf }
76
  | '_'
Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
77 78 79
      { UNDERSCORE }
  | ident as id
      { try Hashtbl.find keywords id with Not_found -> IDENT id }
80 81
  | digit+ as i
      { INTEGER (int_of_string i) }
82 83 84 85 86 87
  | "<-"
      { LARROW }
  | "["
      { LEFTSQ }
  | "]"
      { RIGHTSQ }
Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
88 89 90 91 92 93
  | "("
      { LEFTPAR }
  | ")"
      { RIGHTPAR }
  | "."
      { DOT }
94 95
  | ","
      { COMMA }
96 97
  | "'"
      { QUOTE }
98 99
  | op_char+ as op
      { OPERATOR op }
Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
100
  | "\""
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
101
      { STRING (string lexbuf) }
102 103
  | "import" space*  "\""
      { INPUT (string lexbuf) }
104
  | eof
Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
105 106
      { EOF }
  | _ as c
107
      { raise (IllegalCharacter c) }
Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
108 109 110

{

111
  let parse_file_gen parse input_lexbuf lexbuf =
112 113 114 115 116 117 118 119
    let s = Stack.create () in
    Stack.push lexbuf s;
    let rec multifile lex_dumb =
      let lexbuf = Stack.top s in
      let tok = token lexbuf in
      Loc.transfer_loc lexbuf lex_dumb;
      match tok with
        | INPUT filename ->
120 121
          let dirname = Filename.dirname lexbuf.lex_curr_p.pos_fname in
          let filename = Sysutil.absolutize_filename dirname filename in
122 123 124 125 126 127 128
          Stack.push (input_lexbuf filename) s;
          multifile lex_dumb
        | EOF -> ignore (Stack.pop s);
          if Stack.is_empty s then EOF else multifile lex_dumb
        | _ -> tok in
    let lex_dumb = Lexing.from_function (fun _ _ -> assert false) in
    Loc.transfer_loc lexbuf lex_dumb;
129
    Loc.with_location (parse multifile) lex_dumb
130 131 132

  let parse_file = parse_file_gen Driver_parser.file
  let parse_file_extract = parse_file_gen Driver_parser.file_extract
Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
133
}