@@ -2120,204 +2120,206 @@ class Parser(object):
2120
2120
_right_delim = set (r") ] \} > \rfloor \rangle \rceil" .split ())
2121
2121
2122
2122
def __init__ (self ):
2123
+ p = Bunch ()
2123
2124
# All forward declarations are here
2124
- accent = Forward ()
2125
- ambi_delim = Forward ()
2126
- apostrophe = Forward ()
2127
- auto_delim = Forward ()
2128
- binom = Forward ()
2129
- bslash = Forward ()
2130
- c_over_c = Forward ()
2131
- customspace = Forward ()
2132
- end_group = Forward ()
2133
- float_literal = Forward ()
2134
- font = Forward ()
2135
- frac = Forward ()
2136
- function = Forward ()
2137
- genfrac = Forward ()
2138
- group = Forward ()
2139
- int_literal = Forward ()
2140
- latexfont = Forward ()
2141
- lbracket = Forward ()
2142
- left_delim = Forward ()
2143
- lbrace = Forward ()
2144
- main = Forward ()
2145
- math = Forward ()
2146
- math_string = Forward ()
2147
- non_math = Forward ()
2148
- operatorname = Forward ()
2149
- overline = Forward ()
2150
- placeable = Forward ()
2151
- rbrace = Forward ()
2152
- rbracket = Forward ()
2153
- required_group = Forward ()
2154
- right_delim = Forward ()
2155
- right_delim_safe = Forward ()
2156
- simple = Forward ()
2157
- simple_group = Forward ()
2158
- single_symbol = Forward ()
2159
- space = Forward ()
2160
- sqrt = Forward ()
2161
- stackrel = Forward ()
2162
- start_group = Forward ()
2163
- subsuper = Forward ()
2164
- subsuperop = Forward ()
2165
- symbol = Forward ()
2166
- symbol_name = Forward ()
2167
- token = Forward ()
2168
- unknown_symbol = Forward ()
2125
+ p . accent = Forward ()
2126
+ p . ambi_delim = Forward ()
2127
+ p . apostrophe = Forward ()
2128
+ p . auto_delim = Forward ()
2129
+ p . binom = Forward ()
2130
+ p . bslash = Forward ()
2131
+ p . c_over_c = Forward ()
2132
+ p . customspace = Forward ()
2133
+ p . end_group = Forward ()
2134
+ p . float_literal = Forward ()
2135
+ p . font = Forward ()
2136
+ p . frac = Forward ()
2137
+ p . function = Forward ()
2138
+ p . genfrac = Forward ()
2139
+ p . group = Forward ()
2140
+ p . int_literal = Forward ()
2141
+ p . latexfont = Forward ()
2142
+ p . lbracket = Forward ()
2143
+ p . left_delim = Forward ()
2144
+ p . lbrace = Forward ()
2145
+ p . main = Forward ()
2146
+ p . math = Forward ()
2147
+ p . math_string = Forward ()
2148
+ p . non_math = Forward ()
2149
+ p . operatorname = Forward ()
2150
+ p . overline = Forward ()
2151
+ p . placeable = Forward ()
2152
+ p . rbrace = Forward ()
2153
+ p . rbracket = Forward ()
2154
+ p . required_group = Forward ()
2155
+ p . right_delim = Forward ()
2156
+ p . right_delim_safe = Forward ()
2157
+ p . simple = Forward ()
2158
+ p . simple_group = Forward ()
2159
+ p . single_symbol = Forward ()
2160
+ p . space = Forward ()
2161
+ p . sqrt = Forward ()
2162
+ p . stackrel = Forward ()
2163
+ p . start_group = Forward ()
2164
+ p . subsuper = Forward ()
2165
+ p . subsuperop = Forward ()
2166
+ p . symbol = Forward ()
2167
+ p . symbol_name = Forward ()
2168
+ p . token = Forward ()
2169
+ p . unknown_symbol = Forward ()
2169
2170
2170
2171
# Set names on everything -- very useful for debugging
2171
- for key , val in locals ( ).items ():
2172
- if key != 'self' :
2172
+ for key , val in vars ( p ).items ():
2173
+ if not key . startswith ( '_' ) :
2173
2174
val .setName (key )
2174
2175
2175
- float_literal <<= Regex (r"[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)" )
2176
- int_literal <<= Regex ("[-+]?[0-9]+" )
2176
+ p . float_literal <<= Regex (r"[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)" )
2177
+ p . int_literal <<= Regex ("[-+]?[0-9]+" )
2177
2178
2178
- lbrace <<= Literal ('{' ).suppress ()
2179
- rbrace <<= Literal ('}' ).suppress ()
2180
- lbracket <<= Literal ('[' ).suppress ()
2181
- rbracket <<= Literal (']' ).suppress ()
2182
- bslash <<= Literal ('\\ ' )
2179
+ p . lbrace <<= Literal ('{' ).suppress ()
2180
+ p . rbrace <<= Literal ('}' ).suppress ()
2181
+ p . lbracket <<= Literal ('[' ).suppress ()
2182
+ p . rbracket <<= Literal (']' ).suppress ()
2183
+ p . bslash <<= Literal ('\\ ' )
2183
2184
2184
- space <<= oneOf (list (six .iterkeys (self ._space_widths )))
2185
- customspace <<= (Suppress (Literal (r'\hspace' ))
2186
- - ((lbrace + float_literal + rbrace )
2185
+ p . space <<= oneOf (list (six .iterkeys (self ._space_widths )))
2186
+ p . customspace <<= (Suppress (Literal (r'\hspace' ))
2187
+ - ((p . lbrace + p . float_literal + p . rbrace )
2187
2188
| Error (r"Expected \hspace{n}" )))
2188
2189
2189
2190
unicode_range = "\U00000080 -\U0001ffff "
2190
- single_symbol <<= Regex (r"([a-zA-Z0-9 +\-*/<>=:,.;!\?&'@()\[\]|%s])|(\\[%%${}\[\]_|])" %
2191
+ p . single_symbol <<= Regex (r"([a-zA-Z0-9 +\-*/<>=:,.;!\?&'@()\[\]|%s])|(\\[%%${}\[\]_|])" %
2191
2192
unicode_range )
2192
- symbol_name <<= (Combine (bslash + oneOf (list (six .iterkeys (tex2uni )))) +
2193
+ p . symbol_name <<= (Combine (p . bslash + oneOf (list (six .iterkeys (tex2uni )))) +
2193
2194
FollowedBy (Regex ("[^A-Za-z]" ).leaveWhitespace () | StringEnd ()))
2194
- symbol <<= (single_symbol | symbol_name ).leaveWhitespace ()
2195
+ p . symbol <<= (p . single_symbol | p . symbol_name ).leaveWhitespace ()
2195
2196
2196
- apostrophe <<= Regex ("'+" )
2197
+ p . apostrophe <<= Regex ("'+" )
2197
2198
2198
- c_over_c <<= Suppress (bslash ) + oneOf (list (six .iterkeys (self ._char_over_chars )))
2199
+ p . c_over_c <<= Suppress (p . bslash ) + oneOf (list (six .iterkeys (self ._char_over_chars )))
2199
2200
2200
- accent <<= Group (
2201
- Suppress (bslash )
2201
+ p . accent <<= Group (
2202
+ Suppress (p . bslash )
2202
2203
+ oneOf (list (six .iterkeys (self ._accent_map )) + list (self ._wide_accents ))
2203
- - placeable
2204
+ - p . placeable
2204
2205
)
2205
2206
2206
- function <<= Suppress (bslash ) + oneOf (list (self ._function_names ))
2207
+ p . function <<= Suppress (p . bslash ) + oneOf (list (self ._function_names ))
2207
2208
2208
- start_group <<= Optional (latexfont ) + lbrace
2209
- end_group <<= rbrace .copy ()
2210
- simple_group <<= Group (lbrace + ZeroOrMore (token ) + rbrace )
2211
- required_group <<= Group (lbrace + OneOrMore (token ) + rbrace )
2212
- group <<= Group (start_group + ZeroOrMore (token ) + end_group )
2209
+ p . start_group <<= Optional (p . latexfont ) + p . lbrace
2210
+ p . end_group <<= p . rbrace .copy ()
2211
+ p . simple_group <<= Group (p . lbrace + ZeroOrMore (p . token ) + p . rbrace )
2212
+ p . required_group <<= Group (p . lbrace + OneOrMore (p . token ) + p . rbrace )
2213
+ p . group <<= Group (p . start_group + ZeroOrMore (p . token ) + p . end_group )
2213
2214
2214
- font <<= Suppress (bslash ) + oneOf (list (self ._fontnames ))
2215
- latexfont <<= Suppress (bslash ) + oneOf (['math' + x for x in self ._fontnames ])
2215
+ p . font <<= Suppress (p . bslash ) + oneOf (list (self ._fontnames ))
2216
+ p . latexfont <<= Suppress (p . bslash ) + oneOf (['math' + x for x in self ._fontnames ])
2216
2217
2217
- frac <<= Group (
2218
+ p . frac <<= Group (
2218
2219
Suppress (Literal (r"\frac" ))
2219
- - ((required_group + required_group ) | Error (r"Expected \frac{num}{den}" ))
2220
+ - ((p . required_group + p . required_group ) | Error (r"Expected \frac{num}{den}" ))
2220
2221
)
2221
2222
2222
- stackrel <<= Group (
2223
+ p . stackrel <<= Group (
2223
2224
Suppress (Literal (r"\stackrel" ))
2224
- - ((required_group + required_group ) | Error (r"Expected \stackrel{num}{den}" ))
2225
+ - ((p . required_group + p . required_group ) | Error (r"Expected \stackrel{num}{den}" ))
2225
2226
)
2226
2227
2227
- binom <<= Group (
2228
+ p . binom <<= Group (
2228
2229
Suppress (Literal (r"\binom" ))
2229
- - ((required_group + required_group ) | Error (r"Expected \binom{num}{den}" ))
2230
+ - ((p . required_group + p . required_group ) | Error (r"Expected \binom{num}{den}" ))
2230
2231
)
2231
2232
2232
- ambi_delim <<= oneOf (list (self ._ambi_delim ))
2233
- left_delim <<= oneOf (list (self ._left_delim ))
2234
- right_delim <<= oneOf (list (self ._right_delim ))
2235
- right_delim_safe <<= oneOf (list (self ._right_delim - set (['}' ])) + [r'\}' ])
2233
+ p . ambi_delim <<= oneOf (list (self ._ambi_delim ))
2234
+ p . left_delim <<= oneOf (list (self ._left_delim ))
2235
+ p . right_delim <<= oneOf (list (self ._right_delim ))
2236
+ p . right_delim_safe <<= oneOf (list (self ._right_delim - set (['}' ])) + [r'\}' ])
2236
2237
2237
- genfrac <<= Group (
2238
+ p . genfrac <<= Group (
2238
2239
Suppress (Literal (r"\genfrac" ))
2239
- - (((lbrace + Optional (ambi_delim | left_delim , default = '' ) + rbrace )
2240
- + (lbrace + Optional (ambi_delim | right_delim_safe , default = '' ) + rbrace )
2241
- + (lbrace + float_literal + rbrace )
2242
- + simple_group + required_group + required_group )
2240
+ - (((p . lbrace + Optional (p . ambi_delim | p . left_delim , default = '' ) + p . rbrace )
2241
+ + (p . lbrace + Optional (p . ambi_delim | p . right_delim_safe , default = '' ) + p . rbrace )
2242
+ + (p . lbrace + p . float_literal + p . rbrace )
2243
+ + p . simple_group + p . required_group + p . required_group )
2243
2244
| Error (r"Expected \genfrac{ldelim}{rdelim}{rulesize}{style}{num}{den}" ))
2244
2245
)
2245
2246
2246
- sqrt <<= Group (
2247
+ p . sqrt <<= Group (
2247
2248
Suppress (Literal (r"\sqrt" ))
2248
- - ((Optional (lbracket + int_literal + rbracket , default = None )
2249
- + required_group )
2249
+ - ((Optional (p . lbracket + p . int_literal + p . rbracket , default = None )
2250
+ + p . required_group )
2250
2251
| Error ("Expected \sqrt{value}" ))
2251
2252
)
2252
2253
2253
- overline <<= Group (
2254
+ p . overline <<= Group (
2254
2255
Suppress (Literal (r"\overline" ))
2255
- - (required_group | Error ("Expected \overline{value}" ))
2256
+ - (p . required_group | Error ("Expected \overline{value}" ))
2256
2257
)
2257
2258
2258
- unknown_symbol <<= Combine (bslash + Regex ("[A-Za-z]*" ))
2259
+ p . unknown_symbol <<= Combine (p . bslash + Regex ("[A-Za-z]*" ))
2259
2260
2260
- operatorname <<= Group (
2261
+ p . operatorname <<= Group (
2261
2262
Suppress (Literal (r"\operatorname" ))
2262
- - ((lbrace + ZeroOrMore (simple | unknown_symbol ) + rbrace )
2263
+ - ((p . lbrace + ZeroOrMore (p . simple | p . unknown_symbol ) + p . rbrace )
2263
2264
| Error ("Expected \operatorname{value}" ))
2264
2265
)
2265
2266
2266
- placeable <<= ( accent # Must be first
2267
- | symbol # Must be second
2268
- | c_over_c
2269
- | function
2270
- | group
2271
- | frac
2272
- | stackrel
2273
- | binom
2274
- | genfrac
2275
- | sqrt
2276
- | overline
2277
- | operatorname
2267
+ p . placeable <<= ( p . accent # Must be first
2268
+ | p . symbol # Must be second
2269
+ | p . c_over_c
2270
+ | p . function
2271
+ | p . group
2272
+ | p . frac
2273
+ | p . stackrel
2274
+ | p . binom
2275
+ | p . genfrac
2276
+ | p . sqrt
2277
+ | p . overline
2278
+ | p . operatorname
2278
2279
)
2279
2280
2280
- simple <<= ( space
2281
- | customspace
2282
- | font
2283
- | subsuper
2281
+ p . simple <<= ( p . space
2282
+ | p . customspace
2283
+ | p . font
2284
+ | p . subsuper
2284
2285
)
2285
2286
2286
- subsuperop <<= oneOf (["_" , "^" ])
2287
+ p . subsuperop <<= oneOf (["_" , "^" ])
2287
2288
2288
- subsuper <<= Group (
2289
- (Optional (placeable ) + OneOrMore (subsuperop - placeable ) + Optional (apostrophe ))
2290
- | (placeable + Optional (apostrophe ))
2291
- | apostrophe
2289
+ p . subsuper <<= Group (
2290
+ (Optional (p . placeable ) + OneOrMore (p . subsuperop - p . placeable ) + Optional (p . apostrophe ))
2291
+ | (p . placeable + Optional (p . apostrophe ))
2292
+ | p . apostrophe
2292
2293
)
2293
2294
2294
- token <<= ( simple
2295
- | auto_delim
2296
- | unknown_symbol # Must be last
2295
+ p . token <<= ( p . simple
2296
+ | p . auto_delim
2297
+ | p . unknown_symbol # Must be last
2297
2298
)
2298
2299
2299
- auto_delim <<= (Suppress (Literal (r"\left" ))
2300
- - ((left_delim | ambi_delim ) | Error ("Expected a delimiter" ))
2301
- + Group (ZeroOrMore (simple | auto_delim ))
2300
+ p . auto_delim <<= (Suppress (Literal (r"\left" ))
2301
+ - ((p . left_delim | p . ambi_delim ) | Error ("Expected a delimiter" ))
2302
+ + Group (ZeroOrMore (p . simple | p . auto_delim ))
2302
2303
+ Suppress (Literal (r"\right" ))
2303
- - ((right_delim | ambi_delim ) | Error ("Expected a delimiter" ))
2304
+ - ((p . right_delim | p . ambi_delim ) | Error ("Expected a delimiter" ))
2304
2305
)
2305
2306
2306
- math <<= OneOrMore (token )
2307
+ p . math <<= OneOrMore (p . token )
2307
2308
2308
- math_string <<= QuotedString ('$' , '\\ ' , unquoteResults = False )
2309
+ p . math_string <<= QuotedString ('$' , '\\ ' , unquoteResults = False )
2309
2310
2310
- non_math <<= Regex (r"(?:(?:\\[$])|[^$])*" ).leaveWhitespace ()
2311
+ p . non_math <<= Regex (r"(?:(?:\\[$])|[^$])*" ).leaveWhitespace ()
2311
2312
2312
- main <<= (non_math + ZeroOrMore (math_string + non_math )) + StringEnd ()
2313
+ p . main <<= (p . non_math + ZeroOrMore (p . math_string + p . non_math )) + StringEnd ()
2313
2314
2314
2315
# Set actions
2315
- for key , val in locals ().items ():
2316
- if hasattr (self , key ):
2317
- val .setParseAction (getattr (self , key ))
2316
+ for key , val in vars (p ).items ():
2317
+ if not key .startswith ('_' ):
2318
+ if hasattr (self , key ):
2319
+ val .setParseAction (getattr (self , key ))
2318
2320
2319
- self ._expression = main
2320
- self ._math_expression = math
2321
+ self ._expression = p . main
2322
+ self ._math_expression = p . math
2321
2323
2322
2324
def parse (self , s , fonts_object , fontsize , dpi ):
2323
2325
"""
0 commit comments