#format python """ MoinMoin - Java Source Parser Copyright (c) 2002 by Taesu Pyo All rights reserved. """ # Imports import cgi, string, re, sys ############################################################################# class ReservedWordFormat: def __init__(self,color,bold=1): self.color = color self.bold = bold def formatString(self,word): if self.bold: return '%s' % (self.color,word) else: return '%s' % (self.color,word) class Parser: """ """ formatting_rules = r"""(?P/[*]) (?P//.*$) (?PL?") (?P'\\.'|'[^\\]') (?P[0-9](\.[0-9]*)?(eE[+-][0-9])?[flFLdD]?|0[xX][0-9a-fA-F]+[Ll]?) (?P[a-zA-Z_][0-9a-zA-Z_]*) (?P[~!%^&*()+=|\[\]:;,.<>/?{}-])""" reserved_words = ['class','interface','enum','import','package', 'byte','int','long','float','double','char','short','void','boolean', 'static','final','const','private','public','protected' 'new','this','super','abstract','native','synchronized','transient','volatile','strictfp', 'extends','implements','if','else','while','for','do','switch','case','default','instanceof', 'try','catch','finally','throw','throws','return','continue','break'] constant_words = ['true','false','null'] ID_format = {} for w in reserved_words: ID_format[w] = ReservedWordFormat('#4040ff') for w in constant_words: ID_format[w] = ReservedWordFormat('#008080') ID_def_format = ReservedWordFormat('#000000',bold=0) def __init__(self, raw, request, **kw): self.raw = raw self.rawout = kw.get('out', sys.stdout) self.line_count = 0 def write(self, s): self.rawout.write(s) def format(self, formatter, form): """ Send the text. """ scan_re = re.compile(self.formatting_rules.replace('\n','|'),re.M) self.end_comment_re = re.compile(r'[*]/',re.M) self.end_string_re = re.compile(r'$|[^\\](\\\\)*"',re.M) self.lastpos = 0 self.line = self.raw self.write('
')

		match = scan_re.search(self.line)

		while match and self.lastpos < len(self.line):
			# add the match we found
			self.write_normal_text(self.line[self.lastpos:match.start()])
			self.lastpos = match.end() + (match.end() == self.lastpos)

			self.write_match(match)

			# search for the next one
			match = scan_re.search(self.line, self.lastpos)

		self.write_normal_text(self.line[self.lastpos:])

		self.write('
') def write_normal_text(self,text): text = cgi.escape(text) text = string.expandtabs(text) self.write(text) def write_match(self,match): for type, hit in match.groupdict().items(): if not hit: continue apply(getattr(self, 'write_match_' + type), (hit,)) def write_match_CommentStart(self,hit): self.write('' + str(hit)) match = self.end_comment_re.search(self.line, self.lastpos) if not match: next_lastpos = len(self.line) else: next_lastpos = match.end() + (match.end() == self.lastpos) self.write(self.line[self.lastpos:next_lastpos]) self.lastpos = next_lastpos self.write('') def write_match_StringStart(self,hit): self.write('' + str(hit)) if self.line[self.lastpos] == '"': next_lastpos = self.lastpos + 1 else: match = self.end_string_re.search(self.line, self.lastpos) if not match: next_lastpos = len(self.line) else: next_lastpos = match.end() + (match.end() == self.lastpos) self.write(self.line[self.lastpos:next_lastpos]) self.lastpos = next_lastpos self.write('') def write_match_Char(self,hit): self.write('%s' % hit) def write_match_LineComment(self,hit): self.write('%s' % hit) def write_match_Number(self,hit): self.write('%s' % hit) def write_match_ID(self,hit): c = self.ID_format.get(hit,self.ID_def_format) self.write(c.formatString(hit)) def write_match_SPChar(self,hit): self.write('%s' % cgi.escape(hit)) if __name__ == "__main__": import os, sys # open Java source. source = open('test.java').read() Parser(source, None, out = open('test.html', 'wt')).format(None, None) # load HTML page into browser if os.name == "nt": os.system("explorer test.html") else: os.system("netscape test.html &")