finally upgraded psLib to use re instead of the long obsolete regex module.

git-svn-id: svn://svn.code.sf.net/p/fonttools/code/trunk@271 4cde692c-a291-49d1-8350-778aa11640f8
This commit is contained in:
jvr 2002-06-04 19:11:03 +00:00
parent b6eda92dd1
commit 1e59feca35

View File

@ -1,40 +1,38 @@
import StringIO import re
import regex
import string
from fontTools.misc import eexec
import types import types
from string import whitespace
import StringIO
from fontTools.misc import eexec
from psOperators import * from psOperators import *
ps_special = '()<>[]{}%' # / is one too, but we take care of that one differently ps_special = '()<>[]{}%' # / is one too, but we take care of that one differently
whitespace = string.whitespace skipwhiteRE = re.compile("[%s]*" % whitespace)
skipwhiteRE = regex.compile("[%s]*" % whitespace) endofthingPat = "[^][(){}<>/%%%s]*" % whitespace
endofthingRE = re.compile(endofthingPat)
commentRE = re.compile("%[^\n\r]*")
endofthingPat = "[^][(){}<>/%s%s]*" % ('%', whitespace) # XXX This not entirely correct as it doesn't allow *nested* embedded parens:
endofthingRE = regex.compile(endofthingPat) stringPat = r"""
\(
commentRE = regex.compile("%[^\n\r]*") (
(
# XXX This not entirely correct: [^()]* \ [()]
stringPat = """ )
( |
\( (
\( [^()]* \( [^()]* \)
[^()]* \\\\ [()] )
\) )*
\|
\(
[^()]* ( [^()]* )
\)
\)*
[^()]* [^()]*
) \)
""" """
stringPat = string.join(string.split(stringPat), '') stringPat = "".join(stringPat.split())
stringRE = regex.compile(stringPat) stringRE = re.compile(stringPat)
hexstringRE = regex.compile("<[%s0-9A-Fa-f]*>" % whitespace) hexstringRE = re.compile("<[%s0-9A-Fa-f]*>" % whitespace)
ps_tokenerror = 'ps_tokenerror' ps_tokenerror = 'ps_tokenerror'
ps_error = 'ps_error' ps_error = 'ps_error'
@ -43,15 +41,16 @@ class PSTokenizer(StringIO.StringIO):
def getnexttoken(self, def getnexttoken(self,
# localize some stuff, for performance # localize some stuff, for performance
len = len, len=len,
ps_special = ps_special, ps_special=ps_special,
stringmatch = stringRE.match, stringmatch=stringRE.match,
hexstringmatch = hexstringRE.match, hexstringmatch=hexstringRE.match,
commentmatch = commentRE.match, commentmatch=commentRE.match,
endmatch = endofthingRE.match, endmatch=endofthingRE.match,
whitematch = skipwhiteRE.match): whitematch=skipwhiteRE.match):
self.pos = self.pos + whitematch(self.buf, self.pos) _, nextpos = whitematch(self.buf, self.pos).span()
self.pos = nextpos
if self.pos >= self.len: if self.pos >= self.len:
return None, None return None, None
pos = self.pos pos = self.pos
@ -63,37 +62,41 @@ class PSTokenizer(StringIO.StringIO):
token = char token = char
elif char == '%': elif char == '%':
tokentype = 'do_comment' tokentype = 'do_comment'
commentlen = commentmatch(buf, pos) _, nextpos = commentmatch(buf, pos).span()
token = buf[pos:pos+commentlen] token = buf[pos:nextpos]
elif char == '(': elif char == '(':
tokentype = 'do_string' tokentype = 'do_string'
strlen = stringmatch(buf, pos) m = stringmatch(buf, pos)
if strlen < 0: if m is None:
raise ps_tokenerror, 'bad string at character %d' % pos raise ps_tokenerror, 'bad string at character %d' % pos
token = buf[pos:pos+strlen] _, nextpos = m.span()
token = buf[pos:nextpos]
elif char == '<': elif char == '<':
tokentype = 'do_hexstring' tokentype = 'do_hexstring'
strlen = hexstringmatch(buf, pos) m = hexstringmatch(buf, pos)
if strlen < 0: if m is None:
raise ps_tokenerror, 'bad hexstring at character %d' % pos raise ps_tokenerror, 'bad hexstring at character %d' % pos
token = buf[pos:pos+strlen] _, nextpos = m.span()
token = buf[pos:nextpos]
else: else:
raise ps_tokenerror, 'bad token at character %d' % pos raise ps_tokenerror, 'bad token at character %d' % pos
else: else:
if char == '/': if char == '/':
tokentype = 'do_literal' tokentype = 'do_literal'
endofthing = endmatch(buf, pos + 1) + 1 m = endmatch(buf, pos+1)
else: else:
tokentype = '' tokentype = ''
endofthing = endmatch(buf, pos) m = endmatch(buf, pos)
if endofthing <= 0: if m is None:
raise ps_tokenerror, 'bad token at character %d' % pos raise ps_tokenerror, 'bad token at character %d' % pos
token = buf[pos:pos + endofthing] _, nextpos = m.span()
token = buf[pos:nextpos]
self.pos = pos + len(token) self.pos = pos + len(token)
return tokentype, token return tokentype, token
def skipwhite(self, whitematch = skipwhiteRE.match): def skipwhite(self, whitematch=skipwhiteRE.match):
self.pos = self.pos + whitematch(self.buf, self.pos) _, nextpos = whitematch(self.buf, self.pos).span()
self.pos = nextpos
def starteexec(self): def starteexec(self):
self.pos = self.pos + 1 self.pos = self.pos + 1
@ -111,7 +114,7 @@ class PSTokenizer(StringIO.StringIO):
def flush(self): def flush(self):
if self.buflist: if self.buflist:
self.buf = self.buf + string.join(self.buflist, '') self.buf = self.buf + "".join(self.buflist)
self.buflist = [] self.buflist = []
@ -201,22 +204,22 @@ class PSInterpreter(PSOperators):
raise ps_error, 'name error: ' + str(name) raise ps_error, 'name error: ' + str(name)
def do_token(self, token, def do_token(self, token,
atoi = string.atoi, int=int,
atof = string.atof, float=float,
ps_name = ps_name, ps_name=ps_name,
ps_integer = ps_integer, ps_integer=ps_integer,
ps_real = ps_real): ps_real=ps_real):
try: try:
num = atoi(token) num = int(token)
except (ValueError, OverflowError): except (ValueError, OverflowError):
try: try:
num = atof(token) num = float(token)
except (ValueError, OverflowError): except (ValueError, OverflowError):
if '#' in token: if '#' in token:
hashpos = string.find(token, '#') hashpos = token.find('#')
try: try:
base = string.atoi(token[:hashpos]) base = int(token[:hashpos])
num = string.atoi(token[hashpos+1:], base) num = int(token[hashpos+1:], base)
except (ValueError, OverflowError): except (ValueError, OverflowError):
return ps_name(token) return ps_name(token)
else: else:
@ -238,13 +241,13 @@ class PSInterpreter(PSOperators):
return ps_string(token[1:-1]) return ps_string(token[1:-1])
def do_hexstring(self, token): def do_hexstring(self, token):
hexStr = string.join(string.split(token[1:-1]), '') hexStr = "".join(token[1:-1].split())
if len(hexStr) % 2: if len(hexStr) % 2:
hexStr = hexStr + '0' hexStr = hexStr + '0'
cleanstr = [] cleanstr = []
for i in range(0, len(hexStr), 2): for i in range(0, len(hexStr), 2):
cleanstr.append(chr(string.atoi(hexStr[i:i+2], 16))) cleanstr.append(chr(int(hexStr[i:i+2], 16)))
cleanstr = string.join(cleanstr, '') cleanstr = "".join(cleanstr)
return ps_string(cleanstr) return ps_string(cleanstr)
def do_special(self, token): def do_special(self, token):