Example #1
0
        /// <summary>
        /// Attempts to execute a transducer with content from a <see cref="StringCodeReader" />. If the
        /// state reached is not a terminal state, the reader is rewinded to it's initial position.
        /// </summary>
        /// <typeparam name="OutputT"></typeparam>
        /// <param name="transducer"></param>
        /// <param name="reader"></param>
        /// <param name="output"></param>
        /// <returns>Whether the state reached was a terminal state.</returns>
        public static Boolean TryExecute <OutputT>(this Transducer <Char, OutputT> transducer, ICodeReader reader, [MaybeNull] out OutputT output)
        {
            if (transducer is null)
            {
                throw new ArgumentNullException(nameof(transducer));
            }
            if (reader is null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            var start = reader.Position;
            TransducerState <Char, OutputT> state = transducer.InitialState;

            while (reader.Position != reader.Length)
            {
                if (!state.TransitionTable.TryGetValue(reader.Peek() !.Value, out TransducerState <Char, OutputT>?tmp))
                {
                    break;
                }
                state = tmp;
                reader.Advance(1);
            }

            if (state.IsTerminal)
            {
                output = state.Output;
                return(true);
            }

            reader.Restore(start);
            // Since the analyzer doesn't seems to obey [MaybeNull], we ignore the warning
            output = default;
            return(false);
        }
Example #2
0
 static bool restore(ICodeReader reader, int start)
 {
     reader.Restore(start);
     return(false);
 }
Example #3
0
 static bool restore(ICodeReader reader, int position)
 {
     reader.Restore(position);
     return(false);
 }