/// <inheritdoc />
 IParsingProduct IGrammarProductsFactory.CreateOperatorNode(
     ISourceCodeFragment operatorTerm,
     IUnaryOperatorTermDefinition @operator,
     IParsingProduct exp)
 {
     return(GetFunction(operatorTerm, ExpressionTypeId.UnaryOperator, new[] { exp }));
 }
 protected TerminalExpressionNode(
     ExpressionTypeDescriptor expressionType,
     ISourceCodeFragment fragment)
 {
     Fragment       = fragment;
     ExpressionType = expressionType;
 }
        public static bool Equals(this ISourceCodeFragment source, string text, IEqualityComparer <char> charComparer = null)
        {
            if (source.Count <= 0 && string.IsNullOrEmpty(text))
            {
                return(true);
            }

            if (source.Count != text.Length)
            {
                return(false);
            }

            var idx = 0;

            foreach (var ch1 in source)
            {
                var ch2 = text[idx];
                if (!Equals(ch1, ch2, charComparer))
                {
                    return(false);
                }
                idx++;
            }

            return(true);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="MathExpressionParameterlessFunctionNode"/> class.
 /// </summary>
 /// <param name="expressionTypeId">The expression type identifier.</param>
 /// <param name="functionNameFragment">The function name fragment.</param>
 /// <param name="function">The function to evaluate function value.</param>
 /// <param name="valueTypeId">The value type identifier.</param>
 public MathExpressionParameterlessFunctionNode(
     string expressionTypeId,
     ISourceCodeFragment functionNameFragment,
     Func <object> function,
     string valueTypeId = null)
     : base(ExpressionTypeDescriptor.CreateExpressionClass(expressionTypeId, valueTypeId), functionNameFragment)
 {
     _function = function;
 }
Example #5
0
 protected ParsingProduct(ExpressionTypeDescriptor expressionType,
                          ISourceCodeFragment fragment,
                          IEnumerable <IParsingProduct> subNodes
                          )
 {
     ExpressionType = expressionType;
     Fragment       = fragment;
     SubNodes       = (IReadOnlyList <IParsingProduct>)subNodes?.ToArray() ?? EmptySubNodes.Instance;
 }
Example #6
0
        public static ParsedValue Create(ISourceCodeFragment valueFragment, string valueTypeId)
        {
            if (valueFragment == null)
            {
                return(null);
            }

            return(new ParsedValue(valueFragment, valueTypeId));
        }
        /// <inheritdoc />
        public void Add(IBinaryOperatorTermDefinition operatorTermDefinition, ISourceCodeFragment codeFragment)
        {
            if (IsEmpty)
            {
                throw new InvalidOperationException(
                          "No expression => can't chain operator, first item must be an expression.");
            }

            _last = _last.AddNext(operatorTermDefinition, codeFragment);
        }
Example #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MathExpressionFunctionNode"/> class.
        /// </summary>
        /// <param name="expressionType"></param>
        /// <param name="functionNameFragment">The function name fragment.</param>
        /// <param name="subNodes">The sub nodes (parameters).</param>
        /// <param name="function">The delegate to calculate function value.</param>
        /// <exception cref="ArgumentException">null sub node</exception>
        public MathExpressionFunctionNode(
            ExpressionTypeDescriptor expressionType,
            ISourceCodeFragment functionNameFragment,
            IEnumerable <IParsingProduct> subNodes,
            Func <object[], object> function)
            : base(expressionType, functionNameFragment, subNodes)
        {
            _function = function;

            if (SubNodes.Cast <IExecutableExpressionNode>().Any(n => n == null))
            {
                throw new ArgumentException("null sub node");
            }
        }
Example #9
0
        public static bool TryGetAcceptedFragment(this IParsingContextStream source, out ISourceCodeFragment acceptedFragment)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (source.AcceptedPosition < 0)
            {
                acceptedFragment = null;
                return(false);
            }

            acceptedFragment = source.GetAcceptedFragmentOrEmpty();
            return(true);
        }
        /// <inheritdoc />
        IParsingProduct IGrammarProductsFactory.CreateVariableNode(ISourceCodeFragment variable)
        {
            if (_variables.TryGetValue(variable, out var v))
            {
                return(new MathExpressionParameterlessFunctionNode(ExpressionTypeId.Variable, variable, v));
            }

            if (_constants.TryGetValue(variable, out var c))
            {
                return(new MathExpressionValueNode(
                           ExpressionTypeId.Value,
                           variable,
                           c,
                           "float"));
            }

            return(null);
        }
        private IParsingProduct GetFunction(
            ISourceCodeFragment functionName,
            string expressionTypeId,
            IEnumerable <IParsingProduct> arguments)
        {
            if (!_functionsByNameByArgCount.TryGetValue(functionName, out var functionByArgsCount))
            {
                return(null);
            }

            var args = arguments?.ToArray() ?? Array.Empty <IParsingProduct>();

            if (!functionByArgsCount.TryGetValue(args.Length, out var func))
            {
                return(null);
            }

            return(new MathExpressionFunctionNode(ExpressionTypeDescriptor.CreateExpressionClass(expressionTypeId), functionName, args, func));
        }
