-
Notifications
You must be signed in to change notification settings - Fork 1
/
lexer.mll
67 lines (61 loc) · 2.41 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
{
open Abstract_syntax_tree;;
open Parser;;
}
(* Some helper definitions, mostly for lowercaseId and uppercaseId *)
let numeric = ['0' - '9']
let lowercase = ['a' - 'z']
let uppercase = ['A' - 'Z']
let letter = ['a' - 'z' 'A' - 'Z' '_']
let lowercaseId = lowercase (letter | numeric | "'")*
let uppercaseId = uppercase (letter | numeric | "'")*
(* Main parser *)
rule token = parse
| [' ' '\t' '\n'] { token lexbuf } (* this language is whitespace agnostic *)
| eof { EOF }
| "World" { TknWorld } (* Language keywords *)
| "Quest" { TknQuest }
| "Location" { TknLocation }
| "NPC" { TknNPC }
| "Item" { TknItem }
| "Player" { TknPlayer }
| "Vulnerable" { TknVulnerable }
| "at" { TknAt }
| "to" { TknTo }
| "and" { TknAnd }
| "or" { TknOr }
| "Holding" { TknHolding }
| "is Alive" { TknIsAlive }
| "is Dead" { TknIsDead }
| "is at" { TknIsAt }
| "=>" { TknImplies }
| "not" { TknNot }
| "goto" { TknGoto }
| "get" { TknGet }
| "kill" { TknKill }
| "require" { TknRequire }
| "use" { TknUse }
| "let " (lowercaseId as var) { TknLet var } (* For let expressions *)
| "=" { TknEq }
| "getloc" { TknGetLoc }
| "Subquest " (uppercaseId as subquest) { TknSubquest subquest } (* Defining and running subquests *)
| "run " (uppercaseId as subquest) { TknSubquestRun subquest }
| lowercaseId as var { TknVar var }
| uppercaseId as literal { TknLiteral literal }
| ")" { raise (Failure "mismatched bracket!") }
| "(" { brackets lexbuf }
| "," { brackets lexbuf }
| "[" { TknLBrac }
| "]" { TknRBrac }
and brackets = parse (* This lets us assign special tokens to strings in parentheses/brackets *)
| ")" { token lexbuf }
| "(" { raise (Failure "brackets can only be one level deep") }
| (lowercaseId as var) ")"? { TknFormalParam var }
| (uppercaseId as literal) ")"? { TknArgument literal }
| "Location " (uppercaseId as literal) ")"? { TknArgumentLoc literal }
| "NPC " (uppercaseId as literal) ")"? { TknArgumentNPC literal }
| "NPC Player" ")"? { TknArgumentPlayer }
| "Item " (uppercaseId as literal) ")"? { TknArgumentItem literal }
| ' ' { brackets lexbuf }
{
}