public CharacterClassExpression GetComplement() { var complementCharacters = _alphabet.GetAllCharacters().Where(x => !_literals.ContainsKey(x)).ToList(); var complement = new CharacterClassExpression(_alphabet, _random, _maxInverseLength); complement.AddCharacters(complementCharacters); return(complement); }
private IParseResult <ExpressionMetaData> TryGetAnyCharacterExpression(ITokenStream tokenStream, out IExpression expression) { tokenStream.Pop(); var characterExpression = new CharacterClassExpression(_alphabet, _random, _maxCharClassInverseLength); characterExpression.AddCharacters(_alphabet.GetAllCharacters()); expression = characterExpression; return(new SuccessParseResult <ExpressionMetaData>(tokenStream.CurrentPosition, new ExpressionMetaData())); }
public override IExpression Clone() { var clone = new CharacterClassExpression(_alphabet, _random, _maxInverseLength) { RepeatRange = RepeatRange?.Clone(), ExpansionLength = ExpansionLength }; clone.AddCharacters(_characterList); return(clone); }
private static void ClassifyListsByLength(CharacterClassExpression charClass1, CharacterClassExpression charClass2, out IDictionary <string, string> shortDict, out IDictionary <string, string> longDict) { if (charClass1._literals.Count < charClass2._literals.Count) { shortDict = charClass1._literals; longDict = charClass2._literals; } else { shortDict = charClass2._literals; longDict = charClass1._literals; } }
private IParseResult <ExpressionMetaData> TryGetSingleCharacterExpression(ITokenStream tokenStream, out IExpression expression) { expression = null; var tokenValue = tokenStream.Pop().GetValue <string>(); var characterClassExpression = new CharacterClassExpression(_alphabet, _random, _maxCharClassInverseLength); characterClassExpression.AddCharacters(new List <string> { tokenValue }); expression = characterClassExpression; return(new SuccessParseResult <ExpressionMetaData>(tokenStream.CurrentPosition, new ExpressionMetaData())); }
public CharacterClassExpression GetUnion(CharacterClassExpression charClass) { var union = new CharacterClassExpression(_alphabet, _random, _maxInverseLength) { RepeatRange = RepeatRange }; union.AddCharacters(_characterList); if (charClass != null) { union.AddCharacters(charClass._characterList); } return(union); }
private IParseResult <ExpressionMetaData> TryGetCharacterClassExpression(ITokenStream tokenStream, out IExpression expression) { expression = null; var nextTokenType = tokenStream.Pop().GetType <RegexTokenType>(); var getComplement = nextTokenType == RegexTokenType.OpenNegateCharacterClass; nextTokenType = tokenStream.LookAhead(0).GetType <RegexTokenType>(); var characters = new List <string>(); while (nextTokenType == RegexTokenType.Character || nextTokenType == RegexTokenType.CharacterRange) { var nextToken = tokenStream.Pop(); if (nextToken.GetType <RegexTokenType>() == RegexTokenType.Character) { characters.Add(nextToken.GetValue <string>()); } else { characters.AddRange(nextToken.GetValue <CharacterRange>().Characters); } nextTokenType = tokenStream.LookAhead(0).GetType <RegexTokenType>(); } if (nextTokenType != RegexTokenType.CloseCharacterClass) { return(new FailureParseResult <ExpressionMetaData>(tokenStream.CurrentPosition, RegSeedErrorType.CloseCharacterClassExpected)); } tokenStream.Pop(); var characterClassExpression = new CharacterClassExpression(_alphabet, _random, _maxCharClassInverseLength); characterClassExpression.AddCharacters(characters); expression = getComplement ? characterClassExpression.GetComplement() : characterClassExpression; return(new SuccessParseResult <ExpressionMetaData>(tokenStream.CurrentPosition, new ExpressionMetaData())); }
private UnionExpression GetAllCharacterClassInversesUpToMaxInverseLength() { var minimalLengthTwoRange = new IntegerInterval(); minimalLengthTwoRange.TrySetValue(2, _maxInverseLength); var atLeastLengthTwoWords = new CharacterClassExpression(_alphabet, _random, _maxInverseLength) { RepeatRange = minimalLengthTwoRange }; atLeastLengthTwoWords.AddCharacters(_alphabet.GetAllCharacters()); var inverseExpressions = new List <IExpression> { GetComplement(), atLeastLengthTwoWords }; return(new UnionExpression(inverseExpressions, _random)); }
public CharacterClassExpression GetIntersection(CharacterClassExpression charClass) { if (charClass == null || !charClass._characterList.Any() || !_characterList.Any()) { return(new CharacterClassExpression(_alphabet, _random, _maxInverseLength)); } ClassifyListsByLength(this, charClass, out var shortDict, out var longDict); var intersectList = shortDict.Where(x => longDict.ContainsKey(x.Key)) .Select(y => y.Key) .ToList(); var intersection = new CharacterClassExpression(_alphabet, _random, _maxInverseLength) { RepeatRange = RepeatRange }; intersection.AddCharacters(intersectList); return(intersection); }