public DynamicMethodTokenResolver(DynamicMethod dynamicMethod) { var resolverField = typeof(DynamicMethod).GetField( "m_resolver", BindingFlags.Instance | BindingFlags.NonPublic); var resolver = resolverField?.GetValue(dynamicMethod); if (resolver == null) { throw new ArgumentException("The dynamic method's IL has not been finalized."); } tokenResolver = (TokenResolver)resolver.GetType() .GetMethod("ResolveToken", BindingFlags.Instance | BindingFlags.NonPublic) .CreateDelegate(typeof(TokenResolver), resolver); stringResolver = (StringResolver)resolver.GetType().GetMethod("GetStringLiteral", BindingFlags.Instance | BindingFlags.NonPublic).CreateDelegate(typeof(StringResolver), resolver); signatureResolver = (SignatureResolver)resolver.GetType().GetMethod("ResolveSignature", BindingFlags.Instance | BindingFlags.NonPublic).CreateDelegate(typeof(SignatureResolver), resolver); getTypeFromHandleUnsafe = (GetTypeFromHandleUnsafe)typeof(Type).GetMethod("GetTypeFromHandleUnsafe", BindingFlags.Static | BindingFlags.NonPublic, null, new[] { typeof(IntPtr) }, null).CreateDelegate(typeof(GetTypeFromHandleUnsafe), null); var runtimeType = typeof(RuntimeTypeHandle).Assembly.GetType("System.RuntimeType"); var runtimeMethodHandleInternal = typeof(RuntimeTypeHandle).Assembly.GetType("System.RuntimeMethodHandleInternal"); getMethodBase = runtimeType.GetMethod("GetMethodBase", BindingFlags.Static | BindingFlags.NonPublic, null, new[] { runtimeType, runtimeMethodHandleInternal }, null); runtimeMethodHandleInternalCtor = runtimeMethodHandleInternal.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(IntPtr) }, null); var runtimeFieldInfoStub = typeof(RuntimeTypeHandle).Assembly.GetType("System.RuntimeFieldInfoStub"); runtimeFieldHandleStubCtor = runtimeFieldInfoStub.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, new[] { typeof(IntPtr), typeof(object) }, null); getFieldInfo = runtimeType.GetMethod("GetFieldInfo", BindingFlags.Static | BindingFlags.NonPublic, null, new[] { runtimeType, typeof(RuntimeTypeHandle).Assembly.GetType("System.IRuntimeFieldInfo") }, null); }
public void TestResolve(string expected, string key, string value) { var resolver = new StringResolver(); var model = new ArgumentModel(key, value); Assert.Equal(expected, resolver.Resolve(model)); }
/// <summary> /// Tries the create default resolver. /// </summary> /// <returns><c>true</c>, if create default resolver was tryed, <c>false</c> otherwise.</returns> /// <param name="type">Type.</param> /// <param name="resolver">Resolver.</param> private bool TryCreateDefaultResolver(Type type, out IResolver resolver) { if (type.IsValueType) //Проверяем является ли тип значимым { if (type.IsPrimitive && TryCreatePrimitiveResolver(type, out resolver)) //Проверяем является ли тип примитивным { return(true); } if (TypeOf <Decimal> .Equals(type)) { resolver = new DecimalResolver(); return(true); } } else { if (type.IsArray) { resolver = CreateArrayResolver(type); return(true); } if (TypeOf <Object> .Equals(type)) { resolver = new ObjectResolver(); return(true); } if (TypeOf <String> .Equals(type)) { resolver = new StringResolver(); return(true); } if (RuntimeType.Equals(type)) { resolver = new TypeResolver(Serializator); return(true); } if (Delegate.IsAssignableFrom(type)) { if ((Serializator.Options & SerializatorOption.ThrowOutExceptions) != 0) { throw new NotSupportedException("Serialization delegates is not supported"); } resolver = new EmptyResolver(Serializator); return(true); } } resolver = null; return(false); }
public void Resolve_StartQuoteFollowedByLetters_ShouldNotFindAStringSinceThereIsNoEndQuote() { // Setup test var stream = new CharStream("\"str".ToCharArray()); var resolver = new StringResolver(); var tokens = new HashSet <Token>(); // Perform test var result = resolver.TryResolve(stream, tokens); // Validate result Assert.False(result); Assert.Empty(tokens); Assert.Equal(0, stream.CurrentIndex); }
public void Resolve_StringContainingDoubleQuotes_ShouldConvertDobuleQuotesIntoQuote() { // Setup test var stream = new CharStream("\"str\"\"\" a".ToCharArray()); var resolver = new StringResolver(); var tokens = new HashSet <Token>(); // Perform test var result = resolver.TryResolve(stream, tokens); // Validate result var token = tokens.First(); Assert.True(result); Assert.NotNull(token); Assert.Equal(TokenTypes.String, token.Type); Assert.Equal("str\"", token.Value); Assert.Equal(' ', stream.Current); }
public void Resolve_StringFollowedBySpace_ShouldMoveStreamToSpace() { // Setup test var stream = new CharStream("\"str\" a".ToCharArray()); var resolver = new StringResolver(); var tokens = new HashSet <Token>(); // Perform test var result = resolver.TryResolve(stream, tokens); // Validate result Assert.True(result); var token = tokens.First(); Assert.NotNull(token); Assert.Equal(TokenTypes.String, token.Type); Assert.Equal("str", token.Value); Assert.Equal(' ', stream.Current); }
public DynamicMethodTokenResolver(DynamicMethod dynamicMethod) { object resolver = typeof(DynamicMethod).GetField("m_resolver", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(dynamicMethod); _tokenResolver = (TokenResolver)resolver.GetType().GetMethod("_resolveToken", BindingFlags.Instance | BindingFlags.NonPublic).CreateDelegate(typeof(TokenResolver), resolver); _stringResolver = (StringResolver)resolver.GetType().GetMethod("GetStringLiteral", BindingFlags.Instance | BindingFlags.NonPublic).CreateDelegate(typeof(StringResolver), resolver); _signatureResolver = (SignatureResolver)resolver.GetType().GetMethod("ResolveSignature", BindingFlags.Instance | BindingFlags.NonPublic).CreateDelegate(typeof(SignatureResolver), resolver); _getTypeFromHandleUnsafe = (GetTypeFromHandleUnsafe)typeof(Type).GetMethod("GetTypeFromHandleUnsafe", BindingFlags.Static | BindingFlags.NonPublic, null, new[] { typeof(IntPtr) }, null).CreateDelegate(typeof(GetTypeFromHandleUnsafe), null); Type runtimeType = typeof(RuntimeTypeHandle).Assembly.GetType("System.RuntimeType"); Type runtimeMethodHandleInternal = typeof(RuntimeTypeHandle).Assembly.GetType("System.RuntimeMethodHandleInternal"); _getMethodBase = runtimeType.GetMethod("GetMethodBase", BindingFlags.Static | BindingFlags.NonPublic, null, new[] { runtimeType, runtimeMethodHandleInternal }, null); _runtimeMethodHandleInternalCtor = runtimeMethodHandleInternal.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(IntPtr) }, null); Type runtimeFieldInfoStub = typeof(RuntimeTypeHandle).Assembly.GetType("System.RuntimeFieldInfoStub"); _runtimeFieldHandleStubCtor = runtimeFieldInfoStub.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, new[] { typeof(IntPtr), typeof(object) }, null); _getFieldInfo = runtimeType.GetMethod("GetFieldInfo", BindingFlags.Static | BindingFlags.NonPublic, null, new[] { runtimeType, typeof(RuntimeTypeHandle).Assembly.GetType("System.IRuntimeFieldInfo") }, null); }
public static void Main(string[] args) { // 1. Line { #region Snippet_1a ILine key = LineRoot.Global.PluralRules("Unicode.CLDR35").Key("Key").Format("Hello, {0}"); #endregion Snippet_1a } { #region Snippet_1b ILineRoot root = new LineRoot(); ILine hint1 = root.PluralRules("Unicode.CLDR35"); ILine section1 = hint1.Section("Section2"); ILine section1_1 = hint1.Section("Section1.1"); ILine key1_1_1 = section1_1.Key("Key1"); ILine key1_1_2 = section1_1.Key("Key2"); ILine value1_1_1 = key1_1_1.Format("Hello, {0}"); // ... #endregion Snippet_1b } // 2. Parts { #region Snippet_2a IAsset resourceAsset = new ResourceDictionary(new Dictionary <ILine, byte[]>()); ILine line = LineRoot.Global.Asset(resourceAsset); #endregion Snippet_2a } { #region Snippet_2b ILine line = LineRoot.Global.Logger(Console.Out, LineStatusSeverity.Ok); #endregion Snippet_2b } { #region Snippet_2c ICulturePolicy culturePolicy = new CulturePolicy().SetToCurrentCulture(); ILine line = LineRoot.Global.CulturePolicy(culturePolicy); #endregion Snippet_2c } { #region Snippet_2d #endregion Snippet_2d } { #region Snippet_2e #endregion Snippet_2e } { #region Snippet_2f #endregion Snippet_2f } { #region Snippet_2g #endregion Snippet_2g } { #region Snippet_2h #endregion Snippet_2h } // 3. Hints { #region Snippet_3_ IAsset asset = LineReaderMap.Default.FileAsset("ILine\\Hints.ini"); ILineRoot root = new LineRoot(asset); Console.WriteLine(root.Key("Error").Value(DateTime.Now)); Console.WriteLine(root.Key("Ok")); #endregion Snippet_3_ } { #region Snippet_3a ILine line1 = LineRoot.Global.StringFormat(TextFormat.Default).String("Text"); ILine line2 = LineRoot.Global.StringFormat("Lexical.Localization.StringFormat.TextFormat,Lexical.Localization") .String("Text"); #endregion Snippet_3a Console.WriteLine(line2); } { #region Snippet_3b IPluralRules pluralRules = PluralRulesResolver.Default.Resolve("Unicode.CLDR35"); ILine line1 = LineRoot.Global.PluralRules(pluralRules); ILine line2 = LineRoot.Global.PluralRules("Unicode.CLDR35"); ILine line3 = LineRoot.Global.PluralRules("[Category=cardinal,Case=one]n=1[Category=cardinal,Case=other]true"); #endregion Snippet_3b Console.WriteLine(line2); } { #region Snippet_3c IFormatProvider customFormat = new CustomFormat(); ILine line1 = LineRoot.Global.FormatProvider(customFormat).Format("{0:DATE}").Value(DateTime.Now); ILine line2 = LineRoot.Global.FormatProvider("docs.CustomFormat,docs").Format("{0:DATE}").Value(DateTime.Now); #endregion Snippet_3c Console.WriteLine(line1); Console.WriteLine(line2); } { #region Snippet_3e ResolveSource[] resolveSequence = new ResolveSource[] { ResolveSource.Inline, ResolveSource.Asset, ResolveSource.Line }; IStringResolver stringResolver = new StringResolver(Resolvers.Default, resolveSequence); ILine line = LineRoot.Global.StringResolver(stringResolver); #endregion Snippet_3e Console.WriteLine(line); } { #region Snippet_3f ILine line = LineRoot.Global.StringResolver("Lexical.Localization.StringFormat.StringResolver"); #endregion Snippet_3f Console.WriteLine(line); } { #region Snippet_3g ResolveSource[] resolveSequence = new ResolveSource[] { ResolveSource.Inline, ResolveSource.Asset, ResolveSource.Line }; IResourceResolver resourceResolver = new ResourceResolver(Resolvers.Default, resolveSequence); ILine line = LineRoot.Global.ResourceResolver(resourceResolver); #endregion Snippet_3g Console.WriteLine(line); } { #region Snippet_3h ILine line = LineRoot.Global.ResourceResolver("Lexical.Localization.Resource.ResourceResolver"); #endregion Snippet_3h Console.WriteLine(line); } // 4. Keys { #region Snippet_4a #endregion Snippet_4a } { #region Snippet_4b #endregion Snippet_4b } { #region Snippet_4c #endregion Snippet_4c } { #region Snippet_4d #endregion Snippet_4d } { #region Snippet_4e #endregion Snippet_4e } { #region Snippet_4f #endregion Snippet_4f } { #region Snippet_4g #endregion Snippet_4g } { #region Snippet_4h #endregion Snippet_4h } // 5. Non-canonicals { #region Snippet_5a Assembly asm = typeof(ILine_Examples).Assembly; ILine line1 = LineRoot.Global.Assembly(asm); ILine line2 = LineRoot.Global.Assembly("docs"); #endregion Snippet_5a } { #region Snippet_5b CultureInfo culture = CultureInfo.GetCultureInfo("en"); ILine line1 = LineRoot.Global.Culture(culture); ILine line2 = LineRoot.Global.Culture("en"); #endregion Snippet_5b } { #region Snippet_5c ILine line1 = LineRoot.Global.Type <ILine_Examples>(); ILine line2 = LineRoot.Global.Type(typeof(ILine_Examples)); #endregion Snippet_5c } { #region Snippet_5d #endregion Snippet_5d } { #region Snippet_5e #endregion Snippet_5e } { #region Snippet_5f #endregion Snippet_5f } { #region Snippet_5g #endregion Snippet_5g } { #region Snippet_5h #endregion Snippet_5h } // 6. Canonicals { #region Snippet_6a ILine line = LineRoot.Global.Key("Ok"); #endregion Snippet_6a } { #region Snippet_6b ILine line = LineRoot.Global.Section("Resources").Key("Ok"); #endregion Snippet_6b } { #region Snippet_6c ILine line = LineRoot.Global.Location(@"c:\dir"); #endregion Snippet_6c } { #region Snippet_6d ILine line = LineRoot.Global.BaseName("docs.Resources"); #endregion Snippet_6d } { #region Snippet_6e #endregion Snippet_6e } { #region Snippet_6f #endregion Snippet_6f } { #region Snippet_6g #endregion Snippet_6g } { #region Snippet_6h #endregion Snippet_6h } // 7. Strings { #region Snippet_7a IString str = CSharpFormat.Default.Parse("ErrorCode = 0x{0:X8}"); ILine line = LineRoot.Global.Key("Error").String(str); #endregion Snippet_7a } { #region Snippet_7b ILine line = LineRoot.Global.Key("Error").StringFormat(CSharpFormat.Default).String("ErrorCode = 0x{0:X8}"); #endregion Snippet_7b } { #region Snippet_7c ILine line = LineRoot.Global.Key("Error").Format("ErrorCode = 0x{0:X8}"); #endregion Snippet_7c } { #region Snippet_7c2 int code = 0x100; ILine line = LineRoot.Global.Key("Error").Format($"ErrorCode = 0x{code:X8}"); #endregion Snippet_7c2 Console.WriteLine(line); } { #region Snippet_7d ILine line = LineRoot.Global.Key("Hello").Text("Hello World"); #endregion Snippet_7d } { #region Snippet_7e ILine line = LineRoot.Global.Section("Section").Key("Success") .Format("Success") // Add inlining to the root culture "" .Inline("Culture:en", "Success") // Add inlining to culture "en" .Inline("Culture:fi", "Onnistui") // Add inlining to culture "fi" .Inline("Culture:sv", "Det funkar"); // Add inlining to culture "sv" #endregion Snippet_7e } { #region Snippet_7f ILine line = LineRoot.Global.Section("Section").Key("Success") .Format("Success") // Add inlining to the root culture "" .en("Success") // Add inlining to culture "en" .fi("Onnistui") // Add inlining to culture "fi" .sv("Det funkar"); // Add inlining to culture "sv" #endregion Snippet_7f } // Enumerations { #region Snippet_7l ILine carFeature = LineRoot.Global.Assembly("docs").Type <CarFeature>().Format("{0}"); #endregion Snippet_7l } { #region Snippet_7l2 ILine carFeature = LineRoot.Global.Assembly("docs").Type <CarFeature>().InlineEnum <CarFeature>().Format("{0}"); #endregion Snippet_7l2 } { #region Snippet_7l3 IAssetSource assetSource = LineReaderMap.Default.FileAssetSource(@"ILine\CarFeature.ini"); LineRoot.Builder.AddSource(assetSource).Build(); #endregion Snippet_7l3 } { #region Snippet_7m ILine carFeature = LineRoot.Global.Assembly("docs").Type <CarFeature>() .InlineEnum <CarFeature>() .InlineEnum(CarFeature.Electric, "fi", "Sähkö") .InlineEnum(CarFeature.Petrol, "fi", "Bensiini") .InlineEnum(CarFeature.NaturalGas, "fi", "Maakaasu") .InlineEnum(CarFeature.TwoDoors, "fi", "Kaksiovinen") .InlineEnum(CarFeature.FourDoors, "fi", "Neliovinen") .InlineEnum(CarFeature.FiveDoors, "fi", "Viisiovinen") .InlineEnum(CarFeature.Red, "fi", "Punainen") .InlineEnum(CarFeature.Black, "fi", "Musta") .InlineEnum(CarFeature.White, "fi", "Valkoinen") .Format("{0}"); #endregion Snippet_7m CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(""); #region Snippet_7m2 Console.WriteLine(carFeature.Key(CarFeature.Petrol)); Console.WriteLine(carFeature.Key(CarFeature.Petrol).Culture("fi")); Console.WriteLine(carFeature.Key(CarFeature.Petrol).Culture("sv")); #endregion Snippet_7m2 #region Snippet_7m3 CarFeature features = CarFeature.Petrol | CarFeature.FiveDoors | CarFeature.Black; Console.WriteLine(carFeature.Value(features)); #endregion Snippet_7m3 #region Snippet_7m5 Console.WriteLine(carFeature.Formulate($"{CarFeature.Petrol | CarFeature.Black:|}").Culture("fi")); Console.WriteLine(carFeature.Formulate($"{CarFeature.Petrol | CarFeature.Black: |}").Culture("fi")); #endregion Snippet_7m5 } { #region Snippet_7m6 ILine carFeature = LineRoot.Global.Assembly("docs").Type <CarFeature>().InlineEnum <CarFeature>() .InlineEnum(CarFeature.Electric, "de", "Elektroauto") .InlineEnum(CarFeature.Petrol, "de", "Benzinwagen") .InlineEnum(CarFeature.NaturalGas, "de", "Erdgasauto") .InlineEnum(CarFeature.TwoDoors, "de", "Zweitürig") .InlineEnum(CarFeature.FourDoors, "de", "Viertürig") .InlineEnum(CarFeature.FiveDoors, "de", "Fünftürige") .InlineEnum(CarFeature.Red, "de", "Rot") .InlineEnum(CarFeature.Black, "de", "Schwartz") .InlineEnum(CarFeature.White, "de", "Weiß") .Format("{0}"); ILine message = LineRoot.Global.Assembly("docs").Type("MyClass").Key("Msg") .Format("Your car has following features: {0}") .de("Ihr Auto hat folgende Eigenschaften: {0}"); Console.WriteLine(message.Value(CarFeature.Petrol | CarFeature.Red | CarFeature.TwoDoors).Culture("fi")); Console.WriteLine(message.Value(CarFeature.Petrol | CarFeature.Red | CarFeature.TwoDoors).Culture("sv")); Console.WriteLine(message.Value(CarFeature.Petrol | CarFeature.Red | CarFeature.TwoDoors).Culture("de")); #endregion Snippet_7m6 } { #region Snippet_7m7 ILine carFeature = LineRoot.Global.Assembly("docs").Type <CarFeature>().InlineEnum <CarFeature>() .InlineEnum(CarFeature.Electric, "de", "Elektroauto") .InlineEnum(CarFeature.Petrol, "de", "Benzinwagen") .InlineEnum(CarFeature.NaturalGas, "de", "Erdgasauto") .InlineEnum(CarFeature.TwoDoors, "de", "Zweitürig") .InlineEnum(CarFeature.FourDoors, "de", "Viertürig") .InlineEnum(CarFeature.FiveDoors, "de", "Fünftürige") .InlineEnum(CarFeature.Red, "de", "Rot") .InlineEnum(CarFeature.Black, "de", "Schwartz") .InlineEnum(CarFeature.White, "de", "Weiß") .Format("{0}"); ILine message = LineRoot.Global.Assembly("docs").Type("MyClass").Key("Msg") .Format("Your car has following features: {0}") .de("Ihr Auto hat folgende Eigenschaften: {0}"); CarFeature features = CarFeature.Petrol | CarFeature.Red | CarFeature.TwoDoors; // Inlined enum strings don't work as Enum (unless tool is used) Console.WriteLine(message.Value(features).Culture("de")); // But works when ILine reference is used. Console.WriteLine(message.Value(carFeature.Value(features)).Culture("de")); #endregion Snippet_7m7 } { ILine carFeature = LineRoot.Global.Assembly("docs").Type <CarFeature>().InlineEnum <CarFeature>(); CarFeature features = CarFeature.Petrol | CarFeature.FiveDoors | CarFeature.Black; Console.WriteLine(carFeature.Formulate($"{features:g}").Culture("fi")); Console.WriteLine(carFeature.Formulate($"{features:G}").Culture("fi")); Console.WriteLine(carFeature.Formulate($"{features:f}").Culture("fi")); Console.WriteLine(carFeature.Formulate($"{features:F}").Culture("fi")); Console.WriteLine(carFeature.Formulate($"{features:d}").Culture("fi")); Console.WriteLine(carFeature.Formulate($"{features:D}").Culture("fi")); Console.WriteLine(carFeature.Formulate($"{features:x}").Culture("fi")); Console.WriteLine(carFeature.Formulate($"{features:X}").Culture("fi")); } // 8. Values { #region Snippet_8a ILine line = LineRoot.Global.Key("Error").Format("ErrorCode={0}").Value(0x100); LineString result = line.ResolveString(); #endregion Snippet_8a } { #region Snippet_8b #endregion Snippet_8b } { #region Snippet_8c #endregion Snippet_8c } { #region Snippet_8d #endregion Snippet_8d } { #region Snippet_8e #endregion Snippet_8e } { #region Snippet_8f #endregion Snippet_8f } { #region Snippet_8g #endregion Snippet_8g } { #region Snippet_8h #endregion Snippet_8h } // 10. Resources { #region Snippet_10a ILine line = LineRoot.Global.Key("Error").Resource(new byte[] { 1, 2, 3 }); LineResourceBytes result = line.ResolveBytes(); #endregion Snippet_10a } { #region Snippet_10b ILine line = LineRoot.Global.Key("Error").Resource(new byte[] { 1, 2, 3 }); using (LineResourceStream result = line.ResolveStream()) { } #endregion Snippet_10b } { #region Snippet_10c #endregion Snippet_10c } { #region Snippet_10d #endregion Snippet_10d } { #region Snippet_10e #endregion Snippet_10e } { #region Snippet_10f #endregion Snippet_10f } { #region Snippet_10g #endregion Snippet_10g } { #region Snippet_10h #endregion Snippet_10h } }
protected void Term() { bool minus = false; bool not = false; SkipWhite(); if (Next == Grammar.MINUS) { Consume(Grammar.MINUS); minus = true; } else if (Next == Grammar.EXCL) { Consume(Grammar.EXCL); not = true; } if (IsDigit(Next)) { GetNum(); short value = short.Parse(Current); if (minus) { value = (short)-value; } emitter.Literal(value); } else if (IsString(Next)) { GetString(); if (StringResolver == null) { throw new Exception("Tried to resolve a string without resolver set"); } short sindex = StringResolver.GetStringID(Current); emitter.Literal(sindex); } else { GetName(); if (IsBuiltinFunction(Current)) { BuiltinFunctionCall(); } else if (IsInternal(Current)) { InternalTerm(); } else if (IsScript(Current)) { ScriptTerm(); } else if (variables.ContainsKey(Current)) { VarTerm(); } else { if (NameResolver == null) { throw new Exception("Tried to resolve a name without resolver set"); } short id = NameResolver.GetNameValue(Current); emitter.Literal(id); } } if (not) { emitter.Negate(); } SkipWhite(); }