/// <summary>
        /// Checks the named.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <returns>bool</returns>
        private bool CheckNamed(RegexBuffer buffer)
        {
            // look for ?<Name> or ?'Name' syntax...
            Regex regex = new Regex(
                @"
                        ^                         # anchor to start of string
                        \?(\<|')                  # ?< or ?'
                        (?<Name>[a-zA-Z0-9]+?)    # Capture name
                        (\>|')                    # ?> or ?'
                        (?<Rest>.+)               # The rest of the string
                        ",
                RegexOptions.IgnorePatternWhitespace);

            Match match = regex.Match(buffer.String);

            if (match.Success)
            {
                this.description = $"Capture to <{match.Groups["Name"]}>";

                // advance buffer to the rest of the expression
                buffer.Offset  += match.Groups["Rest"].Index;
                this.expression = new RegexExpression(buffer);

                this.CheckClosingParen(buffer);
                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Checks the non backtracking.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <returns>bool</returns>
        private bool CheckNonBacktracking(RegexBuffer buffer)
        {
            // Look for non-backtracking sub-expression ?>
            Regex regex = new Regex(
                @"
                        ^                         # anchor to start of string
                        \?\>
                        (?<Rest>.+)             # The rest of the expression
                        ",
                RegexOptions.IgnorePatternWhitespace);
            Match match = regex.Match(buffer.String);

            if (match.Success)
            {
                this.description = "Non-backtracking subexpression";

                buffer.Offset  += match.Groups["Rest"].Index;
                this.expression = new RegexExpression(buffer);

                this.CheckClosingParen(buffer);
                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Checks the balanced group.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <returns>bool</returns>
        private bool CheckBalancedGroup(RegexBuffer buffer)
        {
            // look for ?<Name1-Name2> or ?'Name1-Name2' syntax...
            // look for ?<Name> or ?'Name' syntax...
            Regex regex = new Regex(
                @"
                        ^                         # anchor to start of string
                        \?[\<|']                  # ?< or ?'
                        (?<Name1>[a-zA-Z]+?)       # Capture name1
                        -
                        (?<Name2>[a-zA-Z]+?)       # Capture name2
                        [\>|']                    # ?> or ?'
                        (?<Rest>.+)               # The rest of the expression
                        ",
                RegexOptions.IgnorePatternWhitespace);

            Match match = regex.Match(buffer.String);

            if (match.Success)
            {
                this.description = $"Balancing Group <{match.Groups["Name1"]}>-<{match.Groups["Name2"]}>";
                buffer.Offset   += match.Groups["Rest"].Index;
                this.expression  = new RegexExpression(buffer);
                this.CheckClosingParen(buffer);
                return(true);
            }

            return(false);
        }
Exemple #4
0
        /// <summary>
        /// Adds the lookup.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="startLocation">The start location.</param>
        /// <param name="endLocation">The end location.</param>
        /// <param name="canCoalesce">if set to <c>true</c> [can coalesce].</param>
        public void AddLookup(RegexItem item, int startLocation, int endLocation, bool canCoalesce)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item", "RegexItem is null");
            }

            if (this.inseries)
            {
                // in a series, add character to the previous one...
                if (canCoalesce)
                {
                    RegexRef lastItem = (RegexRef)this.expressionLookup[this.expressionLookup.Count - 1];
                    lastItem.StringValue += item.ToString(0);
                    lastItem.Length      += endLocation - startLocation + 1;
                }
                else
                {
                    this.expressionLookup.Add(new RegexRef(item, startLocation, endLocation));
                    this.inseries = false;
                }
            }
            else
            {
                if (canCoalesce)
                {
                    this.inseries = true;
                }

                this.expressionLookup.Add(new RegexRef(item, startLocation, endLocation));
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="RegexRef"/> class.
        /// </summary>
        /// <param name="regexItem">The regex item.</param>
        /// <param name="start">The start.</param>
        /// <param name="end">The end.</param>
        public RegexRef(RegexItem regexItem, int start, int end)
        {
            if (regexItem == null)
            {
                throw new ArgumentNullException("regexItem", "RegexItem is null");
            }

            this.StringValue = regexItem.ToString(0);
            this.start = start;
            this.end = end;
        }
Exemple #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RegexRef"/> class.
        /// </summary>
        /// <param name="regexItem">The regex item.</param>
        /// <param name="start">The start.</param>
        /// <param name="end">The end.</param>
        public RegexRef(RegexItem regexItem, int start, int end)
        {
            if (regexItem == null)
            {
                throw new ArgumentNullException(nameof(regexItem), "RegexItem is null");
            }

            this.StringValue = regexItem.ToString(0);
            this.start       = start;
            this.end         = end;
        }
        /// <summary>
        /// Handles the plain old capture.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <returns>bool</returns>
        private bool HandlePlainOldCapture(RegexBuffer buffer)
        {
            // we're already at the expression. Just create a new
            // expression, and make sure that we're at a ")" when
            // we're done
            if (buffer.ExplicitCapture)
            {
                this.description = "Non-capturing Group";
            }

            this.expression = new RegexExpression(buffer);
            this.CheckClosingParen(buffer);
            return(true);
        }
        private bool CheckLookahead(RegexBuffer buffer)
        {
            Regex regex = new Regex(
                @"
                        ^                         # anchor to start of string
                        \?
                        (?<Assertion><=|<!|=|!)   # assertion char
                        (?<Rest>.+)               # The rest of the expression
                        ",
                RegexOptions.IgnorePatternWhitespace);

            Match match = regex.Match(buffer.String);

            if (match.Success)
            {
                switch (match.Groups["Assertion"].Value)
                {
                case "=":
                    this.description = "zero-width positive lookahead";
                    break;

                case "!":
                    this.description = "zero-width negative lookahead";
                    break;

                case "<=":
                    this.description = "zero-width positive lookbehind";
                    break;

                case "<!":
                    this.description = "zero-width negative lookbehind";
                    break;
                }

                buffer.Offset  += match.Groups["Rest"].Index;
                this.expression = new RegexExpression(buffer);
                this.CheckClosingParen(buffer);
                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Checks the conditional.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        private void CheckConditional(RegexBuffer buffer)
        {
            // Look for conditional (?(name)yesmatch|nomatch)
            // (name can also be an expression)
            Regex regex = new Regex(
                @"
                        ^                         # anchor to start of string
                        \?\(
                        (?<Rest>.+)             # The rest of the expression
                        ",
                RegexOptions.IgnorePatternWhitespace);
            Match match = regex.Match(buffer.String);

            if (match.Success)
            {
                this.description = "Conditional Subexpression";

                buffer.Offset  += match.Groups["Rest"].Index;
                this.expression = new RegexConditional(buffer);
            }
        }
        /// <summary>
        /// Checks the options.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <returns>bool</returns>
        private bool CheckOptions(RegexBuffer buffer)
        {
            // look for ?imnsx-imnsx:
            Regex regex = new Regex(
                @"
                        ^                         # anchor to start of string
                        \?(?<Options>[imnsx-]+):
                        ",
                RegexOptions.IgnorePatternWhitespace);

            Match match = regex.Match(buffer.String);

            if (match.Success)
            {
                string option = match.Groups["Options"].Value;
                this.description = $"Set options to {OptionNames[option]}";
                this.expression  = null;
                buffer.Offset   += match.Groups[0].Length;
                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Checks the balanced group.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <returns>bool</returns>
        private bool CheckBalancedGroup(RegexBuffer buffer)
        {
            // look for ?<Name1-Name2> or ?'Name1-Name2' syntax...
            // look for ?<Name> or ?'Name' syntax...
            Regex regex = new Regex(
                        @"
                        ^                         # anchor to start of string
                        \?[\<|']                  # ?< or ?'
                        (?<Name1>[a-zA-Z]+?)       # Capture name1
                        -
                        (?<Name2>[a-zA-Z]+?)       # Capture name2
                        [\>|']                    # ?> or ?'
                        (?<Rest>.+)               # The rest of the expression
                        ",
                RegexOptions.IgnorePatternWhitespace);

            Match match = regex.Match(buffer.String);
            if (match.Success)
            {
                this.description = string.Format("Balancing Group <{0}>-<{1}>", match.Groups["Name1"], match.Groups["Name2"]);
                buffer.Offset += match.Groups["Rest"].Index;
                this.expression = new RegexExpression(buffer);
                this.CheckClosingParen(buffer);
                return true;
            }

            return false;
        }
        private bool CheckLookahead(RegexBuffer buffer)
        {
            Regex regex = new Regex(
                        @"
                        ^                         # anchor to start of string
                        \?
                        (?<Assertion><=|<!|=|!)   # assertion char
                        (?<Rest>.+)               # The rest of the expression
                        ",
                RegexOptions.IgnorePatternWhitespace);

            Match match = regex.Match(buffer.String);
            if (match.Success)
            {
                switch (match.Groups["Assertion"].Value)
                {
                    case "=":
                        this.description = "zero-width positive lookahead";
                        break;

                    case "!":
                        this.description = "zero-width negative lookahead";
                        break;

                    case "<=":
                        this.description = "zero-width positive lookbehind";
                        break;

                    case "<!":
                        this.description = "zero-width negative lookbehind";
                        break;
                }

                buffer.Offset += match.Groups["Rest"].Index;
                this.expression = new RegexExpression(buffer);
                this.CheckClosingParen(buffer);
                return true;
            }

            return false;
        }
        /// <summary>
        /// Checks the options.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <returns>bool</returns>
        private bool CheckOptions(RegexBuffer buffer)
        {
            // look for ?imnsx-imnsx:
            Regex regex = new Regex(
                        @"
                        ^                         # anchor to start of string
                        \?(?<Options>[imnsx-]+):
                        ",
                RegexOptions.IgnorePatternWhitespace);

            Match match = regex.Match(buffer.String);
            if (match.Success)
            {
                string option = match.Groups["Options"].Value;
                this.description = string.Format("Set options to {0}", OptionNames[option]);
                this.expression = null;
                buffer.Offset += match.Groups[0].Length;
                return true;
            }

            return false;
        }
        /// <summary>
        /// Checks the non backtracking.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <returns>bool</returns>
        private bool CheckNonBacktracking(RegexBuffer buffer)
        {
            // Look for non-backtracking sub-expression ?>
            Regex regex = new Regex(
                        @"
                        ^                         # anchor to start of string
                        \?\>
                        (?<Rest>.+)             # The rest of the expression
                        ",
                RegexOptions.IgnorePatternWhitespace);
            Match match = regex.Match(buffer.String);
            if (match.Success)
            {
                this.description = string.Format("Non-backtracking subexpression");

                buffer.Offset += match.Groups["Rest"].Index;
                this.expression = new RegexExpression(buffer);

                this.CheckClosingParen(buffer);
                return true;
            }

            return false;
        }
        /// <summary>
        /// Adds the lookup.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="startLocation">The start location.</param>
        /// <param name="endLocation">The end location.</param>
        /// <param name="canCoalesce">if set to <c>true</c> [can coalesce].</param>
        public void AddLookup(RegexItem item, int startLocation, int endLocation, bool canCoalesce)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item", "RegexItem is null");
            }

            if (this.inseries)
            {
                    // in a series, add character to the previous one...
                if (canCoalesce)
                {
                    RegexRef lastItem = (RegexRef)this.expressionLookup[this.expressionLookup.Count - 1];
                    lastItem.StringValue += item.ToString(0);
                    lastItem.Length += endLocation - startLocation + 1;
                }
                else
                {
                    this.expressionLookup.Add(new RegexRef(item, startLocation, endLocation));
                    this.inseries = false;
                }
            }
            else
            {
                if (canCoalesce)
                {
                    this.inseries = true;
                }

                this.expressionLookup.Add(new RegexRef(item, startLocation, endLocation));
            }
        }
        /// <summary>
        /// Checks the named.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <returns>bool</returns>
        private bool CheckNamed(RegexBuffer buffer)
        {
            // look for ?<Name> or ?'Name' syntax...
            Regex regex = new Regex(
                @"
                        ^                         # anchor to start of string
                        \?(\<|')                  # ?< or ?'
                        (?<Name>[a-zA-Z0-9]+?)    # Capture name
                        (\>|')                    # ?> or ?'
                        (?<Rest>.+)               # The rest of the string
                        ",
                RegexOptions.IgnorePatternWhitespace);

            Match match = regex.Match(buffer.String);
            if (match.Success)
            {
                this.description = string.Format("Capture to <{0}>", match.Groups["Name"]);
                
                    // advance buffer to the rest of the expression
                buffer.Offset += match.Groups["Rest"].Index;
                this.expression = new RegexExpression(buffer);

                this.CheckClosingParen(buffer);
                return true;
            }

            return false;
        }
        /// <summary>
        /// Checks the conditional.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <returns>bool</returns>
        private bool CheckConditional(RegexBuffer buffer)
        {
            // Look for conditional (?(name)yesmatch|nomatch)
            // (name can also be an expression)
            Regex regex = new Regex(
                        @"
                        ^                         # anchor to start of string
                        \?\(
                        (?<Rest>.+)             # The rest of the expression
                        ",
                RegexOptions.IgnorePatternWhitespace);
            Match match = regex.Match(buffer.String);
            if (match.Success)
            {
                this.description = string.Format("Conditional Subexpression");

                buffer.Offset += match.Groups["Rest"].Index;
                this.expression = new RegexConditional(buffer);
                return true;
            }

            return false;
        }
Exemple #18
0
 /// <summary>
 /// Adds the lookup.
 /// </summary>
 /// <param name="item">The item.</param>
 /// <param name="startLocation">The start location.</param>
 /// <param name="endLocation">The end location.</param>
 public void AddLookup(RegexItem item, int startLocation, int endLocation)
 {
     this.AddLookup(item, startLocation, endLocation, false);
 }
 /// <summary>
 /// Adds the lookup.
 /// </summary>
 /// <param name="item">The item.</param>
 /// <param name="startLocation">The start location.</param>
 /// <param name="endLocation">The end location.</param>
 public void AddLookup(RegexItem item, int startLocation, int endLocation)
 {
     this.AddLookup(item, startLocation, endLocation, false);
 }
        /// <summary>
        /// Handles the plain old capture.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <returns>bool</returns>
        private bool HandlePlainOldCapture(RegexBuffer buffer)
        {
                // we're already at the expression. Just create a new
                // expression, and make sure that we're at a ")" when 
                // we're done
            if (buffer.ExplicitCapture)
            {
                this.description = string.Format("Non-capturing Group");
            }

            this.expression = new RegexExpression(buffer);
            this.CheckClosingParen(buffer);
            return true;
        }