示例#1
0
    /// <summary>
    ///     Initializes a new instance of the <see cref="ExpressionParsingServiceBase" /> class with a specified math
    ///     definition
    ///     object.
    /// </summary>
    /// <param name="definition">The math definition to use.</param>
    protected private ExpressionParsingServiceBase(MathDefinition definition)
    {
        // Preconditions
        Requires.NotNull(
            out workingDefinition,
            definition,
            nameof(definition));

        // Initialized internal state
        constantExtractors            = new LevelDictionary <Type, IConstantsExtractor>();
        constantInterpreters          = new LevelDictionary <Type, IConstantInterpreter>();
        constantPassThroughExtractors = new LevelDictionary <Type, IConstantPassThroughExtractor>();
        stringFormatters = new List <IStringFormatter>();

        nonaryFunctions  = new Dictionary <string, Type>();
        unaryFunctions   = new Dictionary <string, Type>();
        binaryFunctions  = new Dictionary <string, Type>();
        ternaryFunctions = new Dictionary <string, Type>();

        assembliesToRegister = new List <Assembly>
        {
            typeof(ExpressionParsingService).GetTypeInfo()
            .Assembly
        };
    }
示例#2
0
    internal WorkingExpressionSet(
        string expression,
        MathDefinition mathDefinition,
        Dictionary <string, Type> nonaryFunctions,
        Dictionary <string, Type> unaryFunctions,
        Dictionary <string, Type> binaryFunctions,
        Dictionary <string, Type> ternaryFunctions,
        LevelDictionary <Type, IConstantsExtractor> extractors,
        LevelDictionary <Type, IConstantInterpreter> interpreters,
        List <IStringFormatter> stringFormatters,
        CancellationToken cancellationToken)
    {
        ParameterRegistry     = new StandardParameterRegistry(stringFormatters);
        ConstantsTable        = new Dictionary <string, ConstantNodeBase>();
        ReverseConstantsTable = new Dictionary <string, string>();
        SymbolTable           = new Dictionary <string, ExpressionSymbol>();
        ReverseSymbolTable    = new Dictionary <string, string>();
        StringFormatters      = stringFormatters;

        CancellationToken = cancellationToken;
        Expression        = expression;
        Definition        = mathDefinition;

        AllOperatorsInOrder = new[]
        {
            mathDefinition.GreaterThanOrEqualSymbol,
            mathDefinition.LessThanOrEqualSymbol,
            mathDefinition.GreaterThanSymbol,
            mathDefinition.LessThanSymbol,
            mathDefinition.NotEqualsSymbol,
            mathDefinition.EqualsSymbol,
            mathDefinition.XorSymbol,
            mathDefinition.OrSymbol,
            mathDefinition.AndSymbol,
            mathDefinition.AddSymbol,
            mathDefinition.SubtractSymbol,
            mathDefinition.DivideSymbol,
            mathDefinition.MultiplySymbol,
            mathDefinition.PowerSymbol,
            mathDefinition.LeftShiftSymbol,
            mathDefinition.RightShiftSymbol,
            mathDefinition.NotSymbol
        };

        NonaryFunctions  = nonaryFunctions;
        UnaryFunctions   = unaryFunctions;
        BinaryFunctions  = binaryFunctions;
        TernaryFunctions = ternaryFunctions;

        Extractors   = extractors;
        Interpreters = interpreters;

        FunctionRegex = new Regex(
            $@"(?'functionName'.*?){Regex.Escape(mathDefinition.Parentheses.Left)}(?'expression'.*?){Regex.Escape(mathDefinition.Parentheses.Right)}");
    }
    public string ExtractAllConstants(
        string originalExpression,
        IDictionary <string, ConstantNodeBase> constantsTable,
        IDictionary <string, string> reverseConstantsTable,
        MathDefinition mathDefinition)
    {
        var stringIndicatorString = mathDefinition.StringIndicator;
        var stringIndicator       = mathDefinition.StringIndicator.AsSpan();
        var stringIndicatorLength = stringIndicator.Length;
        var escapeCharacter       = mathDefinition.EscapeCharacter.AsSpan();
        var escapeCharacterLength = escapeCharacter.Length;

        var           process = originalExpression.AsSpan();
        StringBuilder?sb      = null;

        while (true)
        {
            var openingPosition = process.IndexOf(
                stringIndicator,
                StringComparison.CurrentCulture);

            if (openingPosition == -1)
            {
                // No string opening
                break;
            }

            var header = process.Slice(
                0,
                openingPosition);

            var rest = process.Slice(openingPosition + stringIndicatorLength);

            int closingPosition;
            ReadOnlySpan <char> body;

            do
            {
                closingPosition = rest.IndexOf(stringIndicator, StringComparison.CurrentCulture);

                if (closingPosition != -1)
                {
                    body = rest.Slice(
                        0,
                        closingPosition);

                    int occurrences = 0;

                    while (body.EndsWith(escapeCharacter))
                    {
                        occurrences++;
                        body = body.Slice(
                            0,
                            body.Length - escapeCharacterLength);
                    }

                    rest = rest.Slice(closingPosition + stringIndicatorLength);

                    if (occurrences % 2 == 0)
                    {
                        break;
                    }
                }
            }while (closingPosition != -1);

            if (closingPosition == -1)
            {
                // No string closing
                break;
            }

            // We have a proper string
            body = process.Slice(
                openingPosition,
                process.Length - header.Length - rest.Length);

            var itemName = ConstantsGenerator.GenerateStringConstant(
                constantsTable,
                reverseConstantsTable,
                originalExpression,
                stringIndicatorString,
                body.ToString());

            sb ??= new StringBuilder(originalExpression.Length);

            #if NETSTANDARD21_OR_GREATER
            sb.Append(header);
            #else
            sb.Append(header.ToString());
            #endif

            sb.Append(itemName);

            process = rest;
        }

        if (sb == null)
        {
            return(originalExpression);
        }

        #if NETSTANDARD21_OR_GREATER
        sb.Append(process);
        #else
        sb.Append(process.ToString());
        #endif

        return(sb.ToString());
    }
 /// <summary>
 ///     Initializes a new instance of the <see cref="ExpressionParsingService" /> class with a specified math definition
 ///     object.
 /// </summary>
 /// <param name="definition">The math definition to use.</param>
 public ExpressionParsingService(MathDefinition definition)
     : base(definition)
 {
 }
示例#5
0
 /// <summary>
 /// Extracts all constants, replacing them from the original expression.
 /// </summary>
 /// <param name="originalExpression">The original expression.</param>
 /// <param name="constantsTable">The constants table.</param>
 /// <param name="reverseConstantsTable">The reverse constants table.</param>
 /// <param name="mathDefinition">The math definition.</param>
 /// <returns>The expression, after replacement.</returns>
 public string ExtractAllConstants(string originalExpression, IDictionary <string, ConstantNodeBase> constantsTable, IDictionary <string, string> reverseConstantsTable, MathDefinition mathDefinition) => exponentialNotationRegex.Replace(originalExpression, "stupid", 1);