脚本语法解析
예제 #1
0
		public static byte[] Serialize(String breviary, string data) {
			List<Token> tokens = new ScriptLexer(data, breviary).GetTokens();
            if (tokens.Count == 0) return new byte[0];
            int sourceLine = 0;
            byte[] ret = null;
            using (MemoryStream stream = new MemoryStream()) {
                using (BinaryWriter writer = new BinaryWriter(stream)) {
                    writer.Write((sbyte)0);          //第一个字符写入一个null 以此判断文件是二进制文件还是字符串文件
                    writer.Write(tokens.Count);
                    for (int i = 0; i < tokens.Count; ++i) {
                        var token = tokens[i];
                        if (sourceLine != token.SourceLine) {
                            sourceLine = token.SourceLine;
                            writer.Write(LineFlag);
                            writer.Write(token.SourceLine);
                        }
                        writer.Write((sbyte)token.Type);
                        switch (token.Type) {
                        case TokenType.Boolean:
                            writer.Write((bool)token.Lexeme ? (sbyte)1 : (sbyte)0);
                            break;
                        case TokenType.String:
                        case TokenType.SimpleString:
                            Util.WriteString(writer, (string)token.Lexeme);
                            break;
                        case TokenType.Identifier:
                            Util.WriteString(writer, (string)token.Lexeme);
                            break;
                        case TokenType.Number:
                            if (token.Lexeme is double) {
                                writer.Write((sbyte)1);
                                writer.Write((double)token.Lexeme);
                            } else {
                                writer.Write((sbyte)2);
                                writer.Write((long)token.Lexeme);
                            }
                            break;
                        }
                    }
                    ret = stream.ToArray();
                }
            }
            return ret;
        }
예제 #2
0
 internal ScriptObject LoadString(String strBreviary, String strBuffer, ScriptContext context, bool clearStack)
 {
     try {
         if (Util.IsNullOrEmpty(strBuffer)) return Null;
         if (clearStack) m_StackInfoStack.Clear();
         ScriptLexer scriptLexer = new ScriptLexer(strBuffer);
         strBreviary = Util.IsNullOrEmpty(strBreviary) ? scriptLexer.GetBreviary() : strBreviary;
         return Load(strBreviary, scriptLexer.GetTokens(), context);
     } catch (System.Exception e) {
         throw new ScriptException("load buffer [" + strBreviary + "] is error : " + e.ToString());
     }
 }