-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.mll
executable file
·101 lines (96 loc) · 3.31 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
92
93
94
95
96
97
98
99
100
101
{
open Parser
let stringfold f init s =
let n = String.length s in
let r = ref init in
for i = 0 to n-1 do r := f s.[i] (!r) done;
!r
let count_newlines s =
stringfold (fun c n -> if c = '\n' then n+1 else n) 0 s
let repeat n thunk = for i = 0 to n-1 do thunk() done
}
let comment = "//" [^'\n']* "\n"
let digit = ['0'-'9']
let number = '-'? digit+ ('.' digit*)?
let lident = ['a' - 'z']['a'-'z' 'A'-'Z' '0'-'9' '_' ]*
let uident = ['A' - 'Z']['a'-'z' 'A'-'Z' '0'-'9' '_' ]*
let whitespace = ['\t' ' ']+
let new_line = '\n' | '\r' | '\r' '\n'
let string_literal = ([^'\\' '\"' '\n'] | "\\n" | "\\t" | "\\\\" |"\\\"" )*
rule token = parse
| "type" {TYPE}
| "int" {INT}
| "lin" {LIN}
| "next" {NEXT}
| "•" {NEXT}
| "cons" {CONS}
| "forall" {FORALL}
| "∀" {FORALL}
| "exists" {EXISTS}
| "∃" {EXISTS}
| "of" {OF}
| "." {DOT}
| "(" {LPAREN}
| ")" {RPAREN}
| "," {COMMA}
| "!" {BANG}
| "□" {BANG}
| "F" {F}
| "fun" {FUN}
| "λ" {FUN}
| "->" {TO}
| "→" {TO}
| "+" {PLUS}
| "-" {MINUS}
| "<" {LT}
| "<=" {LEQ}
| ">=" {GEQ}
| ">" {GT}
| "*" {AST}
| "⊗" {AST}
| "&" {AND}
| "×" {AND}
| "&&" {ANDAND}
| "||" {OR}
| "let" {LET}
| "::" {DOUBLECOLON}
| ":" {COLON}
| "cons" {CONS}
| "=" {EQUAL}
| "in" {IN}
| "G" {G}
| "fix" {FIX}
| "loop" {LOOP}
| "true" {TRUE}
| "false" {FALSE}
| "if" {IF}
| "then" {THEN}
| "else" {ELSE}
| "val" {VAL}
| "rec" {REC}
| "match" {MATCH}
| "with" {WITH}
| "|" {BAR}
| "_" {UNDERSCORE}
| number as n {NUM(float_of_string n)}
| '\"' (string_literal as s) '\"' {repeat (count_newlines s) (fun () -> Lexing.new_line lexbuf); STRING s}
| "run" {RUN}
| "num" {NUMTYPE}
| "stream" {STREAMTYPE}
| "string" {STRINGTYPE}
| "bool" {BOOLTYPE}
| "-o" {LOLLI}
| "⊸" {LOLLI}
| "dom" {DOM}
| "frame" {FRAME}
| "svg" {SVG}
| "unit" {UNITTYPE}
| "I" {I}
| "begin" {BEGIN}
| "end" {END}
| lident as s {IDENT s}
| uident as s {CONID s}
| comment {Lexing.new_line lexbuf; token lexbuf}
| whitespace {token lexbuf}
| new_line {Lexing.new_line lexbuf; token lexbuf}
| eof {EOF}