def __init__(self): family = Regex(r'([^%s]|(\\[%s]))*' % (family_punc, family_punc)) \ .setParseAction(self._family) size = Regex(r"([0-9]+\.?[0-9]*|\.[0-9]+)") \ .setParseAction(self._size) name = Regex(r'[a-z]+') \ .setParseAction(self._name) value = Regex(r'([^%s]|(\\[%s]))*' % (value_punc, value_punc)) \ .setParseAction(self._value) families = (family + ZeroOrMore(Literal(',') + family)).setParseAction( self._families) point_sizes = (size + ZeroOrMore(Literal(',') + size)).setParseAction( self._point_sizes) property = ((name + Suppress(Literal('=')) + value + ZeroOrMore(Suppress(Literal(',')) + value)) | name).setParseAction(self._property) pattern = (Optional(families) + Optional(Literal('-') + point_sizes) + ZeroOrMore(Literal(':') + property) + StringEnd()) self._parser = pattern self.ParseException = ParseException
r'$dz/dt \/ = \/ \gamma xy^2 \/ + \/ \rm{s}(2\pi y+\phi)$', r'$x^1 2$', r'$\alpha_{i+1}^j \/ = \/ \rm{sin}(2\pi f_j t_i) e^{-5 t_i/\tau}$', r'$\cal{R}\prod_{i=\alpha_{i+1}}^\infty a_i\rm{sin}(2 \pi f x_i)$', r'$\bigodot \bigoplus \cal{R} a_i\rm{sin}(2 \pi f x_i)$', r'$x_i$', r'$5\/\angstrom\hspace{ 2.0 }\pi$', r'$x+1$', r'$i$', r'$i^j$', ] #~ w, h, fonts = math_parse_s_ft2font(s, 20, 72) for s in stests: try: print s print(expression + StringEnd()).parseString(s[1:-1]) except ParseException, pe: print "*** ERROR ***", pe.msg print s print 'X' + (' ' * pe.loc) + '^' # how far did we get? print expression.parseString(s[1:-1]) print #w, h, fonts = math_parse_s_ps(s, 20, 72) if __name__ == '__main__': Element.fonts = DummyFonts() for i in range(5, 20): s = '$10^{%02d}$' % i print 'parsing', s