private DustContext SetUpDatabase() { var context = new DustContext(":memory:"); context.Database.Migrate(); return(context); }
private static void Main(string[] args) { Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); DustContext context = new DustContext(); DustRuntimeCompiler compiler = new DustRuntimeCompiler(context); string input; while ((input = Console.ReadLine()) != "exit") { AntlrInputStream inputStream = new AntlrInputStream(input); DustLexer lexer = new DustLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); DustParser parser = new DustParser(commonTokenStream); DustVisitor visitor = new DustVisitor(context); Module module = (Module)visitor.VisitModule(parser.module()); object result = compiler.Compile(module).Value; bool hasErrored = false; foreach (Error error in context.ErrorHandler.Errors) { hasErrored = true; Console.WriteLine(error); } if (result != null && hasErrored == false) { Console.WriteLine(result); } } }
static void Main(string[] args) { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddUserSecrets <Program>() .AddEnvironmentVariables(); var configuration = builder.Build(); var csBuilder = new NpgsqlConnectionStringBuilder(configuration.GetConnectionString("DustDatabase")); csBuilder.ApplicationName = "DMN DataSyncService"; var optionsBuilder = new DbContextOptionsBuilder <DustContext>(); optionsBuilder.UseNpgsql(csBuilder.ToString()); using (var ctx = new DustContext(optionsBuilder.Options)) { foreach (var sensor in ctx.Sensors) { Console.WriteLine($"Sensor ({sensor.Id}): {sensor.Name}@[{sensor.Latitude}, {sensor.Longitude}]"); } } }
public SignatureHelp GetSignatureHelp(TextDocument document, Position position) { string text = document.Text; int index = document.GetPosition(position); string[] lines = text.Split('\n'); string line = lines[position.Line]; int startIndex = line.Substring(0, position.Character).LastIndexOf("(", StringComparison.Ordinal); if (startIndex != -1) { string functionName = line.Substring(0, startIndex).Trim().Split(" ").Last(); // Remove the current line because it might contain errors. lines[position.Line] = ""; DustContext currentContext = document.GetContextAtPosition(position, Project.CompileFile(string.Join('\n', lines)).GlobalContext); Function function = currentContext.GetFunction(functionName); StringBuilder labelBuilder = new StringBuilder($"{function.Name}("); if (function.Parameters.Length > 0) { for (int i = 0; i < function.Parameters.Length; i++) { labelBuilder.Append($"{function.Parameters[i].Identifier.Name}: any"); if (i != function.Parameters.Length - 1) { labelBuilder.Append(", "); } } } labelBuilder.Append("): any"); return(new SignatureHelp { ActiveParameter = line.Substring(startIndex, line.IndexOf(")", startIndex, StringComparison.Ordinal) - startIndex).Count(character => character == ','), ActiveSignature = 0, Signatures = new[] { new SignatureInformation { Label = labelBuilder.ToString(), Parameters = function.Parameters.Select(parameter => new ParameterInformation { Label = parameter.Identifier.Name + ": any" }).ToArray() } } }); } return(new SignatureHelp()); }
public SensorsController(DustContext ctx) { if (ctx == null) { throw new ArgumentNullException(nameof(ctx)); } _ctx = ctx; }
public ClientConfigurationController(DustContext ctx) { if (ctx == null) { throw new ArgumentNullException(nameof(ctx)); } _ctx = ctx; }
public Hover GetHover(TextDocument document, Position position) { string word = document.GetWordAtPosition(position); DustContext context = document.GetContextAtPosition(position, Project.CompileFile(document.Text).GlobalContext); string content = ""; if (context.ContainsPropety(word)) { content = context.GetProperty(word).GetDetail(); } if (context.ContainsFunction(word)) { content = context.GetFunction(word).GetDetail(); } if (string.IsNullOrEmpty(content) == false) { return(new Hover { Contents = (StringOrObject <MarkedString>) $@"```dust {content} ```" }); } return(new Hover()); /* * * string name = document.GetWordAtPosition(position); * string[] lines = document.Text.Split('\n'); * * lines[position.Line] = ""; * * DustContext context = document.GetContextAtPosition(position, Project.CompileFile(string.Join('\n', lines))); * * Function function = context.GetFunction(name); * * if (function != null) * { * return new Hover * { * Contents = new StringOrObject<MarkedString>(new MarkedString * { * Value = function.GetDetail(), * Language = "dust" * }) * }; * } * * return new Hover(); */ }
public Function(string name, FunctionModifier[] modifiers, FunctionParameter[] parameters, Statement[] statements, DustType returnType, DustContext context, Range range) : base(range) { Name = name; Parameters = parameters; Modifiers = modifiers; Statements = statements; ReturnType = returnType; Context = context; }
public static void RegisterDatabase(string databasePath) { Locator.CurrentMutable.RegisterLazySingleton <DustContext>(() => { var dustContext = new DustContext(databasePath); //dustContext.Database.EnsureDeleted(); dustContext.Database.Migrate(); dustContext.ChangeTracker.AutoDetectChangesEnabled = false; return(dustContext); }); }
public static CompileResult <object> CompileFile(string text) { AntlrInputStream inputStream = new AntlrInputStream(text); DustLexer lexer = new DustLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); DustParser parser = new DustParser(commonTokenStream); DustContext context = new DustContext(); DustVisitor visitor = new DustVisitor(context); DustRuntimeCompiler compiler = new DustRuntimeCompiler(context); Module module = (Module)visitor.VisitModule(parser.module()); return(compiler.Compile(module)); }
public CompileResult(Module module, DustContext globalContext, T value) { Module = module; GlobalContext = globalContext; Value = value; }
protected DustBaseCompiler(DustContext compilerContext) { CompilerContext = compilerContext; }
public List <CompletionItem> GetCompletions(TextDocument document, Position position) { string[] lines = document.Text.Split('\n'); string word = lines[position.Line].Substring(0, position.Character).Trim().Split(" ").Last(); List <CompletionItem> completions = new List <CompletionItem>(); bool found = false; foreach (KeyValuePair <string[], CompletionItem[]> completion in keywordCompletions) { foreach (string entry in completion.Key) { if (word == entry) { found = true; completions.AddRange(completion.Value); break; } } } if (found == false) { // Remove the current line because it might contain errors. lines[position.Line] = ""; DustContext globalContext = Project.CompileFile(string.Join('\n', lines)).GlobalContext; DustContext currentContext = document.GetContextAtPosition(position, globalContext); currentContext.Functions.Union(globalContext.Functions).DistinctBy(function => function.Name).ToList().ForEach(function => completions.Add(new CompletionItem { Label = function.Name, Kind = CompletionItemKind.Function, Detail = function.GetDetail() })); currentContext.Properties.Union(globalContext.Properties).ToList().ForEach(property => completions.Add(new CompletionItem { Label = property.Name, Kind = CompletionItemKind.Variable, Detail = property.GetDetail() })); completions.AddRange(new[] { new CompletionItem { Label = "let", Kind = CompletionItemKind.Keyword }, new CompletionItem { Label = "public", Kind = CompletionItemKind.Keyword }, new CompletionItem { Label = "internal", Kind = CompletionItemKind.Keyword }, new CompletionItem { Label = "private", Kind = CompletionItemKind.Keyword } }); } return(completions); }
public static DustContext GetContextAtPosition(this TextDocument document, Position position, DustContext globalContext) { Tree <BraceMatch> tree = new Tree <BraceMatch>(); Stack <BraceMatch> stack = new Stack <BraceMatch>(); string text = document.Text; int index = text.GetPosition(position); for (int i = 0; i < text.Length; i++) { char character = text[i]; if (character == '{') { if (tree.LastAdded()?.Value.End == -1) { tree.LastAdded().AddChild(new BraceMatch(i, -1)); } else { tree.Add(new BraceMatch(i, -1)); } stack.Push(new BraceMatch(i, -1)); } if (character == '}') { LinkedListNode <TreeNode <BraceMatch> > node = tree.Find(new TreeNode <BraceMatch>(stack.Pop())); tree.Modify(node.Value, new BraceMatch(node.Value.Value.Start, i)); } } BraceMatch current = tree.Find(node => node.Value.Start <= index && node.Value.End >= index)?.Value.Value; return(current == null ? globalContext : globalContext.Children[tree.IndexOf(new TreeNode <BraceMatch>(current))]); }
public DustDataService(DustContext dustContext = null) { _dustContext = dustContext ?? Locator.Current.GetService <DustContext>(); }
public FunctionDeclaration(string name, FunctionModifier[] modifiers, FunctionParameter[] parameters, Statement[] statements, DustType returnType, DustContext context, Range range) : base(range) { Function = new Function(name, modifiers, parameters, statements, returnType, context, range); }
public DustRuntimeCompiler(DustContext compilerContext) : base(compilerContext) { }