Example #1
0
        /**
         * nfa - true to attempt as an nfa pattern for regexp. This handles most things except the complex repeates, ie {1,4}
         */
        public void AddPattern(TokenPattern pattern, bool nfa = true)
        {
            switch (pattern.Type)
            {
            case TokenPattern.PatternType.STRING:
                try
                {
                    _stringDfaMatcher.AddPattern(pattern);
                }
                catch (Exception e)
                {
                    throw new ParserCreationException(
                              ParserCreationException.ErrorType.INVALID_TOKEN,
                              pattern.Name,
                              "error adding string token: " +
                              e.Message);
                }
                break;

            case TokenPattern.PatternType.REGEXP:
                if (nfa)
                {
                    try
                    {
                        _nfaMatcher.AddPattern(pattern);
                    }
                    catch (Exception)
                    {
                        nfa = false;
                    }
                }
                if (!nfa)
                {
                    try
                    {
                        _regExpMatcher.AddPattern(pattern);
                    }
                    catch (Exception e)
                    {
                        throw new ParserCreationException(
                                  ParserCreationException.ErrorType.INVALID_TOKEN,
                                  pattern.Name,
                                  "regular expression contains error(s): " +
                                  e.Message);
                    }
                }

                break;

            default:
                throw new ParserCreationException(
                          ParserCreationException.ErrorType.INVALID_TOKEN,
                          pattern.Name,
                          "pattern type " + pattern.Type +
                          " is undefined");
            }
        }
Example #2
0
        public void AddPattern(TokenPattern pattern)
        {
            switch (pattern.Type)
            {
            case TokenPattern.PatternType.STRING:
                try
                {
                    _stringDfaMatcher.AddPattern(pattern);
                }
                catch (Exception e)
                {
                    throw new ParserCreationException(
                              ParserCreationException.ErrorType.INVALID_TOKEN,
                              pattern.Name,
                              "error adding string token: " +
                              e.Message);
                }
                break;

            case TokenPattern.PatternType.REGEXP:
                try
                {
                    //Because of a bug in nfaMatcher's treatment
                    //of repeath specifiers we resort to effectively always using the
                    //regExp matcher.
                    //Under some conditions, the process of throwing the
                    //exception from the nfaMatcher and catching it
                    //incurs an unacceptable slowdown in performance
                    _regExpMatcher.AddPattern(pattern);
                }
                catch (Exception)
                {
                    try
                    {
                        _nfaMatcher.AddPattern(pattern);
                    }
                    catch (Exception e)
                    {
                        throw new ParserCreationException(
                                  ParserCreationException.ErrorType.INVALID_TOKEN,
                                  pattern.Name,
                                  "regular expression contains error(s): " +
                                  e.Message);
                    }
                }
                break;

            default:
                throw new ParserCreationException(
                          ParserCreationException.ErrorType.INVALID_TOKEN,
                          pattern.Name,
                          "pattern type " + pattern.Type +
                          " is undefined");
            }
        }
Example #3
0
        public void AddPattern(TokenPattern pattern)
        {
            switch (pattern.Type)
            {
            case TokenPattern.PatternType.STRING:
                try
                {
                    _stringDfaMatcher.AddPattern(pattern);
                }
                catch (Exception e)
                {
                    throw new ParserCreationException(
                              ParserCreationException.ErrorType.INVALID_TOKEN,
                              pattern.Name,
                              "error adding string token: " +
                              e.Message);
                }
                break;

            case TokenPattern.PatternType.REGEXP:
                try
                {
                    //See comments in NetStandard version for why the regExp matcher is tried first
                    _regExpMatcher.AddPattern(pattern);
                }
                catch (Exception)
                {
                    try
                    {
                        _nfaMatcher.AddPattern(pattern);
                    }
                    catch (Exception e)
                    {
                        throw new ParserCreationException(
                                  ParserCreationException.ErrorType.INVALID_TOKEN,
                                  pattern.Name,
                                  "regular expression contains error(s): " +
                                  e.Message);
                    }
                }
                break;

            default:
                throw new ParserCreationException(
                          ParserCreationException.ErrorType.INVALID_TOKEN,
                          pattern.Name,
                          "pattern type " + pattern.Type +
                          " is undefined");
            }
        }