Beispiel #1
0
        public bool LoadFromString(string content, string resourceName, DslLogDelegation logCallback)
        {
            #if FULL_VERSION
            mDslInfos.Clear();
            Parser.DslLog log = new Parser.DslLog();
            log.OnLog += logCallback;
            Parser.DslToken tokens = new Parser.DslToken(log, content);
            Parser.DslError error = new Parser.DslError(log, tokens);
            Parser.RuntimeAction action = new Parser.RuntimeAction(mDslInfos);
            action.onGetLastToken = () => { return tokens.getLastToken(); };
            action.onGetLastLineNumber = () => { return tokens.getLastLineNumber(); };
            action.onGetComment = (out bool commentOnNewLine) => { commentOnNewLine = tokens.IsCommentOnNewLine(); List<string> ret = new List<string>();ret.AddRange(tokens.GetComments()); tokens.ResetComments(); return ret; };
            action.onSetStringDelimiter = (string begin, string end) => { tokens.setStringDelimiter(begin, end); };
            action.onSetScriptDelimiter = (string begin, string end) => { tokens.setScriptDelimiter(begin, end); };

            Parser.DslParser.parse(action, tokens, error, 0);
            if (error.HasError) {
                for (int i = 0; i < mDslInfos.Count; i++) {
                    mDslInfos[i].Clear();
                }
            } else {
                for (int i = 0; i < mDslInfos.Count; i++) {
                    mDslInfos[i].SetResourceName(resourceName);
                }
            }
            return !error.HasError;
            #else
              return false;
            #endif
        }
Beispiel #2
0
        parse(ref DslAction action,
              ref DslToken tokens,
              ref DslError error,
              short start_symbol)
        {
            short rhs, lhs;
            short production_number, entry, symbol, token, new_token;
            int   production_length, top, index, level;

            short *stack = stackalloc short[65535];

            top        = 65534;
            stack[top] = 0;
            if (start_symbol == 0)
            {
                start_symbol = START_SYMBOL;
            }
            if (top > 0)
            {
                stack[--top] = start_symbol;
            }
            else
            {
                error.message("DslParse: stack overflow\n", ref tokens); return;
            }
            token     = tokens.get();
            new_token = token;

            for (symbol = (stack[top] != 0 ? stack[top++] : (short)0); symbol != 0;)
            {
                if (symbol >= START_ACTION)
                {
                    action.execute(symbol - (START_ACTION - 1));
                }
                else if (symbol >= START_SYMBOL)
                {
                    entry             = 0;
                    level             = 1;
                    production_number = get_conditional_production(symbol);
                    if (production_number != 0)
                    {
                        entry = get_predicted_entry(tokens,
                                                    production_number, token,
                                                    level, 1);
                    }
                    if (entry == 0)
                    {
                        index  = Parse_row[symbol - (START_SYMBOL - 1)];
                        index += token;
                        entry  = Parse[index];
                    }
                    while (entry >= START_CONFLICT)
                    {
                        index  = Conflict_row[entry - (START_CONFLICT - 1)];
                        index += tokens.peek(level);
                        entry  = Conflict[index];
                        ++level;
                    }
                    if (entry != 0)
                    {
                        index             = Production_row[entry];
                        production_length = Production[index] - 1;
                        lhs = Production[++index];
                        if (lhs == symbol)
                        {
                            action.predict(entry);
                            index += production_length;
                            for (; production_length-- > 0; --index)
                            {
                                if (top > 0)
                                {
                                    stack[--top] = Production[index];
                                }
                                else
                                {
                                    error.message("DslParse: stack overflow\n", ref tokens); return;
                                }
                            }
                        }
                        else
                        {
                            new_token = error.no_entry(symbol, token, level - 1, ref tokens);
                        }
                    }
                    else                                           // no table entry
                    {
                        new_token = error.no_entry(symbol, token, level - 1, ref tokens);
                    }
                }
                else if (symbol > 0)
                {
                    if (symbol == token)
                    {
                        token     = tokens.get();
                        new_token = token;
                    }
                    else
                    {
                        new_token = error.mismatch(symbol, token, ref tokens);
                    }
                }
                else
                {
                    error.message("\n parser error: symbol value 0\n", ref tokens);
                }
                if (token != new_token)
                {
                    if (new_token != 0)
                    {
                        token = new_token;
                    }
                    if (token != END_OF_SLK_INPUT_)
                    {
                        continue;
                    }
                }
                symbol = (stack[top] != 0 ? stack[top++] : (short)0);
            }
            if (token != END_OF_SLK_INPUT_)
            {
                error.input_left(ref tokens);
            }
        }
