本文整理汇总了C#中ICSharpCode.NRefactory.CSharp.CSharpParser.AcceptVisitor方法的典型用法代码示例。如果您正苦于以下问题:C# CSharpParser.AcceptVisitor方法的具体用法?C# CSharpParser.AcceptVisitor怎么用?C# CSharpParser.AcceptVisitor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICSharpCode.NRefactory.CSharp.CSharpParser
的用法示例。
在下文中一共展示了CSharpParser.AcceptVisitor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ParseAndWrite
public void ParseAndWrite(PARSE_TYPE type, string inputText)
{
// parse through framework
SyntaxTree tree = new N.CSharpParser().Parse(inputText);
// dispatch to visitor
var defaultVisitor = new NRefactoryVisitor();
tree.AcceptVisitor(defaultVisitor);
IEnumerable<CLRType> CLRTypesDetected = defaultVisitor.CLRTypes;
switch (type)
{
case PARSE_TYPE.FIELDS:
CLRTypesDetected.ToList().ForEach(clrType => clrType.WriteFieldsUML(m_richSb));
break;
case PARSE_TYPE.PROPERTIES:
CLRTypesDetected.ToList().ForEach(clrType => clrType.WritePropertiesUML(m_richSb));
break;
case PARSE_TYPE.METHODS:
CLRTypesDetected.ToList().ForEach(clrType => clrType.WriteMethodsUML(m_richSb));
break;
case PARSE_TYPE.ALL:
CLRTypesDetected.ToList().ForEach(clrType => clrType.Design(m_richSb));
break;
default:
throw new NotSupportedException();
}
}
示例2: TestCollectNodes
public void TestCollectNodes()
{
var cu = new CSharpParser().Parse("using System;\nclass Test {\n int field; // comment\n}");
CollectNodesVisitor v = new CollectNodesVisitor();
cu.AcceptVisitor(v);
Assert.AreEqual(cu.DescendantsAndSelf.ToList(), v.nodes);
}
示例3: Parse
public string Parse(PARSE_TYPE type, string inputText)
{
// parse through framework
SyntaxTree tree = new N.CSharpParser().Parse(inputText);
// dispatch to visitor
var defaultVisitor = new NRefactoryVisitor();
tree.AcceptVisitor(defaultVisitor);
IEnumerable<CLRType> inputTextTypes = defaultVisitor.CLRTypes;
switch (type)
{
case PARSE_TYPE.FIELDS:
return inputTextTypes.Aggregate(new StringBuilder(), (sb, clr) => sb.AppendLine(clr.GetFieldsUML())).ToString();
case PARSE_TYPE.PROPERTIES:
return inputTextTypes.Aggregate(new StringBuilder(), (sb, clr) => sb.AppendLine(clr.GetPropertiesUML())).ToString();
case PARSE_TYPE.METHODS:
return inputTextTypes.Aggregate(new StringBuilder(), (sb, clr) => sb.AppendLine(clr.GetMethodsUML())).ToString();
case PARSE_TYPE.ALL:
return inputTextTypes.Aggregate(new StringBuilder(), (sb, clr) => sb.AppendLine(clr.Design())).ToString();
default:
throw new NotSupportedException();
}
}
示例4: Format
/// <summary>
/// Formats the specified part of the document.
/// </summary>
public static void Format(IDocument document, int offset, int length, CSharpFormattingOptions options)
{
var syntaxTree = new CSharpParser().Parse(document);
var fv = new AstFormattingVisitor(options, document);
fv.FormattingRegion = new DomRegion(document.GetLocation(offset), document.GetLocation(offset + length));
syntaxTree.AcceptVisitor(fv);
fv.ApplyChanges(offset, length);
}
示例5: GetResult
protected static IDocument GetResult(CSharpFormattingOptions policy, string input)
{
var adapter = new ReadOnlyDocument (input);
var visitor = new AstFormattingVisitor (policy, adapter, factory);
var compilationUnit = new CSharpParser ().Parse (new StringReader (input), "test.cs");
compilationUnit.AcceptVisitor (visitor, null);
return new ReadOnlyDocument (ApplyChanges (input, visitor.Changes));
}
示例6: Continue
protected static void Continue(CSharpFormattingOptions policy, IDocument document, string expectedOutput)
{
var visitior = new AstFormattingVisitor (policy, document, factory);
var compilationUnit = new CSharpParser ().Parse (new StringReader (document.Text), "test.cs");
compilationUnit.AcceptVisitor (visitior, null);
string newText = ApplyChanges (document.Text, visitior.Changes);
if (expectedOutput != newText) {
Console.WriteLine (newText);
}
Assert.AreEqual (expectedOutput, newText);
}
示例7: StringBuilder
/*public static string ApplyChanges (string text, List<TextReplaceAction> changes)
{
changes.Sort ((x, y) => y.Offset.CompareTo (x.Offset));
StringBuilder b = new StringBuilder(text);
foreach (var change in changes) {
//Console.WriteLine ("---- apply:" + change);
// Console.WriteLine (adapter.Text);
if (change.Offset > b.Length)
continue;
b.Remove(change.Offset, change.RemovedChars);
b.Insert(change.Offset, change.InsertedText);
}
// Console.WriteLine ("---result:");
// Console.WriteLine (adapter.Text);
return b.ToString();
}*/
protected static IDocument GetResult (CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.OnTheFly)
{
input = NormalizeNewlines (input);
var document = new StringBuilderDocument (input);
var options = new TextEditorOptions ();
options.EolMarker = "\n";
var visitor = new AstFormattingVisitor (policy, document, options);
visitor.FormattingMode = mode;
var compilationUnit = new CSharpParser ().Parse (document, "test.cs");
compilationUnit.AcceptVisitor (visitor);
visitor.ApplyChanges();
return document;
}
示例8: StringBuilder
/*public static string ApplyChanges (string text, List<TextReplaceAction> changes)
{
changes.Sort ((x, y) => y.Offset.CompareTo (x.Offset));
StringBuilder b = new StringBuilder(text);
foreach (var change in changes) {
//Console.WriteLine ("---- apply:" + change);
// Console.WriteLine (adapter.Text);
if (change.Offset > b.Length)
continue;
b.Remove(change.Offset, change.RemovedChars);
b.Insert(change.Offset, change.InsertedText);
}
// Console.WriteLine ("---result:");
// Console.WriteLine (adapter.Text);
return b.ToString();
}*/
protected static IDocument GetResult(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive)
{
input = NormalizeNewlines(input);
var document = new StringBuilderDocument(input);
var options = new TextEditorOptions();
options.EolMarker = "\n";
options.WrapLineLength = 80;
var visitor = new AstFormattingVisitor (policy, document, options);
visitor.FormattingMode = mode;
var syntaxTree = new CSharpParser ().Parse (document, "test.cs");
syntaxTree.AcceptVisitor (visitor);
visitor.ApplyChanges();
return document;
}
示例9: Format
public CodeFormatResponse Format(CodeFormatRequest request)
{
var document = new StringBuilderDocument(request.Buffer);
var options = new TextEditorOptions();
options.EolMarker = Environment.NewLine;
options.WrapLineLength = 80;
options.TabsToSpaces = request.ExpandTab;
var policy = FormattingOptionsFactory.CreateAllman();
var visitor = new AstFormattingVisitor(policy, document, options);
visitor.FormattingMode = FormattingMode.Intrusive;
var syntaxTree = new CSharpParser().Parse(document, request.FileName);
syntaxTree.AcceptVisitor(visitor);
visitor.ApplyChanges();
return new CodeFormatResponse(document.Text);
}
示例10: Continue
protected static void Continue (CSharpFormattingOptions policy, IDocument document, string expectedOutput, FormattingMode formattingMode = FormattingMode.OnTheFly)
{
expectedOutput = NormalizeNewlines (expectedOutput);
var options = new TextEditorOptions ();
options.EolMarker = "\n";
var visitior = new AstFormattingVisitor (policy, document, options);
visitior.FormattingMode = formattingMode;
var syntaxTree = new CSharpParser ().Parse (document, "test.cs");
syntaxTree.AcceptVisitor (visitior);
visitior.ApplyChanges();
string newText = document.Text;
if (expectedOutput != newText) {
Console.WriteLine (newText);
}
Assert.AreEqual (expectedOutput, newText);
}
示例11: Evaluate
public override ValueReference Evaluate (EvaluationContext ctx, string expression, object expectedType)
{
expression = expression.TrimStart ();
if (expression.Length > 0 && expression[0] == '?')
expression = expression.Substring (1).Trim ();
if (expression.StartsWith ("var", StringComparison.Ordinal) && char.IsWhiteSpace (expression[3])) {
expression = expression.Substring (4).Trim (' ', '\t');
string variable = null;
for (int n = 0; n < expression.Length; n++) {
if (!char.IsLetterOrDigit (expression[n]) && expression[n] != '_') {
variable = expression.Substring (0, n);
if (!expression.Substring (n).Trim (' ', '\t').StartsWith ("=", StringComparison.Ordinal))
variable = null;
break;
}
if (n == expression.Length - 1) {
variable = expression;
expression = null;
break;
}
}
if (!string.IsNullOrEmpty (variable))
userVariables[variable] = new UserVariableReference (ctx, variable);
if (expression == null)
return null;
}
expression = ReplaceExceptionTag (expression, ctx.Options.CurrentExceptionTag);
var expr = new CSharpParser ().ParseExpression (expression);
if (expr == null)
throw new EvaluatorException ("Could not parse expression '{0}'", expression);
var evaluator = new NRefactoryExpressionEvaluatorVisitor (ctx, expression, expectedType, userVariables);
return expr.AcceptVisitor<ValueReference> (evaluator);
}
示例12: ParseAndWrite
public void ParseAndWrite(PARSE_TYPE type, string inputText)
{
// parse through framework
SyntaxTree tree = new N.CSharpParser().Parse(inputText);
// dispatch to visitor
var defaultVisitor = new NRefactoryVisitorV2();
tree.AcceptVisitor(defaultVisitor);
IEnumerable<Declaration> CLRDeclarations = defaultVisitor.Declarations;
switch (type)
{
case PARSE_TYPE.FIELDS:
CLRDeclarations.OfType<ClassesAndStructs>().ToList().ForEach(cs =>
cs.Fields.OrderByDescending(x => !x.Static).ToList().ForEach(f => f.Design(m_richSb).WriteLine()));
break;
case PARSE_TYPE.PROPERTIES:
CLRDeclarations.OfType<ClassesAndStructsAndInterfaces>().ToList().ForEach(csi => csi.Properties.OrderByDescending(x => !x.Static).ToList().ForEach(p => p.Design(m_richSb).WriteLine()));
break;
case PARSE_TYPE.METHODS:
CLRDeclarations.OfType<ClassesAndStructsAndInterfaces>().ToList().ForEach(csi =>
csi.Methods.OrderByDescending(x => !x.Static).ToList().Aggregate(new List<Method>(), (l, m) =>
{
l.Insert((m.Ctor && m.Static) ? 0 : l.Count, m);
return l;
})
.ForEach(m => m.Design(m_richSb).WriteLine())
);
break;
case PARSE_TYPE.ALL:
CLRDeclarations.OfType<ITypeDeclaration>().ToList().ForEach(clrType => clrType.DesignType(m_richSb));
break;
default:
throw new NotSupportedException();
}
}
示例13: Resolve
string Resolve (DebuggerSession session, SourceLocation location, string expression, bool tryTypeOf)
{
expression = expression.TrimStart ();
if (expression.Length > 0 && expression[0] == '?')
return "?" + Resolve (session, location, expression.Substring (1).Trim ());
if (expression.StartsWith ("var", StringComparison.Ordinal) && char.IsWhiteSpace (expression[3]))
return "var " + Resolve (session, location, expression.Substring (4).Trim (' ', '\t'));
expression = ReplaceExceptionTag (expression, session.Options.EvaluationOptions.CurrentExceptionTag);
Expression expr = new CSharpParser ().ParseExpression (expression);
if (expr == null)
return expression;
var resolver = new NRefactoryExpressionResolverVisitor (session, location, expression);
expr.AcceptVisitor (resolver);
string resolved = resolver.GetResolvedExpression ();
if (resolved == expression && !tryTypeOf && (expr is BinaryOperatorExpression) && IsTypeName (expression)) {
// This is a hack to be able to parse expressions such as "List<string>". The NRefactory parser
// can parse a single type name, so a solution is to wrap it around a typeof(). We do it if
// the evaluation fails.
string res = Resolve (session, location, "typeof(" + expression + ")", true);
return res.Substring (7, res.Length - 8);
}
return resolved;
}
示例14: Continue
protected static void Continue (CSharpFormattingPolicy policy, ITextEditorAdapter adapter, string expectedOutput)
{
var visitior = new AstFormattingVisitor (policy, adapter);
var compilationUnit = new CSharpParser ().Parse (new StringReader (adapter.Text));
compilationUnit.AcceptVisitor (visitior, null);
((TextEditorTestAdapter)adapter).ApplyChanges (visitior.Changes);
Assert.AreEqual (expectedOutput, adapter.Text);
}
示例15: Continue
protected static void Continue (CSharpFormattingOptions policy, ITextEditorAdapter adapter, string expectedOutput)
{
var visitior = new AstFormattingVisitor (policy, adapter, factory);
var compilationUnit = new CSharpParser ().Parse (new StringReader (adapter.Text));
compilationUnit.AcceptVisitor (visitior, null);
ApplyChanges (((TextEditorTestAdapter)adapter), visitior.Changes);
if (expectedOutput != adapter.Text) {
Console.WriteLine (adapter.Text);
}
Assert.AreEqual (expectedOutput, adapter.Text);
}