internal static ScriptFile ParseFile(IAddonManager addonManager, string content) { if (addonManager == null) { addonManager = AddonManager.Create(); addonManager.AddAssembly(typeof(Math).Assembly, false); addonManager.AddAssembly(typeof(Enumerable).Assembly, false); } addonManager.AddAssembly(AddonManager.StepBroCoreAssembly, true); // Add StepBro.Core always. ITokenSource lexer = new Grammar.StepBroLexer(new AntlrInputStream(content)); ITokenStream tokens = new CommonTokenStream(lexer); var parser = new SBP(tokens); var file = new ScriptFile(); parser.RemoveErrorListeners(); parser.AddErrorListener(file.Errors as ErrorCollector); parser.BuildParseTree = true; var listener = new StepBroListener(file.Errors as ErrorCollector, addonManager, file); var context = parser.compilationUnit(); var walker = new ParseTreeWalker(); walker.Walk(listener, context); if (file.Errors.ErrorCount > 0) { throw new Exception("PARSING ERRORS: " + file.Errors[0].ToString()); } //file?.InitializeFileVariables(); return(file); }
internal static FileBuilder ParseProcedure( IAddonManager addonManager, string[] usings, Type usingType, params string[] content) { if (addonManager == null) { addonManager = AddonManager.Create(); addonManager.AddAssembly(typeof(Math).Assembly, false); addonManager.AddAssembly(typeof(Enumerable).Assembly, false); } addonManager.AddAssembly(AddonManager.StepBroCoreAssembly, true); // Add StepBro.Core always. if (usingType != null) { addonManager.AddAssembly(usingType.Assembly, false); } var contentBuilder = new StringBuilder(); foreach (var s in content) { contentBuilder.AppendLine(s); } var file = new ScriptFile(); file.AddNamespaceUsing(-1, addonManager.Lookup(null, "System")); file.AddNamespaceUsing(-1, addonManager.Lookup(null, "System.Linq")); file.AddNamespaceUsing(-1, addonManager.Lookup(null, typeof(StepBro.Core.DataReport).Namespace)); if (usings != null) { foreach (var u in usings) { file.AddNamespaceUsing(-1, addonManager.Lookup(null, u)); } } if (usingType != null) { file.AddNamespaceUsing(-1, addonManager.Lookup(null, usingType.Namespace)); } var builder = new FileBuilder(new AntlrInputStream(contentBuilder.ToString()), addonManager, file); builder.Listener.PrepareForExpressionParsing("StepBroFileBuilder.ParseExpression"); var context = builder.Parser.procedureDeclaration(); var walker = new ParseTreeWalker(); walker.Walk(builder.Listener, context); if (file.Errors.ErrorCount > 0) { throw new Exception("PARSING ERRORS"); } return(builder); }
[Ignore] // Enable again when some extension methods actually are added. public void TestExtensionMethods() { var usings = new string[] { "StepBro.Core.Execution" }; IAddonManager addons = AddonManager.Create(); addons.AddAssembly(AddonManager.StepBroCoreAssembly, false); var methods = addons.ListExtensionMethods( typeof(IProcedureReference)).Where(mi => mi.Name == "DynamicInvoke").ToList(); Assert.AreEqual(1, methods.Count); Assert.AreEqual("DynamicInvoke", methods[0].Name); }
public void TestExtensionMethodBrowsing() { var usings = new string[] { "System", "System.Linq" }; IAddonManager addons = AddonManager.Create(); addons.AddAssembly(typeof(System.Linq.Enumerable).Assembly, false); var methods = new List <System.Reflection.MethodInfo>( addons.ListExtensionMethods( typeof(IEnumerable <int>)).Where(mi => mi.Name == "Select")); Assert.IsTrue(methods.Select(mi => mi.Name).Contains("Select")); methods = new List <System.Reflection.MethodInfo>( addons.ListExtensionMethods( typeof(List <int>)).Where(mi => mi.Name == "Select")); Assert.IsTrue(methods.Select(mi => mi.Name).Contains("Select")); }
public static FileBuilder Parse <T>( string returnValueExpression = "0", string statements = "", bool varGeneration = true, bool varDummyClass = false) { string typeName = ""; if (typeof(T) == typeof(long)) { typeName = "int"; } else if (typeof(T) == typeof(double)) { typeName = "decimal"; } else if (typeof(T) == typeof(bool)) { typeName = "bool"; } else if (typeof(T) == typeof(string)) { typeName = "string"; } else if (typeof(T) == typeof(TimeSpan)) { typeName = "timespan"; } else if (typeof(T) == typeof(DateTime)) { typeName = "datetime"; } else if (typeof(T) == typeof(Verdict)) { typeName = "verdict"; } else if (typeof(T) == typeof(DummyDataClass)) { typeName = "DummyDataClass"; } else if (typeof(T) == typeof(DataReport)) { typeName = "DataReport"; } else { throw new NotImplementedException(); } StringBuilder source = new StringBuilder(); source.AppendLine(typeName + " ExpressionProcedure(){"); if (varGeneration || varDummyClass) { AddLocalVariables(source, varDummyClass); } source.AppendLine(statements); source.AppendLine(typeName + " result = " + returnValueExpression + ";"); source.AppendLine("return result;"); source.AppendLine("}"); IAddonManager addons = AddonManager.Create(); addons.AddAssembly(typeof(Enumerable).Assembly, false); addons.AddAssembly(typeof(ExpressionParser).Assembly, false); return(FileBuilder.ParseProcedure( addons, new string[] { typeof(DataReport).Namespace, typeof(DummyClass).FullName, typeof(DummyClass).Namespace }, null, source.ToString())); }