101 lines
3.6 KiB
Python
101 lines
3.6 KiB
Python
import sys
|
|
import lex2, lex2_perl
|
|
|
|
color_list = []
|
|
color_list.extend(['\033[3%dm' % x for x in range(0, 8)])
|
|
color_list.extend(['\033[3%d;1m' % x for x in range(0, 8)])
|
|
color_list.append('\033[0m')
|
|
|
|
color_names = [
|
|
'black', 'dred', 'dgreen', 'brown', 'dblue', 'dpurple', 'dcyan', 'lgrey',
|
|
'dgrey', 'lred', 'lgreen', 'yellow', 'lblue', 'lpurple', 'lcyan', 'white',
|
|
'unset',
|
|
]
|
|
|
|
color_dict ={}
|
|
for i in range(0, len(color_list)):
|
|
color_dict[color_names[i]] = color_list[i]
|
|
|
|
token_colors = {
|
|
'null': 'white',
|
|
'delimiter': 'white',
|
|
'pod.start': 'lred',
|
|
'pod.null': 'lred',
|
|
'pod.end': 'lred',
|
|
'pod.header': 'lpurple',
|
|
'sub': 'lcyan',
|
|
'number': 'white',
|
|
'operator': 'white',
|
|
'heredoc': 'lgreen',
|
|
'endblock': 'lred',
|
|
'pod': 'lred',
|
|
'comment': 'lred',
|
|
'string1': 'lgreen',
|
|
'string1.start': 'lgreen',
|
|
'string1.null': 'lgreen',
|
|
'string1.escaped': 'lpurple',
|
|
'string1.scalar': 'yellow',
|
|
'string1.system_scalar': 'yellow',
|
|
'string1.hash_deref': 'yellow',
|
|
'string1.hash_bareword_index': 'lgreen',
|
|
'string1.end': 'lgreen',
|
|
'string2': 'lgreen',
|
|
'string2.start': 'lgreen',
|
|
'string2.null': 'lgreen',
|
|
'string2.end': 'lgreen',
|
|
'evalstring': 'lcyan',
|
|
'default_string': 'lgreen',
|
|
'keyword': 'lpurple',
|
|
'length_scalar': 'yellow',
|
|
'system_scalar': 'yellow',
|
|
'system_array': 'yellow',
|
|
'scalar': 'yellow',
|
|
'dereference': 'yellow',
|
|
'array': 'yellow',
|
|
'hash': 'yellow',
|
|
'hash_bareword_index': 'lgreen',
|
|
'quoted_region': 'lcyan',
|
|
'match_regex': 'lcyan',
|
|
'replace_regex': 'lcyan',
|
|
'literal_hash_bareword_index': 'lgreen',
|
|
'interpolated_scalar': 'yellow',
|
|
'interpolated_system_scalar': 'yellow',
|
|
'interpolated_array': 'yellow',
|
|
'interpolated_system_array': 'yellow',
|
|
'interpolated_hash': 'yellow',
|
|
'label': 'lcyan',
|
|
'package': 'lcyan',
|
|
'use': 'lcyan',
|
|
'method': 'lcyan',
|
|
'methodref': 'lcyan',
|
|
'method_declaration': 'lcyan',
|
|
'instance_method': 'lcyan',
|
|
'static_method': 'lcyan',
|
|
'builtin_method': 'lpurple',
|
|
'bareword_method': 'lcyan',
|
|
'bareword': 'yellow',
|
|
'bizzaro': 'lpurple',
|
|
}
|
|
|
|
paths = sys.argv[1:]
|
|
for path in paths:
|
|
f = open(path, 'r')
|
|
data = f.read()
|
|
f.close()
|
|
|
|
lines = data.split('\n')
|
|
|
|
grammar = lex2_perl.PerlGrammar()
|
|
lexer = lex2.Lexer('lexer', grammar)
|
|
|
|
lexer.lex(lines)
|
|
y = 0
|
|
for token in lexer:
|
|
while token.y > y:
|
|
sys.stdout.write('\n')
|
|
y += 1
|
|
#color_name = token_colors.get(token.name, 'white')
|
|
color_name = token_colors[token.name]
|
|
sys.stdout.write(color_dict[color_name])
|
|
sys.stdout.write(token.string)
|
|
sys.stdout.write('\n') |