-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexerlp.py
77 lines (58 loc) · 1.22 KB
/
lexerlp.py
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
from io import TextIOWrapper as FILE
from errorlp import errlex
from iolp import nextch, seekprv
def init(file : str) -> FILE:
fp : FILE = open(file)
return fp
def droptoken(fp : FILE) -> str:
c = nextch(fp)
seekprv(fp)
if c == '':
return None
if c.isspace():
wspace(fp)
return droptoken(fp)
if c == '-':
comment(fp)
return droptoken(fp)
if c.isalpha():
return iden(fp)
if c.isnumeric():
return num(fp)
errlex("near token '%s'" % c)
exit(1)
def gettoken(fp : FILE):
pos = fp.tell()
t = droptoken(fp)
fp.seek(pos, 0)
return t
def wspace(fp : FILE):
c = nextch(fp)
while c.isspace():
c = nextch(fp)
if c != '':
seekprv(fp)
def comment(fp : FILE):
nextch(fp)
if nextch(fp) != '-':
errlex("expected -")
while nextch(fp) != '\n':
pass
def iden(fp : FILE):
c = nextch(fp)
t = ''
while c.isalpha():
t += c
c = nextch(fp)
if c != '':
seekprv(fp)
return t
def num(fp : FILE):
c = nextch(fp)
t = ''
while c.isnumeric():
t += c
c = nextch(fp)
if c != '':
seekprv(fp)
return t