Beispiel #1
0
        public ITransformCommand Construct(GenericReplacementCommand command)
        {
            var transMode = TranspositionMode.OneToOne;

            if (command.Flags == "d")
            {
                transMode = TranspositionMode.DeleteMissingTo;
            }
            else if (command.Flags == "r")
            {
                transMode = TranspositionMode.RepeatLastTo;
            }
            else if (command.Flags != "")
            {
                Logger.LogInformation("incorrect flags {Flags}", command.Flags);
                return(null);
            }

            Dictionary <int, int> transpositionDictionary = ParseTranspositions(
                command.OldString, command.NewString, transMode
                );

            if (transpositionDictionary == null)
            {
                return(null);
            }

            return(new TransposeCommand(transpositionDictionary));
        }
Beispiel #2
0
        public ITransformCommand Construct(GenericReplacementCommand command)
        {
            SubFlags subFlags = ParseSubFlags(command.Flags);

            if (subFlags == null)
            {
                // invalid flags
                Logger.LogInformation("invalid flags {Flags}", command.Flags);
                return(null);
            }

            string replacementString = TransformReplacementString(command.NewString);
            Regex  pattern;

            try
            {
                pattern = new Regex(command.OldString, subFlags.Options);
            }
            catch (ArgumentException)
            {
                // syntactic error in pattern
                Logger.LogInformation("syntactic error in pattern {Pattern}", command.OldString);
                return(null);
            }

            return(new SubCommand(
                       pattern,
                       replacementString,
                       subFlags.FirstMatch,
                       subFlags.ReplaceAll
                       ));
        }
Beispiel #3
0
        public virtual List <ITransformCommand> ParseSubCommands(string message)
        {
            string trimmedMessage = message.Trim();

            // shortest possible command
            if (trimmedMessage.Length < "s/a//".Length)
            {
                // too short
                // (if we fail at this stage, it's probably not supposed to be a sed command)
                return(null);
            }
            if (trimmedMessage.Count(c => Splitters.Contains(c)) < 2)
            {
                // not enough splitter characters: not a command
                return(null);
            }
            if (Splitters.Max(splitter => trimmedMessage.Count(c => c == splitter)) < 2)
            {
                // not enough of the same splitter character: not a command
                return(null);
            }

            var replacementCommands = new List <GenericReplacementCommand>();

            for (;;)
            {
                string rest;
                bool   invalidCommand;
                GenericReplacementCommand subCommand = TakeReplacementCommand(trimmedMessage, out rest, out invalidCommand);

                if (subCommand == null)
                {
                    if (invalidCommand)
                    {
                        // assume it's not supposed to be a sed command
                        return(null);
                    }
                    else
                    {
                        // assume it's a syntactically incorrect sed command
                        break;
                    }
                }
                else if (subCommand.Flags == null)
                {
                    // no flags: assume syntactically incorrect sed command as well
                    break;
                }

                // ensure that the string is getting shorter
                Debug.Assert(rest.Length < trimmedMessage.Length);

                replacementCommands.Add(subCommand);
                trimmedMessage = rest;
            }

            // probably is supposed to be a sed command but they are doing it wrong
            // return an empty list
            if (replacementCommands.Count == 0)
            {
                Logger.LogInformation("already the first replacement command was invalid in {ReplacementsString}", trimmedMessage);
                return(new List <ITransformCommand>());
            }

            var ret = new List <ITransformCommand>(replacementCommands.Count);

            foreach (GenericReplacementCommand replacementCommand in replacementCommands)
            {
                IReplacementFactory factory;
                if (!CommandsToFactories.TryGetValue(replacementCommand.Command, out factory))
                {
                    return(new List <ITransformCommand>());
                }

                ITransformCommand command = factory.Construct(replacementCommand);
                if (command == null)
                {
                    return(new List <ITransformCommand>());
                }

                ret.Add(command);
            }

            return(ret);
        }