public void BindingTokenizer_ExpressionWithBraces() { var tokenizer = new BindingTokenizer(); var results = tokenizer.Parse("Binding Model.Command(Id, Address.Street)").ToList(); Assert.AreEqual(11, results.Count); Assert.IsInstanceOfType(results[1], typeof(BindingTextToken)); Assert.AreEqual("Model", ((BindingTextToken)results[1]).Text); Assert.IsInstanceOfType(results[2], typeof(BindingDotToken)); Assert.IsInstanceOfType(results[3], typeof(BindingTextToken)); Assert.AreEqual("Command", ((BindingTextToken)results[3]).Text); Assert.IsInstanceOfType(results[4], typeof(BindingOpenBraceToken)); Assert.IsInstanceOfType(results[5], typeof(BindingTextToken)); Assert.AreEqual("Id", ((BindingTextToken)results[5]).Text); Assert.IsInstanceOfType(results[6], typeof(BindingCommaToken)); Assert.IsInstanceOfType(results[7], typeof(BindingTextToken)); Assert.AreEqual("Address", ((BindingTextToken)results[7]).Text); Assert.IsInstanceOfType(results[8], typeof(BindingDotToken)); Assert.IsInstanceOfType(results[9], typeof(BindingTextToken)); Assert.AreEqual("Street", ((BindingTextToken)results[9]).Text); Assert.IsInstanceOfType(results[10], typeof(BindingCloseBraceToken)); }
protected virtual IAbstractDirective ProcessImportDirective(DothtmlDirectiveNode directiveNode) { var tokenizer = new BindingTokenizer(); tokenizer.Tokenize(directiveNode.ValueNode.Text); var parser = new BindingParser() { Tokens = tokenizer.Tokens }; var valueSyntaxRoot = parser.ReadDirectiveValue(); if (!parser.OnEnd()) { directiveNode.AddError($"Unexpected token: {parser.Peek()?.Text}."); } BindingParserNode alias = null; BindingParserNode name = null; if (valueSyntaxRoot is BinaryOperatorBindingParserNode) { var assigment = valueSyntaxRoot.CastTo <BinaryOperatorBindingParserNode>(); alias = assigment.FirstExpression; name = assigment.SecondExpression; } else { name = valueSyntaxRoot; } return(treeBuilder.BuildImportDirective(directiveNode, alias, name)); }
private static BindingParserNode Parse(string expression) { var tokenizer = new BindingTokenizer(); tokenizer.Tokenize(new StringReader(expression)); var parser = new BindingParser(); parser.Tokens = tokenizer.Tokens; return(parser.ReadExpression()); }
public BindingParser SetupParser(string expression) { var tokenizer = new BindingTokenizer(); tokenizer.Tokenize(expression); var parser = new BindingParser(); parser.Tokens = tokenizer.Tokens; return(parser); }
public void BindingTokenizer_Empty() { var tokenizer = new BindingTokenizer(); var results = tokenizer.Parse("Binding").ToList(); Assert.AreEqual(1, results.Count); Assert.AreEqual("Binding", ((BindingTypeToken)results[0]).BindingTypeName); Assert.AreEqual(0, results[0].SpanPosition.AbsolutePosition); Assert.AreEqual(7, results[0].SpanPosition.Length); }
public void BindingTokenizer_EqualWithText() { var tokenizer = new BindingTokenizer(); var results = tokenizer.Parse("Binding Test=Text").ToList(); Assert.AreEqual(4, results.Count); Assert.IsInstanceOfType(results[1], typeof(BindingTextToken)); Assert.AreEqual("Test", ((BindingTextToken)results[1]).Text); Assert.IsInstanceOfType(results[2], typeof(BindingEqualsToken)); Assert.IsInstanceOfType(results[3], typeof(BindingTextToken)); Assert.AreEqual("Text", ((BindingTextToken)results[3]).Text); }
protected virtual BindingParserNode ParseDirectiveTypeName(DothtmlDirectiveNode directiveNode) { var tokenizer = new BindingTokenizer(); tokenizer.Tokenize(directiveNode.ValueNode.Text); var parser = new BindingParser() { Tokens = tokenizer.Tokens }; var valueSyntaxRoot = parser.ReadDirectiveTypeName(); if (!parser.OnEnd()) { directiveNode.AddError($"Unexpected token: {parser.Peek()?.Text}."); } return(valueSyntaxRoot); }
public Expression Parse(string expression, DataContextStack dataContexts, BindingParserOptions options, params KeyValuePair <string, Expression>[] additionalSymbols) { try { var tokenizer = new BindingTokenizer(); tokenizer.Tokenize(expression); var parser = new BindingParser(); parser.Tokens = tokenizer.Tokens; var node = parser.ReadExpression(); if (!parser.OnEnd()) { throw new BindingCompilationException( $"Unexpected token '{expression.Substring(0, parser.Peek().StartPosition)} ---->{parser.Peek().Text}<---- {expression.Substring(parser.Peek().EndPosition)}'", null, new TokenBase[] { parser.Peek() }); } foreach (var n in node.EnumerateNodes()) { if (n.HasNodeErrors) { throw new BindingCompilationException(string.Join(", ", n.NodeErrors), n); } } var symbols = InitSymbols(dataContexts); symbols = options.AddImportedTypes(symbols, compiledAssemblyCache); symbols = symbols.AddSymbols(options.ExtensionParameters.Select(p => CreateParameter(dataContexts, p.Identifier, p))); symbols = symbols.AddSymbols(additionalSymbols); var visitor = new ExpressionBuildingVisitor(symbols); visitor.Scope = symbols.Resolve(options.ScopeParameter); return(visitor.Visit(node)); } catch (Exception ex) { ex.ForInnerExceptions <BindingCompilationException>(bce => { if (bce.Expression == null) { bce.Expression = expression; } }); throw; } }
protected BindingParserNode ParseImportDirectiveValue(DothtmlDirectiveNode directiveNode) { var tokenizer = new BindingTokenizer(); tokenizer.Tokenize(directiveNode.ValueNode.Text); var parser = new BindingParser() { Tokens = tokenizer.Tokens }; var result = parser.ReadDirectiveValue(); if (!parser.OnEnd()) { directiveNode.AddError($"Unexpected token: {parser.Peek()?.Text}."); } return(result); }
private static List <BindingToken> Tokenize(string expression) { // tokenize var tokenizer = new BindingTokenizer(); tokenizer.Tokenize(expression); var tokens = tokenizer.Tokens; // ensure that whole input was tokenized and that there are no holes var position = 0; foreach (var token in tokens) { Assert.AreEqual(position, token.StartPosition); position += token.Length; } Assert.AreEqual(position, expression.Length); return(tokens); }
public Expression Parse(string expression, DataContextStack dataContexts, BindingParserOptions options) { try { var tokenizer = new BindingTokenizer(); tokenizer.Tokenize(new StringReader(expression)); var parser = new BindingParser(); parser.Tokens = tokenizer.Tokens; var node = parser.ReadExpression(); if (!parser.OnEnd()) { throw new BindingCompilationException( $"Unexpected token '{expression.Substring(0, parser.Peek().StartPosition)} ---->{parser.Peek().Text}<---- {expression.Substring(parser.Peek().StartPosition + parser.Peek().Length)}'", null, new TokenBase[] { parser.Peek() }); } foreach (var n in node.EnumerateNodes()) { if (n.HasNodeErrors) throw new BindingCompilationException(string.Join(", ", n.NodeErrors), n); } var symbols = InitSymbols(dataContexts); symbols = options.AddTypes(symbols); var visitor = new ExpressionBuildingVisitor(symbols); visitor.Scope = symbols.Resolve(options.ScopeParameter); return visitor.Visit(node); } catch (Exception ex) { ex.ForInnerExceptions<BindingCompilationException>(bce => { if (bce.Expression == null) bce.Expression = expression; }); throw; } }
/// <summary> /// Reads the binding. /// </summary> private bool ReadBinding(bool doubleCloseBrace) { // read open brace Assert(Peek() == '{'); Read(); if (!doubleCloseBrace && Peek() == '{') { doubleCloseBrace = true; Read(); } CreateToken(DothtmlTokenType.OpenBinding); SkipWhitespace(); // read binding name if (!ReadIdentifier(DothtmlTokenType.Text, ':')) { CreateToken(DothtmlTokenType.Text, errorProvider: t => CreateTokenError(t, DothtmlTokenType.OpenBinding, DothtmlTokenizerErrors.BindingInvalidFormat)); } else { SkipWhitespace(); } // colon if (Peek() != ':') { CreateToken(DothtmlTokenType.Colon, errorProvider: t => CreateTokenError(t, DothtmlTokenType.OpenBinding, DothtmlTokenizerErrors.BindingInvalidFormat)); } else { Read(); CreateToken(DothtmlTokenType.Colon); SkipWhitespace(); } // binding value if (Peek() == '}') { CreateToken(DothtmlTokenType.Text, errorProvider: t => CreateTokenError(t, DothtmlTokenType.OpenBinding, DothtmlTokenizerErrors.BindingInvalidFormat)); } else { char ch; while ((ch = Peek()) != '}') { if (ch == '\'' || ch == '"') { // string literal - ignore curly braces inside string errorMessage; BindingTokenizer.ReadStringLiteral(Peek, Read, out errorMessage); } else if (ch == NullChar) { CreateToken(DothtmlTokenType.Text, errorProvider: t => CreateTokenError()); CreateToken(DothtmlTokenType.CloseBinding, errorProvider: t => CreateTokenError(t, DothtmlTokenType.OpenBinding, DothtmlTokenizerErrors.BindingNotClosed)); return(true); } else { Read(); } } CreateToken(DothtmlTokenType.Text); } // close brace if (Peek() != '}') { CreateToken(DothtmlTokenType.CloseBinding, errorProvider: t => CreateTokenError(t, DothtmlTokenType.OpenBinding, DothtmlTokenizerErrors.BindingNotClosed)); return(true); } Read(); if (doubleCloseBrace) { if (Peek() != '}') { CreateToken(DothtmlTokenType.CloseBinding, errorProvider: t => CreateTokenError(t, DothtmlTokenType.OpenBinding, DothtmlTokenizerErrors.DoubleBraceBindingNotClosed)); return(true); } Read(); } CreateToken(DothtmlTokenType.CloseBinding); return(true); }
private static List<BindingToken> Tokenize(string expression) { // tokenize var tokenizer = new BindingTokenizer(); tokenizer.Tokenize(new StringReader(expression)); var tokens = tokenizer.Tokens; // ensure that whole input was tokenized and that there are no holes var position = 0; foreach (var token in tokens) { Assert.AreEqual(position, token.StartPosition); position += token.Length; } Assert.AreEqual(position, expression.Length); return tokens; }
public void BindingTokenizer_SingleIdentifier() { var tokenizer = new BindingTokenizer(); var results = tokenizer.Parse("Binding Test").ToList(); Assert.AreEqual(2, results.Count); Assert.IsInstanceOfType(results[1], typeof(BindingTextToken)); Assert.AreEqual("Test", ((BindingTextToken)results[1]).Text); }
public void BindingTokenizer_MultipleExpressions() { var tokenizer = new BindingTokenizer(); var results = tokenizer.Parse("Binding Path=Model.Command(Id, Address.Street), Expr2=Expr3").ToList(); Assert.AreEqual(17, results.Count); Assert.IsInstanceOfType(results[1], typeof(BindingTextToken)); Assert.AreEqual("Path", ((BindingTextToken)results[1]).Text); Assert.IsInstanceOfType(results[2], typeof(BindingEqualsToken)); Assert.IsInstanceOfType(results[3], typeof(BindingTextToken)); Assert.AreEqual("Model", ((BindingTextToken)results[3]).Text); Assert.IsInstanceOfType(results[4], typeof(BindingDotToken)); Assert.IsInstanceOfType(results[5], typeof(BindingTextToken)); Assert.AreEqual("Command", ((BindingTextToken)results[5]).Text); Assert.IsInstanceOfType(results[6], typeof(BindingOpenBraceToken)); Assert.IsInstanceOfType(results[7], typeof(BindingTextToken)); Assert.AreEqual("Id", ((BindingTextToken)results[7]).Text); Assert.IsInstanceOfType(results[8], typeof(BindingCommaToken)); Assert.IsInstanceOfType(results[9], typeof(BindingTextToken)); Assert.AreEqual("Address", ((BindingTextToken)results[9]).Text); Assert.IsInstanceOfType(results[10], typeof(BindingDotToken)); Assert.IsInstanceOfType(results[11], typeof(BindingTextToken)); Assert.AreEqual("Street", ((BindingTextToken)results[11]).Text); Assert.IsInstanceOfType(results[12], typeof(BindingCloseBraceToken)); Assert.IsInstanceOfType(results[13], typeof(BindingCommaToken)); Assert.IsInstanceOfType(results[14], typeof(BindingTextToken)); Assert.AreEqual("Expr2", ((BindingTextToken)results[14]).Text); Assert.IsInstanceOfType(results[15], typeof(BindingEqualsToken)); Assert.IsInstanceOfType(results[16], typeof(BindingTextToken)); Assert.AreEqual("Expr3", ((BindingTextToken)results[16]).Text); }
public void BindingTokenizer_IndexerOnly() { var tokenizer = new BindingTokenizer(); var results = tokenizer.Parse("Binding [13]").ToList(); Assert.AreEqual(5, results.Count); Assert.AreEqual("Binding", ((BindingTypeToken)results[0]).BindingTypeName); Assert.AreEqual(0, results[0].SpanPosition.AbsolutePosition); Assert.AreEqual(8, results[0].SpanPosition.Length); Assert.AreEqual("", ((BindingTextToken)results[1]).Text); Assert.IsInstanceOfType(results[2], typeof(BindingOpenIndexerToken)); Assert.AreEqual("13", ((BindingTextToken)results[3]).Text); Assert.IsInstanceOfType(results[4], typeof(BindingCloseIndexerToken)); }
public void BindingTokenizer_IdentifierWithDotsAndIndexers() { var tokenizer = new BindingTokenizer(); var results = tokenizer.Parse("Binding Test[1].Test2.Test3[Key=\"hello\"]").ToList(); Assert.AreEqual(14, results.Count); Assert.IsInstanceOfType(results[1], typeof(BindingTextToken)); Assert.AreEqual("Test", ((BindingTextToken)results[1]).Text); Assert.IsInstanceOfType(results[2], typeof(BindingOpenIndexerToken)); Assert.IsInstanceOfType(results[3], typeof(BindingTextToken)); Assert.AreEqual("1", ((BindingTextToken)results[3]).Text); Assert.IsInstanceOfType(results[4], typeof(BindingCloseIndexerToken)); Assert.IsInstanceOfType(results[5], typeof(BindingDotToken)); Assert.IsInstanceOfType(results[6], typeof(BindingTextToken)); Assert.AreEqual("Test2", ((BindingTextToken)results[6]).Text); Assert.IsInstanceOfType(results[7], typeof(BindingDotToken)); Assert.IsInstanceOfType(results[8], typeof(BindingTextToken)); Assert.AreEqual("Test3", ((BindingTextToken)results[8]).Text); Assert.IsInstanceOfType(results[9], typeof(BindingOpenIndexerToken)); Assert.IsInstanceOfType(results[10], typeof(BindingTextToken)); Assert.AreEqual("Key", ((BindingTextToken)results[10]).Text); Assert.IsInstanceOfType(results[11], typeof(BindingEqualsToken)); Assert.IsInstanceOfType(results[12], typeof(BindingQuotedTextToken)); Assert.AreEqual("hello", ((BindingTextToken)results[12]).Text); Assert.IsInstanceOfType(results[13], typeof(BindingCloseIndexerToken)); }
public void BindingTokenizer_IdentifierWithDots() { var tokenizer = new BindingTokenizer(); var results = tokenizer.Parse("Binding Test.Test2.Test3").ToList(); Assert.AreEqual(6, results.Count); Assert.IsInstanceOfType(results[1], typeof(BindingTextToken)); Assert.AreEqual("Test", ((BindingTextToken)results[1]).Text); Assert.IsInstanceOfType(results[2], typeof(BindingDotToken)); Assert.IsInstanceOfType(results[3], typeof(BindingTextToken)); Assert.AreEqual("Test2", ((BindingTextToken)results[3]).Text); Assert.IsInstanceOfType(results[4], typeof(BindingDotToken)); Assert.IsInstanceOfType(results[5], typeof(BindingTextToken)); Assert.AreEqual("Test3", ((BindingTextToken)results[5]).Text); }
/// <summary> /// Gets the tokens. /// </summary> private List<BindingToken> GetTokens(string expression) { // add binding type if it is not specified if (!Regex.IsMatch(expression, @"^[a-zA-Z]+\s") && !MappingTable.Keys.Contains(expression)) { expression = DefaultBindingType + " " + expression; } // parse the token stream var tokenizer = new BindingTokenizer(); var tokens = tokenizer.Parse(expression).ToList(); return tokens; }