您的位置:首页 > Web前端 > JavaScript

python 写的javascript格式化程序

2010-03-16 16:25 281 查看
最近用python语言写了一个js格式化文件,支持 单个文件或者目录下所有js文件的格式化,功能还行.算法的思想来自于Einar Lielmanis,详见http://jsbeautifier.org/

这个程序花了我几天的时间,熬了几个晚上,加上感冒,几次想放弃...所以写下来,纪念下.截取部分代码如下:

代码一:


while (True) :
token_text, token_type= get_next_token()
if (token_type == 'TK_EOF') :
break
eval("proc_"+token_type)()

constants.last_last_text = constants.last_text
constants.last_type = token_type
constants.last_text = token_text

return re.sub(r"[\n]+$","",''.join(constants.output))


代码二:


def proc_TK_START_BLOCK():
if (constants.last_word == 'do') :
set_mode('DO_BLOCK')
else :
set_mode('BLOCK')

if (constants.last_type == 'TK_OPERATOR' and constants.last_type == 'TK_START_EXPR') :
if (constants.last_type == 'TK_START_BLOCK') :
print_newline()
else :
print_single_space()
indent()
print_token()


代码三:


def proc_TK_END_BLOCK():
#pdb.set_trace()
restore_mode()
if (constants.last_type == 'TK_START_BLOCK') :
# nothing
if (constants.just_added_newline) :

remove_indent()
else :
trim_output()
else :
print_newline()
print_token()


最后提供一个可执行程序的下载:

http://files.cnblogs.com/ms_config/dist.rar

源程序:

http://files.cnblogs.com/ms_config/python_js_beautiful_source.rar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: