diff --git a/test3.py b/test3.py index d6a9ea6..6e1417b 100644 --- a/test3.py +++ b/test3.py @@ -1,5 +1,5 @@ import sys -import lex2, lex2_perl, highlight2 +import lex2, lex2_perl, lex2_python, highlight2 color_list = [] color_list.extend(['\033[3%dm' % x for x in range(0, 8)]) @@ -17,88 +17,131 @@ for i in range(0, len(color_list)): color_dict[color_names[i]] = color_list[i] token_colors = { - # basic stuff - 'escaped': 'lpurple', - 'null': 'white', - 'delimiter': 'white', - 'sub': 'lcyan', - 'number': 'white', - 'operator': 'white', - 'endblock': 'lred', - 'keyword': 'lpurple', - 'scalar': 'yellow', - 'array': 'yellow', - 'deref': 'yellow', - 'hash': 'yellow', - 'hash_key': 'lgreen', - 'comment': 'lred', - 'function': 'lcyan', - 'builtin': 'lpurple', - 'method': 'lcyan', - 'bareword': 'white', - 'label': 'lcyan', - 'package': 'lcyan', - 'class': 'lcyan', - 'use': 'lcyan', - 'method': 'lcyan', + 'perl': { + # basic stuff + 'escaped': 'lpurple', + 'null': 'white', + 'delimiter': 'white', + 'sub': 'lcyan', + 'number': 'white', + 'operator': 'white', + 'endblock': 'lred', + 'keyword': 'lpurple', + 'scalar': 'yellow', + 'array': 'yellow', + 'deref': 'yellow', + 'hash': 'yellow', + 'hash_key': 'lgreen', + 'comment': 'lred', + 'function': 'lcyan', + 'builtin': 'lpurple', + 'method': 'lcyan', + 'bareword': 'white', + 'label': 'lcyan', + 'package': 'lcyan', + 'class': 'lcyan', + 'use': 'lcyan', + 'method': 'lcyan', + + # heredoc + 'heredoc1.start': 'lgreen', + 'heredoc1.null': 'lgreen', + 'heredoc1.end': 'lgreen', + 'heredoc2.start': 'lgreen', + 'heredoc2.null': 'lgreen', + 'heredoc2.end': 'lgreen', + 'eval_heredoc.start': 'lcyan', + 'eval_heredoc.null': 'lcyan', + 'eval_heredoc.end': 'lcyan', + + # pod + 'pod.start': 'lred', + 'pod.null': 'lred', + 'pod.entry': 'lpurple', + 'pod.end': 'lred', + + # "" strings + 'string1.start': 'lgreen', + 'string1.null': 'lgreen', + 'string1.escaped': 'lpurple', + 'string1.deref': 'yellow', + 'string1.end': 'lgreen', + + # '' strings + 'string2.start': 'lgreen', + 'string2.null': 'lgreen', + 'string2.end': 'lgreen', + + # `` strings + 'evalstring': 'lcyan', + + # quoted region + 'quoted': 'lcyan', + 'quoted.start': 'lcyan', + 'quoted.null': 'lcyan', + 'quoted.end': 'lcyan', + + # match regex + 'match.start': 'lcyan', + 'match.end': 'lcyan', + 'match.null': 'lcyan', + + # replace regex + 'replace.start': 'lcyan', + 'replace.middle': 'lcyan', + 'replace.end': 'lcyan', + 'replace.null': 'lcyan', + + # translate regex + 'translate.start': 'lpurple', + 'translate.middle': 'lpurple', + 'translate.end': 'lpurple', + 'translate.null': 'lpurple', + }, - # heredoc - 'heredoc1.start': 'lgreen', - 'heredoc1.null': 'lgreen', - 'heredoc1.end': 'lgreen', - 'heredoc2.start': 'lgreen', - 'heredoc2.null': 'lgreen', - 'heredoc2.end': 'lgreen', - 'eval_heredoc.start': 'lcyan', - 'eval_heredoc.null': 'lcyan', - 'eval_heredoc.end': 'lcyan', + 'python': { + 'keyword': 'lcyan', + 'builtin_method': 'lcyan', + 'methodname': 'lblue', + 'classname': 'lgreen', - # pod - 'pod.start': 'lred', - 'pod.null': 'lred', - 'pod.entry': 'lpurple', - 'pod.end': 'lred', + 'string.start': 'lgreen', + 'string.null': 'lgreen', + 'string.escaped': 'lpurple', + 'string.octal': 'lpurple', + 'string.format': 'yellow', + 'string.end': 'lgreen', - # "" strings - 'string1.start': 'lgreen', - 'string1.null': 'lgreen', - 'string1.escaped': 'lpurple', - 'string1.deref': 'yellow', - 'string1.end': 'lgreen', + 'tq_string.start': 'lgreen', + 'tq_string.null': 'lgreen', + 'tq_string.end': 'lgreen', - # '' strings - 'string2.start': 'lgreen', - 'string2.null': 'lgreen', - 'string2.end': 'lgreen', - - # `` strings - 'evalstring': 'lcyan', - - # quoted region - 'quoted': 'lcyan', - 'quoted.start': 'lcyan', - 'quoted.null': 'lcyan', - 'quoted.end': 'lcyan', - - # match regex - 'match.start': 'lcyan', - 'match.end': 'lcyan', - 'match.null': 'lcyan', - - # replace regex - 'replace.start': 'lcyan', - 'replace.middle': 'lcyan', - 'replace.end': 'lcyan', - 'replace.null': 'lcyan', - - # translate regex - 'translate.start': 'lpurple', - 'translate.middle': 'lpurple', - 'translate.end': 'lpurple', - 'translate.null': 'lpurple', + 'docstring.start': 'lgreen', + 'docstring.null': 'lgreen', + 'docstring.end': 'lgreen', + 'comment': 'lred', + 'continuation': 'lred', + #'operator': 'yellow', + #'delimiter': 'lpurple', + 'system_identifier': 'lcyan', + #'bound method': color.build('yellow', 'default'), + 'import': 'lpurple', + #'bizzaro': 'lpurple', + }, } +grammars = { + 'perl': lex2_perl.PerlGrammar, + 'python': lex2_python.PythonGrammar, +} + +#t = 'perl' +t = 'python' + +m = True +#m = False + paths = sys.argv[1:] for path in paths: f = open(path, 'r') @@ -106,10 +149,14 @@ for path in paths: f.close() lines = data.split('\n') + lexer = lex2.Lexer('lexer', grammars[t]()) - grammar = lex2_perl.PerlGrammar() - lexer = lex2.Lexer('lexer', grammar) - highlighter = highlight2.Highlighter(lexer) - - highlighter.highlight(lines) - highlighter.display(token_colors) + if m: + highlighter = highlight2.Highlighter(lexer) + + highlighter.highlight(lines) + highlighter.display(token_colors[t]) + else: + lexer.lex(lines) + for token in lexer: + print '%-28s| %r' % (token.name, token.string)