示例#1
0
        /// <summary>
        /// Tries to parse all specified items except some others.
        /// </summary>
        public bool ParseExcept(LexicalState state, LexicalItem main, LexicalItem exception)
        {
            int index = state.Position;

            if (!main.Parse(state))
            {
                return(false);
            }

            string parsed = state.Get(main.Key, index);

            LexicalState check = new LexicalState(parsed);

            if (exception.Parse(check))
            {
                if (check.IsEndOfData)
                {
                    state.Reset(index);
                    return(false);
                }
            }

            state.AddBack(Key, index);
            return(true);
        }
示例#2
0
        /// <summary>
        /// Tries to parse any of specified words.
        /// </summary>
        public bool ParseWord(LexicalState state, bool ignoreCase, params string[] words)
        {
            if (state.IsEndOfData)
            {
                return(false);
            }

            foreach (string word in words)
            {
                if (state.Position + word.Length > state.Length)
                {
                    continue;
                }

                string captured = state.Get(state.Position, word.Length);
                bool   matches  = ignoreCase
                                        ? String.Compare(captured, word, StringComparison.OrdinalIgnoreCase) == 0
                                        : captured == word;

                if (matches)
                {
                    state.AddIncrement(Key, word.Length);
                    return(true);
                }
            }

            return(false);
        }
示例#3
0
        public void LexicalState_Uncovered()
        {
            Ensure.Throws(() => new LexicalState(null));

            LexicalState state = new LexicalState("test");
            Assert.IsNull(state.Get("key", 0));
        }
示例#4
0
        /// <summary>
        /// Tries to parse a single character using specified function.
        /// </summary>
        public bool ParseCharacter(LexicalState state, Func <char, bool> check)
        {
            if (state.IsEndOfData)
            {
                return(false);
            }

            if (!check(state.Get(state.Position)))
            {
                return(false);
            }

            state.AddIncrement(Key, 1);
            return(true);
        }
        public override bool Parse(LexicalState state)
        {
            int index = state.Position;

            if (!UnicodeEscapeSequence.S.Parse(state))
                return false;

            string sequence = state.Get(UnicodeEscapeSequence.S.Key, index);
            char c = (char)Convert.ToInt32(sequence.Substring(2), 16);

            if (!m_categories.Contains(Char.GetUnicodeCategory(c)))
            {
                state.Reset(index);
                return false;
            }

            state.AddBack(Key, index);
            return true;
        }
示例#6
0
        /// <summary>
        /// Tries to parse all specified items except some others.
        /// </summary>
        public bool ParseExcept(LexicalState state, LexicalItem main, LexicalItem exception)
        {
            int index = state.Position;

            if (!main.Parse(state))
                return false;

            string parsed = state.Get(main.Key, index);

            LexicalState check = new LexicalState(parsed);
            if (exception.Parse(check))
            {
                if (check.IsEndOfData)
                {
                    state.Reset(index);
                    return false;
                }
            }

            state.AddBack(Key, index);
            return true;
        }
示例#7
0
        /// <summary>
        /// Tries to parse a single character using specified function.
        /// </summary>
        public bool ParseCharacter(LexicalState state, Func<char, bool> check)
        {
            if (state.IsEndOfData)
                return false;

            if (!check(state.Get(state.Position)))
                return false;

            state.AddIncrement(Key, 1);
            return true;
        }
示例#8
0
        /// <summary>
        /// Tries to parse any of specified words.
        /// </summary>
        public bool ParseWord(LexicalState state, bool ignoreCase, params string[] words)
        {
            if (state.IsEndOfData)
                return false;

            foreach (string word in words)
            {
                if (state.Position + word.Length > state.Length)
                    continue;

                string captured = state.Get(state.Position, word.Length);
                bool matches = ignoreCase
                    ? String.Compare(captured, word, StringComparison.OrdinalIgnoreCase) == 0
                    : captured == word;

                if (matches)
                {
                    state.AddIncrement(Key, word.Length);
                    return true;
                }
            }

            return false;
        }