Esempio n. 1
0
        /// <summary>
        /// Create a TregexPattern from this tregex string using the headFinder and
        /// basicCat function this TregexPatternCompiler was created with.
        /// Implementation note: If there is an invalid token in the Tregex
        /// parser, JavaCC will throw a TokenMgrError.  This is a class
        /// that extends Error, not Exception (OMG! - bad!), and so rather than
        /// requiring clients to catch it, we wrap it in a ParseException.
        /// (The original Error's are thrown in TregexParserTokenManager.)
        /// </summary>
        /// <param name="tregex">The pattern to parse</param>
        /// <returns>A new TregexPattern object based on this string</returns>
        /// <exception cref="TregexParseException">If the expression is syntactically invalid</exception>
        public TregexPattern Compile(string tregex)
        {
            foreach (Tuple <string, string> macro in macros)
            {
                //tregex = tregex.replaceAll(macro.first(), macro.second());
                tregex = Regex.Replace(tregex, macro.Item1, macro.Item2);
            }
            TregexPattern pattern;

            try
            {
                var parser = new TregexParser(new StringReader(tregex + '\n'),
                                              basicCatFunction.Apply, headFinder);
                pattern = parser.Root();
            }
            catch (TokenMgrException tme)
            {
                throw new TregexParseException("Could not parse " + tregex /*, tme*/);
            }
            catch (ParseException e)
            {
                throw new TregexParseException("Could not parse " + tregex /*, e*/);
            }
            pattern.SetPatternString(tregex);
            return(pattern);
        }
Esempio n. 2
0
 /// <summary>
 /// Create a TregexPattern from this tregex string using the headFinder and
 /// basicCat function this TregexPatternCompiler was created with.
 /// Implementation note: If there is an invalid token in the Tregex
 /// parser, JavaCC will throw a TokenMgrError.  This is a class
 /// that extends Error, not Exception (OMG! - bad!), and so rather than
 /// requiring clients to catch it, we wrap it in a ParseException.
 /// (The original Error's are thrown in TregexParserTokenManager.)
 /// </summary>
 /// <param name="tregex">The pattern to parse</param>
 /// <returns>A new TregexPattern object based on this string</returns>
 /// <exception cref="TregexParseException">If the expression is syntactically invalid</exception>
 public TregexPattern Compile(string tregex)
 {
     foreach (Tuple<string, string> macro in macros)
     {
         //tregex = tregex.replaceAll(macro.first(), macro.second());
         tregex = Regex.Replace(tregex, macro.Item1, macro.Item2);
     }
     TregexPattern pattern;
     try
     {
         var parser = new TregexParser(new StringReader(tregex + '\n'),
             basicCatFunction.Apply, headFinder);
         pattern = parser.Root();
     }
     catch (TokenMgrException tme)
     {
         throw new TregexParseException("Could not parse " + tregex /*, tme*/);
     }
     catch (ParseException e)
     {
         throw new TregexParseException("Could not parse " + tregex /*, e*/);
     }
     pattern.SetPatternString(tregex);
     return pattern;
 }