예제 #1
0
        internal IElement Run()
        {
            Initialize();
            bool escaping = false;

            while (index < input.Length)
            {
                if (!escaping && input[index] == EscapeCharacter)
                {
                    escaping = true;
                }
                else if (!escaping && parsingInfo.IsInitiator(input[index]))
                {
                    initiatorCount++;
                }
                else if (!escaping && parsingInfo.IsTerminator(input[index]))
                {
                    if (initiatorCount == 0)
                    {
                        int length;
                        var quantifier = GetQuantifier(index + 1, out length);
                        index += length;
                        return parsingInfo.ParseToken(token.Close(true), quantifier ?? Quantifier.One);
                    }
                    else
                    {
                        initiatorCount--;
                    }
                }
                else if (!escaping && parsingInfo.IsRoot && !ParsingInfo.FromInitiator(input[index]).IsRoot)
                {
                    FinalizeElement(Quantifier.One);
                    var innerParser = new Parser(input.Substring(index + 1), Quantifier.One, ParsingInfo.FromInitiator(input[index]));
                    children.Add(innerParser.Run());
                    this.index += innerParser.index + 1;
                }
                else
                {
                    int length;
                    var quantifier = GetQuantifier(index + 1, out length);

                    if (parsingInfo.AcceptMetacharacters && quantifier != null)
                    {
                        FinalizeElement(Quantifier.One);
                        token.Open();
                        token.Add(input[index], escaping);
                        FinalizeElement(quantifier);
                        index += length;
                    }
                    else
                    {
                        token.Open();
                        token.Add(input[index], escaping);
                    }

                    escaping = false;
                }

                index++;
            }

            if (!parsingInfo.IsRoot)
            {
                throw new RegexLiteException(String.Format("The specified regular expression cannot be parsed. " +
                    "Expected to find the closing character '{0}', but none was found.", parsingInfo.Terminator));
            }

            return GetResult();
        }
예제 #2
0
 /// <summary>
 /// RegexLite text parser.
 /// </summary>
 /// <param name="input">The text to be parsed.</param>
 /// <returns>A root element containing the resulting regular expression tree.</returns>
 public static IElement Run(string input)
 {
     var parser = new Parser(input, Quantifier.One);
     return parser.Run();
 }