MCPcopy Index your code
hub / github.com/RustPython/RustPython / _parse

Function _parse

Lib/gettext.py:118–165  ·  view source on GitHub ↗
(tokens, priority=-1)

Source from the content-addressed store, hash-verified

116
117
118def _parse(tokens, priority=-1):
119 result = ''
120 nexttok = next(tokens)
121 while nexttok == '!':
122 result += 'not '
123 nexttok = next(tokens)
124
125 if nexttok == '(':
126 sub, nexttok = _parse(tokens)
127 result = '%s(%s)' % (result, sub)
128 if nexttok != ')':
129 raise ValueError('unbalanced parenthesis in plural form')
130 elif nexttok == 'n':
131 result = '%s%s' % (result, nexttok)
132 else:
133 try:
134 value = int(nexttok, 10)
135 except ValueError:
136 raise _error(nexttok) from None
137 result = '%s%d' % (result, value)
138 nexttok = next(tokens)
139
140 j = 100
141 while nexttok in _binary_ops:
142 i = _binary_ops[nexttok]
143 if i < priority:
144 break
145 # Break chained comparisons
146 if i in (3, 4) and j in (3, 4): # '==', '!=', '<', '>', '<=', '>='
147 result = '(%s)' % result
148 # Replace some C operators by their Python equivalents
149 op = _c2py_ops.get(nexttok, nexttok)
150 right, nexttok = _parse(tokens, i + 1)
151 result = '%s %s %s' % (result, op, right)
152 j = i
153 if j == priority == 4: # '<', '>', '<=', '>='
154 result = '(%s)' % result
155
156 if nexttok == '?' and priority <= 0:
157 if_true, nexttok = _parse(tokens, 0)
158 if nexttok != ':':
159 raise _error(nexttok)
160 if_false, nexttok = _parse(tokens)
161 result = '%s if %s else %s' % (if_true, result, if_false)
162 if priority == 0:
163 result = '(%s)' % result
164
165 return result, nexttok
166
167
168def _as_int(n):

Callers 2

c2pyFunction · 0.70
parseMethod · 0.50

Calls 3

nextFunction · 0.85
_errorFunction · 0.70
getMethod · 0.45

Tested by

no test coverage detected