driver_lexer.mll 2.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
(**************************************************************************)
(*                                                                        *)
(*  Copyright (C) 2010-                                                   *)
(*    Francois Bobot                                                      *)
(*    Jean-Christophe Filliatre                                           *)
(*    Johannes Kanig                                                      *)
(*    Andrei Paskevich                                                    *)
(*                                                                        *)
(*  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
drivers  
Jean-Christophe Filliâtre committed
21
  open Format
22 23
  open Lexing
  open Driver_parser
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
24
  open Lexer
Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
25 26

  let keywords = Hashtbl.create 97
27 28
  let () =
    List.iter
Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
29 30 31 32 33
      (fun (x,y) -> Hashtbl.add keywords x y)
      [ "theory", THEORY;
	"end", END;
	"syntax", SYNTAX;
	"remove", REMOVE;
34
	"meta", META;
35
	"cloned", CLONED;
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
36 37 38 39
	"prelude", PRELUDE;
	"printer", PRINTER;
	"valid", VALID;
	"invalid", INVALID;
40
        "timeout", TIMEOUT;
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
41 42
	"unknown", UNKNOWN;
	"fail", FAIL;
43 44 45
        "logic", LOGIC;
        "type", TYPE;
        "prop", PROP;
46
        "filename", FILENAME;
47
        "transformation", TRANSFORM;
48
        "plugin", PLUGIN
Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
49 50
      ]

51 52
}

Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
53 54 55 56 57
let space = [' ' '\t' '\r']
let alpha = ['a'-'z' 'A'-'Z' '_']
let digit = ['0'-'9']
let ident = alpha (alpha | digit | '\'')*

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

61
rule token = parse
Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
62 63
  | '\n'
      { newline lexbuf; token lexbuf }
64
  | space+
Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
65
      { token lexbuf }
66 67
  | "(*)"
      { LEFTPAR_STAR_RIGHTPAR }
Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
68 69
  | "(*"
      { comment lexbuf; token lexbuf }
70
  | '_'
Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
71 72 73
      { UNDERSCORE }
  | ident as id
      { try Hashtbl.find keywords id with Not_found -> IDENT id }
74 75
  | digit+ as i
      { INTEGER (int_of_string i) }
Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
76 77 78 79 80 81
  | "("
      { LEFTPAR }
  | ")"
      { RIGHTPAR }
  | "."
      { DOT }
82 83 84 85
  | ","
      { COMMA }
  | op_char+ as op
      { OPERATOR op }
Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
86
  | "\""
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
87
      { STRING (string lexbuf) }
88
  | eof
Jean-Christophe Filliâtre's avatar
drivers  
Jean-Christophe Filliâtre committed
89 90 91 92 93 94 95 96 97 98 99 100
      { EOF }
  | _ as c
      { raise (Error (IllegalCharacter c)) }

{
  let loc lb = (lexeme_start_p lb, lexeme_end_p lb)

  let with_location f lb =
    try f lb with e -> raise (Loc.Located (loc lb, e))

  let parse_file = with_location (Driver_parser.file token)
}