/// <summary>
    /// Save the supplied configuration to file.
    /// </summary>
    /// <param name="config">Configuration to save.</param>
    /// <param name="fileName">File to save to.</param>
    /// <returns><c>true</c> if successful, otherwise <c>false</c>.</returns>
    public static bool Save(Configuration config, string fileName)
    {
      try
      {
        XmlSerializer writer = new XmlSerializer(typeof (Configuration));
        using (StreamWriter file = new StreamWriter(fileName))
          writer.Serialize(file, config);

        return true;
      }
      catch (Exception ex)
      {
        IrssLog.Error(ex);
        return false;
      }
    }
    /// <summary>
    /// Imports the specified configuration into this configuration.
    /// </summary>
    /// <param name="config">The configuration to merge in.</param>
    public void Import(Configuration config)
    {
      // TODO: Improve import logic ...

      _mappedEvents.AddRange(config.Events);
      _programSettings.AddRange(config.Programs);
      _systemWideMappings.AddRange(config.SystemWideMappings);
    }
Exemple #3
0
 public Warning(int i = -1, Configuration.elem s = new Configuration.elem(), Configuration.elem s1 = new Configuration.elem())
 {
     nTerm = s;
     term = s1;
     id = i;
 }
Exemple #4
0
        //Синтаксический анализ кода
        public bool GoAnalyze()
        {
            if (Code.SyntError != "none") return false;

            Gramma.Init();
            #if DEBUG
            Gramma.Check();
            #endif
            Configuration config = new Configuration();

            //История конфигураций
            Stack<Configuration> history = new Stack<Configuration>();
            history.Push(config);

            while (true) //додумать условие цикла
            {
                //Отношение перехода делаем на токенах

                if (config.s == 'q')
                {
                    if (config.L2.Count != 0)
                    {
                        //1 пункт отношения, "На верху L2 не терминал, растим дерево"
                        if (!config.L2.Peek().term)
                        {

                            Configuration.elem item = new Configuration.elem(config.L2.Pop());
                            Pravilo current = Gramma.p.Find(x => x.leftSide == item.value); //текущее правило
                            if (item.value != "string")    //првоеряю не ожидалась ли строка
                            {
                                if (item.value != "chislo" && item.value != "integer" && item.value != "real")
                                {
                                    if (item.value != "id")
                                    {

                                        config.L1.Push(new Configuration.elem(current.leftSide, false, 1));//в магазин L1 суём левую часть правила
                                        for (int altIndex = current.rightSide[0].Count - 1; altIndex >= 0; altIndex--)//суём первую альтернативу в магазин L2 по элементно
                                        {
                                            if (current.rightSide[0][altIndex].notTerminal != "")
                                            {
                                                config.L2.Push(new Configuration.elem(current.rightSide[0][altIndex].notTerminal, false, 1));
                                            }
                                            else
                                            {
                                                config.L2.Push(new Configuration.elem(current.rightSide[0][altIndex].terminal, true, 1));
                                            }
                                        }
                                    }
                                    else
                                    {
                                        //Успешное прохождение идентификатора
                                        if (Code.Tokens[config.i].klass == "идентификатор")
                                        {
                                            config.L1.Push(new Configuration.elem(current.leftSide, false, 1));
                                            config.L2.Push(new Configuration.elem(Code.Tokens[config.i].value, true, 1));

                                        }
                                        else
                                        {
                                            config.L1.Push(new Configuration.elem(current.leftSide, false, 1));//в магазин L1 суём левую часть правила
                                            for (int altIndex = current.rightSide[0].Count - 1; altIndex >= 0; altIndex--)//суём первую альтернативу в магазин L2 по элементно
                                            {
                                                if (current.rightSide[0][altIndex].notTerminal != "")
                                                {
                                                    config.L2.Push(new Configuration.elem(current.rightSide[0][altIndex].notTerminal, false, 1));
                                                }
                                                else
                                                {
                                                    config.L2.Push(new Configuration.elem(current.rightSide[0][altIndex].terminal, true, 1));
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    //Успешное прохождение числа
                                    if (Code.Tokens[config.i].klass == "число   ")
                                    {
                                        if (Code.Tokens[config.i].type == "real")
                                        {
                                            config.L1.Push(new Configuration.elem(current.leftSide, false, 2));
                                            config.L2.Push(new Configuration.elem(Code.Tokens[config.i].value, true, 2));
                                        }
                                        else
                                        {
                                            config.L1.Push(new Configuration.elem(current.leftSide, false, 1));
                                            config.L2.Push(new Configuration.elem(Code.Tokens[config.i].value, true, 1));
                                        }
                                    }
                                    else
                                    {
                                        /*
                                        Code.SyntError = "В строке " + Code.Tokens[config.i].str_num + " столбце " + Code.Tokens[config.i].pos_num + " ожидалось число";
                                        return false;
                                         */
                                        config.L1.Push(new Configuration.elem(current.leftSide, false, 1));//в магазин L1 суём левую часть правила
                                        for (int altIndex = current.rightSide[0].Count - 1; altIndex >= 0; altIndex--)//суём первую альтернативу в магазин L2 по элементно
                                        {
                                            if (current.rightSide[0][altIndex].notTerminal != "")
                                            {
                                                config.L2.Push(new Configuration.elem(current.rightSide[0][altIndex].notTerminal, false, 1));
                                            }
                                            else
                                            {
                                                config.L2.Push(new Configuration.elem(current.rightSide[0][altIndex].terminal, true, 1));
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                //Успешное прохождение строки
                                if (Code.Tokens[config.i].klass == "строка")
                                {
                                    config.L1.Push(new Configuration.elem(current.leftSide, false, 1));
                                    config.L2.Push(new Configuration.elem(Code.Tokens[config.i].value, true, 1));
                                }
                                else
                                {
                                    /*
                                    Code.SyntError = "В строке " + Code.Tokens[config.i].str_num + " столбце " + Code.Tokens[config.i].pos_num + " ожидалась строка";
                                    return false;
                                        */
                                    config.L1.Push(new Configuration.elem(current.leftSide, false, 1));//в магазин L1 суём левую часть правила
                                    for (int altIndex = current.rightSide[0].Count - 1; altIndex >= 0; altIndex--)//суём первую альтернативу в магазин L2 по элементно
                                    {
                                        if (current.rightSide[0][altIndex].notTerminal != "")
                                        {
                                            config.L2.Push(new Configuration.elem(current.rightSide[0][altIndex].notTerminal, false, 1));
                                        }
                                        else
                                        {
                                            config.L2.Push(new Configuration.elem(current.rightSide[0][altIndex].terminal, true, 1));
                                        }
                                    }
                                }
                            }
                            history.Push(config);
                        }

                        //2 пункт отношения, "На верху L2 терминал сравниваем входной токен"
                        else
                        {
                            if (config.i < Code.Tokens.Count)
                            {
                                Configuration.elem item = new Configuration.elem(config.L2.Peek());
                                //"Успешное сравнение"
                                if ((Code.Tokens[config.i].value == item.value) || (item.value == "lambda"))
                                {
                                    if ((warn != null) && (warn.id < config.i))
                                    {
                                        //Убираем подозрительный токен
                                        warn = new Warning();
                                    }
                                    config.L2.Pop();
                                    config.L1.Push(item);
                                    if (item.value != "lambda") config.i++;
                                }
                                //4 пункт отношения. Не успешное сравнение
                                else
                                {
                                    //Сохраняем подозрительный токен
                                    if (warn.id < config.i)
                                    {
                                        warn = new Warning();
                                        warn.nTerm = config.L1.Peek();
                                        warn.term = item;
                                        warn.id = config.i;
                                    }
                                    config.s = 'b';
                                }
                            }
                            else
                            {
                                Code.SyntError = "Неожиданный конец файла";
                                return false;
                            }
                            history.Push(config);
                        }
                    }
                    else
                    {
                        //3 пункт отншения. Успешное завершение
                        if (config.i == Code.Tokens.Count)
                        {
                            config.s = 't';
                            config.L2.Push(new Configuration.elem("lambda", true, 0));
                            history.Push(config);
                            Code.conclusion = config.Seq();
                            return true;
                        }
                        else
                        {
                            ErrorForm(warn);
                            //Code.SyntError = "Неожиданный конец файла";
                            return false;
                        }
                    }

                }
                else
                {
                    if (!config.L1.Peek().term)
                    //6. Испытание очередной альтернативы
                    {
                        //тестовый костыль
                        if (!config.L2.Peek().term || !(config.L1.Peek().value == "id"))
                        {

                            Configuration.elem item = new Configuration.elem(config.L1.Pop());
                            Pravilo current = Gramma.p.Find(x => x.leftSide == item.value); //текущее правило
                            if (current.rightSide.Count > item.altIndex)
                            //a. замена альтернативы если существует следующая альтернатива
                            {
                                for (int elemIndex = 0; elemIndex < current.rightSide[item.altIndex - 1].Count; elemIndex++)
                                {
                                    if (config.L2.Count != 0)
                                    {
                                        config.L2.Pop();
                                    }
                                    else
                                    {
                                        ErrorForm(warn);
                                        return false;
                                    }
                                }
                                for (int elemIndex = current.rightSide[item.altIndex].Count - 1; elemIndex >= 0; elemIndex--)//суём j+1 альтернативу в магазин L2 по элементно
                                {
                                    if (current.rightSide[item.altIndex][elemIndex].notTerminal != "")
                                    {
                                        config.L2.Push(new Configuration.elem(current.rightSide[item.altIndex][elemIndex].notTerminal, false, item.altIndex + 1));
                                    }
                                    else
                                    {
                                        config.L2.Push(new Configuration.elem(current.rightSide[item.altIndex][elemIndex].terminal, true, item.altIndex + 1));
                                    }
                                }
                                item.altIndex++;
                                config.L1.Push(item);
                                config.s = 'q';
                            }
                            else
                            {
                                if (config.i == 0 && item.value == "main")
                                //б. прекращение разбора
                                {
                                    ErrorForm(warn);
                                    //Code.SyntError = "Цепочка не принадлежит языку";
                                    return false;
                                }
                                else
                                //в. Отмена результата
                                {
                                    for (int elemIndex = 0; elemIndex < current.rightSide[item.altIndex - 1].Count; elemIndex++)
                                    {
                                        config.L2.Pop();
                                    }
                                    config.L2.Push(new Configuration.elem(item.value, false, 1));
                                }
                            }

                        }
                        else
                        {
                            config.L2.Pop();
                            config.L2.Push(new Configuration.elem(config.L1.Pop().value, false, 1));
                        }
                    }

                    else
                    //5 Возврат по ходу
                    {
                        Configuration.elem item = new Configuration.elem(config.L1.Pop());
                        config.L2.Push(item);
                        //config.L2.Push(config.L1.Pop()); //на будующее когда всё будет работать
                        if (item.value != "lambda") config.i--;
                    }
                    history.Push(config);
                }
            }
        }
    private static void Main(string[] args)
    {
      _connectionFailures = 0;
      _configFile = DefaultConfigFile;

      if (args.Length > 0)
      {
        try
        {
          if (ProcessCommandLine(args))
            return;
        }
        catch (CommandExecutionException ex)
        {
          MessageBox.Show(ex.Message, "Translator", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        catch (CommandStructureException ex)
        {
          MessageBox.Show(ex.Message, "Translator", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        catch (Exception ex)
        {
          MessageBox.Show(ex.ToString(), "Translator - Error processing command line", MessageBoxButtons.OK,
                          MessageBoxIcon.Error);
        }
      }

      // Check for multiple instances.
      if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length != 1)
      {
        CopyDataWM.SendCopyDataMessage(Common.CmdPrefixShowTrayIcon);
        return;
      }

      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);

      IrssLog.LogLevel = IrssLog.Level.Debug;
      IrssLog.Open("Translator.log");

      Application.ThreadException += Application_ThreadException;

      // Initialize Variable List.
      _variables = new VariableList();

      // Load configuration ...
      _config = Configuration.Load(_configFile);
      if (_config == null)
      {
        IrssLog.Warn("Failed to load configuration file ({0}), creating new configuration", _configFile);
        _config = new Configuration();
      }

      // Adjust process priority ...
      AdjustPriority(_config.ProcessPriority);


      //foreach (ProgramSettings progSettings in _config.Programs)
      //{
      //  AppProfile profile = new AppProfile();

      //  profile.Name = progSettings.Name;
      //  profile.MatchType = AppProfile.DetectionMethod.Executable;
      //  profile.MatchParameters = progSettings.FileName;
      //  profile.ButtonMappings.AddRange(progSettings.ButtonMappings);

      //  AppProfile.Save(profile, "C:\\" + profile.Name + ".xml");
      //}


      // Setup notify icon ...
      _container = new Container();
      _notifyIcon = new NotifyIcon(_container);
      _notifyIcon.ContextMenuStrip = new ContextMenuStrip();
      _notifyIcon.Icon = Resources.Icon16Connecting;
      _notifyIcon.Text = "Translator - Connecting ...";
      _notifyIcon.DoubleClick += ClickSetup;
      _notifyIcon.Visible = !_config.HideTrayIcon;

      // Setup the main form ...
      _mainForm = new MainForm();

      // Start server communications ...
      bool clientStarted = false;

      IPAddress serverIP = Network.GetIPFromName(_config.ServerHost);
      IPEndPoint endPoint = new IPEndPoint(serverIP, Server.DefaultPort);

      try
      {
        clientStarted = StartClient(endPoint);
      }
      catch (Exception ex)
      {
        IrssLog.Error(ex);
        clientStarted = false;
      }

      if (clientStarted)
      {
        // Setup event notification ...
        SystemEvents.SessionEnding += SystemEvents_SessionEnding;
        SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;

        try
        {
          _copyDataWM = new CopyDataWM();
          _copyDataWM.Start();
        }
        catch (Win32Exception ex)
        {
          IrssLog.Error("Error enabling CopyData messages: {0}", ex.ToString());
        }

        Application.Run();

        if (_copyDataWM != null)
        {
          _copyDataWM.Dispose();
          _copyDataWM = null;
        }

        SystemEvents.SessionEnding -= SystemEvents_SessionEnding;
        SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;

        StopClient();
      }
      else
      {
        MessageBox.Show("Failed to start IR Server communications, refer to log file for more details.",
                        "Translator - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

        _inConfiguration = true;

        _mainForm.ShowDialog();

        _inConfiguration = false;
      }

      // Dispose NotifyIcon ...
      _notifyIcon.Visible = false;
      _notifyIcon.Dispose();
      _notifyIcon = null;

      // Dispose Container ...
      _container.Dispose();
      _container = null;

      Application.ThreadException -= Application_ThreadException;

      IrssLog.Close();
    }