Exemple #1
0
        private bool TryToCheckSubRegions(KeLexerState state, RegionSearchState mainRegionState)
        {
            var currentParsedString = state.CurrentParsedString;

            while (true)
            {
                if (!state.CharEnumerator.MoveNextWhileWhiteSpace())
                {
                    return(false);
                }

                var currentChar = state.CharEnumerator.Current.Value;
                state.CharEnumerator.MoveNext();
                var nextChar = state.CharEnumerator.Current;

                var mainRegionCheckResult = mainRegionState.RegionRule.Check(currentChar, nextChar, currentParsedString);
                if (mainRegionCheckResult.HasFlag(RuleCheckResult.Found))
                {
                    mainRegionState.Index = mainRegionState.Index;
                    mainRegionState.AddSymbol(currentChar, mainRegionCheckResult);
                    mainRegionState.SetLastCheckResult(mainRegionCheckResult);
                    return(true);
                }

                state.CharEnumerator.SetIndex(state.CharEnumerator.Index - 1);

                var subRegion = this.FindNextRegion(state);
                if (subRegion is null)
                {
                    return(mainRegionCheckResult.HasFlag(RuleCheckResult.Found));
                }

                mainRegionState.AddSubRegion(subRegion);
            }
        }
Exemple #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        /// <remarks>Char enumerator should be setted on next symbol after region.</remarks>
        private RegionSearchState FindNextRegion(KeLexerState state)
        {
            var currentChar = state.CharEnumerator.Current.Value;

            state.CharEnumerator.MoveNext();
            var nextChar = state.CharEnumerator.Current;

            List <RegionSearchState> possibleRegions = new List <RegionSearchState>();

            foreach (var regionRule in this.RegionRules)
            {
                var checkResult = regionRule.Check(currentChar, nextChar, string.Empty);
                if (checkResult.HasFlag(RuleCheckResult.NotFound))
                {
                    continue;
                }

                var regionToken = new RegionSearchState(regionRule)
                {
                    Index = state.CharEnumerator.Index
                };

                regionToken.AddSymbol(currentChar, checkResult);
                regionToken.SetLastCheckResult(checkResult);
                possibleRegions.Add(regionToken);
            }

            if (possibleRegions.Count == 0)
            {
                return(null);
            }

            if (possibleRegions.Count == 1 && possibleRegions[0].LastCheckResult.HasFlag(RuleCheckResult.Found))
            {
                return(possibleRegions[0]);
            }

            state.CurrentParsedString = currentChar.ToString();

            var foundedRegion = this.ObserveToSeveralRegions(state, possibleRegions);

            if (foundedRegion is null)
            {
                return(null);
            }

            return(foundedRegion);
        }