Example #1
0
            private void BeginReadCommands()
            {
                var tokens = new List <CommandToken>();

                if (EndOfLine && _chord != null)
                {
                    tokens.AddRange(ReadCommands());
                    if (tokens.Count == 0)
                    {
                        throw new ParseException(Location, "Expecting a command.");
                    }
                }
                else
                {
                    tokens.Add(ReadCommand());
                }
                foreach (var token in tokens)
                {
                    if (token.HasFlag(CommandTypes.Chordless) && _chord != null)
                    {
                        throw CreateException(token, "This command cannot be bound to a chord.");
                    }
                    if (!token.HasFlag(CommandTypes.Chordless) && _chord == null)
                    {
                        throw CreateException(token, "This command has to be bound to a chord.");
                    }
                    if (token.HasFlag(CommandTypes.TopLevelOnly) && _sections.Count > 1)
                    {
                        throw CreateException(token, "This command is only valid at the top level.");
                    }
                    if (token.HasFlag(CommandTypes.ModeOnly) && !(_sections.Peek() is Mode))
                    {
                        throw CreateException(token, $"This command is only valid in a {Constants.InputModeSectionIdentifier} or" +
                                              $" {Constants.ComposeModeSectionIdentifier} section.");
                    }
                    if (token.HasFlag(CommandTypes.ComposeModeOnly) && (!(_sections.Peek() is Mode mode) || !mode.IsComposeMode))
                    {
                        throw CreateException(token, $"This command is only valid in a {Constants.ComposeModeSectionIdentifier} section.");
                    }
                    if (token.HasFlag(CommandTypes.InputModeOnly) && (!(_sections.Peek() is Mode mode1) || !mode1.IsInputMode))
                    {
                        throw CreateException(token, $"This command is only valid in a {Constants.InputModeSectionIdentifier} section.");
                    }
                    if (token.HasFlag(CommandTypes.StandardSectionOnly) && !(_sections.Peek() is StandardSection))
                    {
                        throw CreateException(token, "This command is only valid in a standard section.");
                    }
                    if (token.HasFlag(CommandTypes.ExecuteAtParseTime) && tokens.Count > 1)
                    {
                        throw CreateException(token, "This command cannot be combined with other commands.");
                    }
                }
                if (tokens[0].HasFlag(CommandTypes.ExecuteAtParseTime))
                {
                    tokens[0].Action(Combo.None);
                }
                else
                {
                    var action      = tokens.Count > 1 ? CreateAction(tokens.Select(z => z.Action)) : tokens[0].Action;
                    var description = _chord + " " + tokens[0].LocatedName.Value + " " + tokens[0].LocatedArguments.Value;
                    _parserOutput.AddHotkey(_sections.Peek(), _chord, action, description);
                }
            }