public void Resolve(ISyntaxTree tree) { IList <string> identifiers = LinkingCache[tree.Head.Identifier]; foreach (var identifier in identifiers) { if (!LinkingCache.ContainsKey(identifier)) { Console.WriteLine($"Linking Failed for type <{identifier}>"); tree.TraverseTree((node, lon) => { if (PropertyTsInfo.IsAPropertyOrField(ref node) && PropertyTsInfo.RawTypeIs(ref node, identifier)) { PropertyTsInfo.SetAsUnknownType(ref node); } }); continue; } var refNode = new GenericNode { Kind = Kind.Import, Identifier = identifier }; for (int i = 0; i < ImportTsInfo.TokenSize; i++) { var token = new GenericToken(); token.Text = ImportTsInfo.TokenContent(i, identifier); token.Trivia = ImportTsInfo.GetTokenTrivia(i); refNode.Tokens.Add(token); } tree.Prepend(refNode, Level.Sibling); } }
/// <inheritdoc cref="ITranslator.Compile"/> private void InternalCompile() { string text = File.ReadAllText(CurrentFilePath, Encoding.UTF8); Tree = CSharpSyntaxTree.ParseText(text, CSharpParseOptions.Default); Root = Tree.GetRoot(); ThrowIfNotCSharpLang(); SyntaxNode namespaceNode = GetNamespaceNode(); SyntaxNode classOrInterfaceNode = GetDeclarationNode(namespaceNode); IEnumerable <SyntaxNode> children = GetFilteredNodes(classOrInterfaceNode); CreateCSharpHeadNode(classOrInterfaceNode); foreach (var child in children) { InsertNodeInTree(child); } ISyntaxTree head = Generator.GetSyntaxTree(Head); Builder.Add(new FileBuilder(CurrentOutputPath, Configuration.OverrideExistingFile)); Builder.Last().Build(head); }
public ISyntaxTree GetSyntaxTree() { ExpressionParser expressionParser = new ExpressionParser(Line[4]); expressionParser.Normalize(); Line[4].Element = expressionParser.Line[0].Element; //ISyntaxTree blockTrue = new AssignTree(); //blockTrue.Context = Line[3].Element; //blockTrue.Childs.Add(expressionParser.GetSyntaxTree()); ISyntaxTree blockTrue = Line[3].ElementReference.GetParser().GetSyntaxTree(); //ISyntaxTree blockFalse = new AssignTree(); //if (Line.Count > 5) //{ // expressionParser = new ExpressionParser(Line[6]); // expressionParser.Normalize(); // Line[6] = expressionParser.Line[0]; // blockFalse.Context = Line[5]; // blockFalse.Childs.Add(expressionParser.GetSyntaxTree()); //} //else blockFalse = null; ISyntaxTree blockFalse = new FunctionTree(); //if (Line[5].ElementReference != null) if (Line[4].ElementReference != null) { //ISyntaxTree function = Line[5].ElementReference.GetParser().GetSyntaxTree(); ISyntaxTree function = Line[4].ElementReference.GetParser().GetSyntaxTree(); blockFalse.Childs.Add(function); } else { blockFalse = null; } expressionParser = new ExpressionParser(Line[0]); expressionParser.Normalize(); Line[0] = expressionParser.Line[0]; ISyntaxTree leftOp = expressionParser.GetSyntaxTree(); expressionParser = new ExpressionParser(Line[2]); expressionParser.Normalize(); Line[2] = expressionParser.Line[0]; ISyntaxTree rightOp = expressionParser.GetSyntaxTree(); ISyntaxTree condition = GetConditionTree(Line[1].Element); condition.Childs.Add(leftOp); condition.Childs.Add(rightOp); ISyntaxTree tree = new IFTree(blockTrue, blockFalse); tree.Childs.Add(condition); return(tree); }
public CodeGenerator(ISyntaxTree root) { this.root = root; compiledMethod = new DynamicMethod("Sample", typeof(int), new[] { typeof(int) }); il = compiledMethod.GetILGenerator(); }
protected SyntaxNode(SyntaxNode parent, ISyntaxTree tree, IToken token) : this() { Parent = parent ?? throw new ArgumentNullException(nameof(parent)); Tree = tree ?? throw new ArgumentNullException(nameof(tree)); Syntax = token?.Text ?? throw new ArgumentNullException(nameof(token)); Start = new Location(token, false); End = new Location(token, true); Trivia = true; }
private void CompileId(ISyntaxTree tree) { if (tree.Children.Any()) { CompileCall(tree); return; } il.Emit(OpCodes.Ldarg_0); }
/// <summary> /// Creates a qualified exeuction plan that can be executed to fulfill a user's request. /// </summary> /// <param name="syntaxTree">The syntax tree that was lexed from the original source supplied by a user.</param> /// <returns>Task<IGraphQueryPlan>.</returns> public async Task <IGraphQueryPlan> CreatePlan(ISyntaxTree syntaxTree) { Validation.ThrowIfNull(syntaxTree, nameof(syntaxTree)); var queryPlan = this.CreatePlanInstance(); // ------------------------------------------ // Step 1: Parse the document. // ------------------------------------------ // Validate that the lexed syntax tree is internally consistant and // is valid in context to the schema it is to be processed against // at the same time build up the query document in anticipation that it will be correct // as in production this should mostly be the case. Should it fail return the messages // on an empty query plan. // ------------------------------------------ var document = _documentGenerator.CreateDocument(syntaxTree); this.InspectSyntaxDepth(document); queryPlan.MaxDepth = document.MaxDepth; queryPlan.Messages.AddRange(document.Messages); if (!queryPlan.IsValid) { return(queryPlan); } // ------------------------------------------ // Step 2: Plan Construction // ------------------------------------------ // The document is garunteed to be syntactically correct and, barring anything user related (variable data, custom code etc.), // will resolve to produce a result. Using the correct operation (or the anon operation), extract the resolvers for all possible concrete // types needed to fulfill the user's request and generate a query plan that can be executed to fulfill the request. // ------------------------------------------ var generator = new ExecutableOperationGenerator(_schema); foreach (var operation in document.Operations.Values) { var executableOperation = await generator.Create(operation).ConfigureAwait(false); queryPlan.AddOperation(executableOperation); // estimate the complexity for any successfully parsed operations if (executableOperation.Messages.IsSucessful) { var complexity = _complexityCalculator.Calculate(executableOperation); if (complexity > queryPlan.EstimatedComplexity) { queryPlan.EstimatedComplexity = complexity; } } } this.InspectQueryPlanComplexity(queryPlan); return(queryPlan); }
/// <summary> /// Interpretes the syntax tree and generates a contextual document that can be transformed into /// a query plan. /// </summary> /// <param name="syntaxTree">The syntax tree to create a document for.</param> /// <returns>IGraphQueryDocument.</returns> public IGraphQueryDocument CreateDocument(ISyntaxTree syntaxTree) { Validation.ThrowIfNull(syntaxTree, nameof(syntaxTree)); // -------------------------------------------- // Step 1: Parse the syntax tree // -------------------------------------------- // Walk all nodes of the tree and on a "per node" basis perform actions // that are required of that node be it a specification validation rule or // an incremental addition to the document context being built. // // Note: All packages are rendered and then named fragment nodes are processed first // as they are required by any operations referenced elsewhere in the document // -------------------------------------------- var nodeProcessor = new DocumentConstructionRuleProcessor(); var docContext = new DocumentContext(_schema); var nodeContexts = new List <DocumentConstructionContext>(); foreach (var node in syntaxTree.Nodes) { var nodeContext = docContext.ForTopLevelNode(node); nodeContexts.Add(nodeContext); } nodeContexts.Sort(new TopLevelNodeProcessingOrder()); var completedAllSteps = nodeProcessor.Execute(nodeContexts); // -------------------------------------------- // Step 2: Validate the document parts // -------------------------------------------- // Inspect the document parts that were generated during part one and, as a whole, run additional // validation rules and perform final changes before constructing the final document. // e.g. ensure all fragments were called, all variables were referenced at least once etc. // -------------------------------------------- if (completedAllSteps) { var documentProcessor = new DocumentValidationRuleProcessor(); var validationContexts = new List <DocumentValidationContext>(); foreach (var part in docContext.Children) { var partContext = new DocumentValidationContext(docContext, part); validationContexts.Add(partContext); } documentProcessor.Execute(validationContexts); } // -------------------------------------------- // Step 3: Build out the final document // -------------------------------------------- return(docContext.ConstructDocument()); }
private TokenLocation GetLocation(ISyntaxTree node) { var interval = node.SourceInterval; if (interval.Length == 0) { return(new TokenLocation(tokenStream.Get(0), tokenStream.Get(0))); } var start = tokenStream.Get(interval.a); var end = tokenStream.Get(interval.b); return(new TokenLocation(start, end)); }
public CommentTrivia(SyntaxNode parent, ISyntaxTree tree, IToken token, bool multiLine) : base(parent, tree, token) { var text = token.Text; if (multiLine) { LineCount = (ulong)text.Where(x => x == '\n').Count() + 1; Comment = text.Substring(2, text.Length - 4); } else { Comment = text.Substring(2); } }
private void CompileCall(ISyntaxTree tree) { var callee = GetMethod(tree.Token.Value, tree.Children.Count()); foreach (var child in tree.Children) { if (child.Token.Type == TokenType.Identifier) { CompileId(child); } else { CompileInt(child); } } il.Emit(OpCodes.Call, callee); }
public AbstractSyntaxTree(ISyntaxTree left, ISyntaxTree right) : this() { this.Childs.Add(left); this.Childs.Add(right); }
public EqualTree(ISyntaxTree left, ISyntaxTree right) : base(left, right) { }
public GreaterOrEqual(ISyntaxTree left, ISyntaxTree right) : base(left, right) { }
public DivisionTree(ISyntaxTree left, ISyntaxTree right) : base(left, right) { }
public AddTree(ISyntaxTree left, ISyntaxTree right) : base(left, right) { }
private ISyntaxTree CompressOperation(List <string> Operands, List <ISyntaxTree> exp, ISyntaxTree op, int index, ref int treeNum) { int expNum; SymbolLex symbolLex = new SymbolLex(); if (symbolLex.GetSymbolType(Operands[Operands.Count - index][0]) == SymbolType.Digit) { op.Childs.Add(new ConstTree(Convert.ToInt32(Operands[Operands.Count - index]))); } else if (symbolLex.GetSymbolType(Operands[Operands.Count - index][0]) == SymbolType.SquareBracket) { if (index == 1) { expNum = GetExpressionNum(Operands[Operands.Count - index]); op.Childs.Add(exp[expNum]); treeNum--; } else { int expNum2 = GetExpressionNum(Operands[Operands.Count - index]); op.Childs.Add(exp[expNum2]); treeNum--; } } else { op.Childs.Add(new VariableTree(Operands[Operands.Count - index])); } return(op); }
public void Build(ISyntaxTree tree) { _extension = tree.Extension; tree.TraverseTree(OnNextNode); }
public SubtractionTree(ISyntaxTree left, ISyntaxTree right) : base(left, right) { }
public MultiplicationTree(ISyntaxTree left, ISyntaxTree right) : base(left, right) { }
protected void Build(SyntaxNode left, SyntaxNode right, ISyntaxTree tree, IList <IToken> tokens) { if (tree == null) { throw new ArgumentNullException(nameof(tree)); } if (tokens == null) { throw new InvalidOperationException(nameof(tokens)); } if (Tree != null) { throw new InvalidOperationException("Tree has already been set!"); } Tree = tree; SyntaxNode childLeft = left; for (var I = 0; I < children.Count; ++I) { var child = children[I]; var childRight = I < children.Count - 1 ? children[I + 1] : right; child.Build(childLeft, childRight, tree, tokens); childLeft = child; } var leftmost = (left?.stopTokenIndex ?? -1) + 1; var rightmost = (right?.startTokenIndex ?? tokens.Count); var offset = 0; int RecheckSkipRange() => offset >= children.Count ? rightmost : children[offset].startTokenIndex; var check = RecheckSkipRange(); for (var I = leftmost; I < rightmost; ++I) { if (I == check) { I = children[offset].stopTokenIndex + 1; ++offset; check = RecheckSkipRange(); continue; } SyntaxNode newNode; var token = tokens[I]; switch (token.Type) { case TypemakerLexer.NEWLINES: newNode = new WhitespaceTrivia(WhitespaceType.Newlines, this, tree, token); break; case TypemakerLexer.TABS: newNode = new WhitespaceTrivia(WhitespaceType.Tabs, this, tree, token); break; case TypemakerLexer.SPACES: newNode = new WhitespaceTrivia(WhitespaceType.Spaces, this, tree, token); break; case TypemakerLexer.SINGLE_LINE_COMMENT: newNode = new CommentTrivia(this, tree, token, false); break; case TypemakerLexer.DELIMITED_COMMENT: newNode = new CommentTrivia(this, tree, token, true); break; default: continue; throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Invalid trivia token {0} ({1})!", token.Type, token.Text)); } children.Insert(offset, newNode); ++offset; } }
public LessTree(ISyntaxTree left, ISyntaxTree right) : base(left, right) { }
public GreaterTree(ISyntaxTree left, ISyntaxTree right) : base(left, right) { }
public AbstractSyntaxTree(string name, ISyntaxTree exp) : this() { this.Context = name; this.Childs.Add(exp); }
public WhitespaceTrivia(WhitespaceType type, SyntaxNode syntaxNode, ISyntaxTree tree, IToken token) : base(syntaxNode, tree, token) { Type = type; Amount = (ulong)token.Text.Length; }
public AssignTree(string name, ISyntaxTree exp) : base(name, exp) { }
private void CompileInt(ISyntaxTree tree) { int value = Convert.ToInt32(tree.Token.Value); il.Emit(OpCodes.Ldc_I4, value); }
public void AddChild(ISyntaxTree node) { children.Add(node); }
public ISyntaxTree GetSyntaxTree() { List <string> Operands = new List <string>(); List <string> Functions = new List <string>(); List <ISyntaxTree> exp = new List <ISyntaxTree>(); SymbolLex symbolLex = new SymbolLex(); int treeNum = 0; bool arifmetic = false; string var = string.Empty; if (Context == string.Empty) { exp.Add(new ConstTree("")); } for (int i = 0; i < Context.Length; i++) { if (symbolLex.GetSymbolType(Context[i]) != SymbolType.Arifmetic && symbolLex.GetSymbolType(Context[i]) != SymbolType.Bracket) { var += Context[i]; } else { arifmetic = true; } if (arifmetic || i == Context.Length - 1) { if (var != string.Empty) { Operands.Add(var); } if (i != Context.Length - 1 || Context[i] == ')') { Functions.Add(Context[i].ToString()); } var = string.Empty; arifmetic = false; if (Functions.Count == 0 && i == Context.Length - 1) { exp.Add(OperationAnalyse(Operands, exp, 1)); break; } if (symbolLex.GetSymbolType(Functions[Functions.Count - 1][0]) != SymbolType.Bracket) { if (Functions.Count > 1) { int expNum = treeNum; int valueLast = GetOperationValue(Functions[Functions.Count - 2][0]); int valueNew = GetOperationValue(Functions[Functions.Count - 1][0]); if (valueNew >= valueLast) { ISyntaxTree op = GetOperationTree(Functions[Functions.Count - 2][0]); CompressOperation(Operands, exp, op, 2, ref treeNum); CompressOperation(Operands, exp, op, 1, ref treeNum); Operands.RemoveAt(Operands.Count - 1); Operands.RemoveAt(Operands.Count - 1); Functions.RemoveAt(Functions.Count - 2); Operands.Add("[" + treeNum + "]"); for (int del = expNum; del > treeNum; del--) { exp.RemoveAt(exp.Count - 1); } exp.Insert(treeNum, op); treeNum++; } } } else { if (Functions[Functions.Count - 1][0] != '(') { Functions.RemoveAt(Functions.Count - 1); ISyntaxTree tmpOp = null; for (int j = Functions.Count - 1; j >= 0; j--) { int tmpNum = treeNum; if (Functions[j][0] == '(') { Functions.RemoveAt(Functions.Count - 1); break; } else { ISyntaxTree op = GetOperationTree(Functions[Functions.Count - 1][0]); CompressOperation(Operands, exp, op, 2, ref treeNum); CompressOperation(Operands, exp, op, 1, ref treeNum); Operands.RemoveAt(Operands.Count - 1); Operands.RemoveAt(Operands.Count - 1); Functions.RemoveAt(Functions.Count - 1); Operands.Add("[" + treeNum + "]"); exp.Insert(treeNum, op); treeNum++; if (tmpOp != null) { op.Childs.Add(tmpOp); } tmpOp = op; } } } } } } if (Functions.Count == 0) { return(exp[0]); } ISyntaxTree lastOp = GetOperationTree(Functions[Functions.Count - 1][0]); for (int i = Functions.Count - 1; i >= 0; i--) { ISyntaxTree operation = GetOperationTree(Functions[i][0]); if (i == Functions.Count - 1) { operation.Childs.Add(OperationAnalyse(Operands, exp, 2)); operation.Childs.Add(OperationAnalyse(Operands, exp, 1)); Operands.RemoveAt(Operands.Count - 1); Operands.RemoveAt(Operands.Count - 1); lastOp = operation; } else { operation.Childs.Add(OperationAnalyse(Operands, exp, 1)); Operands.RemoveAt(Operands.Count - 1); Functions.RemoveAt(Functions.Count - 1); operation.Childs.Add(lastOp); lastOp = operation; } } return(lastOp); }