pgm_lexer.mll 5.93 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.                  *)
(*                                                                        *)
(**************************************************************************)

{
21
  open Format
22
  open Lexing
23
  open Why
24 25 26 27
  open Lexer
  open Term
  open Pgm_parser

28 29 30 31 32 33 34 35
  type error = 
    | UnterminatedLogic

  exception Error of error

  let report fmt = function
    | UnterminatedLogic -> fprintf fmt "unterminated logic block"

36 37 38 39 40 41
  let keywords = Hashtbl.create 97
  let () = 
    List.iter 
      (fun (x,y) -> Hashtbl.add keywords x y)
      [ "absurd", ABSURD;
	"and", AND;
42
	"any", ANY;
43 44
	"as", AS;
	"assert", ASSERT;
45
	"assume", ASSUME;
46 47 48 49 50 51 52 53 54
	"begin", BEGIN;
        "check", CHECK;
	"do", DO;
	"done", DONE;
        "else", ELSE;
	"end", END;
	"exception", EXCEPTION; 
	"for", FOR;
	"fun", FUN;
55
	"ghost", GHOST;
56 57 58
	"if", IF;
	"in", IN;
	"invariant", INVARIANT;
59
	"label", LABEL;
60 61 62
	"let", LET;
	"match", MATCH;
	"not", NOT;
63
	"of", OF;
64
	"parameter", PARAMETER;
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	"raise", RAISE;
	"raises", RAISES;
	"reads", READS;
	"rec", REC;
	"then", THEN;
	"try", TRY;
	"type", TYPE;
	"variant", VARIANT;
	"while", WHILE;
	"with", WITH;
        "writes", WRITES;
      ]
	
  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;
      }

88 89 90 91 92
  let logic_start_loc = ref Loc.dummy_position
  let logic_buffer = Buffer.create 1024

  let loc lb = (lexeme_start_p lb, lexeme_end_p lb)

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
}

let newline = '\n'
let space = [' ' '\t' '\r']
let lalpha = ['a'-'z' '_']
let ualpha = ['A'-'Z']
let alpha = lalpha | ualpha
let digit = ['0'-'9']
let lident = lalpha (alpha | digit | '\'')*
let uident = ualpha (alpha | digit | '\'')*
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
let hexadigit = ['0'-'9' 'a'-'f' 'A'-'F']

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 }
  | '_'
      { UNDERSCORE }
  | lident as id  
      { try Hashtbl.find keywords id with Not_found -> LIDENT id }
  | uident as id  
      { UIDENT id }
  | int_literal as s
      { 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))?
134
      { REAL (RConstDecimal (i, f, Util.option_map remove_leading_plus e)) }
135 136 137 138
  | '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)
139
      { REAL (RConstHexa (i, f, remove_leading_plus e)) }
140 141 142 143 144 145 146 147 148 149 150 151 152 153
  | "(*"
      { comment lexbuf; token lexbuf }
  | "'"
      { QUOTE }
  | ","
      { COMMA }
  | "("
      { LEFTPAR }
  | ")"
      { RIGHTPAR }
  | ":"
      { COLON }
  | ";"
      { SEMICOLON }
154 155
  | "!"
      { BANG }
156 157 158 159 160 161
  | ":="
      { COLONEQUAL }
  | "->"
      { ARROW }
  | "="
      { EQUAL }
162 163 164 165 166 167 168 169 170 171
  | "<>"
      { OP0 "ne" }
  | "<"
      { OP0 "lt" }
  | "<="
      { OP0 "le" }
  | ">"
      { OP0 "gt" }
  | ">="
      { OP0 "ge" }
172 173
  | "+" | "-" as c
      { OP2 (String.make 1 c) }
174
  | "*" | "/" | "%" as c
175 176 177 178 179 180 181 182 183 184
      { OP3 (String.make 1 c) }
  | "@"
      { AT }
  | "."
      { DOT }
  | "["
      { LEFTSQ }
  | "]"
      { RIGHTSQ }
  | "{"
185 186
      { logic_start_loc := loc lexbuf; 
	let s = logic lexbuf in
187
	LOGIC (fst !logic_start_loc, s) }
188 189 190
  (* FIXME: allow newlines as well *)
  | "{" space* "}"
      { LOGIC (lexeme_start_p lexbuf, "true") }
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  | "{{"
      { LEFTBLEFTB }
  | "}}"
      { RIGHTBRIGHTB }
  | "|"
      { BAR }
  | "||"
      { BARBAR }
  | "&&" 
      { AMPAMP }
  | "=>"
      { BIGARROW }
  | "\""
      { STRING (string lexbuf) }
  | eof 
      { EOF }
  | _ as c
208
      { raise (Lexer.Error (IllegalCharacter c)) }
209

210 211 212 213 214 215 216 217 218 219 220
and logic = parse
  | "}"
      { let s = Buffer.contents logic_buffer in
	Buffer.clear logic_buffer;
	s }
  | newline
      { newline lexbuf; Buffer.add_char logic_buffer '\n'; logic lexbuf }
  | eof
      { raise (Loc.Located (!logic_start_loc, Error UnterminatedLogic)) }
  | _ as c
      { Buffer.add_char logic_buffer c; logic lexbuf }
221 222 223 224 225 226 227 228 229 230


{

  let parse_file = with_location (file token)

}

(*
Local Variables: 
231
compile-command: "unset LANG; make -C ../.. testl"
232 233 234
End: 
*)