自己动手设计代码编辑器——(五)分析源代码
来源:互联网 发布:大数据底层开发工程师 编辑:程序博客网 时间:2024/04/29 14:53
因为之前的设计不合理,就重新开了工程。花了几天时间,重做的进度赶上之前的了
今天来说说分析源代码
源码分析中会用到的其它东西,之前都介绍过了。
接下来看UParser这个类中最后的函数GetNextCut,是用来把源码分析为UCodeCut集合的。其中包括关键字设别,变量设别等等
开始
UCodeCut cut = new UCodeCut();List<byte> cutData = new List<byte>();byte b;UCutType currType = UCutType.None; // 状态机的状态bool inString = false; // 是否解析字符串if (EndOfCode == true){cut.CutType = UCutType.End;return cut;}
cut用于保存分析玩的CodeCut
cutData是cut中的具体数据
currType是当前状态机的状态
接下来是状态机的循环
while (!EndOfCode){b = GetNextByte();cutData.Add(b);......}
然后是状态机的入口
#region UCutType.None if (currType == UCutType.None) { if (b == UConfig.Space) { currType = UCutType.Space; continue; } if (b == UConfig.Tab) { currType = UCutType.Tab; break; } if (b == UConfig.DoubleQuote) { currType = UCutType.String; inString = true; continue; } if (b == UConfig.NewLine) { currType = UCutType.NewLine; // 跳过回车符 if (PeekNextByte() == UConfig.Enter) { GetNextByte(); } break; } if (b == UConfig.BackSlash) { if (PeekNextByte() == UConfig.BackSlash) { currType = UCutType.Annotation; continue; } } if (UHelper.IsSymbol(b)) { currType = UCutType.Symbol; break; } if (UHelper.IsCharacter(b)) { currType = UCutType.Normal; continue; } if (UHelper.IsDigit(b)) { currType = UCutType.Digit; continue; } } #endregion
接下来是Normal状态,就是一些普通的字符串
#region UCutType.Normal if (currType == UCutType.Normal) { if (UHelper.IsCutEnd(b)) { BackToLastByte(); cutData.RemoveAt(cutData.Count - 1); break; } } #endregion
接下来是String状态,是字符串
#region UCutType.String if (currType == UCutType.String) { if (b == UConfig.NewLine) { BackToLastByte(); currType = UCutType.Normal; break; } if (b == UConfig.DoubleQuote) { inString = false; break; } if (b == UConfig.Slash) { // 添加 \ 后的字符 if (inString) { //ch = (char)GetNextChar(); //cutData.Add((byte)ch); cutData.Add(GetNextByte()); continue; } } } #endregion
接下来 数字状态、注释等等,都一样的判断格式
最后循环结束,检查关键字
// 替换Tab为Space if (currType == UCutType.Tab) { cut.Data = UConfig.TabString; cut.CutType = UCutType.Space; } else { cut.Data = UHelper.GetStringByBytes(cutData.ToArray()); cut.CutType = currType; // 如果是普通的一段文本,判断是否为关键字 if (currType == UCutType.Normal) { if (UHelper.IsKeyWord(cut.Data)) { cut.CutType = UCutType.KeyWord; } } }
最后检查,是否为变量或者类
if (currType == UCutType.Normal) { if (LastCut2.CutType == UCutType.KeyWord && LastCut1.CutType == UCutType.Space) { // 类的定义 if (LastCut2.Data == UConfig.ClassString) { cut.CutType = UCutType.ClassName; } else { // 函数名的定义 foreach (string str in UConfig.FunctionDefineString) { if (str == LastCut2.Data) { if (PeekNextByte() == (byte)'(') { cut.CutType = UCutType.FunctionName; } else { cut.CutType = UCutType.VariableName; } break; } } } } } LastCut2 = LastCut1; LastCut1 = cut;
这就是一个CodeCut的分析过程了
接下来只要不断的调用GetNextCut()就能分析完整个代码
- 自己动手设计代码编辑器——(五)分析源代码
- 自己动手设计代码编辑器——(二)导入源代码
- 自己动手设计代码编辑器——(一)源代代码分析
- 自己动手设计代码编辑器——(四)代码智能提示(自动完成功能)
- 自己动手设计代码编辑器——(三)撤销与重做
- 自己动手设计代码编辑器——(六)自己写XML管理类
- 自己动手做QQ(毕业设计)—p2p聊天工具(IM)源代码
- Iperf 源代码分析(五)
- 自己动手写编辑器——Lumia Sidebar
- 自己动手打造ORM(五) —实现
- 自己动手设计 PHP MVC 框架(一)——URL
- WebKit内核源代码分析(五)
- WebKit内核源代码分析(五)
- Chrome源代码分析之socket(五)
- Nginx源代码分析之connect(五)
- eMule源代码分析(五)
- P2psim源代码分析五
- 自己动手写操作系统(五)
- 在ios中的Document中创建多级文件夹以及文件
- Common Programming Tasks in the JavaScript Object Model
- 志同道合的团队
- 删除mysql重复记录的办法
- 从VC6到VC9移植代码问题总结 from 吹泡泡的小猫
- 自己动手设计代码编辑器——(五)分析源代码
- 斯特林数
- 汇编之寄存器详解
- vs2010 无法断点调试 无可用源问题
- 【盘点电脑容易中毒各种原因】
- 关于ESB实施的几点建议
- tp快捷路径 __URL__和__APP__
- 编写push notification之服务器端发送通知 java apns
- 基于apache cxf的webservice小例子