39
39
40
40
import re , keyword
41
41
42
- # Build up a regular expression which will match anything
43
- # interesting, including multi-line triple-quoted strings.
42
+ # Build up a regular expression which will match anything interesting,
43
+ # including multi-line triple-quoted strings.
44
44
commentPat = "#.*"
45
45
46
46
pat = "q[^\q\n ]*(\\ \\ [\000 -\377 ][^\q\n ]*)*q"
68
68
pat = '' .join (pat .split ()) # get rid of whitespace
69
69
tripleQuotePat = pat .replace ("q" , "'" ) + "|" + pat .replace ('q' , '"' )
70
70
71
- # Build up a regular expression which matches all and only
72
- # Python keywords. This will let us skip the uninteresting
73
- # identifier references.
74
- # nonKeyPat identifies characters which may legally precede
75
- # a keyword pattern.
71
+ # Build up a regular expression which matches all and only Python keywords.
72
+ # This will let us skip the uninteresting identifier references.
73
+ # nonKeyPat identifies characters which may legally precede a keyword pattern.
76
74
nonKeyPat = "(^|[^a-zA-Z0-9_.\" '])"
77
75
keywordsPat = '|' .join (keyword .kwlist )
78
76
keyPat = nonKeyPat + "(" + keywordsPat + ")" + nonKeyPat
79
77
80
78
matchPat = keyPat + "|" + commentPat + "|" + tripleQuotePat + "|" + quotePat
81
79
matchRE = re .compile (matchPat )
82
80
83
- idKeyPat = "[ \t ]*[A-Za-z_][A-Za-z_0-9.]*" # Ident w. leading whitespace.
81
+ idKeyPat = "[ \t ]*[A-Za-z_][A-Za-z_0-9.]*" # ident with leading whitespace
84
82
idRE = re .compile (idKeyPat )
85
83
84
+
86
85
def fontify (pytext , searchfrom = 0 , searchto = None ):
87
86
if searchto is None :
88
87
searchto = len (pytext )
@@ -99,40 +98,40 @@ def fontify(pytext, searchfrom=0, searchto=None):
99
98
matchObject = matchRE .search (pytext , end )
100
99
if not matchObject :
101
100
break
102
- ( start , end ) = matchObject .span ()
101
+ start , end = matchObject .span ()
103
102
match = matchObject .group (0 )
104
103
c = match [0 ]
105
104
if c not in "#'\" " :
106
105
# Must have matched a keyword.
107
- if start != searchfrom :
108
- # there's still a redundant char before and after it, strip!
106
+ if start == searchfrom :
107
+ # this is the first keyword in the text
108
+ match = match [:- 1 ] # only a space at the end
109
+ else :
110
+ # there's still a redundant char before and after it
109
111
match = match [1 :- 1 ]
110
112
start += 1
111
- else :
112
- # this is the first keyword in the text.
113
- # Only a space at the end.
114
- match = match [:- 1 ]
115
113
end -= 1
116
114
tags .append ((keywordTag , start , end , None ))
117
- # If this was a defining keyword, look ahead to the
118
- # following identifier.
119
- if match in [ " def" , " class" ] :
115
+ # If this was a defining keyword,
116
+ # look ahead to the following identifier.
117
+ if match in ( ' def' , ' class' ) :
120
118
idMatchObject = idRE .search (pytext , end )
121
119
if idMatchObject :
122
- ( start , end ) = idMatchObject .span ()
120
+ start , end = idMatchObject .span ()
123
121
match = idMatchObject .group (0 )
124
- tags .append ((( match == 'def' )
125
- and functionTag or classTag , start , end , None ))
122
+ tags .append ((match == 'def' and functionTag or classTag ,
123
+ start , end , None ))
126
124
elif c == "#" :
127
125
tags .append ((commentTag , start , end , None ))
128
126
else :
129
127
tags .append ((stringTag , start , end , None ))
130
128
return tags
131
129
130
+
132
131
def test (path ):
133
132
f = open (path )
134
133
text = f .read ()
135
134
f .close ()
136
135
tags = fontify (text )
137
136
for tag , start , end , sublist in tags :
138
- print tag , ` text[start:end]` , start , end
137
+ print tag , repr ( text [start :end ]) , start , end
0 commit comments