Example #1
0
 private char[] GetLettersSymbols(TokenGroup group, Token token)
 {
     return(GetLetters(group, token).Concat(GetSymbols(group, token)).ToArray());
     //if (ModifierType.NONE == token.Modifier)
     //{
     //    if (group.Modifier.HasFlag(ModifierType.UPPERCASE))
     //    {
     //        return UPPER_LETTERS_SYMBOLS;
     //    }
     //    else if (group.Modifier.HasFlag(ModifierType.LOWERCASE))
     //    {
     //        return LOWER_LETTERS_SYMBOLS;
     //    }
     //    else
     //    {
     //        return LETTERS_SYMBOLS;
     //    }
     //}
     //else
     //{
     //    if (token.Modifier.HasFlag(ModifierType.UPPERCASE))
     //    {
     //        return UPPER_LETTERS_SYMBOLS;
     //    }
     //    else if (token.Modifier.HasFlag(ModifierType.LOWERCASE))
     //    {
     //        return LOWER_LETTERS_SYMBOLS;
     //    }
     //    else
     //    {
     //        return LETTERS_SYMBOLS;
     //    }
     //}
 }
Example #2
0
 private char[] GetNumbersSymbols(TokenGroup group, Token token)
 {
     return(GetNumbers(group, token).Concat(GetSymbols(group, token)).ToArray());
     //if (ModifierType.NONE == token.Modifier)
     //{
     //    if (group.Modifier.HasFlag(ModifierType.EXCLUDE_ZERO))
     //    {
     //        return NUMBERS_EXCEPT_0_SYMBOLS;
     //    }
     //    else
     //    {
     //        return NUMBERS_SYMBOLS;
     //    }
     //}
     //else
     //{
     //    if (token.Modifier.HasFlag(ModifierType.EXCLUDE_ZERO))
     //    {
     //        return NUMBERS_EXCEPT_0_SYMBOLS;
     //    }
     //    else
     //    {
     //        return NUMBERS_SYMBOLS;
     //    }
     //}
 }
Example #3
0
 public PatternBuilder BeginGroup(int minRepeats, int maxRepeats, ModifierType modifiers, IEnumerable <char> exclude)
 {
     if (_patternList.Last().Tokens.Count > 0)
     {
         var tg = new TokenGroup()
         {
             MinimumCount = minRepeats, MaximumCount = maxRepeats, Modifier = modifiers
         };
         if (null != exclude && exclude.Count() > 0)
         {
             tg.ControlBlock = new ControlBlock()
             {
                 Type = ControlBlockType.ECB, ExceptValues = exclude.ToArray()
             }
         }
         ;
         _patternList.Add(tg);
     }
     else
     {
         var tg = _patternList.Last();
         tg.MinimumCount = minRepeats;
         tg.MaximumCount = maxRepeats;
         tg.Modifier     = modifiers;
         if (null != exclude && exclude.Count() > 0)
         {
             tg.ControlBlock = new ControlBlock()
             {
                 Type = ControlBlockType.ECB, ExceptValues = exclude.ToArray()
             }
         }
         ;
     }
     return(this);
 }
Example #4
0
        private char[] GetSymbols(TokenGroup group, Token token)
        {
            if ((string.IsNullOrWhiteSpace(group.CultureName) && string.IsNullOrWhiteSpace(token.CultureName)) ||
                string.IsNullOrWhiteSpace(group.CultureName) && (token.CultureName.Equals("en-US", StringComparison.InvariantCultureIgnoreCase) || token.CultureName.Equals("en", StringComparison.InvariantCultureIgnoreCase)) ||
                (group.CultureName.Equals("en-US", StringComparison.InvariantCultureIgnoreCase) || group.CultureName.Equals("en", StringComparison.InvariantCultureIgnoreCase) && string.IsNullOrWhiteSpace(token.CultureName)) ||
                (group.CultureName.Equals("en-US", StringComparison.InvariantCultureIgnoreCase) || group.CultureName.Equals("en", StringComparison.InvariantCultureIgnoreCase)) &&
                (token.CultureName.Equals("en-US", StringComparison.InvariantCultureIgnoreCase) || token.CultureName.Equals("en", StringComparison.InvariantCultureIgnoreCase)))
            {
                return(SYMBOLS);
            }
            else
            {
                var name = string.Empty;
                if (!string.IsNullOrWhiteSpace(token.CultureName))
                {
                    name = token.CultureName;
                }
                else
                {
                    name = group.CultureName;
                }

                if (!Cultures.TryGetValue(name, out var ci))
                {
                    if (ThrowExceptionOnUnknowLanguage)
                    {
                        throw new UnknownCultureException($"Culture '{name}' was found in the pattern but has not been specified in the generator.");
                    }
                    else
                    {
                        ci = Cultures[DEFAULT_CULTURE];
                    }
                }

                if (ci.SymbolsSpecified)
                {
                    return(ci.Symbols);
                }
                else
                {
                    return(SYMBOLS);
                }
            }
        }
