-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexer.fsl
48 lines (45 loc) · 1.15 KB
/
Lexer.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
38
39
40
41
42
43
44
45
46
47
48
{
open FSharp.Text.Lexing
open Parser
let lexeme lexbuf = LexBuffer<_>.LexemeString lexbuf
}
let TRUE = "true"
let FALSE = "false"
let NULL = "null"
let char = ['a'-'z' 'A'-'Z']
let digit = [ '0'-'9' ]
let int = digit+
let identifier = char(char|digit|'_')*
let strliteral = '"'[^'"']*'"'
let whitespace = [ ' ' '\t' ]
let newline = '\n' | 'r' | "\n\r"
rule tokenize = parse
| whitespace { tokenize lexbuf; }
| newline { lexbuf. EndPos <- lexbuf.EndPos.NextLine; tokenize lexbuf; }
| '+' { ADD }
| '-' { SUB }
| ';' { SEMICOLON }
| ',' { COMMA }
| '=' { EQ }
| "==" { EQUAL }
| "!=" { NOTEQ }
| '<' { LT }
| '>' { GT }
| "<=" { LE }
| ">=" { GE }
| '(' { LPAR }
| ')' { RPAR }
| '[' { LSQR }
| ']' { RSQR }
| '{' { LCUR }
| '}' { RCUR }
| TRUE { TrueTok }
| FALSE { FalseTok }
| NULL { NullTok }
| "let" { LET }
| "fn" { FN }
| int { INT <| lexeme lexbuf }
| identifier { ID <| lexeme lexbuf }
| strliteral { let s = lexeme lexbuf in s.[1..((String.length s) - 2)] |> STR }
| eof { EOF }
| _ { INVALID }