aboutsummaryrefslogtreecommitdiff
path: root/lib/lexer.mll
blob: 1985c2d383746e38764a1764edcadacf148e52b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{
open Parser

exception SyntaxError of string
}

let digit = ['0'-'9']
let frac = '.' digit*
let exp = ['e' 'E'] ['-' '+']? digit+
let float = digit* frac? exp?
let int = digit+
let white = [' ' '\t']+
let newline = '\r' | '\n' | "\r\n"
let id = ['a'-'z' 'A'-'Z' '-' '_']*
let newline = '\r' | '\n' | "\r\n"
let many_newline = newline+

rule read = 
  parse 
  | "uuidv4" { UUIDV4 }
  | "email" { EMAIL }
  | "name" { NAME }
  | "increment" { INCREMENT }
  | "username" { USERNAME }
  | "int" { INTSYMBOL } 
  | id { IDENTIFIER (Lexing.lexeme lexbuf) }
  | "(" { LBRACE }
  | ")" { RBRACE }
  | '"' { read_string (Buffer.create 20) lexbuf }
  | int { INT (Lexing.lexeme lexbuf |> int_of_string) }
  | white { read lexbuf } 
  | ":" { COLON }
  | ";" { SEMICOLON }
  | "," { COMMA }
  | many_newline { NEWLINE }
  | "." { DOT }
  | eof { EOF } 
	| _ as c { failwith (Printf.sprintf "unexpected character: %C" c) }

and read_string buf =
  parse
  | '"'       { STRING (Buffer.contents buf) }
  | '\\' '/'  { Buffer.add_char buf '/'; read_string buf lexbuf }
  | '\\' '\\' { Buffer.add_char buf '\\'; read_string buf lexbuf }
  | '\\' 'b'  { Buffer.add_char buf '\b'; read_string buf lexbuf }
  | '\\' 'f'  { Buffer.add_char buf '\012'; read_string buf lexbuf }
  | '\\' 'n'  { Buffer.add_char buf '\n'; read_string buf lexbuf }
  | '\\' 'r'  { Buffer.add_char buf '\r'; read_string buf lexbuf }
  | '\\' 't'  { Buffer.add_char buf '\t'; read_string buf lexbuf }
  | [^ '"' '\\']+
    { Buffer.add_string buf (Lexing.lexeme lexbuf);
      read_string buf lexbuf
    }
  | _ { raise (SyntaxError ("Illegal string character: " ^ Lexing.lexeme lexbuf)) }
  | eof { raise (SyntaxError ("String is not terminated")) }