Beispiel #1
0
        /// <summary>
        /// Constructs the blockifier by initialising block parsers
        /// </summary>
        public Blockifier()
        {
            var commentParser              = new CommentParser();
            var bindingParser              = new ModelBindingParser();
            var conditionalBindingParser   = new ConditionalBindingParser();
            var functionParser             = new FunctionParser();
            var variableParser             = new VariableParser();
            var variableReassignmentParser = new VariableReassignmentParser();
            var variableIncrementerParser  = new VariableIncrementerParser();
            var variableDecrementerParser  = new VariableDecrementerParser();
            var flagParser      = new FlagParser();
            var eachLoopParser  = new ForEachLoopParser(this);
            var whileLoopParser = new WhileLoopParser(this);
            var ifParser        = new IfStatementParser(this);
            var partialParser   = new RenderPartialParser();

            _parsers = new List <IBlockParser>()
            {
                commentParser,
                bindingParser,
                conditionalBindingParser,
                functionParser,
                variableParser,
                variableReassignmentParser,
                variableIncrementerParser,
                variableDecrementerParser,
                flagParser,
                eachLoopParser,
                whileLoopParser,
                ifParser,
                partialParser
            };
        }
        /// <summary>
        /// Parses the value signature into the value type specified
        /// </summary>
        /// <param name="type">The value type</param>
        /// <param name="signature">The value signature</param>
        /// <returns>The parsed value</returns>
        public static object ParseValue
        (
            this NettleValueType type,
            string signature
        )
        {
            if (String.IsNullOrEmpty(signature))
            {
                return(null);
            }

            var convertedValue = default(object);

            switch (type)
            {
            case NettleValueType.String:
            {
                if (signature.Equals("\"\""))
                {
                    convertedValue = String.Empty;
                }
                else if (signature.StartsWith("\"") && signature.EndsWith("\""))
                {
                    convertedValue = signature.Crop
                                     (
                        1,
                        signature.Length - 2
                                     );
                }
                else
                {
                    convertedValue = signature;
                }

                break;
            }

            case NettleValueType.Number:
            {
                convertedValue = Double.Parse(signature);
                break;
            }

            case NettleValueType.Boolean:
            {
                convertedValue = Boolean.Parse(signature);
                break;
            }

            case NettleValueType.ModelBinding:
            {
                var bindingPath = signature;

                if (bindingPath.StartsWith(@"{{") && bindingPath.EndsWith(@"}}"))
                {
                    bindingPath = bindingPath.Crop
                                  (
                        2,
                        bindingPath.Length - 3
                                  );
                }

                if (bindingPath.StartsWith(@"$") && bindingPath.Length > 1)
                {
                    // Ensure a property reference isn't being made on the model
                    if (false == bindingPath.StartsWith(@"$."))
                    {
                        bindingPath = bindingPath.Crop(1);
                    }
                }

                convertedValue = bindingPath;
                break;
            }

            case NettleValueType.Variable:
            {
                // NOTE: this isn't resolvable until runtime
                convertedValue = signature;
                break;
            }

            case NettleValueType.Function:
            {
                var functionParser  = new FunctionParser();
                var templateContent = String.Copy(signature);
                var positionOffset  = default(int);

                convertedValue = functionParser.Parse
                                 (
                    ref templateContent,
                    ref positionOffset,
                    signature
                                 );

                break;
            }

            case NettleValueType.BooleanExpression:
            {
                var expressionParser = new BooleanExpressionParser();
                var expression       = signature;

                if (expression.StartsWith(@"(") && expression.EndsWith(@")"))
                {
                    expression = expression.Crop
                                 (
                        1,
                        expression.Length - 2
                                 );
                }

                convertedValue = expressionParser.Parse
                                 (
                    expression
                                 );

                break;
            }

            case NettleValueType.KeyValuePair:
            {
                var pairParser = new KeyValuePairParser();

                convertedValue = pairParser.Parse
                                 (
                    signature
                                 );

                break;
            }

            case NettleValueType.AnonymousType:
            {
                var typeParser = new AnonymousTypeParser();

                convertedValue = typeParser.Parse
                                 (
                    signature
                                 );

                break;
            }
            }

            return(convertedValue);
        }