Example #5
0
        private char[] GetLettersNumbers(TokenGroup group, Token token)
        {
            return(GetLetters(group, token).Concat(GetNumbers(group, token)).ToArray());
            //if (ModifierType.NONE == token.Modifier)
            //{
            //    if (group.Modifier.HasFlag(ModifierType.UPPERCASE))
            //    {
            //        if (group.Modifier.HasFlag(ModifierType.EXCLUDE_ZERO))
            //        {
            //            return UPPER_LETTERS_NUMBERS_EXCEPT_0;
            //        }
            //        else
            //        {
            //            return UPPER_LETTERS_NUMBERS;
            //        }
            //    }

            //    else if (group.Modifier.HasFlag(ModifierType.LOWERCASE))
            //    {
            //        if (group.Modifier.HasFlag(ModifierType.EXCLUDE_ZERO))
            //        {
            //            return LOWER_LETTERS_NUMBERS_EXCEPT_0;
            //        }
            //        else
            //        {
            //            return LOWER_LETTERS_NUMBERS;
            //        }
            //    }

            //    else
            //    {
            //        if (group.Modifier.HasFlag(ModifierType.EXCLUDE_ZERO))
            //        {
            //            return LETTERS_NUMBERS_EXCEPT_0;
            //        }
            //        else
            //        {
            //            return LETTERS_NUMBERS;
            //        }
            //    }
            //}
            //else
            //{
            //    if (token.Modifier.HasFlag(ModifierType.UPPERCASE))
            //    {
            //        if (token.Modifier.HasFlag(ModifierType.EXCLUDE_ZERO))
            //        {
            //            return UPPER_LETTERS_NUMBERS_EXCEPT_0;
            //        }
            //        else
            //        {
            //            return UPPER_LETTERS_NUMBERS;
            //        }
            //    }

            //    else if (token.Modifier.HasFlag(ModifierType.LOWERCASE))
            //    {
            //        if (token.Modifier.HasFlag(ModifierType.EXCLUDE_ZERO))
            //        {
            //            return LOWER_LETTERS_NUMBERS_EXCEPT_0;
            //        }
            //        else
            //        {
            //            return LOWER_LETTERS_NUMBERS;
            //        }
            //    }

            //    else
            //    {
            //        if (token.Modifier.HasFlag(ModifierType.EXCLUDE_ZERO))
            //        {
            //            return LETTERS_NUMBERS_EXCEPT_0;
            //        }
            //        else
            //        {
            //            return LETTERS_NUMBERS;
            //        }
            //    }
            //}
        }
