/// <inheritdoc />
 IParsingProduct IGrammarProductsFactory.CreateOperatorNode(
     ISourceCodeFragment operatorTerm,
     IUnaryOperatorTermDefinition @operator,
     IParsingProduct exp)
 {
     return(GetFunction(operatorTerm, ExpressionTypeId.UnaryOperator, new[] { exp }));
 }
        public static IReadOnlyList <IParsingProduct> WithoutEmptyProducts(
            this IReadOnlyCollection <IParsingProduct> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var count = source.WhereNotEmpty().Count();

            if (count <= 0)
            {
                return(EmptySubNodes.Instance);
            }

            if (count == 1)
            {
                return(new SingleSubNode(source.WhereNotEmpty().Single()));
            }

            var items = new IParsingProduct[count];

            var idx = 0;

            foreach (var x in source.WhereNotEmpty())
            {
                items[idx] = x;
                idx++;
            }

            return(items);
        }
Пример #3
0
        /// <inheritdoc />
        public void Add(IParsingProduct expression)
        {
            if (IsEmpty)
            {
                _first = new ExpressionListNode(expression);
                _last  = _first;
                return;
            }

            _last = _last.AddNext(expression);
        }
Пример #4
0
 public IParsingProduct CreateOperatorTreeNode(
     IParsingProduct exp1,
     IParsingProduct exp2,
     Func <
         ISourceCodeFragment,
         IBinaryOperatorTermDefinition,
         IParsingProduct,
         IParsingProduct, IParsingProduct> createOperatorNode
     )
 {
     return(createOperatorNode(_codeFragment, _definition, exp1, exp2));
 }
        public static IReadOnlyList <IParsingProduct> ToList(this IParsingProduct source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (source.ProductType == ParsingProductType.Empty)
            {
                return(EmptySubNodes.Instance);
            }

            return(new SingleSubNode(source));
        }
        public static IReadOnlyList <IParsingProduct> Concatenate(this IReadOnlyCollection <IParsingProduct> source,
                                                                  IReadOnlyCollection <IParsingProduct> other)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            var count = source.WhereNotEmpty().Count() + other.WhereNotEmpty().Count();

            if (count <= 0)
            {
                return(EmptySubNodes.Instance);
            }

            if (count == 1)
            {
                return(new SingleSubNode(source.WhereNotEmpty().SingleOrDefault() ?? other.WhereNotEmpty().Single()));
            }

            var items = new IParsingProduct[count];

            var idx = 0;

            foreach (var x in source.WhereNotEmpty())
            {
                items[idx] = x;
                idx++;
            }

            foreach (var y in other.WhereNotEmpty())
            {
                items[idx] = y;
                idx++;
            }

            return(items);
        }
Пример #7
0
        public static IParsingProduct TryAlternativeParse(
            this IEnumerable <IExpressionParser> parsers,
            ISourceCode src,
            ParsingAlternationType parsingAlternationType = ParsingAlternationType.TakeFirst,
            ParsingOptions options = DefaultParsingOptions)
        {
            if (parsingAlternationType == ParsingAlternationType.TakeFirst)
            {
                return(parsers.Select(p => p.TryParse(src, options)).FirstNotNullOrDefault());
            }

            if (parsingAlternationType != ParsingAlternationType.TakeLongest &&
                parsingAlternationType != ParsingAlternationType.TakeShortest)
            {
                throw new NotSupportedException($"{parsingAlternationType} is not supported.");
            }

            var shortest   = parsingAlternationType == ParsingAlternationType.TakeShortest;
            var replaceCmp = shortest ? -1 : 1;

            IParsingContextStream selectedCtx = null;
            IParsingProduct       result      = null;

            foreach (var parser in parsers)
            {
                var ctx = src.GetFurtherContext();
                var res = parser.TryParse(src, options);

                if (res != null && (result == null || replaceCmp == ctx.CompareTo(selectedCtx)))
                {
                    selectedCtx?.Dispose();

                    selectedCtx = ctx;
                    result      = res;
                }
            }

            selectedCtx?.Accept();
            return(result);
        }
Пример #8
0
        public static IParsingProduct TryAggregateParse(this IReadOnlyCollection <IExpressionParser> parsers, ISourceCode src, ParsingOptions options = DefaultParsingOptions)
        {
            using var ctx = src.GetFurtherContext();
            var nodes = new IParsingProduct[parsers.Count];
            var idx   = 0;

            foreach (var parser in parsers)
            {
                var res = parser.TryParse(ctx, options);

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

                nodes[idx] = res;
                idx++;
            }

            ctx.Accept();
            return(ctx.CreateExpressionForAcceptedFragment(FXB.ExpressionTypes.Concatenation, nodes));
        }
 public static IReadOnlyList <IParsingProduct> Concatenate(this IParsingProduct source, params IParsingProduct[] other)
 {
     return(Concatenate(source.ToList(), (IReadOnlyCollection <IParsingProduct>)other));
 }
Пример #10
0
 public static bool IsEmptyNode(IParsingProduct node)
 {
     return(node?.ProductType == ParsingProductType.Empty);
 }
Пример #11
0
 public override ListNode AddNext(IParsingProduct expression)
 {
     Next = new ExpressionListNode(expression);
     return(Next);
 }
Пример #12
0
 public override ListNode AddNext(IParsingProduct expression)
 {
     throw new InvalidOperationException(
               "Last item is an expression => can't chain expression to expression, operator expected.");
 }
Пример #13
0
 public ExpressionListNode(IParsingProduct expression, OperatorListNode next = null)
 {
     _expression = expression ?? throw new ArgumentNullException(nameof(expression));
     Next        = next;
 }
Пример #14
0
 public abstract ListNode AddNext(IParsingProduct expression);