OILS / pgen2 / token.py View on Github | oilshell.org

149 lines, 79 significant
1"""Token constants (from "token.h")."""
2
3# Taken from Python (r53757) and modified to include some tokens
4# originally monkeypatched in by pgen2.tokenize
5
6#--start constants--
7ENDMARKER = 0
8NAME = 1
9NUMBER = 2
10STRING = 3
11NEWLINE = 4
12INDENT = 5
13DEDENT = 6
14LPAR = 7
15RPAR = 8
16LSQB = 9
17RSQB = 10
18COLON = 11
19COMMA = 12
20SEMI = 13
21PLUS = 14
22MINUS = 15
23STAR = 16
24SLASH = 17
25VBAR = 18
26AMPER = 19
27LESS = 20
28GREATER = 21
29EQUAL = 22
30DOT = 23
31PERCENT = 24
32BACKQUOTE = 25
33LBRACE = 26
34RBRACE = 27
35EQEQUAL = 28
36NOTEQUAL = 29
37LESSEQUAL = 30
38GREATEREQUAL = 31
39TILDE = 32
40CIRCUMFLEX = 33
41LEFTSHIFT = 34
42RIGHTSHIFT = 35
43DOUBLESTAR = 36
44PLUSEQUAL = 37
45MINEQUAL = 38
46STAREQUAL = 39
47SLASHEQUAL = 40
48PERCENTEQUAL = 41
49AMPEREQUAL = 42
50VBAREQUAL = 43
51CIRCUMFLEXEQUAL = 44
52LEFTSHIFTEQUAL = 45
53RIGHTSHIFTEQUAL = 46
54DOUBLESTAREQUAL = 47
55DOUBLESLASH = 48
56DOUBLESLASHEQUAL = 49
57AT = 50
58ATEQUAL = 51
59OP = 52
60COMMENT = 53
61NL = 54
62RARROW = 55
63AWAIT = 56
64ASYNC = 57
65ERRORTOKEN = 58
66N_TOKENS = 59
67NT_OFFSET = 256
68#--end constants--
69
70tok_name = {}
71for _name, _value in list(globals().items()):
72 if type(_value) is type(0):
73 tok_name[_value] = _name
74
75
76def ISTERMINAL(x):
77 # type: (int) -> bool
78 return x < NT_OFFSET
79
80def ISNONTERMINAL(x):
81 # type: (int) -> bool
82 return x >= NT_OFFSET
83
84def ISEOF(x):
85 # type: (int) -> bool
86 return x == ENDMARKER
87
88
89# Map from operator to number. NOTE: This was moved here from
90# pgen2/grammar.py. Oil doesn't need this; only parsers that use Python's
91# tokenizer (like pgen itself) need it.
92
93opmap_raw = """
94( LPAR
95) RPAR
96[ LSQB
97] RSQB
98: COLON
99, COMMA
100; SEMI
101+ PLUS
102- MINUS
103* STAR
104/ SLASH
105| VBAR
106& AMPER
107< LESS
108> GREATER
109= EQUAL
110. DOT
111% PERCENT
112` BACKQUOTE
113{ LBRACE
114} RBRACE
115@ AT
116@= ATEQUAL
117== EQEQUAL
118!= NOTEQUAL
119<> NOTEQUAL
120<= LESSEQUAL
121>= GREATEREQUAL
122~ TILDE
123^ CIRCUMFLEX
124<< LEFTSHIFT
125>> RIGHTSHIFT
126** DOUBLESTAR
127+= PLUSEQUAL
128-= MINEQUAL
129*= STAREQUAL
130/= SLASHEQUAL
131%= PERCENTEQUAL
132&= AMPEREQUAL
133|= VBAREQUAL
134^= CIRCUMFLEXEQUAL
135<<= LEFTSHIFTEQUAL
136>>= RIGHTSHIFTEQUAL
137**= DOUBLESTAREQUAL
138// DOUBLESLASH
139//= DOUBLESLASHEQUAL
140-> RARROW
141"""
142
143import sys
144this_module = sys.modules[__name__]
145opmap = {}
146for line in opmap_raw.splitlines():
147 if line:
148 op, name = line.split()
149 opmap[op] = getattr(this_module, name)