Debian Codegraph @

pypy/interpreter/location.py

raw symbols SCIP
1 from rpython.rlib import rstring
2
3 class DecodeError(Exception):
4 pass
5
6 def encode_varint_unsigned(i, res):
7 # XXX can't use code in dawg because that doesn't take a StringBuilder :-(
8 # https://en.wikipedia.org/wiki/LEB128 unsigned variant
9 more = True
10 if i < 0:
11 raise ValueError("only positive numbers supported", i)
12 while more:
13 lowest7bits = i & 0b1111111
14 i >>= 7
15 if i == 0:
16 more = False
17 else:
18 lowest7bits |= 0b10000000
19 res.append(chr(lowest7bits))
20
21 def decode_varint_unsigned(b, index=0):
22 # can't use code in dawg because this variant needs to be safe against
23 # invalidly encoded varints
24 res = 0
25 shift = 0
26 while True:
27 byte = ord(b[index])
28 res = res | ((byte & 0b1111111) << shift)
29 index += 1
30 if not (byte & 0b10000000):
31 return res, index
32 shift += 7
33 if index == len(b):
34 raise DecodeError
35
36
37 def encode_positions(l, firstlineno):
38 # l is a list of four-tuples (lineno, end_lineno, col_offset,
39 # end_col_offset) coming out of the AST nodes
40 table = rstring.StringBuilder(4 * len(l))
41 for position_info in l:
42 encode_single_position(table, position_info, firstlineno)
43
44
45 return table.build()
46
47 def encode_single_position(table, position_info, firstlineno):
48 # this is really inefficient for now. we can always change it though
49
50 # three formats:
51 # (1) 0:
52 # no info at all
53 # (2) varint lineno_delta 0:
54 # just a line number no further info, the lineno_delta is 1 too big and
55 # is relative to co_firstlineno
56 # (3) varint lineno_delta, char col_offset, char end_col_offset, char end_lineno_delta
57 # full info, col_offset and end_col_offset are 1 too big to distinguish
58 # from case (2)
59
60 # XXX clarify what missing values are, 0 or -1?
61 lineno, end_lineno, col_offset, end_col_offset = position_info
62 if lineno == -1 or lineno < firstlineno:
63 table.append(chr(0))
64 return
65 # case (1)
66 lineno_delta = lineno - firstlineno + 1
67 end_line_delta = end_lineno - lineno
68 # encode lineno_delta as a varsized int
69 encode_varint_unsigned(lineno_delta, table)
70
71 # the rest gets one byte each (or a single 0 for 'everything invalid')
72 if (
73 col_offset >= 255
74 or end_col_offset >= 255
75 or col_offset == -1
76 or end_col_offset == -1
77 or end_line_delta < 0
78 or end_line_delta > 255
79 ):
80 #
81 table.append(chr(0))
82 else:
83 table.append(chr(col_offset + 1))
84 table.append(chr(end_col_offset + 1))
85 table.append(chr(end_line_delta))
86
87 def _decode_entry(table, firstlineno, position):
88 if position >= len(table):
89 raise DecodeError
90 lineno, position = decode_varint_unsigned(table, position)
91 if lineno == 0:
92 return (-1, -1, -1, -1, position)
93 lineno -= 1
94 lineno += firstlineno
95 if position == len(table):
96 raise DecodeError
97 col_offset = ord(table[position]) - 1
98 position += 1
99 if col_offset == -1: # was a single 0, no more bytes
100 end_col_offset = -1
101 end_lineno = -1
102 else:
103 if position + 1 >= len(table):
104 raise DecodeError
105 end_col_offset = ord(table[position]) - 1
106 end_line_delta = ord(table[position + 1])
107 position += 2
108 end_lineno = lineno + end_line_delta
109 return lineno, end_lineno, col_offset, end_col_offset, position
110
111
112 def decode_positions(table, firstlineno):
113 res = []
114 position = 0
115 while position < len(table):
116 lineno, end_lineno, col_offset, end_col_offset, position = _decode_entry(table, firstlineno, position)
117 res.append((lineno, end_lineno, col_offset, end_col_offset))
118 return res
119
120 def offset2lineno(c, stopat):
121 if stopat == -1:
122 return c.co_firstlineno
123 try:
124 return _offset2lineno(c.co_linetable, c.co_firstlineno, stopat // 2)
125 except DecodeError:
126 return -1
127
128 def _offset2lineno(linetable, firstlineno, stopat):
129 position = 0
130 lineno = -1
131 for i in range(stopat + 1):
132 tup = _decode_entry(linetable, firstlineno, position)
133 position = tup[4]
134 lineno = tup[0]
135 return lineno
136
137
138 # converting back to lnotab
139
140 def _encode_lnotab_pair(addr, line, table):
141 while addr > 255:
142 table.append(chr(255))
143 table.append(chr(0))
144 addr -= 255
145 while line < -128:
146 table.append(chr(addr))
147 table.append(chr(-128 + 256))
148 line += 128
149 addr = 0
150 while line > 127:
151 table.append(chr(addr))
152 table.append(chr(127))
153 line -= 127
154 addr = 0
155 table.append(chr(addr))
156
157 # store as signed char
158 assert -128 <= line <= 127
159 if line < 0:
160 line += 256
161 table.append(chr(line))
162
163 def linetable2lnotab(linetable, firstlineno):
164 position = 0
165 res = []
166 line = firstlineno
167 start_pc = pc = 0
168 while position < len(linetable):
169 try:
170 lineno, _, _, _, position = _decode_entry(linetable, firstlineno, position)
171 except DecodeError:
172 return b''
173 if lineno != line:
174 bdelta = pc - start_pc
175 ldelta = lineno - line
176 _encode_lnotab_pair(bdelta, ldelta, res)
177 line = lineno
178 start_pc = pc
179 pc += 2
180 return b"".join(res)
181
182 def marklines(linetable, firstlineno):
183 res = []
184 line = -1
185 position = 0
186 while position < len(linetable):
187 lineno, _, _, _, position = _decode_entry(linetable, firstlineno, position)
188 if lineno != line and lineno != -1:
189 res.append(lineno)
190 line = lineno
191 else:
192 res.append(-1)
193 return res