Example #6
0
        private char[] GetNumbers(TokenGroup group, Token token)
        {
            if ((string.IsNullOrWhiteSpace(group.CultureName) && string.IsNullOrWhiteSpace(token.CultureName)) ||
                string.IsNullOrWhiteSpace(group.CultureName) && (token.CultureName.Equals("en-US", StringComparison.InvariantCultureIgnoreCase) || token.CultureName.Equals("en", StringComparison.InvariantCultureIgnoreCase)) ||
                (group.CultureName.Equals("en-US", StringComparison.InvariantCultureIgnoreCase) || group.CultureName.Equals("en", StringComparison.InvariantCultureIgnoreCase) && string.IsNullOrWhiteSpace(token.CultureName)) ||
                (group.CultureName.Equals("en-US", StringComparison.InvariantCultureIgnoreCase) || group.CultureName.Equals("en", StringComparison.InvariantCultureIgnoreCase)) &&
                (token.CultureName.Equals("en-US", StringComparison.InvariantCultureIgnoreCase) || token.CultureName.Equals("en", StringComparison.InvariantCultureIgnoreCase)))
            {
                if (ModifierType.NONE == token.Modifier)
                {
                    // Token has no  modifier
                    // Check group modifier
                    if (group.Modifier.HasFlag(ModifierType.EXCLUDE_ZERO))
                    {
                        return(NUMBERS_EXCEPT_0);
                    }
                    else
                    {
                        return(NUMBERS);
                    }
                }
                else if (token.Modifier.HasFlag(ModifierType.EXCLUDE_ZERO))
                {
                    return(NUMBERS_EXCEPT_0);
                }
                else
                {
                    return(NUMBERS);
                }
            }
            else
            {
                var name = string.Empty;
                if (!string.IsNullOrWhiteSpace(token.CultureName))
                {
                    name = token.CultureName;
                }
                else
                {
                    name = group.CultureName;
                }

                if (!Cultures.TryGetValue(name, out var ci))
                {
                    if (ThrowExceptionOnUnknowLanguage)
                    {
                        throw new UnknownCultureException($"Culture '{name}' was found in the pattern but has not been specified in the generator.");
                    }
                    else
                    {
                        ci = Cultures[DEFAULT_CULTURE];
                    }
                }

                if (!ci.NumbersSpecified)
                {
                    if (ModifierType.NONE == token.Modifier)
                    {
                        // Token has no  modifier
                        // Check group modifier
                        if (group.Modifier.HasFlag(ModifierType.EXCLUDE_ZERO))
                        {
                            return(NUMBERS_EXCEPT_0);
                        }
                        else
                        {
                            return(NUMBERS);
                        }
                    }
                    else if (token.Modifier.HasFlag(ModifierType.EXCLUDE_ZERO))
                    {
                        return(NUMBERS_EXCEPT_0);
                    }
                    else
                    {
                        return(NUMBERS);
                    }
                }
                else
                {
                    return(ci.Numbers);
                }
            }
        }
Example #7
0
        private char[] GetLetters(TokenGroup group, Token token)
        {
            if ((string.IsNullOrWhiteSpace(group.CultureName) && string.IsNullOrWhiteSpace(token.CultureName)) ||
                string.IsNullOrWhiteSpace(group.CultureName) && (token.CultureName.Equals("en-US", StringComparison.InvariantCultureIgnoreCase) || token.CultureName.Equals("en", StringComparison.InvariantCultureIgnoreCase)) ||
                (group.CultureName.Equals("en-US", StringComparison.InvariantCultureIgnoreCase) || group.CultureName.Equals("en", StringComparison.InvariantCultureIgnoreCase) && string.IsNullOrWhiteSpace(token.CultureName)) ||
                (group.CultureName.Equals("en-US", StringComparison.InvariantCultureIgnoreCase) || group.CultureName.Equals("en", StringComparison.InvariantCultureIgnoreCase)) &&
                (token.CultureName.Equals("en-US", StringComparison.InvariantCultureIgnoreCase) || token.CultureName.Equals("en", StringComparison.InvariantCultureIgnoreCase)))
            {
                // Use the built the standard, built-in en-US culture
                if (ModifierType.NONE == token.Modifier)
                {
                    // Token has no modifier
                    // Check group modifier
                    if (group.Modifier.HasFlag(ModifierType.UPPERCASE))
                    {
                        // Group modifier is uppercase
                        return(UPPERCASE);
                    }
                    else if (group.Modifier.HasFlag(ModifierType.LOWERCASE))
                    {
                        // Group modifier is lowercase
                        return(LOWERCASE);
                    }
                    else
                    {
                        // No token or group modifier
                        return(ALL_LETTERS);
                    }
                }
                else if (token.Modifier.HasFlag(ModifierType.UPPERCASE))
                {
                    return(UPPERCASE);
                }
                else
                {
                    return(LOWERCASE);
                }
            }
            else
            {
                var name = string.Empty;
                if (!string.IsNullOrWhiteSpace(token.CultureName))
                {
                    name = token.CultureName;
                }
                else
                {
                    name = group.CultureName;
                }

                if (!Cultures.TryGetValue(name, out var ci))
                {
                    if (ThrowExceptionOnUnknowLanguage)
                    {
                        throw new UnknownCultureException($"Culture '{name}' was found in the pattern but has not been specified in the generator.");
                    }
                    else
                    {
                        ci = Cultures[DEFAULT_CULTURE];
                    }
                }

                if (!ci.UppercaseLowercaseSpecified)
                {
                    return(ci.UppercaseLetters);                                  // There are no upper & lowercase letters so just return the single, uppercase array
                }
                if (ModifierType.NONE == token.Modifier)
                {
                    // Token has no modifier
                    // Check group modifier
                    if (group.Modifier.HasFlag(ModifierType.UPPERCASE))
                    {
                        // Group modifier is uppercase
                        return(ci.UppercaseLetters);
                    }
                    else if (group.Modifier.HasFlag(ModifierType.LOWERCASE))
                    {
                        // Group modifier is lowercase
                        return(ci.LowercaseLetters);
                    }
                    else
                    {
                        // No token or group modifier
                        return(ci.UppercaseLetters.Concat(ci.LowercaseLetters).ToArray());
                    }
                }
                else if (token.Modifier.HasFlag(ModifierType.UPPERCASE))
                {
                    return(ci.UppercaseLetters);
                }
                else
                {
                    return(ci.LowercaseLetters);
                }
            }
        }
