-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.mll
103 lines (96 loc) · 3.49 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
102
(***************************************************************)
(* Copyright 2014 Pierre Hyvernat. All rights reserved. *)
(* This file is distributed under the terms of the *)
(* GNU General Public License, described in file COPYING. *)
(***************************************************************)
{
open Parser
let get_symbol s = String.sub s 1 ((String.length s) - 1)
let get_index s = int_of_string (String.sub s 1 ((String.length s) - 1))
let get_random s =
let s = String.sub s 6 ((String.length s) - 6) in
try int_of_string s
with Failure _ -> 5
}
let character = [ 'a'-'z' ]
let reg = "R" [ '0'-'9' ]+
let dfa = "D" [ '0'-'9' ]+
let nfa = "N" [ '0'-'9' ]+
let lang = "L" [ '0'-'9' ]+
let random = "RANDOM" [ '0'-'9' ]*
let line = "-----" "-"*
let num = [ '0'-'9' ]+
let help = ":help" | ":h"
let spaces = [ ' ' '\t' ]+
let var = [ 'X' 'Y' 'Z'] [ 'a'-'z' 'A'-'Z' '0'-'9' '_']*
rule token = parse
| '(' { LPAR }
| "(#" { LPARHASH }
| ')' { RPAR }
| '[' { LBR }
| ']' { RBR }
| "{I" { LCURLI }
| "{D" { LCURLD }
| '{' { LCURL }
| '}' { RCURL }
| '+' { PLUS }
| '*' { STAR }
| "^*" { STAR } (* to allow regex pasted from TeX source *)
| '0' { ZERO }
| '1' { ONE }
| character { SYMB(Lexing.lexeme_char lexbuf 0) }
| ',' { COMMA }
| '/' { SLASH }
| '\\' { BACKSLASH }
| '!' { BANG }
| '~' { TILDE }
| '<' { LANGL }
| '>' { RANGL }
| "<<" { LT }
| ">>" { GT }
| "==" { DOUBLE_EQUAL }
| '&' { AMPER }
| '|' { PIPE }
| '.' { DOT }
| "TRANSPOSE" { TRANS }
| "PREFIX" { PREF }
| "EMPTY" { EMPTY }
| "INFINITE" { INFINITE }
| random { RANDOM(get_random (Lexing.lexeme lexbuf)) }
| reg { REG(get_index (Lexing.lexeme lexbuf)) }
| dfa { DFA(get_index (Lexing.lexeme lexbuf)) }
| nfa { NFA(get_index (Lexing.lexeme lexbuf)) }
| lang { LANG(get_index (Lexing.lexeme lexbuf)) }
| var { VAR(Lexing.lexeme lexbuf)}
| ":=" { AFFECT }
| "->" { ARROW }
| "_" { UNDERSCORE }
| line { LINE }
| '?' { QUESTION }
| num { NUM(int_of_string (Lexing.lexeme lexbuf)) }
| "IN" { IN }
| '"' { QUOTE }
| [' ' '\t'] { token lexbuf }
| '\n' { Lexing.new_line lexbuf; NEWLINE }
| eof { EOF }
| ":quit" { QUIT }
| ":set" { SET }
| "verbose" { VERBOSE }
| "quiet" { QUIET }
| "alphabet" { ALPHABET }
| help spaces "word" { HELP_WORD }
| help spaces "regex" { HELP_REGEX }
| help spaces "dfa" { HELP_DFA }
| help spaces "nfa" { HELP_NFA }
| help spaces "lang" { HELP_LANG }
| help { HELP }
| ":assert" { ASSERT }
| ":derivatives" { DERIVATIVES }
| "NOT" { NOT }
| "{-" { comments 0 lexbuf }
and comments level = parse
| "-}" { if level = 0 then token lexbuf else comments (level-1) lexbuf }
| "{-" { comments (level+1) lexbuf }
| '\n' { Lexing.new_line lexbuf; comments level lexbuf }
| _ { comments level lexbuf }
| eof { EOF }