updated highlighting update

--HG--
branch : pmacs2
This commit is contained in:
Erik Osheim 2012-02-10 21:58:35 -05:00
parent 9c8895c345
commit 1fe29b34aa
1 changed files with 31 additions and 22 deletions

View File

@ -31,7 +31,8 @@ class StringGrammar(Grammar):
class SubTypeGrammar(Grammar): pass class SubTypeGrammar(Grammar): pass
SubTypeGrammar.rules = [ SubTypeGrammar.rules = [
RegionRule('sub', r'\[', SubTypeGrammar, r'\]'), RegionRule('sub', r'\[', SubTypeGrammar, r'\]'),
PatternRule('scala.type', '(?:[a-zA-Z0-9_.]+| *=> *)+'), #PatternRule('scala.type', '(?:[a-zA-Z0-9_.]+| *=> *)+'),
PatternRule('scala.type', '[A-Z][a-zA-Z0-9_.]+'),
PatternRule('spaces', ' +'), PatternRule('spaces', ' +'),
PatternRule('scala.annotation', '@[a-zA-Z_][a-zA-Z0-9_]*'), PatternRule('scala.annotation', '@[a-zA-Z_][a-zA-Z0-9_]*'),
] ]
@ -42,17 +43,17 @@ class ScalaGrammar(Grammar):
RegionRule('scala.comment', r'/\*', NestedCommentGrammar, r'\*/'), RegionRule('scala.comment', r'/\*', NestedCommentGrammar, r'\*/'),
RegionRule('scala.script', r'#!.+$', ShGrammar, r'!#'), RegionRule('scala.script', r'#!.+$', ShGrammar, r'!#'),
PatternMatchRule('x', r'(?<!:)(:)((?:[a-zA-Z0-9_.]+| *=> *)+)', #PatternMatchRule('x', r'(?<!:)(:)((?:[a-zA-Z0-9_.]+| *=> *)+)',
'delimiter', 'scala.type'),
PatternMatchRule('x', r'(?<!:)(:)( +)((?:[a-zA-Z0-9_.]+| *=> *)+)',
'delimiter', 'spaces', 'scala.type'),
#PatternMatchRule('x', r'(?<=[a-zA-Z0-9_ ])(:)([a-zA-Z0-9_]+)',
# 'delimiter', 'scala.type'), # 'delimiter', 'scala.type'),
#PatternMatchRule('x', r'(?<=[a-zA-Z0-9_ ])(:)( +)([a-zA-Z0-9_]+)', #PatternMatchRule('x', r'(?<!:)(:)( +)((?:[a-zA-Z0-9_.]+| *=> *)+)',
# 'delimiter', 'spaces', 'scala.type'), # 'delimiter', 'spaces', 'scala.type'),
PatternMatchRule('x', r'(?<=[a-zA-Z0-9_ ])(:)([a-zA-Z0-9_]+)',
'delimiter', 'scala.type'),
PatternMatchRule('x', r'(?<=[a-zA-Z0-9_ ])(:)( +)([a-zA-Z0-9_]+)',
'delimiter', 'spaces', 'scala.type'),
PatternMatchRule('x', r'(extends|with|new)( +)([a-zA-Z0-9_.]+)', #PatternMatchRule('x', r'(extends|with|new)( +)([a-zA-Z0-9_.]+)',
'scala.reserved', 'spaces', 'scala.type'), # 'scala.reserved', 'spaces', 'scala.type'),
#PatternMatchRule('x', r'(with)( +)([a-zA-Z0-9_.]+)', #PatternMatchRule('x', r'(with)( +)([a-zA-Z0-9_.]+)',
# 'scala.reserved', 'spaces', 'scala.type'), # 'scala.reserved', 'spaces', 'scala.type'),
#PatternMatchRule('x', r'(new)( +)([a-zA-Z0-9_.]+)', #PatternMatchRule('x', r'(new)( +)([a-zA-Z0-9_.]+)',
@ -92,12 +93,11 @@ class ScalaGrammar(Grammar):
PatternRule('scala.symbol', "'[a-zA-Z_][a-zA-Z0-9_]*"), PatternRule('scala.symbol', "'[a-zA-Z_][a-zA-Z0-9_]*"),
PatternRule('scala.annotation', '@[a-zA-Z_][a-zA-Z0-9_]*'), PatternRule('scala.annotation', '@[a-zA-Z_][a-zA-Z0-9_]*'),
PatternRule('scala.type', '[A-Z][a-zA-Z0-9_]*'),
PatternRule('scala.bareword', '[a-zA-Z_][a-zA-Z0-9_]*'), PatternRule('scala.bareword', '[a-zA-Z_][a-zA-Z0-9_]*'),
] ]
class ScalaTabber(StackTabber2): class ScalaTabber(StackTabber2):
#open_tokens = {'delimiter': {'{': '}', '(': ')', '[': ']'}}
#close_tokens = {'delimiter': {'}': '{', ')': '(', ']': '['}}
open_tokens = {'delimiter': {'{': '}', '(': ')'}, open_tokens = {'delimiter': {'{': '}', '(': ')'},
'sub.start': {'[': ']'}} 'sub.start': {'[': ']'}}
close_tokens = {'delimiter': {'}': '{', ')': '('}, close_tokens = {'delimiter': {'}': '{', ')': '('},
@ -201,6 +201,7 @@ class ScalaDecompile(Method):
} }
field_re = re.compile('^Field\s+([^:]+):(.+)$') field_re = re.compile('^Field\s+([^:]+):(.+)$')
long_re = re.compile('^long\s+(\d+)[lL]$')
sig_re = re.compile('^(\w+)\s+([^:]+):\(([^)]*)\)(.+)$') sig_re = re.compile('^(\w+)\s+([^:]+):\(([^)]*)\)(.+)$')
def _parse_sig(self, line): def _parse_sig(self, line):
if line.startswith('class'): if line.startswith('class'):
@ -208,19 +209,25 @@ class ScalaDecompile(Method):
return "class " + self._abbrev(name) return "class " + self._abbrev(name)
elif line.startswith('String'): elif line.startswith('String'):
return 'string "%s"' % line return 'string "%s"' % line[7:]
elif line.startswith('Field'): elif line.startswith('Field'):
m = self.field_re.match(line) m = self.field_re.match(line)
if not m: raise Exception("failed to match %r" % line) if not m: raise Exception("failed to match (1) %r" % line)
name = self._abbrev(m.group(1)) name = self._abbrev(m.group(1))
result = self._parse_args(m.group(2))[0] result = self._parse_args(m.group(2))[0]
#return "field %s -> %s" % (name, result) #return "field %s -> %s" % (name, result)
return "field %s %s" % (result, name) return "field %s %s" % (result, name)
elif line.startswith('long'):
return 'long %s' % line[5:]
elif line.startswith('int'):
return 'int %s' % line[5:]
else: else:
m = self.sig_re.match(line) m = self.sig_re.match(line)
if not m: raise Exception("failed to match %r" % line) if not m: raise Exception("failed to match (3) %r" % line)
typ = self.typs[m.group(1)] typ = self.typs[m.group(1)]
name = self._abbrev(m.group(2)) name = self._abbrev(m.group(2))
@ -233,7 +240,7 @@ class ScalaDecompile(Method):
def _parse_instruction(self, line): def _parse_instruction(self, line):
if '//' in line: if '//' in line:
real, x = line.split('//') real, x = line.split('//')
sig = self._parse_sig(x) sig = self._parse_sig(x.strip())
else: else:
real, sig = line, None real, sig = line, None
@ -250,8 +257,9 @@ class ScalaDecompile(Method):
return "%4s %-22s" % (n, ins + " " + args) return "%4s %-22s" % (n, ins + " " + args)
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
cp = 'target/scala-2.9.1.final/classes' #FIXME clsname = vargs['classname']
argv = ['javap', '-classpath', cp, '-c', vargs['classname']] cp = '.:target/scala-2.9.1.final/classes' #FIXME
argv = ['javap', '-classpath', cp, '-c', '-private', clsname]
p = Popen(argv, stdout=PIPE, stderr=STDOUT) p = Popen(argv, stdout=PIPE, stderr=STDOUT)
lines = p.stdout.readlines() lines = p.stdout.readlines()
@ -269,7 +277,7 @@ class ScalaDecompile(Method):
obj = self._parse_obj(line) obj = self._parse_obj(line)
elif line == "Code:": elif line == "Code:":
continue continue
elif line == "": elif line == "" or line == "}":
if curr: if curr:
methods.append((curr, instructions)) methods.append((curr, instructions))
curr = None curr = None
@ -277,13 +285,14 @@ class ScalaDecompile(Method):
elif self.ins_re.match(line): elif self.ins_re.match(line):
instructions.append(self._parse_instruction(line)) instructions.append(self._parse_instruction(line))
else: else:
import sys
curr = self._parse_method(line) curr = self._parse_method(line)
outlines = [] outlines = []
if self.pkg: if self.pkg:
outlines.append('package ' + self.pkg[:-1]) outlines.append('package ' + self.pkg[:-1])
outlines.append('') outlines.append('')
outlines.append(obj + " {") outlines.append(str(obj) + " {")
for method, instructions in methods: for method, instructions in methods:
if instructions: if instructions:
outlines.append(" " + method + " {") outlines.append(" " + method + " {")
@ -296,8 +305,8 @@ class ScalaDecompile(Method):
output = "\n".join(outlines) output = "\n".join(outlines)
#w.application.data_buffer("*Javap*", output, switch_to=True) name = "*Javap:%s*" % clsname
w.application.data_buffer("*Javap*", output, modename='javap', switch_to=True) w.application.data_buffer(name, output, modename='javap', switch_to=True)
# white is for delimiters, operators, numbers # white is for delimiters, operators, numbers
default = ('default', 'default') default = ('default', 'default')