Example #8
0
        private void HandleFunctionBlock(ref Token token, string pattern, int originalPosition)
        {
            var EOS   = false;
            var force = false;
            var cb    = new ControlBlock()
            {
                Type = ControlBlockType.FCB
            };
            var  functionName = new StringBuilder();
            var  formatter    = new StringBuilder(string.Empty);
            char functionCode = 'x';

            while (!EOS)
            {
                pos++;
                if (pos >= pattern.Length)
                {
                    throw new InvalidPatternException($"No closing control block token found for the control block starting at position {originalPosition}.");
                }

                switch (pattern[pos])
                {
                case 'T':
                case 'G':
                    functionCode = pattern[pos];
                    pos++;
                    if (pos >= pattern.Length)
                    {
                        throw new InvalidPatternException($"No closing control block token found for the control block starting at position {originalPosition}.");
                    }
                    if (char.Equals(pattern[pos], '}'))
                    {
                        EOS = true;
                        //cb.FunctionName = functionCode.ToString();
                        //if (char.Equals(functionCode, 'T'))
                        //    cb.Function = () => DateTime.Now.ToString();
                        //else
                        //    cb.Function = () => Guid.NewGuid().ToString();

                        //token.ControlBlock = cb;
                        //EOS = true;
                    }
                    else if (char.Equals(pattern[pos], '?'))
                    {
                        if (!char.Equals(pattern[pos + 1], '}'))
                        {
                            throw new InvalidPatternException($"Expecting a control block closing brace at positions {pos + 1}, which was not found.");
                        }

                        force = true;
                        EOS   = true;
                        pos++;
                    }
                    else if (char.Equals(pattern[pos], ':'))
                    {
                        cb.FunctionName = functionCode.ToString();
                        while (!EOS)
                        {
                            pos++;
                            if (pos >= pattern.Length)
                            {
                                throw new InvalidPatternException($"No closing control block token found for the control block starting at position {originalPosition}.");
                            }
                            if (char.Equals(pattern[pos], '}'))
                            {
                                if (formatter.Length < 1)
                                {
                                    throw new InvalidPatternException($"Expected function formatting string at position {pos}.");
                                }

                                EOS = true;
                            }
                            else if (char.Equals(pattern[pos], '?'))
                            {
                                if (!char.Equals(pattern[pos + 1], '}'))
                                {
                                    throw new InvalidPatternException($"Expecting a control block closing brace at positions {pos + 1}, which was not found.");
                                }

                                force = true;
                            }
                            else
                            {
                                formatter.Append(pattern[pos]);
                            }
                        }
                    }
                    else
                    {
                        throw new InvalidPatternException($"An unexpected token '{pattern[pos]}' was found at position {pos}.");
                    }
                    break;

                default:
                    functionName.Append(pattern[pos]);
                    while (true)
                    {
                        pos++;
                        if (pos >= pattern.Length)
                        {
                            throw new InvalidPatternException($"No closing control block token found for the control block starting at position {originalPosition}.");
                        }

                        if (pattern[pos].Equals('}'))
                        {
                            break;
                        }
                        functionName.Append(pattern[pos]);
                    }
                    cb.FunctionName = functionName.ToString();

                    if (string.IsNullOrWhiteSpace(cb.FunctionName))
                    {
                        throw new InvalidPatternException($"User-defined function name cannot be null, empty, or whitespace (UDF starting at position {originalPosition}).");
                    }

                    EOS = true;
                    break;
                }
            }

            var format = formatter.ToString();

            if (char.Equals(functionCode, 'G'))
            {
                cb.FunctionName = "GUID";
                if (!string.IsNullOrWhiteSpace(format))
                {
                    if (!string.Equals("N", format, StringComparison.CurrentCulture) && !string.Equals("D", format, StringComparison.CurrentCulture) &&
                        !string.Equals("B", format, StringComparison.CurrentCulture) && !string.Equals("P", format, StringComparison.CurrentCulture) &&
                        !string.Equals("X", format, StringComparison.CurrentCulture))
                    {
                        throw new InvalidPatternException($"An unrecognized GUID format string was found.  Format string found: '{format}'.");
                    }


                    if (force)
                    {
                        token.Type  = TokenType.LITERAL;
                        token.Value = Guid.NewGuid().ToString(format);
                        cb          = null;
                    }
                    else
                    {
                        cb.Function = () => Guid.NewGuid().ToString(format);
                    }
                }
                else
                {
                    if (force)
                    {
                        token.Type  = TokenType.LITERAL;
                        token.Value = Guid.NewGuid().ToString();
                        cb          = null;
                    }
                    else
                    {
                        cb.Function = () => Guid.NewGuid().ToString();
                    }
                }
            }
            else if (char.Equals(functionCode, 'T'))
            {
                cb.FunctionName = "DATETIME";
                if (!string.IsNullOrWhiteSpace(format))
                {
                    if (force)
                    {
                        token.Type  = TokenType.LITERAL;
                        token.Value = DateTime.Now.ToString(format);
                        cb          = null;
                    }
                    else
                    {
                        cb.Function = () => DateTime.Now.ToString(format);
                    }
                }
                else
                {
                    if (force)
                    {
                        token.Type  = TokenType.LITERAL;
                        token.Value = DateTime.Now.ToString();
                        cb          = null;
                    }
                    else
                    {
                        cb.Function = () => DateTime.Now.ToString();
                    }
                }
            }

            token.ControlBlock = cb;

            if (TokenType.DUMMY == token.Type)
            {
                token.Type = TokenType.CONTROL_BLOCK;

                if (isInGroup)
                {
                    tokenizedGroup.Add(currentGroup);
                    currentGroup = new TokenGroup();
                    currentGroup.Tokens.Add(token);
                    handled = true;
                }
            }
        }
