public void TestAttributeParser() { var data4 = new ParsableData("base KNZ:varchar(len=\"50\", primary_key=\"true\");"); var p4 = new AttributeParser(data4); var result4 = p4.Parse(); Console.WriteLine($"Result4: {result4.AttributeType} {result4.Name} {result4.Parameters.Count}"); data4 = new ParsableData("base KURZBEZ:varchar ( len = \"100\" ) ; "); p4 = new AttributeParser(data4); result4 = p4.Parse(); Console.WriteLine($"Result4: {result4.AttributeType} {result4.Name} {result4.Parameters.Count}"); data4 = new ParsableData("fact Anzahl_F:decimal(len=\"1,0\",\n\t default=\"1\"); // ein default=\"1\" wäre hier noch nett"); p4 = new AttributeParser(data4); result4 = p4.Parse(); Console.WriteLine($"Result4: {result4.AttributeType} {result4.Name} {result4.Parameters.Count}"); data4 = new ParsableData("ref JaNein.KNZ as Zulassung; // Zulassung_JaNein"); p4 = new AttributeParser(data4); result4 = p4.Parse(); Console.WriteLine($"Result4: {result4.AttributeType} {result4.InterfaceName}.{result4.FieldName} {result4.Alias}"); // Neuer Testfall: ref-Attribute sollen auch Primärschlüssel-Bestandteile sein können! data4 = new ParsableData("ref Tag.KNZ(primary_key=\"true\");"); p4 = new AttributeParser(data4); result4 = p4.Parse(); Console.WriteLine($"Result4: {result4.AttributeType} {result4.InterfaceName}.{result4.FieldName} {result4.Alias}"); }
private static string AppendCreateDictionaryLocalVariable(string dictionary, CodeDomClassBuilder builder) { var parser = new AttributeParser(dictionary); parser.Parse(); var keyValuePairType = new CodeTypeReference(typeof(KeyValuePair<string,object>)); var createDictionaryMethod = new CodeMethodInvokeExpression(); foreach (var attribute in parser.Attributes) { var newKeyValueExpression = new CodeObjectCreateExpression {CreateType = keyValuePairType}; var keyExpression = new CodePrimitiveExpression {Value = attribute.Name}; newKeyValueExpression.Parameters.Add(keyExpression); if (attribute.Type == ParsedAttributeType.String) { AppendStringDictValue(attribute, newKeyValueExpression); } else { newKeyValueExpression.Parameters.Add(new CodeSnippetExpression { Value = attribute.Value }); } createDictionaryMethod.Parameters.Add(newKeyValueExpression); } var getDictionaryMethod = new CodeMethodReferenceExpression { MethodName = "GetDictionaryFromKeyValue", TargetObject = new CodeVariableReferenceExpression { VariableName = "NHamlMonoRailView" } }; createDictionaryMethod.Method = getDictionaryMethod; var variableName = string.Format("nhamlTempDictionary{0}", tempDictionaryCount); tempDictionaryCount++; var dictionaryDecleration = new CodeVariableDeclarationStatement { InitExpression = createDictionaryMethod, Name = variableName, Type = new CodeTypeReference(typeof (IDictionary<string, object>)) }; builder.RenderMethod.Statements.Add(dictionaryDecleration); return variableName; }
public void Empty() { var parser = new AttributeParser(""); parser.Parse(); Assert.AreEqual(0, parser.Attributes.Count); }
private static void ParseAndRenderAttributes(TemplateClassBuilder builder, Match tagMatch) { var idAndClasses = tagMatch.Groups[2].Value; var attributesHash = tagMatch.Groups[4].Value.Trim(); var match = _idClassesRegex.Match(idAndClasses); var classes = new List <string>(); foreach (Capture capture in match.Groups[2].Captures) { classes.Add(capture.Value); } if (classes.Count > 0) { attributesHash = PrependAttribute(attributesHash, Class, string.Join(" ", classes.ToArray())); } string id = null; foreach (Capture capture in match.Groups[1].Captures) { id = capture.Value; break; } if (!string.IsNullOrEmpty(id)) { attributesHash = PrependAttribute(attributesHash, Id, id); } if (string.IsNullOrEmpty(attributesHash)) { return; } var attributeParser = new AttributeParser(attributesHash); attributeParser.Parse(); foreach (var attribute in attributeParser.Attributes) { if (attribute.Type == ParsedAttributeType.String) { var expressionStringParser = new ExpressionStringParser(attribute.Value); expressionStringParser.Parse(); builder.AppendAttributeTokens(attribute.Schema, attribute.Name, expressionStringParser.ExpressionStringTokens); } else { var token = new ExpressionStringToken(attribute.Value, true); builder.AppendAttributeTokens(attribute.Schema, attribute.Name, new[] { token }); } } }
public void EmptyValue() { var parser = new AttributeParser(@"a="""""); parser.Parse(); Assert.AreEqual(1, parser.Attributes.Count); AssertAttribute(parser, "a", "", ParsedAttributeType.String); }
public void SimpleSchemaReference() { var parser = new AttributeParser(" xml:lang=en:us "); parser.Parse(); Assert.AreEqual(1, parser.Attributes.Count); AssertAttribute(parser, "xml", "lang", "en:us", ParsedAttributeType.Reference); }
public void SimpleEncodingIsIgnoredIfItIsUsedAsStopper() { var parser = new AttributeParser(@"c=#{abc\\} "); parser.Parse(); Assert.AreEqual(1, parser.Attributes.Count); AssertAttribute(parser, "c", @"abc\\", ParsedAttributeType.Expression); }
public void SimpleReference() { var parser = new AttributeParser(" a=bb "); parser.Parse(); Assert.AreEqual(1, parser.Attributes.Count); AssertAttribute(parser, "a", "bb", ParsedAttributeType.Reference); }
public void CurlyBracesInsideCode() { var parser = new AttributeParser(@"action = #{Convert.ToString(new { ID=5\})}"); parser.Parse(); Assert.AreEqual(1, parser.Attributes.Count); AssertAttribute(parser, "action", "Convert.ToString(new { ID=5\\})", ParsedAttributeType.Expression); }
public void SimpleSingleQuotes() { var parser = new AttributeParser("a='b'"); parser.Parse(); Assert.AreEqual(1, parser.Attributes.Count); AssertAttribute(parser, "a", "b", ParsedAttributeType.String); }
public void SimpleOnlyReferenceWithSpace() { var parser = new AttributeParser("a "); parser.Parse(); Assert.AreEqual(1, parser.Attributes.Count); AssertAttribute(parser, "a", "a", ParsedAttributeType.String); }
public void SingleQuoteOpen() { var parser = new AttributeParser(@" a=text' "); parser.Parse(); Assert.AreEqual(1, parser.Attributes.Count); AssertAttribute(parser, "a", "text'", ParsedAttributeType.Reference); }
public void ValueEndsWithCurly() { var parser = new AttributeParser(@" a=text} "); parser.Parse(); Assert.AreEqual(1, parser.Attributes.Count); AssertAttribute(parser, "a", "text}", ParsedAttributeType.Reference); }
public void ExpressionWithoutHash() { var parser = new AttributeParser(@"src = Html.Content().AppRoot() "); parser.Parse(); Assert.AreEqual(1, parser.Attributes.Count); AssertAttribute(parser, "src", "Html.Content().AppRoot()", ParsedAttributeType.Reference); }
public void SchemaOnly() { var parser = new AttributeParser(" a:b "); parser.Parse(); Assert.AreEqual(1, parser.Attributes.Count); AssertAttribute(parser, "a", "b", "b", ParsedAttributeType.String); }
public void AttributeWithSchemaIsDifferent() { var parser = new AttributeParser("lang='en' xml:lang='en:us'"); parser.Parse(); Assert.AreEqual(2, parser.Attributes.Count); AssertAttribute(parser, null, "lang", "en", ParsedAttributeType.String); AssertAttribute(parser, "xml", "lang", "en:us", ParsedAttributeType.String); }
public void DoubleAndSingleQuotesEncoded() { var parser = new AttributeParser(@"b='a\'b\'' dd=""\""d\""e"""); parser.Parse(); Assert.AreEqual(2, parser.Attributes.Count); AssertAttribute(parser, "b", @"a\'b\'", ParsedAttributeType.String); AssertAttribute(parser, "dd", @"\""d\""e", ParsedAttributeType.String); }
public void ExpressionInsideOfDoubleSingleQuotesAndEncodedQuotes() { var parser = new AttributeParser("a='#{\"a\"}' c=\"#{a}\""); parser.Parse(); Assert.AreEqual(2, parser.Attributes.Count); AssertAttribute(parser, "a", "#{\"a\"}", ParsedAttributeType.String); AssertAttribute(parser, "c", "#{a}", ParsedAttributeType.String); }
public void DoubleQuotes() { var parser = new AttributeParser("a=\"b\" cc=\"d\" eee=\"f\" "); parser.Parse(); Assert.AreEqual(3, parser.Attributes.Count); AssertAttribute(parser, "a", "b", ParsedAttributeType.String); AssertAttribute(parser, "cc", "d", ParsedAttributeType.String); AssertAttribute(parser, "eee", "f", ParsedAttributeType.String); }
public void OnlyReference() { var parser = new AttributeParser("a cc e"); parser.Parse(); Assert.AreEqual(3, parser.Attributes.Count); AssertAttribute(parser, "a", "a", ParsedAttributeType.String); AssertAttribute(parser, "cc", "cc", ParsedAttributeType.String); AssertAttribute(parser, "e", "e", ParsedAttributeType.String); }
public void SingleQuotes() { var parser = new AttributeParser("a='b' c='d' ee='f'"); parser.Parse(); Assert.AreEqual(3, parser.Attributes.Count); AssertAttribute(parser, "a", "b", ParsedAttributeType.String); AssertAttribute(parser, "c", "d", ParsedAttributeType.String); AssertAttribute(parser, "ee", "f", ParsedAttributeType.String); }
public void EncodingIsIgnoredIfItIsUsedAsStopper() { var parser = new AttributeParser(@" a=""abc\\"" b='abc\\' c=#{abc\\} "); parser.Parse(); Assert.AreEqual(3, parser.Attributes.Count); AssertAttribute(parser, "a", @"abc\\", ParsedAttributeType.String); AssertAttribute(parser, "b", @"abc\\", ParsedAttributeType.String); AssertAttribute(parser, "c", @"abc\\", ParsedAttributeType.Expression); }
public void Schema() { var parser = new AttributeParser(@" a:b b:ccc='eee' eee:c=e "); parser.Parse(); Assert.AreEqual(3, parser.Attributes.Count); AssertAttribute(parser, "a", "b", "b", ParsedAttributeType.String); AssertAttribute(parser, "b", "ccc", "eee", ParsedAttributeType.String); AssertAttribute(parser, "eee", "c", "e", ParsedAttributeType.Reference); }
public void Expressions() { var parser = new AttributeParser("aaa=#{1+1} bb=#{\"t\"} c=#{f.ToString()} d=#{f=>\\{return 1\\}}"); parser.Parse(); Assert.AreEqual(4, parser.Attributes.Count); AssertAttribute(parser, "aaa", "1+1", ParsedAttributeType.Expression); AssertAttribute(parser, "bb", "\"t\"", ParsedAttributeType.Expression); AssertAttribute(parser, "c", "f.ToString()", ParsedAttributeType.Expression); AssertAttribute(parser, "d", @"f=>\{return 1\}", ParsedAttributeType.Expression); }
public void SpacesBettwenKeyAndValue() { var parser = new AttributeParser("a =a bbb= b c = 'c' dd = #{d} "); parser.Parse(); Assert.AreEqual(4, parser.Attributes.Count); AssertAttribute(parser, "a", "a", ParsedAttributeType.Reference); AssertAttribute(parser, "bbb", "b", ParsedAttributeType.Reference); AssertAttribute(parser, "c", "c", ParsedAttributeType.String); AssertAttribute(parser, "dd", "d", ParsedAttributeType.Expression); }
public void ReferenceAsValue() { var parser = new AttributeParser("aa=b c=d eee=f ff=ff.aa ggg=ggg.bb.aa"); parser.Parse(); Assert.AreEqual(5, parser.Attributes.Count); AssertAttribute(parser, "aa", "b", ParsedAttributeType.Reference); AssertAttribute(parser, "c", "d", ParsedAttributeType.Reference); AssertAttribute(parser, "eee", "f", ParsedAttributeType.Reference); AssertAttribute(parser, "ff", "ff.aa", ParsedAttributeType.Reference); AssertAttribute(parser, "ggg", "ggg.bb.aa", ParsedAttributeType.Reference); }
public void TestValidPermutations_Manual() { Console.WriteLine("Input,Visibility,Name,Type,Min,Max,Required,Identity,Value"); foreach (string s in input) { string _input = s?.Split('{')[0].Trim(';'); if (s == null) { throw new Exception("bad input"); } ParseResult result = AttributeParser.Parse(_input); Assert.IsNotNull(result, s); Console.WriteLine($"{s},{result.SetterVisibility ?? SetterAccessModifier.Public},{result.Name},{result.Type ?? "string"},{result.MinLength ?? 0},{result.MaxLength ?? 0},{result.Required ?? false},{result.IsIdentity},{result.InitialValue ?? "(null)"}"); } }
public void ValueEndsWithCurly() { var parser = new AttributeParser(@" a=text} "); parser.Parse(); Assert.AreEqual(1, parser.Attributes.Count); AssertAttribute(parser, "a", "text }
static void Main(string[] args) { IParser parser = new AttributeParser(); parser.Parse(); }
private static string AppendCreateDictionaryLocalVariable(string dictionary, CodeDomClassBuilder builder) { var parser = new AttributeParser(dictionary); parser.Parse(); var keyValuePairType = new CodeTypeReference(typeof(KeyValuePair <string, object>)); var createDictionaryMethod = new CodeMethodInvokeExpression(); foreach (var attribute in parser.Attributes) { var newKeyValueExpression = new CodeObjectCreateExpression { CreateType = keyValuePairType }; var keyExpression = new CodePrimitiveExpression { Value = attribute.Name }; newKeyValueExpression.Parameters.Add(keyExpression); if (attribute.Type == ParsedAttributeType.String) { var expressionStringParser = new ExpressionStringParser(attribute.Value); expressionStringParser.Parse(); var values = expressionStringParser.Tokens; if (values.Count == 1) { var expressionStringToken = values[0]; if (expressionStringToken.IsExpression) { newKeyValueExpression.Parameters.Add(new CodeSnippetExpression { Value = expressionStringToken.Value }); } else { newKeyValueExpression.Parameters.Add(new CodePrimitiveExpression { Value = expressionStringToken.Value }); } } else { var concatExpression = CodeDomClassBuilder.GetConcatExpression(values); newKeyValueExpression.Parameters.Add(concatExpression); } } else { newKeyValueExpression.Parameters.Add(new CodeSnippetExpression { Value = attribute.Value }); } createDictionaryMethod.Parameters.Add(newKeyValueExpression); } var getDictionaryMethod = new CodeMethodReferenceExpression { MethodName = "GetDictionaryFromKeyValue", TargetObject = new CodeVariableReferenceExpression { VariableName = "NHamlMonoRailView" } }; createDictionaryMethod.Method = getDictionaryMethod; var variableName = "nhamlTempDictionary" + tempDictionaryCount; tempDictionaryCount++; var _decl1 = new CodeVariableDeclarationStatement { InitExpression = createDictionaryMethod, Name = variableName, Type = new CodeTypeReference(typeof(IDictionary <string, object>)) }; builder.RenderMethod.Statements.Add(_decl1); return(variableName); }
private static void ParseAndRenderAttributes(TemplateClassBuilder builder, Match tagMatch) { var idAndClasses = tagMatch.Groups[2].Value; var attributesHash = tagMatch.Groups[4].Value.Trim(); var match = _idClassesRegex.Match(idAndClasses); var classes = new List<string>(); foreach (Capture capture in match.Groups[2].Captures) { classes.Add(capture.Value); } if (classes.Count > 0) { attributesHash = PrependAttribute(attributesHash, Class, string.Join(" ", classes.ToArray())); } string id = null; foreach (Capture capture in match.Groups[1].Captures) { id = capture.Value; break; } if (!string.IsNullOrEmpty(id)) { attributesHash = PrependAttribute(attributesHash, Id, id); } if (string.IsNullOrEmpty(attributesHash)) { return; } var attributeParser = new AttributeParser(attributesHash); attributeParser.Parse(); foreach (var attribute in attributeParser.Attributes) { if (attribute.Type == ParsedAttributeType.String) { var expressionStringParser = new ExpressionStringParser(attribute.Value); expressionStringParser.Parse(); builder.AppendAttributeTokens(attribute.Schema, attribute.Name, expressionStringParser.ExpressionStringTokens); } else { var token = new ExpressionStringToken(attribute.Value, true); builder.AppendAttributeTokens(attribute.Schema, attribute.Name, new[] { token }); } } }
public ParseResult ParseTest(string txt) { ParseResult result = AttributeParser.Parse(txt); return(result); }
private static string AppendCreateDictionaryLocalVariable(string dictionary, CodeDomClassBuilder builder) { var parser = new AttributeParser(dictionary); parser.Parse(); var keyValuePairType = new CodeTypeReference(typeof(KeyValuePair<string,object>)); var createDictionaryMethod = new CodeMethodInvokeExpression(); foreach (var attribute in parser.Attributes) { var newKeyValueExpression = new CodeObjectCreateExpression {CreateType = keyValuePairType}; var keyExpression = new CodePrimitiveExpression {Value = attribute.Name}; newKeyValueExpression.Parameters.Add(keyExpression); if (attribute.Type == ParsedAttributeType.String) { var expressionStringParser = new ExpressionStringParser(attribute.Value); expressionStringParser.Parse(); var values = expressionStringParser.Tokens; if (values.Count == 1) { var expressionStringToken = values[0]; if (expressionStringToken.IsExpression) { newKeyValueExpression.Parameters.Add(new CodeSnippetExpression { Value = expressionStringToken.Value }); } else { newKeyValueExpression.Parameters.Add(new CodePrimitiveExpression { Value = expressionStringToken.Value }); } } else { var concatExpression = CodeDomClassBuilder.GetConcatExpression(values); newKeyValueExpression.Parameters.Add(concatExpression); } } else { newKeyValueExpression.Parameters.Add(new CodeSnippetExpression { Value = attribute.Value }); } createDictionaryMethod.Parameters.Add(newKeyValueExpression); } var getDictionaryMethod = new CodeMethodReferenceExpression { MethodName = "GetDictionaryFromKeyValue", TargetObject = new CodeVariableReferenceExpression { VariableName = "NHamlMonoRailView" } }; createDictionaryMethod.Method = getDictionaryMethod; var variableName = "nhamlTempDictionary" + tempDictionaryCount; tempDictionaryCount++; var _decl1 = new CodeVariableDeclarationStatement { InitExpression = createDictionaryMethod, Name = variableName, Type = new CodeTypeReference(typeof (IDictionary<string, object>)) }; builder.RenderMethod.Statements.Add(_decl1); return variableName; }
private static string AppendCreateDictionaryLocalVariable(string dictionary, CodeDomClassBuilder builder) { var parser = new AttributeParser(dictionary); parser.Parse(); var keyValuePairType = new CodeTypeReference(typeof(KeyValuePair <string, object>)); var createDictionaryMethod = new CodeMethodInvokeExpression(); foreach (var attribute in parser.Attributes) { var newKeyValueExpression = new CodeObjectCreateExpression { CreateType = keyValuePairType }; var keyExpression = new CodePrimitiveExpression { Value = attribute.Name }; newKeyValueExpression.Parameters.Add(keyExpression); if (attribute.Type == ParsedAttributeType.String) { AppendStringDictValue(attribute, newKeyValueExpression); } else { newKeyValueExpression.Parameters.Add(new CodeSnippetExpression { Value = attribute.Value }); } createDictionaryMethod.Parameters.Add(newKeyValueExpression); } var getDictionaryMethod = new CodeMethodReferenceExpression { MethodName = "GetDictionaryFromKeyValue", TargetObject = new CodeVariableReferenceExpression { VariableName = "NHamlMonoRailView" } }; createDictionaryMethod.Method = getDictionaryMethod; var variableName = string.Format("nhamlTempDictionary{0}", tempDictionaryCount); tempDictionaryCount++; var dictionaryDecleration = new CodeVariableDeclarationStatement { InitExpression = createDictionaryMethod, Name = variableName, Type = new CodeTypeReference(typeof(IDictionary <string, object>)) }; builder.RenderMethod.Statements.Add(dictionaryDecleration); return(variableName); }