示例#1
0
        /// <summary>
        /// Returns a value indicating whether the input <see cref="Lexical.Token"/> matches the grammar rule.
        /// </summary>
        /// <param name="lexer">The <see cref="BranchedLexer"/> that provides the input <see cref="Lexical.Token"/>.</param>
        /// <param name="result">When this method returns, contains the parsing result if the input <see cref="Lexical.Token"/> matches the grammar rule; otherwise, is <c>default(TResult)</c></param>
        /// <returns><see langword="true"/> if the input <see cref="Lexical.Token"/> matches the grammar rule; otherwise <see langword="false"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="lexer"/> is <see langword="null"/>.</exception>
        public override bool TryParse(BranchedLexer lexer, out TResult result)
        {
            if (lexer == null)
            {
                throw new ArgumentNullException();
            }
            TResult       r;
            BranchedLexer branch;

            branch = lexer.NewBranch();
            if (First.TryParse(branch, out r))
            {
                lexer.Merge(branch);
                branch.Dispose();
                result = r;
                return(true);
            }
            branch.Dispose();
            branch = lexer.NewBranch();
            if (Second.TryParse(branch, out r))
            {
                lexer.Merge(branch);
                branch.Dispose();
                result = r;
                return(true);
            }
            else
            {
                branch.Dispose();
                result = default(TResult);
                return(false);
            }
        }
示例#2
0
        /// <summary>
        /// Returns a value indicating whether the input <see cref="Lexical.Token"/> matches the grammar rule.
        /// </summary>
        /// <param name="lexer">The <see cref="BranchedLexer"/> that provides the input <see cref="Lexical.Token"/>.</param>
        /// <param name="result">When this method returns, contains the parsing result if the input <see cref="Lexical.Token"/> matches the grammar rule; otherwise, is <c>default(TResult)</c></param>
        /// <returns><see langword="true"/> if the input <see cref="Lexical.Token"/> matches the grammar rule; otherwise <see langword="false"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="lexer"/> is <see langword="null"/>.</exception>
        public override bool TryParse(BranchedLexer lexer, out TResult result)
        {
            if (lexer == null)
            {
                throw new ArgumentNullException();
            }
            T             loopr;
            List <T>      list = new List <T>();
            BranchedLexer branch0, branch1;

            branch0 = lexer.NewBranch();
            bool check = true;
            int  count = 0;

            while (true)
            {
                branch1 = branch0.NewBranch();
                check   = Content.TryParse(branch1, out loopr);
                if (check)
                {
                    branch0.Dispose();
                    branch0 = branch1;
                    list.Add(loopr);
                    count++;
                }
                else
                {
                    break;
                }
                if (Max > 0 && count == Max)
                {
                    break;
                }
            }
            branch1.Dispose();
            if (count < Min)
            {
                branch0.Dispose();
                result = default(TResult);
                return(false);
            }
            else
            {
                lexer.Merge(branch0);
                branch0.Dispose();
                result = Func(list);
                return(true);
            }
        }