-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.mll
92 lines (85 loc) · 1.56 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
{
open Parser
exception Error of string
}
(* This rule looks for a single line, terminated with '\n' or eof.
It returns a pair of an optional string (the line that was found)
and a Boolean flag (false if eof was reached). *)
rule line = parse
| ([^'\n']* '\n') as line
(* Normal case: one line, no eof. *)
{ Some line, true }
| eof
(* Normal case: no data, eof. *)
{ None, false }
| ([^'\n']+ as line) eof
(* Special case: some data but missing '\n', then eof.
Consider this as the last line, and add the missing '\n'. *)
{ Some (line ^ "\n"), false }
(* This rule analyzes a single line and turns it into a stream of
tokens. *)
and token = parse
| [' ' '\t']
{ token lexbuf }
| '\n'
{ EOL }
| ['0'-'9']+ as i
{ INT (int_of_string i) }
| ['0'-'9']+ '.' ['0'-'9']+ as f
{ FLOAT (float_of_string f) }
| '+'
{ PLUS }
| '-'
{ MINUS }
| '*'
{ TIMES }
| '/'
{ DIV }
| '('
{ LPAREN }
| ';'
{ SEMICOLON }
| ':'
{ COLON }
| ')'
{ RPAREN }
| "<"
{ LESSTHAN }
| ">"
{ GREATERTHAN }
| "="
{ EQUAL }
| "fd"
{ FORWARD }
| "bk"
{ BACKWARD }
| "forward"
{ FORWARD }
| "pd"
{ PENDOWN }
| "pendown"
{ PENDOWN }
| "repeat"
{ REPEAT }
| "pu"
{ PENUP }
| "if"
{ IF }
| "end"
{ END }
| "stop"
{ STOP }
| "print"
{ PRINT }
| "pendown"
{ PENUP }
| "rt"
{ TURNRIGHT }
| "lt"
{ TURNLEFT }
| "to"
{ TO }
| ['a'-'z']+ as s
{ ID (s) }
| _
{ raise (Error (Printf.sprintf "At offset %d: unexpected character.\n" (Lexing.lexeme_start lexbuf))) }