Example #9
0
        internal bool Tokenize(string pattern)
        {
            handled        = false;
            tokenizedGroup = new List <TokenGroup>();
            currentGroup   = new TokenGroup();

            for (pos = 0; pos < pattern.Length; pos++)
            {
                if (handled)
                {
                    handled = false;
                }
                var token = new Token();
                switch (pattern[pos])
                {
                case 'a':
                    token.Type = TokenType.LETTER;
                    break;

                case '0':
                    token.Type = TokenType.NUMBER;
                    break;

                case '9':
                    token.Type = TokenType.NUMBER_EXCEPT_ZERO;
                    break;

                case '@':
                    token.Type = TokenType.SYMBOL;
                    break;

                case '.':
                    token.Type = TokenType.LETTER_NUMBER;
                    break;

                case '+':
                    token.Type = TokenType.LETTER_SYMBOL;
                    break;

                case '%':
                    token.Type = TokenType.NUMBER_SYMBOL;
                    break;

                case '*':
                    token.Type = TokenType.LETTER_NUMBER_SYMBOL;
                    break;

                case '{':
                    token.Type = TokenType.CONTROL_BLOCK;
                    HandleControlBlock(ref token, pattern);
                    break;

                case '[':
                    token.Type = TokenType.LITERAL;
                    HandleLiteral(ref token, pattern);
                    break;

                case '#':
                    token.Type = TokenType.OPTIONAL;
                    HandleOptional(ref token, pattern);
                    break;

                case '<':
                    token.Type = TokenType.RANGE;
                    HandleRange(ref token, pattern);
                    break;

                case '/':
                    if (!isInGroup)
                    {
                        // This is the start of a new group
                        isInGroup = true;
                        if (currentGroup.Tokens.Count > 0)
                        {
                            tokenizedGroup.Add(currentGroup);
                            currentGroup = new TokenGroup();
                        }
                    }
                    else
                    {
                        // This is the end of a group
                        // Need to check for modifiers, repeat count, and ECB
                        if (pos != pattern.Length - 1)
                        {
                            if (pattern[pos + 1].Equals('(') || MODIFIERS.Contains(pattern[pos + 1]) || pattern[pos + 1].Equals('{') || pattern[pos + 1].Equals('&'))
                            {
                                do
                                {
                                    if (pos == pattern.Length - 1)
                                    {
                                        break;
                                    }
                                    if (pattern[pos + 1].Equals('('))
                                    {
                                        pos++; HandleCount(pattern);
                                    }
                                    if (pos == pattern.Length - 1)
                                    {
                                        break;
                                    }
                                    if (MODIFIERS.Contains(pattern[pos + 1]))
                                    {
                                        pos++; HandleModifier(pattern, true);
                                    }
                                    if (pos == pattern.Length - 1)
                                    {
                                        break;
                                    }
                                    if (pattern[pos + 1].Equals('&'))
                                    {
                                        pos++; HandleCulture(pattern, true);
                                    }
                                    if (pos == pattern.Length - 1)
                                    {
                                        break;
                                    }
                                    if (pattern[pos + 1].Equals('{'))
                                    {
                                        var dummy = new Token(); pos++; HandleControlBlock(ref dummy, pattern);
                                    }
                                    if (pos == pattern.Length - 1)
                                    {
                                        break;
                                    }
                                    if (TOKENS.Contains(pattern[pos + 1]))
                                    {
                                        break;
                                    }
                                } while ((pattern[pos + 1].Equals('(') || MODIFIERS.Contains(pattern[pos + 1])) && !pattern[pos + 1].Equals('{') && !pattern[pos + 1].Equals('&'));
                            }
                        }

                        tokenizedGroup.Add(currentGroup);
                        currentGroup = new TokenGroup();
                        isInGroup    = false;
                    }
                    handled = true;
                    break;

                case '&':
                    HandleCulture(pattern);
                    handled = true;
                    break;

                case '\\':
                    token.Type = TokenType.LITERAL;
                    pos++;
                    if (pattern[pos].Equals('n'))
                    {
                        token.Value = Environment.NewLine;
                    }
                    else if (pattern[pos].Equals('t'))
                    {
                        token.Value = "\t";
                    }
                    else
                    {
                        throw new InvalidPatternException($"Unknown escape sequence \\{pattern[pos]} at position {pos - 1}.");
                    }
                    break;

                case '^':
                case '!':
                case '~':
                    HandleModifier(pattern);
                    handled = true;
                    break;

                case '(':
                    HandleCount(pattern);
                    handled = true;
                    break;

                case '>':
                    HandleFormatControlBlock(pattern);
                    handled = true;
                    break;

                default:
                    throw new InvalidPatternException($"Unknown token '{pattern[pos]}' found in Position {pos}.");
                }

                if (!handled)
                {
                    //tokenizedList.Add(token);
                    currentGroup.Tokens.Add(token);
                }
            }

            if (currentGroup.Tokens.Count > 0)
            {
                tokenizedGroup.Add(currentGroup);
            }
            //TokenizedPattern = tokenizedList;
            TokenizedPattern = tokenizedGroup;
            return(true);
        }