0
"""Module to analyze Python source code; for syntax coloring tools.
0
- for tag, start, end, sublist in fontify(pytext, searchfrom, searchto):
0
+ for tag, start, end, sublist in fontify(pytext, searchfrom, searchto):
0
The 'pytext' argument is a string containing Python source code.
0
-The (optional) arguments 'searchfrom' and 'searchto' may contain a slice in pytext.
0
+The (optional) arguments 'searchfrom' and 'searchto' may contain a slice in pytext.
0
The returned value is a list of tuples, formatted like this:
0
-
[('keyword', 0, 6, None), ('keyword', 11, 17, None), ('comment', 23, 53, None), etc. ]
0
+
[('keyword', 0, 6, None), ('keyword', 11, 17, None), ('comment', 23, 53, None), etc. ]
0
The tuple contents are always like this:
0
-
(tag, startindex, endindex, sublist)
0
+
(tag, startindex, endindex, sublist)
0
tag is one of 'keyword', 'string', 'comment' or 'identifier'
0
-sublist is not used, hence always None.
0
+sublist is not used, hence always None.
0
# Based on FontText.py by Mitchell S. Chapman,
0
# which was modified by Zachary Roadhouse,
0
# then un-Tk'd by Just van Rossum.
0
# Many thanks for regular expression debugging & authoring are due to:
0
-#
Tim (the-incredib-ly y'rs) Peters and Cristian Tismer
0
+#
Tim (the-incredib-ly y'rs) Peters and Cristian Tismer
0
# So, who owns the copyright? ;-) How about this:
0
from __future__ import generators
0
@@ -45,25 +45,25 @@ quotePat = pat.replace("q", "'") + "|" + pat.replace('q', '"')
0
-pat = "".join(pat.split())
# get rid of whitespace
0
+pat = "".join(pat.split())
# get rid of whitespace
0
tripleQuotePat = pat.replace("q", "'") + "|" + pat.replace('q', '"')
0
# Build up a regular expression which matches all and only
0
@@ -74,68 +74,68 @@ keyPat = r"\b(" + "|".join(keywordsList) + r")\b"
0
matchPat = commentPat + "|" + keyPat + "|(" + tripleQuotePat + "|" + quotePat + ")"
0
matchRE = re.compile(matchPat)
0
-idKeyPat = "[ \t]*([A-Za-z_][A-Za-z_0-9.]*)"
# Ident w. leading whitespace.
0
+idKeyPat = "[ \t]*([A-Za-z_][A-Za-z_0-9.]*)"
# Ident w. leading whitespace.
0
idRE = re.compile(idKeyPat)
0
asRE = re.compile(r".*?\b(as)\b")
0
def fontify(pytext, searchfrom=0, searchto=None):
0
- searchto = len(pytext)
0
- # Cache a few attributes for quicker reference.
0
- search = matchRE.search
0
- commentTag = 'comment'
0
- keywordTag = 'keyword'
0
- identifierTag = 'identifier'
0
- m = search(pytext, end)
0
- if keyword is not None:
0
- start, end = m.span(1)
0
- yield keywordTag, start, end, None
0
- if keyword in ["def", "class"]:
0
- # If this was a defining keyword, color the
0
- # following identifier.
0
- m = idMatch(pytext, end)
0
- start, end = m.span(1)
0
- yield identifierTag, start, end, None
0
- elif keyword == "import":
0
- # color all the "as" words on same line;
0
- # cheap approximation to the truth
0
- m = asMatch(pytext, end)
0
- start, end = m.span(1)
0
- yield keywordTag, start, end, None
0
- elif m.group(0)[0] == "#":
0
- yield commentTag, start, end, None
0
- yield stringTag, start, end, None
0
+ searchto = len(pytext)
0
+ # Cache a few attributes for quicker reference.
0
+ search = matchRE.search
0
+ commentTag = 'comment'
0
+ keywordTag = 'keyword'
0
+ identifierTag = 'identifier'
0
+ m = search(pytext, end)
0
+ if keyword is not None:
0
+ start, end = m.span(1)
0
+ yield keywordTag, start, end, None
0
+ if keyword in ["def", "class"]:
0
+ # If this was a defining keyword, color the
0
+ # following identifier.
0
+ m = idMatch(pytext, end)
0
+ start, end = m.span(1)
0
+ yield identifierTag, start, end, None
0
+ elif keyword == "import":
0
+ # color all the "as" words on same line;
0
+ # cheap approximation to the truth
0
+ m = asMatch(pytext, end)
0
+ start, end = m.span(1)
0
+ yield keywordTag, start, end, None
0
+ elif m.group(0)[0] == "#":
0
+ yield commentTag, start, end, None
0
+ yield stringTag, start, end, None
0
- for tag, start, end, sublist in fontify(text):
0
- print tag, repr(text[start:end])
0
+ for tag, start, end, sublist in fontify(text):
0
+ print tag, repr(text[start:end])
0
if __name__ == "__main__":
Comments
No one has commented yet.