-
Notifications
You must be signed in to change notification settings - Fork 3
/
lexer.mll
58 lines (50 loc) · 1.52 KB
/
lexer.mll
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
56
57
58
{
open Lexing
open Parser
exception SyntaxError of string
let next_line lexbuf =
let pos = lexbuf.lex_curr_p in
lexbuf.lex_curr_p <-
{ pos with pos_bol = lexbuf.lex_curr_pos;
pos_lnum = pos.pos_lnum + 1
}
}
let unitt = "()"
let ident = ['a'-'z' 'A'-'Z' '_']+
let whitespace = [' ' '\t']+
let newline = '\r' | '\n' | "\r\n"
rule read =
parse
| whitespace { read lexbuf }
| newline { next_line lexbuf; read lexbuf }
| unitt { Tok_Unit }
| '(' { Tok_LParen }
| ')' { Tok_RParen }
| "let" { Tok_Let }
| "in" { Tok_In }
| '=' { Tok_Eq }
| '.' { Tok_Dot }
| '\\' { Tok_Backslash }
| ident { Tok_Ident (Lexing.lexeme lexbuf) }
| eof { Tok_EOF }
{
let print_position lexbuf =
let pos = lexbuf.lex_curr_p in
Printf.printf "%s:%d:%d" pos.pos_fname
pos.pos_lnum (pos.pos_cnum - pos.pos_bol + 1)
let parse_with_error lexbuf =
try Some (Parser.parse_expr read lexbuf) with
| SyntaxError msg ->
print_position lexbuf;
Printf.printf ": %s\n" msg;
None
| Parser.Error ->
print_position lexbuf;
print_endline ": syntax error";
None
let rec parse str =
let lexbuf = Lexing.from_string str in
match parse_with_error lexbuf with
| Some value -> value
| None -> Unit
}