string ConvertMember(string code) { CSharpParser parser = new CSharpParser(); var expr = parser.ParseTypeMembers(code).Single(); Assert.IsFalse(parser.HasErrors); return(ConvertMember(expr)); }
public EntityDeclaration CreateDataMember(string modifiers, string dataMemberTypeFullName, string dataMemberName, string initializerExpression = "") { Ensure.ArgumentNotNull(dataMemberTypeFullName, "dataMemberTypeFullName"); Ensure.ArgumentNotNullOrEmpty(dataMemberName, "dataMemberName"); if (ContainsDataMember(dataMemberName)) #region Throw Exception { throw new Exception(string.Format( Strings.ExceptionCreateDataMemberFailedBecauseClassAlreadyContainsDataMemberName, dataMemberName)); } #endregion var initializationString = string.IsNullOrEmpty(initializerExpression) ? ";" : initializerExpression.Trim().EnsureStartsWith("=").EnsureEndsWith(";"); var parsedDataMemberSource = string.Format("{0} {1} {2} {3}", modifiers, dataMemberTypeFullName, dataMemberName, initializationString); var parsedDataMember = CSharpParser.ParseTypeMembers(parsedDataMemberSource) .FirstOrDefault() as FieldDeclaration; if (null == parsedDataMember) #region Throw Exception { throw new Exception(string.Format(Strings.ExceptionParsingCodeInCodeGeneratorProxy, "Data Member", parsedDataMemberSource)); } #endregion GeneratedClassSyntaxTree.AddChild(parsedDataMember, Roles.TypeMemberRole); return(parsedDataMember); }
public static T ParseTypeMember <T>(string expr, bool expectErrors = false) where T : AttributedNode { CSharpParser parser = new CSharpParser(); var members = parser.ParseTypeMembers(new StringReader(expr)); Assert.AreEqual(expectErrors, parser.HasErrors, "HasErrors"); AttributedNode m = members.Single(); Type type = typeof(T); Assert.IsTrue(type.IsAssignableFrom(m.GetType()), String.Format("Parsed member was {0} instead of {1} ({2})", m.GetType(), type, m)); return((T)m); }
public static T ParseTypeMember <T>(string expr, bool expectErrors = false) where T : EntityDeclaration { CSharpParser parser = new CSharpParser(); var members = parser.ParseTypeMembers(new StringReader(expr)); if (parser.HasErrors) { parser.ErrorPrinter.Errors.ForEach(err => Console.WriteLine(err.Message)); } Assert.AreEqual(expectErrors, parser.HasErrors, "HasErrors"); EntityDeclaration m = members.Single(); Type type = typeof(T); Assert.IsTrue(type.IsAssignableFrom(m.GetType()), String.Format("Parsed member was {0} instead of {1} ({2})", m.GetType(), type, m)); return((T)m); }
public static T ParseTypeMember <T>(string expr, bool expectErrors = false) where T : EntityDeclaration { CSharpParser parser = new CSharpParser(); var members = parser.ParseTypeMembers(expr); StringBuilder errorOutput = new StringBuilder(); foreach (var error in parser.Errors) { errorOutput.AppendLine(error.Message); } Assert.AreEqual(expectErrors, parser.HasErrors, errorOutput.ToString()); EntityDeclaration m = members.Single(); Type type = typeof(T); Assert.IsTrue(type.IsAssignableFrom(m.GetType()), String.Format("Parsed member was {0} instead of {1} ({2})", m.GetType(), type, m)); return((T)m); }
public static void AddHyperLinkControl(TypeDeclaration classObject, TargetField field, string fullTypeName) { StringBuilder sb = new StringBuilder(); sb.AppendFormat("[{0}(\"{1}\",\"{2}\")] ", typeof(UIClientPropertyAttribute).FullName, field.SourceFieldName, fullTypeName); sb.AppendFormat("public Link {0}", field.SourceFieldName); sb.AppendFormat("{{ get {{ return this.TryGetLink(\"ctrl00_{0}\"); }} }}", field.SourceFieldName); using (StringReader reader = new StringReader(sb.ToString())) { CSharpParser parser = new CSharpParser(); var memberList = parser.ParseTypeMembers(reader); foreach (var member in memberList) { var property = (PropertyDeclaration)member; var role = new ICSharpCode.NRefactory.Role <ICSharpCode.NRefactory.CSharp.AttributedNode>("Member"); property.Remove(); classObject.AddChild(property, TypeDeclaration.MemberRole); } } }
public ParseResult Parse(string code) { var result = new ParseResult(); var parser = new CSharpParser(); var syntaxTree = parser.Parse(code); var codeLines = code.Split(new[] { Environment.NewLine }, StringSplitOptions.None); var codeLinesDictionary = new Dictionary <int, Tuple <string, bool> >(); for (int i = 0; i < codeLines.Length; i++) { codeLinesDictionary.Add(i, new Tuple <string, bool>(codeLines[i], true)); } var typeMembersTree = parser.ParseTypeMembers(code); foreach (var typeMember in typeMembersTree.Where(x => x is TypeDeclaration || x is MethodDeclaration)) { var element = typeMember.GetText(); if (typeMember is TypeDeclaration) { result.Declarations += element; } //else //{ // result.Declarations += "public static partial ScriptCsMethod {"; // result.Declarations += element; // result.Declarations += "}"; //} for (var i = typeMember.StartLocation.Line - 1; i < typeMember.StartLocation.Line; i++) { var oldItem = codeLinesDictionary[i]; codeLinesDictionary[i] = new Tuple <string, bool>(oldItem.Item1, false); } } var keysToRemove = codeLinesDictionary.Where(x => x.Value.Item2 == false).Select(i => i.Key); keysToRemove.ToList().ForEach(x => codeLinesDictionary.Remove(x)); foreach (var correct in syntaxTree.Members) { var element = correct.GetText();; result.Declarations += element; } if (syntaxTree.Errors.Any()) { var evalLines = codeLines.Skip(syntaxTree.Errors.First().Region.BeginLine - 1).ToList(); result.Evaluations += string.Join(Environment.NewLine, evalLines); //result.Evaluations = "public void ScriptCsInvoke() {" + Environment.NewLine; //result.Evaluations = string.Join(Environment.NewLine, codeLinesDictionary.Select(i => i.Value.Item1)); //result.Evaluations += Environment.NewLine + "}"; } var evaluationTree = parser.ParseStatements(result.Evaluations); return(result); }