-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexer.fsl
96 lines (77 loc) · 2.17 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
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
92
93
{
open System
open FSharp.Text.Lexing
open FSharp.Common.Parsing
open FSharp.Common.Parsing.LexYacc
open TinyML.Ast
open TinyML.Parser
let trim c lexbuf = let s = lexeme lexbuf in s.TrimStart [|c|]
}
let whitespace = [' ' '\t' ]
let newline = ('\n' | "\r\n")
let digit = ['0'-'9']
let nat = digit+
let ureal = digit* '.' digit+ | digit+ '.' digit*
let sreal = ['-']? ureal
let real = sreal | sreal 'e' int | int 'e' int | sreal 'f'
let int = ['-']? nat
let long = int 'l'
let quoted = "\"" [^'"']* "\""
let idbody = ['a'-'z' 'A'-'Z' '0'-'9' '_']*['\'']*
let Uid = ['A'-'Z'] idbody
let Lid = ['a'-'z' '_'] idbody
let id = Uid | Lid
rule comment level = parse
| "(*" { comment (level + 1) lexbuf }
| "*)" { if level = 0 then tokenize lexbuf else comment (level - 1) lexbuf }
| "*)" { tokenize lexbuf }
| newline { newline lexbuf; comment level lexbuf }
| _ { comment level lexbuf }
and linecomment = parse
| newline { newline lexbuf; tokenize lexbuf }
| _ { linecomment lexbuf }
and tokenize = parse
| eof { EOF }
| whitespace { tokenize lexbuf }
| newline { newline lexbuf; tokenize lexbuf }
| "//" { linecomment lexbuf }
| "(*" { comment 0 lexbuf }
| '+' { PLUS }
| '-' { MINUS }
| '*' { STAR }
| '/' { SLASH }
| '%' { PERCENT }
| '=' { EQ }
| "<>" { NEQ }
| '<' { LT }
| '>' { GT }
| "<=" { LEQ }
| ">=" { GEQ }
| "or" { OR }
| "and" { AND }
| "not" { NOT }
// keywords
| "if" { IF }
| "then" { THEN }
| "else" { ELSE }
| "true" { TRUE }
| "false" { FALSE }
| "fun" { FUN }
| "->" { ARROW }
| "let" { LET }
| "rec" { REC }
| "in" { IN }
// brakets
| '(' { BRA }
| ')' { KET }
// punctuation
| ':' { COLON }
| ";;" { SEMICOLON2 }
| ',' { COMMA }
// literals
| "\"" [^'"']* "\"" { let s = lexeme lexbuf in STRING (s.Trim [|'\"'|]) }
| '\'' [^'\''] '\'' { let s = lexeme lexbuf in CHAR ((s.Trim [|'\''|]).Chars 0) }
| real { FLOAT (parse_float (lexeme lexbuf)) }
| int { INT (Int32.Parse (lexeme lexbuf)) }
// identifiers
| id { ID (lexeme lexbuf) }