public void Parse(string source, string expectedKey, string expectedFormat, string expectedArgs) { var literalParserMock = new Mock <ILiteralParser>(); var sb = new StringBuilder(source); literalParserMock.Setup(x => x.ParseLiterals(sb)); literalParserMock.Setup(x => x.ParseLiterals(sb)) .Returns(new[] { new Literal(0, source.Length, 1, 1, new StringBuilder(source)) }); var subject = new PatternParser(literalParserMock.Object); // Warm up (JIT) Benchmark.Start("Parsing formatter patterns (first time before JIT)", this.outputHelper); subject.Parse(sb); Benchmark.End(this.outputHelper); Benchmark.Start("Parsing formatter patterns (after warm-up)", this.outputHelper); var actual = subject.Parse(sb); Benchmark.End(this.outputHelper); Assert.Equal(1, actual.Count()); var first = actual.First(); Assert.Equal(expectedKey, first.Variable); Assert.Equal(expectedFormat, first.FormatterName); Assert.Equal(expectedArgs, first.FormatterArguments); }
public void Parse() { var subject = new PatternParser(new LiteralParser()); const string Source = @"Hi, {Name, select, male={guy} female={gal}}, you have {count, plural, zero {no friends}, other {# friends} }"; Benchmark.Start("First run (warm-up)", this.outputHelper); subject.Parse(new StringBuilder(Source)); Benchmark.End(this.outputHelper); Benchmark.Start("Next one (warmed up)", this.outputHelper); var actual = subject.Parse(new StringBuilder(Source)); Benchmark.End(this.outputHelper); Assert.Equal(2, actual.Count()); var formatterParam = actual.First(); Assert.Equal("Name", formatterParam.Variable); Assert.Equal("select", formatterParam.FormatterName); Assert.Equal("male={guy} female={gal}", formatterParam.FormatterArguments); formatterParam = actual.ElementAt(1); Assert.Equal("count", formatterParam.Variable); Assert.Equal("plural", formatterParam.FormatterName); Assert.Equal("zero {no friends}, other {# friends}", formatterParam.FormatterArguments); }
protected override void ParseProperties(PatternParser patternParser) { _parsedTopic = string.IsNullOrEmpty(_topic) ? null : patternParser.Parse(_topic); //_parsedDelaySeconds = string.IsNullOrEmpty(_delaySeconds) // ? (int?)null // : Convert.ToInt32(patternParser.Parse(_delaySeconds)); _parsedMessage = string.IsNullOrEmpty(_message) ? null : patternParser.Parse(_message); }
/// <summary> /// Parses the rules' patterns. /// </summary> /// <param name="proj">The project.</param> /// <param name="module">The module description.</param> /// <param name="context">The working context.</param> /// <returns>Parsed rule patterns.</returns> /// <exception cref="T:System.ArgumentException"> /// One of the rules has invalid pattern. /// </exception> // Token: 0x06000195 RID: 405 RVA: 0x0000D320 File Offset: 0x0000B520 protected Dictionary <Rule, PatternExpression> ParseRules(ConfuserProject proj, ProjectModule module, ConfuserContext context) { Dictionary <Rule, PatternExpression> ret = new Dictionary <Rule, PatternExpression>(); PatternParser parser = new PatternParser(); foreach (Rule rule in proj.Rules.Concat(module.Rules)) { try { ret.Add(rule, parser.Parse(rule.Pattern)); } catch (InvalidPatternException ex) { context.Logger.ErrorFormat("Invalid rule pattern: " + rule.Pattern + ".", new object[] { ex }); throw new ConfuserException(ex); } foreach (SettingItem <Protection> setting in rule) { if (!this.protections.ContainsKey(setting.Id)) { context.Logger.ErrorFormat("Cannot find protection with ID '{0}'.", new object[] { setting.Id }); throw new ConfuserException(null); } } } return(ret); }
private async void Set <T>(CommandArgs args) where T : class, ITemplate { var parameters = args.Parameters; var player = args.Player; if (parameters.Count < 1) { var commandName = args.GetCommandName(); player.SendErrorMessage($"Syntax: //{commandName.ToLowerInvariant()} <pattern>"); return; } var parser = new PatternParser(Parsers[typeof(T)]); var inputPattern = string.Join(" ", parameters); var pattern = parser.Parse(inputPattern); if (pattern == null) { player.SendErrorMessage($"Invalid pattern '{inputPattern}'."); return; } var session = Plugin.GetOrCreateSession(player); var editSession = session.CreateEditSession(); var region = session.Selection; await Task.Run(() => session.Submit(() => { var count = editSession.ModifyTiles(region, pattern); player.SendSuccessMessage($"Modified {count} tiles."); })).SendExceptions(player); }
public void FormatMessage_caches_reused_pattern() { var parserMock = new Mock <IPatternParser>(); var realParser = new PatternParser(new LiteralParser()); parserMock.Setup(x => x.Parse(It.IsAny <StringBuilder>())) .Returns((StringBuilder sb) => realParser.Parse(sb)); var library = new FormatterLibrary(); var subject = new MessageFormatter(patternParser: parserMock.Object, library: library, useCache: true); var pattern = "Hi {gender, select, male {Sir} female {Ma'am}}!"; var actual = subject.FormatMessage(pattern, new { gender = "male" }); Assert.Equal("Hi Sir!", actual); // '2' because it did not format "Ma'am" yet. parserMock.Verify(x => x.Parse(It.IsAny <StringBuilder>()), Times.Exactly(2)); actual = subject.FormatMessage(pattern, new { gender = "female" }); Assert.Equal("Hi Ma'am!", actual); parserMock.Verify(x => x.Parse(It.IsAny <StringBuilder>()), Times.Exactly(3)); // '3' because it has cached all options actual = subject.FormatMessage(pattern, new { gender = "female" }); Assert.Equal("Hi Ma'am!", actual); parserMock.Verify(x => x.Parse(It.IsAny <StringBuilder>()), Times.Exactly(3)); }
public void MultiTagOptionsTest(string pattern, BaseTag expectedTag) { List <BaseTag> actualTags = PatternParser.Parse(pattern); Assert.AreEqual(1, actualTags.Count); Assert.AreEqual(expectedTag, actualTags[0]); }
public void TestStackTracePattern() { var p = new PatternParser(GetLoggingEvent()); var s = p.Parse("%stacktrace{6}"); Assert.AreEqual("LayoutSkeleton.Format > PatternLayout.Format > PatternConverter.Format > PatternLayoutConverter.Convert > StackTracePatternConverter.Convert > LoggingEvent.get_LocationInformation", s); }
/// <summary> /// Parses the rules' patterns. /// </summary> /// <param name="proj">The project.</param> /// <param name="module">The module description.</param> /// <param name="context">The working context.</param> /// <returns>Parsed rule patterns.</returns> /// <exception cref="System.ArgumentException"> /// One of the rules has invalid pattern. /// </exception> protected Rules ParseRules(ConfuserProject proj, ProjectModule module, ConfuserContext context) { var ret = new Rules(); var parser = new PatternParser(); foreach (Rule rule in proj.Rules.Concat(module.Rules)) { try { ret.Add(rule, parser.Parse(rule.Pattern)); } catch (InvalidPatternException ex) { context.Logger.ErrorFormat("Invalid rule pattern: " + rule.Pattern + ".", ex); throw new ConfuserException(ex); } foreach (var setting in rule) { if (!protections.ContainsKey(setting.Id)) { context.Logger.ErrorFormat("Cannot find protection with ID '{0}'.", setting.Id); throw new ConfuserException(null); } } } return(ret); }
internal static int Main(string[] args) { var app = new CommandLineApplication(); app.Name = "MyMediaRenamer"; app.Description = "Bulk renamer of photo and video files."; app.HelpOption("-h|--help"); var defaultStringOption = app.Option("--default-string", "The default string used when a tag fails to produce a valid string", CommandOptionType.SingleValue); var dryRunOption = app.Option("-d|--dry-run", "Do a dry-run where the program does not actually rename any files", CommandOptionType.NoValue); var recursiveOption = app.Option("-r|--recursive", "Recursively access files under given directories", CommandOptionType.NoValue); var skipNullOption = app.Option("--skip-null", "Skip renaming a file if a tag fails to produce a valid string", CommandOptionType.NoValue); var patternArgument = app.Argument("File Name Pattern", "Pattern used to determine the new file name of each file", false); var filesArgument = app.Argument("Media Files", "File(s) to rename", true); app.OnExecute(() => { if (string.IsNullOrEmpty(patternArgument.Value) || !(filesArgument.Values.Count > 0)) { app.ShowHint(); _returnCode = 1; } if (defaultStringOption.HasValue()) { Renamer.NullTagString = defaultStringOption.Value(); } if (skipNullOption.HasValue()) { Renamer.SkipOnNullTag = true; } if (dryRunOption.HasValue()) { Renamer.DryRun = true; } try { var tags = PatternParser.Parse(patternArgument.Value); ProcessFilePaths(filesArgument.Values.ToArray(), recursiveOption.HasValue()); Renamer.Execute(MediaFiles, tags); } catch (PatternInvalidException e) { WriteError("Pattern provided by the user is invalid.", e, false); _returnCode = 1; } catch (Exception e) { WriteError("Program has encountered an unexpected error!\n", e, true); _returnCode = 1; } return(_returnCode); }); return(app.Execute(args)); }
public void Parse_exits_early_when_no_literals_have_been_found() { var literalParserMock = new Mock <ILiteralParser>(); var subject = new PatternParser(literalParserMock.Object); literalParserMock.Setup(x => x.ParseLiterals(It.IsAny <StringBuilder>())).Returns(new Literal[0]); Assert.Empty(subject.Parse(new StringBuilder())); }
public void Parse_NullS_ThrowsArgumentNullException() { var parser = Mock.Of <TemplateParser>(); var patternParser = new PatternParser(parser); // ReSharper disable once AssignNullToNotNullAttribute Assert.That(() => patternParser.Parse(null), Throws.ArgumentNullException); }
public void TestStackTracePattern() { var p = new PatternParser(GetLoggingEvent()); var s = p.Parse("%stacktrace{6}"); Assert.AreEqual("log4net.Layout.LayoutSkeleton.Format > log4net.Layout.PatternLayout.Format > log4net.Util.PatternConverter.Format > log4net.Layout.Pattern.PatternLayoutConverter.Convert > log4net.Layout.Pattern.StackTracePatternConverter.Convert > log4net.Core.LoggingEvent.get_LocationInformation", s); }
public static Func <Complex, Complex> Parse(string text) { Initialize(); var body = _parser.Parse(_lexer.Tokenize(text.ToLower())); return(Expression.Lambda <Func <Complex, Complex> >(body, X).Compile()); }
public void Parse_InvalidPattern_ReturnsNull(string s) { var template = Mock.Of <ITemplate>(); var template2 = Mock.Of <ITemplate>(); var parser = Mock.Of <TemplateParser>(p => p.Parse("a") == template && p.Parse("b") == template2); var patternParser = new PatternParser(parser); Assert.That(patternParser.Parse(s), Is.Null); }
protected override void ParseProperties(PatternParser patternParser) { _parsedStreamName = string.IsNullOrEmpty(_streamName) ? null : patternParser.Parse(_streamName); _parsedGroupName = string.IsNullOrEmpty(_groupName) ? null : patternParser.Parse(_groupName); _parsedMessage = string.IsNullOrEmpty(_message) ? null : patternParser.Parse(_message); _dateTimeOffset = string.IsNullOrEmpty(_timestamp) ? null : (DateTime?)DateTime.Parse(patternParser.Parse(_timestamp)); }
public void TestMessageAsNamePattern() { var loggingEvent = GetLoggingEvent(); var p = new PatternParser(loggingEvent); p.AddConverter("message-as-name", typeof(MessageAsNamePatternConverter)); var s = p.Parse("%message-as-name{-2}"); Assert.AreEqual("Tw", s, "%message-as-name not registered"); }
public void NTagsTest(string pattern, List <BaseTag> expectedFilePathTags) { List <BaseTag> actualFilePathTags = PatternParser.Parse(pattern); Assert.AreEqual(expectedFilePathTags.Count, actualFilePathTags.Count); for (int i = 0; i < expectedFilePathTags.Count; i++) { Assert.AreEqual(expectedFilePathTags[i], actualFilePathTags[i]); } }
public void SimplePatternStringTest() { var generator = PatternParser.Parse("{1|2|3}"); var actual = generator.GetVariationsString(); var expected = new StringBuilder(); expected.AppendLine("1"); expected.AppendLine("2"); expected.AppendLine("3"); Assert.Equal(expected.ToString(), actual); }
public void Parse_OneEntry() { var template = Mock.Of <ITemplate>(); var template2 = Mock.Of <ITemplate>(); var parser = Mock.Of <TemplateParser>(p => p.Parse("a") == template && p.Parse("b") == template2); var patternParser = new PatternParser(parser); var pattern = (Pattern)patternParser.Parse("a"); Assert.That(pattern, Is.Not.Null); Assert.That(pattern.Entries[0].Template, Is.EqualTo(template)); }
public void TestMessageAsNamePattern() { var loggingEvent = GetLoggingEvent(); var p = new PatternParser(loggingEvent); p.AddConverter("message-as-name", typeof(MessageAsNamePatternConverter)); var s = p.Parse("%message-as-name{-2}"); Assert.AreEqual("Tw/o", s, "%message-as-name not registered"); }
protected override void ParseProperties(PatternParser patternParser) { _parsedDimensions = !_dimensions.Any() ? null : _dimensions .Select(x => new Dimension { Name = x.Key, Value = patternParser.Parse(x.Value.Value) }). ToDictionary(x => x.Name, y => y); _parsedNamespace = string.IsNullOrEmpty(_namespace) ? null : patternParser.Parse(_namespace); _parsedMetricName = string.IsNullOrEmpty(_metricName) ? null : patternParser.Parse(_metricName); _dateTimeOffset = string.IsNullOrEmpty(_timestamp) ? null : (DateTimeOffset?)DateTimeOffset.Parse(patternParser.Parse(_timestamp)); }
private async void Replace <T>(CommandArgs args) where T : class, ITemplate { var parameters = args.Parameters; var player = args.Player; var inputPatterns = string.Join(" ", parameters).Split('|'); if (parameters.Count < 1 || inputPatterns.Length != 2) { var commandName = args.GetCommandName(); player.SendErrorMessage($"Syntax: //{commandName.ToLowerInvariant()} <from-pattern>|<to-pattern>"); return; } var parser = new PatternParser(Parsers[typeof(T)]); var fromPattern = parser.Parse(inputPatterns[0]); if (fromPattern == null) { player.SendErrorMessage($"Invalid from-pattern '{inputPatterns[0]}'."); return; } var toPattern = parser.Parse(inputPatterns[1]); if (toPattern == null) { player.SendErrorMessage($"Invalid to-pattern '{inputPatterns[1]}'."); return; } var session = Plugin.GetOrCreateSession(player); var editSession = session.CreateEditSession(); var region = session.Selection; await Task.Run(() => session.Submit(() => { var count = editSession.ModifyTiles(region, toPattern, new TemplateMask(fromPattern)); player.SendSuccessMessage($"Modified {count} tiles."); })).SendExceptions(player); }
public void Parse_TwoEntriesWithWeights() { var template = Mock.Of <ITemplate>(); var template2 = Mock.Of <ITemplate>(); var parser = Mock.Of <TemplateParser>(p => p.Parse("a") == template && p.Parse("b") == template2); var patternParser = new PatternParser(parser); var pattern = (Pattern)patternParser.Parse("5*a,6*b"); Assert.That(pattern, Is.Not.Null); Assert.That(pattern.Entries, Has.Some.Matches <PatternEntry>(e => e.Template == template && e.Weight == 5)); Assert.That(pattern.Entries, Has.Some.Matches <PatternEntry>(e => e.Template == template2 && e.Weight == 6)); }
internal PatternConverter(string dotNetPattern, RegexOptions options) { DotNetPattern = dotNetPattern; var isIgnorePatternWhitespace = (options & RegexOptions.IgnorePatternWhitespace) == RegexOptions.IgnorePatternWhitespace; var isExpicitCapture = (options & RegexOptions.ExplicitCapture) == RegexOptions.ExplicitCapture; var parser = new PatternParser(dotNetPattern, isIgnorePatternWhitespace, isExpicitCapture); var parseItems = parser.Parse(); patternMap = new PatternMap(parseItems); var builder = new PatternBuilder(dotNetPattern, patternMap); JavaPattern = builder.Build(parseItems); }
protected string PreProcess(LoggingEvent loggingEvent, string renderedString) { var patternParser = new PatternParser(loggingEvent); if (renderedString.Contains("%")) renderedString = patternParser.Parse(renderedString); LogLog.Debug(GetType(), string.Format("RenderedString: {0}", renderedString)); if (_dirtyParsedProperties) { ParseProperties(patternParser); if ( !loggingEvent.Properties.GetKeys() .Any(key => key.StartsWith("IsqsAppender.MetaData.") && key.EndsWith(".Error"))) _dirtyParsedProperties = false; } return renderedString; }
public void PatternGroupTest_Complex() { var group = _parser.Parse("[[{a,e}{[+high],[dental]}][-dorsal]]", EnvironmentType.After); Assert.IsNotNull(group); Assert.AreEqual(group.GroupType, GroupType.And); Assert.AreEqual(2, group.Members.Count); // Verify the second member of the group. var groupMember = group.Members[1] as PatternGroupMember; Assert.IsNotNull(groupMember); Assert.AreEqual(MemberType.Binary, groupMember.MemberType); Assert.AreEqual("-dorsal", groupMember.Member); FeatureMask mask = App.BFeatureCache.GetMask("-dorsal"); Assert.IsTrue(groupMember.BMask.ContainsOneOrMore(mask)); // Nested within the first group should be 2 other groups. PatternGroup nestedGroup = group.Members[0] as PatternGroup; Assert.IsNotNull(nestedGroup); Assert.AreEqual(2, nestedGroup.Members.Count); Assert.AreEqual(GroupType.And, nestedGroup.GroupType); // Verify the first group nested in the nested group. var nestedGroup1 = nestedGroup.Members[0] as PatternGroup; Assert.IsNotNull(nestedGroup1); Assert.AreEqual(2, nestedGroup1.Members.Count); Assert.AreEqual(GroupType.Or, nestedGroup1.GroupType); // Verify the first member of the nested, nested group. groupMember = nestedGroup1.Members[0] as PatternGroupMember; Assert.IsNotNull(groupMember); Assert.AreEqual(MemberType.SinglePhone, groupMember.MemberType); Assert.AreEqual("a", groupMember.Member); // Verify the second member of the nested, nested group. groupMember = nestedGroup1.Members[1] as PatternGroupMember; Assert.IsNotNull(groupMember); Assert.AreEqual(MemberType.SinglePhone, groupMember.MemberType); Assert.AreEqual("e", groupMember.Member); // Verify the second group nested in the nested group. var nestedGroup2 = nestedGroup.Members[1] as PatternGroup; Assert.IsNotNull(nestedGroup2); Assert.AreEqual(2, nestedGroup2.Members.Count); Assert.AreEqual(GroupType.Or, nestedGroup2.GroupType); // Verify the first member of the nested, nested group. groupMember = nestedGroup2.Members[0] as PatternGroupMember; Assert.IsNotNull(groupMember); Assert.AreEqual(MemberType.Binary, groupMember.MemberType); Assert.AreEqual("+high", groupMember.Member); mask = App.BFeatureCache.GetMask("+high"); Assert.IsTrue(groupMember.BMask.ContainsOneOrMore(mask)); // Verify the second member of the nested, nested group. groupMember = nestedGroup2.Members[1] as PatternGroupMember; Assert.IsNotNull(groupMember); Assert.AreEqual(MemberType.Articulatory, groupMember.MemberType); Assert.AreEqual("dental", groupMember.Member); mask = App.AFeatureCache.GetMask("dental"); Assert.IsTrue(groupMember.AMask.ContainsOneOrMore(mask)); }
public virtual void LoadPatterns(XmlReader source) { PatternParser pp = new PatternParser(this); ivalues = new TernaryTree(); pp.Parse(source); // patterns/values should be now in the tree // let's optimize a bit TrimToSize(); vspace.TrimToSize(); classmap.TrimToSize(); // get rid of the auxiliary map ivalues = null; }
private void TestSqlPattern(PatternParser inputDefn, PatternParser outputDefn, string input, string expected) { var patternOpts = inputDefn.Parse(input); Assert.AreEqual(expected, outputDefn.Render(patternOpts)); }
public void InvalidPatternTest(string pattern) { Assert.That(() => PatternParser.Parse(pattern), Throws.Exception); }
private void LoadDocument(LucyDocument model, Analyzer exactAnalyzer, Analyzer fuzzyAnalyzer, Boolean useAllBuiltIns) { this._lucyModel = model; this._exactAnalyzer = exactAnalyzer ?? GetAnalyzerForLocale(model.Locale); this._fuzzyAnalyzer = exactAnalyzer ?? fuzzyAnalyzer ?? Analyzer.NewAnonymous((field, textReader) => { Tokenizer tokenizer = new StandardTokenizer(LuceneVersion.LUCENE_48, textReader); TokenStream stream = new DoubleMetaphoneFilter(tokenizer, 6, false); //TokenStream stream = new BeiderMorseFilterFactory(new Dictionary<string, string>() // { // { "nameType", NameType.GENERIC.ToString()}, // { "ruleType", RuleType.APPROX.ToString() }, // { "languageSet", "auto"} // }).Create(tokenizer); return(new TokenStreamComponents(tokenizer, stream)); }); this._patternParser = new PatternParser(this._exactAnalyzer, this._fuzzyAnalyzer);; if (_lucyModel.Macros == null) { _lucyModel.Macros = new Dictionary <string, string>(); } if (_lucyModel.Entities != null) { foreach (var entityModel in _lucyModel.Entities) { if (entityModel.Patterns != null) { foreach (var patternModel in entityModel.Patterns) { var first = patternModel.First(); string resolution = first.Any(ch => ch == '@' || ch == '|' || ch == '+' || ch == '*' || ch == '?') || first.Contains("___") ? null : first.Trim('~', '(', ')'); foreach (var pattern in patternModel.Select(pat => ExpandMacros(pat)).OrderByDescending(pat => pat.Length)) { if (pattern.StartsWith('/') && pattern.EndsWith('/')) { RegexEntityPatterns.Add(new RegexEntityRecognizer(entityModel.Name, pattern.Trim('/'))); } else { var patternMatcher = _patternParser.Parse(pattern, entityModel.FuzzyMatch); if (patternMatcher != null) { var ignoreWords = entityModel.Ignore?.Select(ignoreText => ((TokenResolution)Tokenize(ignoreText).First().Resolution).Token) ?? Array.Empty <string>(); // Trace.TraceInformation($"{expandedPattern} => {patternMatcher}"); if (patternMatcher.ContainsWildcard()) { // we want to process wildcard patterns last WildcardEntityPatterns.Add(new EntityPattern(entityModel.Name, resolution, patternMatcher, ignoreWords)); } else { EntityPatterns.Add(new EntityPattern(entityModel.Name, resolution, patternMatcher, ignoreWords)); } } } } } } } // Auto detect all references to built in entities foreach (var pattern in this.EntityPatterns.ToList()) { foreach (var reference in pattern.PatternMatcher.GetEntityReferences().Select(r => r.TrimStart('@'))) { if (reference == "datetime" || reference == "datetimeV2") { this.BuiltinEntities.Add("datetime"); // add default pattern for datetime = (all permutations of datetime) EntityPatterns.Add(new EntityPattern("datetime", _patternParser.Parse("(@datetimeV2.date|@datetimeV2.time|@datetimeV2.datetime|@datetimeV2.daterange|@datetimeV2.timerange|@datetimeV2.datetimerange|@datetimeV2.duration)"))); } if (builtinEntities.Contains(reference) || builtinEntities.Contains(reference.Split('.').First())) { this.BuiltinEntities.Add(reference); } } } if (model.ExternalEntities != null) { foreach (var externalEntity in model.ExternalEntities) { if (builtinEntities.Contains(externalEntity) || builtinEntities.Contains(externalEntity.Split('.').First())) { this.BuiltinEntities.Add(externalEntity); } } } } if (useAllBuiltIns) { BuiltinEntities = new HashSet <string>(builtinEntities); // add default pattern for datetime = (all permutations of datetime) EntityPatterns.Add(new EntityPattern("datetime", _patternParser.Parse("(@datetimeV2.date|@datetimeV2.time|@datetimeV2.datetime|@datetimeV2.daterange|@datetimeV2.timerange|@datetimeV2.datetimerange|@datetimeV2.duration)"))); } ValidateModel(); }
public DefaultLogFormatter(string rawPattern) { _rawPattern = rawPattern.IsNullOrWhiteSpace() ? DefaultRawPattern : rawPattern; _patternTranslators = PatternParser.Parse(_rawPattern); }