Example #12
0
 private ParsedValue(ISourceCodeFragment valueFragment, string valueTypeId)
 {
     ValueFragment = valueFragment ?? throw new ArgumentNullException(nameof(valueFragment));
     ValueTypeId   = valueTypeId ?? throw new ArgumentNullException(nameof(valueTypeId));
 }
Example #13
0
 public static TermNode Create(ExpressionTypeDescriptor expressionType, ISourceCodeFragment fragment)
 {
     return(new TermNode(expressionType, fragment));
 }
 /// <inheritdoc />
 IParsingProduct IGrammarProductsFactory.CreateFunctionInvocationNode(
     ISourceCodeFragment functionName,
     IEnumerable <IParsingProduct> arguments)
 {
     return(GetFunction(functionName, ExpressionTypeId.Function, arguments));
 }
Example #15
0
 public IParsingProduct CreateExpression(ExpressionTypeDescriptor expressionType, ISourceCodeFragment codeFragment, IReadOnlyCollection <IParsingProduct> subNodes)
 {
     return(ExpressionNode.Create(expressionType, codeFragment, subNodes));
 }
Example #16
0
 private TermNode(ExpressionTypeDescriptor expressionType, ISourceCodeFragment fragment)
 {
     ExpressionType = expressionType;
     Fragment       = fragment;
 }
Example #17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MathExpressionValueNode"/> class.
 /// </summary>
 /// <param name="expressionTypeId">The expression type identifier.</param>
 /// <param name="valueFragment">The source code fragment that represents the value.</param>
 /// <param name="value">The value.</param>
 /// <param name="valueTypeId">The value type identifier.</param>
 public MathExpressionValueNode(string expressionTypeId, ISourceCodeFragment valueFragment, object value, string valueTypeId = null)
     : base(ExpressionTypeDescriptor.CreateExpressionClass(expressionTypeId, valueTypeId), valueFragment)
 {
     _value = value;
 }
 public abstract ListNode AddNext(
     IBinaryOperatorTermDefinition definition,
     ISourceCodeFragment codeFragment);
 public override ListNode AddNext(IBinaryOperatorTermDefinition definition, ISourceCodeFragment codeFragment)
 {
     throw new InvalidOperationException(
               "Last item is an operator => can't chain operator to operator, expression expected.");
 }
 public OperatorListNode(IBinaryOperatorTermDefinition definition, ISourceCodeFragment codeFragment)
 {
     _definition   = definition ?? throw new ArgumentNullException(nameof(definition));
     _codeFragment = codeFragment ?? throw new ArgumentNullException(nameof(codeFragment));
 }
 public override ListNode AddNext(IBinaryOperatorTermDefinition definition, ISourceCodeFragment codeFragment)
 {
     Next = new OperatorListNode(definition, codeFragment);
     return(Next);
 }
Example #22
0
        public static IParsingProduct Create(ExpressionTypeDescriptor expressionType, ISourceCodeFragment fragment, IReadOnlyCollection <IParsingProduct> subNodes)
        {
            var nodes = subNodes.WithoutEmptyProducts();

            return(new ExpressionNode(expressionType, fragment, nodes));
        }
Example #23
0
 private ExpressionNode(ExpressionTypeDescriptor expressionType, ISourceCodeFragment fragment, IReadOnlyList <IParsingProduct> subNodes)
 {
     Fragment       = fragment;
     SubNodes       = subNodes.WithoutEmptyProducts();
     ExpressionType = expressionType;
 }
Example #24
0
 public ParsedSourceError(IParsingError error, ISourceCodeFragment parsedFragment, ISourceCodeFragment notParsedFragment)
 {
     Error             = error;
     NotParsedFragment = notParsedFragment;
     ParsedFragment    = parsedFragment;
 }
Example #25
0
 public IParsingProduct CreateTerm(ExpressionTypeDescriptor expressionType, ISourceCodeFragment codeFragment)
 {
     return(TermNode.Create(expressionType, codeFragment));
 }