-
Notifications
You must be signed in to change notification settings - Fork 0
/
TinyLex.fsl
37 lines (34 loc) · 1.1 KB
/
TinyLex.fsl
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
{
module TinyLex
open TinyParser
open Microsoft.FSharp.Text.Lexing
let keywords =
[ "read", READ; "not", NOT; "output", OUTPUT;
"if", IF; "then", THEN; "else", ELSE; "fi", FI;
"while", WHILE; "do", DO; "od", OD; ] |> Map.ofList
}
let digit = ['0'-'9']
let number = digit+
let whitespace = [' ' '\t']
let newline = "\r\n" | '\n' | '\r'
let bool_true = "true"
let bool_false = "false"
let letter = ['a'-'z' 'A'-'Z' '_']
let id = letter (letter | digit)*
rule tokenize = parse
| whitespace { tokenize lexbuf }
| newline { tokenize lexbuf }
| number { NUM(System.Convert.ToInt32(
LexBuffer<_>.LexemeString lexbuf)) }
| bool_true { BOOL(true) }
| bool_false { BOOL(false) }
| id { let s = LexBuffer<_>.LexemeString
lexbuf
match keywords.TryFind(s) with
| Some(token) -> token
| None -> ID(s) }
| '=' { EQU }
| '+' { PLUS }
| ":=" { ASSIGN }
| ';' { SEMI }
| eof { EOF }