Beispiel #3
0
        public string GenerateBinaryCode(string content, Dictionary<string, string> encodeTable, DslLogDelegation logCallback)
        {
            #if FULL_VERSION
            List<DslInfo> infos = new List<DslInfo>();
            Parser.DslLog log = new Parser.DslLog();
            log.OnLog += logCallback;
            Parser.DslToken tokens = new Parser.DslToken(log, content);
            Parser.DslError error = new Parser.DslError(log, tokens);
            Parser.RuntimeAction action = new Parser.RuntimeAction(infos);
            action.onGetLastToken = () => { return tokens.getLastToken(); };
            action.onGetLastLineNumber = () => { return tokens.getLastLineNumber(); };
            action.onSetStringDelimiter = (string begin, string end) => { tokens.setStringDelimiter(begin, end); };
            action.onSetScriptDelimiter = (string begin, string end) => { tokens.setScriptDelimiter(begin, end); };
            Parser.DslParser.parse(action, tokens, error, 0);
            if (error.HasError) {
                return string.Empty;
            } else {
                MemoryStream stream = new MemoryStream();
                List<string> identifiers = new List<string>();
                foreach (DslInfo info in infos) {
                    Utility.writeBinary(stream, identifiers, (StatementData)info);
                }

                byte[] bytes = stream.ToArray();
                SortedDictionary<string, int> dict = new SortedDictionary<string, int>();
                int ct = identifiers.Count;
                if (ct > 0x00004000) {
                    System.Diagnostics.Debug.Assert(false);
                    //Console.WriteLine("Identifiers count {0} too large than 0x04000", ct);
                    return string.Empty;
                }
                for (int i = 0; i < ct; ++i) {
                    string key = identifiers[i];
                    key = Encode(key, encodeTable);
                    identifiers[i] = key;
                    if (!dict.ContainsKey(key)) {
                        dict.Add(key, 0);
                    }
                }
                List<string> keys = new List<string>(dict.Keys);
                byte[] bytes2;
                using (MemoryStream ms = new MemoryStream()) {
                    for (int i = 0; i < ct; ++i) {
                        string key = identifiers[i];
                        int ix = keys.BinarySearch(key);
                        if (ix < 0x80) {
                            ms.WriteByte((byte)ix);
                        } else {
                            ms.WriteByte((byte)((ix & 0x0000007f) | 0x00000080));
                            ms.WriteByte((byte)(ix >> 7));
                        }
                    }
                    bytes2 = ms.ToArray();
                }
                return string.Format("{0}|{1}|{2}", Convert.ToBase64String(bytes), Convert.ToBase64String(bytes2), string.Join("`", keys.ToArray()));
            }
            #else
              return string.Empty;
            #endif
        }
Beispiel #4
0
Datei: Dsl.cs Projekt: CQiao/DSL
    public bool LoadFromString(string content, string resourceName, DslLogDelegation logCallback)
    {
#if FULL_VERSION
      mDslInfos.Clear();
      Parser.DslLog log = new Parser.DslLog();
      log.OnLog += logCallback;
		  Parser.DslToken tokens = new Parser.DslToken(log, content);
		  Parser.DslError error = new Parser.DslError(log, tokens);
      Parser.RuntimeAction action = new Parser.RuntimeAction(mDslInfos);
      action.onGetLastToken = () => { return tokens.getLastToken(); };
      action.onGetLastLineNumber = () => { return tokens.getLastLineNumber(); };
		  Parser.DslParser.parse(action,tokens,error,0);
		  if(error.HasError)
		  {
        for (int i = 0; i < mDslInfos.Count; i++ )
        {
          mDslInfos[i].Clear();
        }
		  }
      else
      {
        for (int i = 0; i < mDslInfos.Count; i++)
        {
          mDslInfos[i].SetResourceName(resourceName);
        }
		  }
		  return !error.HasError;
#else
      return false;
#endif
    }