1 """
   2 	MoinMoin - Java Source Parser
   3 
   4 	Copyright (c) 2002 by Taesu Pyo <bigflood@hitel.net>
   5 	All rights reserved.
   6 
   7 """
   8 
   9 # Imports
  10 import cgi, string, re, sys
  11 
  12 
  13 #############################################################################
  14 
  15 
  16 class ReservedWordFormat:
  17 	def __init__(self,color,bold=1):
  18 		self.color = color
  19 		self.bold = bold
  20 	def formatString(self,word):
  21 		if self.bold:
  22 			return '<font color="%s"><b>%s</b></font>' % (self.color,word)
  23 		else:
  24 			return '<font color="%s">%s</font>' % (self.color,word)
  25 
  26 
  27 class Parser:
  28 	"""
  29 	"""
  30 	formatting_rules = r"""(?P<CommentStart>/[*])
  31 (?P<LineComment>//.*$)
  32 (?P<StringStart>L?")
  33 (?P<Char>'\\.'|'[^\\]')
  34 (?P<Number>[0-9](\.[0-9]*)?(eE[+-][0-9])?[flFLdD]?|0[xX][0-9a-fA-F]+[Ll]?)
  35 (?P<ID>[a-zA-Z_][0-9a-zA-Z_]*)
  36 (?P<SPChar>[~!%^&*()+=|\[\]:;,.<>/?{}-])"""
  37 	
  38 	reserved_words = ['class','interface','enum','import','package',
  39 	'byte','int','long','float','double','char','short','void','boolean',
  40 	'static','final','const','private','public','protected'
  41 	'new','this','super','abstract','native','synchronized','transient','volatile','strictfp',
  42 	'extends','implements','if','else','while','for','do','switch','case','default','instanceof',
  43 	'try','catch','finally','throw','throws','return','continue','break']
  44 
  45 	constant_words = ['true','false','null']
  46 
  47 	ID_format = {}
  48 	for w in reserved_words:
  49 		ID_format[w] = ReservedWordFormat('#4040ff')
  50 	
  51 	for w in constant_words:
  52 		ID_format[w] = ReservedWordFormat('#008080')
  53 
  54 	ID_def_format = ReservedWordFormat('#000000',bold=0)
  55 
  56 
  57 	def __init__(self, raw, request, **kw):
  58 		self.raw = raw
  59 		self.rawout = kw.get('out', sys.stdout)
  60 		self.line_count = 0
  61 	
  62 	
  63 	def write(self, s):
  64 		self.rawout.write(s)
  65 
  66 
  67 	def format(self, formatter, form):
  68 		""" Send the text.
  69 		"""
  70 
  71 		scan_re = re.compile(self.formatting_rules.replace('\n','|'),re.M)
  72 		self.end_comment_re = re.compile(r'[*]/',re.M)
  73 		self.end_string_re = re.compile(r'$|[^\\](\\\\)*"',re.M)
  74 
  75 		self.lastpos = 0
  76 		self.line = self.raw
  77 
  78 		self.write('<pre><font face="Lucida,Courier New" color="#C00000">')
  79 
  80 		match = scan_re.search(self.line)
  81 
  82 		while match and self.lastpos < len(self.line):
  83 			# add the match we found
  84 			self.write_normal_text(self.line[self.lastpos:match.start()])
  85 			self.lastpos = match.end() + (match.end() == self.lastpos)
  86 
  87 			self.write_match(match)
  88 
  89 			# search for the next one
  90 			match = scan_re.search(self.line, self.lastpos)
  91 
  92 		self.write_normal_text(self.line[self.lastpos:])
  93 
  94 		self.write('</font></pre>')
  95 
  96 
  97 	def write_normal_text(self,text):
  98 		text = cgi.escape(text)
  99 		text = string.expandtabs(text)
 100 		self.write(text)
 101 
 102 	def write_match(self,match):
 103 		for type, hit in match.groupdict().items():
 104 			if not hit: continue
 105 			apply(getattr(self, 'write_match_' + type), (hit,))
 106 	
 107 
 108 	def write_match_CommentStart(self,hit):
 109 		self.write('<font color="#808080">' + str(hit))
 110 		match = self.end_comment_re.search(self.line, self.lastpos)
 111 		if not match:
 112 			next_lastpos = len(self.line)
 113 		else:
 114 			next_lastpos = match.end() + (match.end() == self.lastpos)
 115 		self.write(self.line[self.lastpos:next_lastpos])
 116 		self.lastpos = next_lastpos
 117 		self.write('</font>')
 118 
 119 	def write_match_StringStart(self,hit):
 120 		self.write('<font color="#004080">' + str(hit))
 121 
 122 		if self.line[self.lastpos] == '"':
 123 			next_lastpos = self.lastpos + 1
 124 		else:
 125 			match = self.end_string_re.search(self.line, self.lastpos)
 126 			if not match:
 127 				next_lastpos = len(self.line)
 128 			else:
 129 				next_lastpos = match.end() + (match.end() == self.lastpos)
 130 		self.write(self.line[self.lastpos:next_lastpos])
 131 		self.lastpos = next_lastpos
 132 		self.write('</font>')
 133 
 134 	def write_match_Char(self,hit):
 135 		self.write('<font color="#004080">%s</font>' % hit)
 136 
 137 	def write_match_LineComment(self,hit):
 138 		self.write('<font color="#808080">%s</font>' % hit)
 139 
 140 	def write_match_Number(self,hit):
 141 		self.write('<font color="#008080"><B>%s</B></font>' % hit)
 142 
 143 	def write_match_ID(self,hit):
 144 		c = self.ID_format.get(hit,self.ID_def_format)
 145 		self.write(c.formatString(hit))
 146 
 147 	def write_match_SPChar(self,hit):
 148 		self.write('<font color="#0000C0">%s</font>' % cgi.escape(hit))
 149 
 150 
 151 if __name__ == "__main__":
 152 	import os, sys
 153 
 154 	# open Java source.
 155 	source = open('test.java').read()
 156 
 157 	Parser(source, None, out = open('test.html', 'wt')).format(None, None)
 158 
 159 	# load HTML page into browser
 160 	if os.name == "nt":
 161 		os.system("explorer test.html")
 162 	else:
 163 		os.system("netscape test.html &")

MoinMoin: ParserMarket/java.py (last edited 2007-10-29 19:20:38 by localhost)