Example #1
0
        /// <summary>
        /// Tries to parse the given specifier into an Atom. For that all categories of Atoms are checked in the following order:
        ///  1. Group
        ///  2. Class
        ///  3. Literal
        /// When it succeeds, the given expression will be assigned a SingleAtomExpression containing the Atom and it's Quantifier.
        /// The parsed atom will be removed from the specifier and the method returns true. To check whether the complete specifier was an Atom,
        /// one needs to examine the specifier after calling this method. If it was, the specifier is empty after calling.
        /// </summary>
        /// <param name="specifier">The specifier to extract the leading Atom out of. Will be shortened if an Atom was successfully extracted</param>
        /// <param name="expression">The resulting SingleAtomExpression</param>
        /// <returns>True, if an Atom could be extracted, false otherwise</returns>
        // Note: could be rewritten to not consume the specifier and instead return an integer specifying the consumed length of specifier. This would remove the by-ref passed string hack
        // internal for testing
        internal static bool TryParseAsAtom(ref string specifier, out IRegularExpression expression)
        {
            Match m = groupWithQuantifier.Match(specifier);

            if (m.Success)
            {
                string atom       = m.Groups["expression"].Value;
                string quantifier = m.Groups["quantifier"].Value;
                specifier  = specifier.Substring(atom.Length + 2 + quantifier.Length);
                expression = new SingleAtomExpression(new Group("(" + atom + ")", new Quantifier(quantifier)));
                return(true);
            }
            m = characterClassWithQuantifier.Match(specifier);
            if (m.Success)
            {
                string atom       = m.Groups["expression"].Value;
                string quantifier = m.Groups["quantifier"].Value;
                specifier  = specifier.Substring(atom.Length + 2 + quantifier.Length);
                expression = new SingleAtomExpression(new CharacterClass("[" + atom + "]", new Quantifier(quantifier)));
                return(true);
            }
            m = literalWithQuantifier.Match(specifier);
            if (m.Success)
            {
                string atom       = m.Groups["expression"].Value;
                string quantifier = m.Groups["quantifier"].Value;
                specifier  = specifier.Substring(atom.Length + quantifier.Length);
                expression = new SingleAtomExpression(new Literal(atom, new Quantifier(quantifier)));
                return(true);
            }
            expression = null;
            return(false);
        }
 public override bool Equals(object obj)
 {
     if (obj is SingleAtomExpression)
     {
         SingleAtomExpression other = obj as SingleAtomExpression;
         return(other.Atom.Equals(Atom) && other.Quantifier.Equals(Quantifier));
     }
     return(false);
 }