public void Tokenize(ISemanticModel semanticModel, string code, IContentType contentType) { if (contentType.TypeName == Constants.TstXContentType) { _rootContext = _contexts.Find(nameof(RootContext)); } else if (contentType.TypeName == Constants.TstContentType) { _rootContext = _contexts.Find(nameof(File)); } else { throw new Exception($"Unable to Tokenize unknown ContentType {contentType}"); } _objectLiteralEnds = 0; _isSymbol = false; var context = new Stack <Context>(new[] { _rootContext }); Parse(code, semanticModel, context, 0, 0); if (semanticModel.Tokens.BraceStack.IsBalanced(']') == false) { semanticModel.ErrorTokens.Add(new Token { QuickInfo = "] expected", Start = code.Length, Length = 0 }); } }
private void ParseDollar(Stream stream) { if (stream.Current == '$') { var identifier = GetIdentifier(stream); if (identifier != null) { stream.Advance(identifier.Name.Length); if (identifier.IsCollection) { context.Push(contexts.Find(identifier.Context)); ParseFilter(stream); ParseFileOutput(stream); ParseBlock(stream); // template context.Pop(); ParseBlock(stream); // separator } else if (identifier.IsBoolean) { ParseBlock(stream); // true ParseBlock(stream); // false } else if (identifier.IsParent) { var current = context.Pop(); ParseBlock(stream); // template context.Push(current); } else if (identifier.HasContext) { context.Push(contexts.Find(identifier.Context)); ParseBlock(stream); // template context.Pop(); } } } }
private bool ParseDollar(Stream stream, SemanticModel semanticModel, Stack <Context> context) { if (stream.Current == '$') { var identifier = GetIdentifier(stream, semanticModel, context); if (identifier != null) { if (identifier.IsParent) { var parent = context.Skip(1).FirstOrDefault()?.Name.ToLowerInvariant(); semanticModel.Tokens.Add(Classifications.Property, stream.Position, identifier.Name.Length + 1, identifier.QuickInfo.Replace("$parent", parent)); stream.Advance(identifier.Name.Length); var current = context.Pop(); ParseBlock(stream, semanticModel, context); // template context.Push(current); } else { semanticModel.Tokens.Add(Classifications.Property, stream.Position, identifier.Name.Length + 1, identifier.QuickInfo); stream.Advance(identifier.Name.Length); if (identifier.IsCollection) { context.Push(Contexts.Find(identifier.Context)); ParseFilter(stream, semanticModel, context); ParseBlock(stream, semanticModel, context); // template context.Pop(); ParseBlock(stream, semanticModel, context); // separator } else if (identifier.IsBoolean) { ParseBlock(stream, semanticModel, context); // true ParseBlock(stream, semanticModel, context); // false } else if (identifier.HasContext) { context.Push(Contexts.Find(identifier.Context)); //ParseDot(stream, SemanticModel, Contexts.Find(identifier.Context), context); // Identifier ParseBlock(stream, semanticModel, context); // template context.Pop(); } } return(true); } } return(false); }
public CodeLexer(Contexts contexts) { this.contexts = contexts; this.fileContext = contexts.Find(nameof(File)); }
public TemplateLexer(Contexts contexts) { _contexts = contexts; _fileContext = contexts.Find(nameof(File)); }
public TstXCodeLexer(Contexts contexts) { this.contexts = contexts; this.fileContext = contexts.Find(nameof(RootContext)); }
private static bool ParseLambda(Stream stream, ShadowClass shadowClass, Contexts contexts, ref string template) { if (stream.Current == '$') { var identifier = stream.PeekWord(1); if (identifier != null) { var filter = stream.PeekBlock(identifier.Length + 2, '(', ')'); if (filter != null && stream.Peek(filter.Length + 2 + identifier.Length + 1) == '[') { try { var index = filter.IndexOf("=>", StringComparison.Ordinal); if (index > 0) { var name = filter.Substring(0, index); var contextName = identifier; // Todo: Make the TemplateCodeParser context aware if (contextName == "TypeArguments") contextName = "Types"; else if (contextName.StartsWith("Nested")) contextName = contextName.Remove(0, 6); var type = contexts.Find(contextName)?.Type.FullName; if (type == null) return false; var methodIndex = counter++; shadowClass.AddLambda(filter, type, name, methodIndex); stream.Advance(filter.Length + 2 + identifier.Length); template += $"${identifier}($__{methodIndex})"; return true; } } catch { } } } } return false; }