lexer.mll 1.74 KB
Newer Older
1
2
{
  open Parser
3
  exception Unexpected_char of string
Raphaël Cauderlier's avatar
Raphaël Cauderlier committed
4
5
6
7
8
9
  exception End_of_file_looking_for_double_quotes

  let chars_read = ref ""
  let add_char c = chars_read := Printf.sprintf "%s%c" !chars_read c

  let flush () = chars_read := ""
10
11
}

12
let id = [ 'a'-'z' '_' '0'-'9' ] ['a'-'z' 'A'-'Z' '_' '0'-'9']*
13

14
rule token = parse
Raphaël Cauderlier's avatar
Raphaël Cauderlier committed
15
    | [ ' ' '\t' ] { token lexbuf }
Raphaël Cauderlier's avatar
Raphaël Cauderlier committed
16
    | '\n' { Lexing.new_line lexbuf; token lexbuf }
17
18
19
20
    (* The several meanings of "." are disambiguated at lexing *)
    | '.' (id as s) ' '* "<=" { UPDATE (s) }
    | '.' (id as s) ' '* ":=" { FUPD(s) }
    | '.' (id as s) { SELECT (s) }
21
22
23
24
25
26
27
28
    | '[' { LBRACK }
    | ']' { RBRACK }
    | ':' { COLUMN }
    | ';' { SEMICOLUMN }
    | '(' { LPAR }
    | ')' { RPAR }
    | "ς" { SIGMA }
    | '=' { EQUAL }
Raphaël Cauderlier's avatar
Raphaël Cauderlier committed
29
    | "::=" { DEF }
30
31
32
    | '.' { DOT }
    | "->" { ARR }
    | "λ" { LAMBDA }
Raphaël Cauderlier's avatar
Raphaël Cauderlier committed
33
34
35
36
37
38
    | "Bool" { BOOL }
    | "true" { TRUE }
    | "false" { FALSE }
    | "if" { IF }
    | "then" { THEN }
    | "else" { ELSE }
39
    | "type" { TYPE (Parsing.rhs_start_pos 1) }
40
    | "let" { VAR (Parsing.rhs_start_pos 1) }
41
42
    | "check" { CHECK (Parsing.rhs_start_pos 1) }
    | "norm" { NORM (Parsing.rhs_start_pos 1) }
Raphaël Cauderlier's avatar
Raphaël Cauderlier committed
43
    | "print" { PRINT (Parsing.rhs_start_pos 1) }
44
    | id as s { ID (s) }
45
    | [ 'A'-'Z' ] ['a'-'z' 'A'-'Z' '_' '0'-'9']* as s { CID (s) }
Raphaël Cauderlier's avatar
Raphaël Cauderlier committed
46
    | '"' { flush (); string lexbuf }
47
    | _ as c { raise (Unexpected_char (Printf.sprintf"'%c'" c)) }
48
    | eof { raise End_of_file }
Raphaël Cauderlier's avatar
Raphaël Cauderlier committed
49
50
51
52
53
54
55
56

and string = parse
    | "\\n" { add_char '\n'; string lexbuf }
    | '\\' (_ as c) { add_char '\\'; add_char c; string lexbuf }
    | '\n' { Lexing.new_line lexbuf ; add_char '\n'; string lexbuf }
    | '"'  { STRING (!chars_read) }
    | _ as c { add_char c; string lexbuf }
    | eof { raise End_of_file_looking_for_double_quotes }