コード例 #1
0
        /// <summary>
        /// Determines if a signature matches the block type of the parser
        /// </summary>
        /// <param name="signatureBody">The signature body</param>
        /// <returns>True, if it matches; otherwise false</returns>
        /// <remarks>
        /// The rules for matching a variable adjuster are as follows:
        ///
        /// - The trimmed signature body must not contain spaces
        /// - The variable name must be valid
        /// - Must end with the adjuster operand signature
        /// </remarks>
        public virtual bool Matches
        (
            string signatureBody
        )
        {
            // Rule: must not contain spaces
            if (signatureBody.Contains(" "))
            {
                return(false);
            }

            // Rule: the variable name must be valid
            var variableName = ExtractVariableName
                               (
                signatureBody
                               );

            var isValidName = VariableParser.IsValidVariableName
                              (
                variableName
                              );

            // Rule: must end with the adjuster signature
            var hasAdjuster = signatureBody.EndsWith
                              (
                this.AdjusterSignature
                              );

            if (false == hasAdjuster)
            {
                return(false);
            }

            return(true);
        }
コード例 #2
0
ファイル: VariableParser.cs プロジェクト: shahiddev/Nettle
        /// <summary>
        /// Parses the code block signature into a code block object
        /// </summary>
        /// <param name="templateContent">The template content</param>
        /// <param name="positionOffSet">The position offset index</param>
        /// <param name="signature">The block signature</param>
        /// <returns>The parsed code block</returns>
        public virtual CodeBlock Parse
        (
            ref string templateContent,
            ref int positionOffSet,
            string signature
        )
        {
            var body        = UnwrapSignatureBody(signature);
            var nameIndex   = this.Prefix.Length;
            var equalsIndex = body.IndexOf('=');

            if (equalsIndex == -1)
            {
                var message = "The variable declaration '{0}' has invalid syntax.";

                throw new NettleParseException
                      (
                          message.With(signature),
                          positionOffSet
                      );
            }

            // Extract the variable name and value signature
            var variableName   = body.Crop(nameIndex, equalsIndex - 1).Trim();
            var valueSignature = body.Crop(equalsIndex + 1).Trim();

            var type  = ResolveType(valueSignature);
            var value = type.ParseValue(valueSignature);

            var isValidName = VariableParser.IsValidVariableName
                              (
                variableName
                              );

            if (false == isValidName)
            {
                var message = "The variable name '{0}' is invalid.";

                throw new NettleParseException
                      (
                          message.With(variableName),
                          positionOffSet
                      );
            }

            var startPosition = positionOffSet;
            var endPosition   = (startPosition + signature.Length);

            TrimTemplate
            (
                ref templateContent,
                ref positionOffSet,
                signature
            );

            var declaration = new VariableDeclaration()
            {
                Signature              = signature,
                StartPosition          = startPosition,
                EndPosition            = endPosition,
                VariableName           = variableName,
                AssignedValueSignature = valueSignature,
                AssignedValue          = value,
                ValueType              = type
            };

            return(declaration);
        }