分析牛人js版删除代码注释(状态机机制)
2012-11-23 18:34
609 查看
花了几天时间看源码才弄清楚状态之间的转换关系,画了如下状态关系转换图:
牛人源代码如下:
原文地址:http://blog.misakamm.org/p/395
牛人源代码如下:
function on_format(id) { var textarea = document.getElementById(id); var text = textarea.value; var obj = format_obj(); obj.init(); obj.g_trigraph_on = document.getElementById('format_trigraph').checked; len = text.length; for (var c = 0; c < len; ++c) { obj.deal(text.substring(c, c+1)); } obj.deal('EOF'); textarea.value = obj.g_output_string; } function format_obj() { return { s_normal: 0, s_char: 1, s_char_conv: 2, s_string: 3, s_string_conv: 4, s_linecomment: 5, s_linecomment_conv: 6, s_comment1: 7, s_comment: 8, s_commented1: 9, s_conv: 10, s_eof: 11, g_state: 0, g_conv_state: 0, g_convlinecnt: 0, g_trigraph: 0, g_output_string: "", g_trigraph_on: 1, put: function(c) { this.g_output_string += c; }, init: function() { this.g_state = this.s_normal; this.g_conv_state = this.s_normal; this.g_trigraph = 0; this.g_output_string = "" }, deal: function(c) { if (this.g_trigraph_on) { if (c == '?') { // trigraph pre process if (this.g_trigraph < 2) { this.g_trigraph++; return 0; } } else if (this.g_trigraph == 2 && c == '/') { c = '\\'; this.g_trigraph = 0; } else if (this.g_trigraph > 0) { var t = this.g_trigraph; this.g_trigraph = 2; while (t--) this.deal('?'); this.g_trigraph = 0; } } if (this.g_conv_state == this.s_conv) { // '\' at end of line this.g_conv_state = this.s_normal; if (c == '\n') { ++this.g_convlinecnt; return 0; } else { if (this.g_state == this.s_comment1) { this.put('/'); while (this.g_convlinecnt) --this.g_convlinecnt, this.put("\\\n"); this.put('\\'); this.g_state = this.s_normal; } else if (this.g_state == this.s_commented1) { this.g_state = this.s_comment; } } } else if (c == '\\') { if (this.g_state == this.s_comment1 || this.g_state == this.s_commented1) { this.g_conv_state = this.s_conv; return 0; } } switch(this.g_state) { case this.s_normal: if (c == '\"') this.g_state = this.s_string, this.put(c); else if (c == '\'') this.g_state = this.s_char, this.put(c); else if (c == '/') this.g_state = this.s_comment1, this.g_convlinecnt = 0; else if (c == 'EOF') this.g_state = this.s_eof; else this.put(c); break; case this.s_char: this.put(c); if (c == '\'') this.g_state = this.s_normal; else if (c == '\\') this.g_state = this.s_char_conv; else if (c == 'EOF') this.g_state = this.s_eof; break; case this.s_char_conv: this.put(c); this.g_state = this.s_char; break; case this.s_string: this.put(c); if (c == '\"') this.g_state = this.s_normal; else if (c == '\\') this.g_state = this.s_string_conv; else if (c == 'EOF') this.g_state = this.s_eof; break; case this.s_string_conv: this.put(c); this.g_state = this.s_string; break; case this.s_linecomment: if (c == '\\') this.g_state = this.s_linecomment_conv; else if (c == '\n') this.g_state = this.s_normal, this.put(c); else if (c == 'EOF') this.g_state = this.s_eof; break; case this.s_linecomment_conv: if (c == '\\') ; else if (c == 'EOF') this.g_state = this.s_eof; else this.g_state = this.s_linecomment; break; case this.s_comment1: if (c == '/') this.g_state = this.s_linecomment; else if (c == '*') this.g_state = this.s_comment; else if (c == 'EOF') this.g_state = this.s_eof; else { this.put('/'); while (this.g_convlinecnt) --this.g_convlinecnt, this.put("\\\n"); this.g_state = this.s_normal; this.deal(c); } break; case this.s_comment: if (c == '*') this.g_state = this.s_commented1; else if (c == 'EOF') this.g_state = this.s_eof; break; case this.s_commented1: if (c == '/') this.g_state = this.s_normal, this.put(' '); else if (c == 'EOF') this.g_state = this.s_eof; else this.g_state = this.s_comment; break; case this.s_eof: return -1; default: return -2; } return 0; }, } }
原文地址:http://blog.misakamm.org/p/395
相关文章推荐
- 分析牛人js版删除代码注释(状态机机制)
- JSP中混杂HTML、JS、CSS删除各种代码注释
- 1.13Android 学习+进度之十三-删除冗余代码+注释+分析设计方法
- 状态机编程思想(2):删除代码注释(目前支持C/C++和Java)
- 怎样删除C/C++代码中的所有注释?浅谈状态机的编程思想
- 用js读、写、删除Cookie代码分享及详细注释说明
- 怎样删除C/C++代码中的所有注释?浅谈状态机的编程思想
- 状态机编程思想(2):删除代码注释(目前支持C/C++和Java)
- 用js读、写、删除Cookie代码分享及详细注释说明
- node.js require 实现机制初窥;pomelo代码分析5----------- pomel-loader模块
- Qt 信号槽机制解析二 分析原代码
- 用js写“算24”游戏的思路分析与实现代码
- (C语言版)链表(一)——实现单向链表创建、插入、删除等简单操作(包含个人理解说明及注释,新手跟着写代码)
- 利用js动态添加删除table行的示例代码
- go context专题(三)- context 工作机制和代码分析
- 用js读、写、删除Cookie代码续篇
- js分析 快速定位 js 代码, 还原被混淆压缩的 js 代码
- 将C/C++代码中的注释删除
- thinkphp点击删除,或者修改,附带id 跳转到相应的页面的js 代码
- RxCache 的代码分析,含缓存时间duration的在代码中改变的自己实现的机制