Browse Source

add new VBDemo

newNRvisualizers
Siegfried Pammer 15 years ago
parent
commit
dd8c7f7325
  1. 7
      ICSharpCode.NRefactory.Demo/ICSharpCode.NRefactory.Demo.csproj
  2. 40
      ICSharpCode.NRefactory.Demo/MainForm.Designer.cs
  3. 67
      ICSharpCode.NRefactory.Demo/VBDemo.Designer.cs
  4. 115
      ICSharpCode.NRefactory.Demo/VBDemo.cs
  5. 5
      ICSharpCode.NRefactory.VB/OutputVisitor/OutputVisitor.cs

7
ICSharpCode.NRefactory.Demo/ICSharpCode.NRefactory.Demo.csproj

@ -52,6 +52,10 @@ @@ -52,6 +52,10 @@
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="VBDemo.cs" />
<Compile Include="VBDemo.Designer.cs">
<DependentUpon>VBDemo.cs</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ICSharpCode.NRefactory.VB\ICSharpCode.NRefactory.VB.csproj">
@ -70,6 +74,9 @@ @@ -70,6 +74,9 @@
<EmbeddedResource Include="MainForm.resx">
<DependentUpon>MainForm.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="VBDemo.resx">
<DependentUpon>VBDemo.cs</DependentUpon>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />
</Project>

40
ICSharpCode.NRefactory.Demo/MainForm.Designer.cs generated

@ -32,10 +32,13 @@ namespace ICSharpCode.NRefactory.Demo @@ -32,10 +32,13 @@ namespace ICSharpCode.NRefactory.Demo
{
this.miniToolStrip = new System.Windows.Forms.ToolStrip();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.tabControl1 = new System.Windows.Forms.TabControl();
this.csDemo1 = new ICSharpCode.NRefactory.Demo.CSDemo();
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.vbDemo1 = new ICSharpCode.NRefactory.Demo.VBDemo();
this.tabPage1.SuspendLayout();
this.tabControl1.SuspendLayout();
this.tabPage2.SuspendLayout();
this.SuspendLayout();
//
// miniToolStrip
@ -61,9 +64,18 @@ namespace ICSharpCode.NRefactory.Demo @@ -61,9 +64,18 @@ namespace ICSharpCode.NRefactory.Demo
this.tabPage1.Text = "C#";
this.tabPage1.UseVisualStyleBackColor = true;
//
// csDemo1
//
this.csDemo1.Dock = System.Windows.Forms.DockStyle.Fill;
this.csDemo1.Location = new System.Drawing.Point(3, 3);
this.csDemo1.Name = "csDemo1";
this.csDemo1.Size = new System.Drawing.Size(501, 452);
this.csDemo1.TabIndex = 0;
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tabControl1.Location = new System.Drawing.Point(0, 0);
this.tabControl1.Name = "tabControl1";
@ -71,13 +83,24 @@ namespace ICSharpCode.NRefactory.Demo @@ -71,13 +83,24 @@ namespace ICSharpCode.NRefactory.Demo
this.tabControl1.Size = new System.Drawing.Size(515, 484);
this.tabControl1.TabIndex = 0;
//
// csDemo1
// tabPage2
//
this.csDemo1.Dock = System.Windows.Forms.DockStyle.Fill;
this.csDemo1.Location = new System.Drawing.Point(3, 3);
this.csDemo1.Name = "csDemo1";
this.csDemo1.Size = new System.Drawing.Size(501, 452);
this.csDemo1.TabIndex = 0;
this.tabPage2.Controls.Add(this.vbDemo1);
this.tabPage2.Location = new System.Drawing.Point(4, 22);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
this.tabPage2.Size = new System.Drawing.Size(507, 458);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "VB";
this.tabPage2.UseVisualStyleBackColor = true;
//
// vbDemo1
//
this.vbDemo1.Dock = System.Windows.Forms.DockStyle.Fill;
this.vbDemo1.Location = new System.Drawing.Point(3, 3);
this.vbDemo1.Name = "vbDemo1";
this.vbDemo1.Size = new System.Drawing.Size(501, 452);
this.vbDemo1.TabIndex = 0;
//
// MainForm
//
@ -89,8 +112,11 @@ namespace ICSharpCode.NRefactory.Demo @@ -89,8 +112,11 @@ namespace ICSharpCode.NRefactory.Demo
this.Text = "NRefactory Demo";
this.tabPage1.ResumeLayout(false);
this.tabControl1.ResumeLayout(false);
this.tabPage2.ResumeLayout(false);
this.ResumeLayout(false);
}
private ICSharpCode.NRefactory.Demo.VBDemo vbDemo1;
private System.Windows.Forms.TabPage tabPage2;
private ICSharpCode.NRefactory.Demo.CSDemo csDemo1;
private System.Windows.Forms.ToolStrip miniToolStrip;
private System.Windows.Forms.TabPage tabPage1;

67
ICSharpCode.NRefactory.Demo/VBDemo.Designer.cs generated

@ -33,11 +33,9 @@ namespace ICSharpCode.NRefactory.Demo @@ -33,11 +33,9 @@ namespace ICSharpCode.NRefactory.Demo
{
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.codeView = new System.Windows.Forms.TextBox();
this.editNodeButton = new System.Windows.Forms.Button();
this.clearSpecialsButton = new System.Windows.Forms.Button();
this.syntaxTree = new ICSharpCode.NRefactory.Demo.VBAstView();
this.generateCodeButton = new System.Windows.Forms.Button();
this.parseButton = new System.Windows.Forms.Button();
this.treeView = new System.Windows.Forms.TreeView();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
@ -57,11 +55,9 @@ namespace ICSharpCode.NRefactory.Demo @@ -57,11 +55,9 @@ namespace ICSharpCode.NRefactory.Demo
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.editNodeButton);
this.splitContainer1.Panel2.Controls.Add(this.clearSpecialsButton);
this.splitContainer1.Panel2.Controls.Add(this.syntaxTree);
this.splitContainer1.Panel2.Controls.Add(this.generateCodeButton);
this.splitContainer1.Panel2.Controls.Add(this.parseButton);
this.splitContainer1.Panel2.Controls.Add(this.treeView);
this.splitContainer1.Size = new System.Drawing.Size(462, 391);
this.splitContainer1.SplitterDistance = 173;
this.splitContainer1.TabIndex = 1;
@ -79,64 +75,41 @@ namespace ICSharpCode.NRefactory.Demo @@ -79,64 +75,41 @@ namespace ICSharpCode.NRefactory.Demo
this.codeView.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.codeView.Size = new System.Drawing.Size(462, 173);
this.codeView.TabIndex = 0;
this.codeView.Text = "Imports System\r\nClass Test\r\n Public Sub Main(ByVal args As String())\r\n " +
" Console.WriteLine(\"Hello, World\")\r\n End Sub\r\nEnd Class";
this.codeView.Text = "Option Explicit";
this.codeView.WordWrap = false;
//
// editNodeButton
//
this.editNodeButton.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.editNodeButton.Location = new System.Drawing.Point(331, 2);
this.editNodeButton.Name = "editNodeButton";
this.editNodeButton.Size = new System.Drawing.Size(100, 23);
this.editNodeButton.TabIndex = 4;
this.editNodeButton.Text = "Edit node";
this.editNodeButton.UseVisualStyleBackColor = true;
this.editNodeButton.Click += new System.EventHandler(this.EditNodeButtonClick);
//
// clearSpecialsButton
//
this.clearSpecialsButton.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.clearSpecialsButton.Location = new System.Drawing.Point(225, 2);
this.clearSpecialsButton.Name = "clearSpecialsButton";
this.clearSpecialsButton.Size = new System.Drawing.Size(100, 23);
this.clearSpecialsButton.TabIndex = 3;
this.clearSpecialsButton.Text = "Clear 0 specials";
this.clearSpecialsButton.UseVisualStyleBackColor = true;
this.clearSpecialsButton.Click += new System.EventHandler(this.ClearSpecialsButtonClick);
//
// syntaxTree
//
this.syntaxTree.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.syntaxTree.Location = new System.Drawing.Point(3, 32);
this.syntaxTree.Name = "syntaxTree";
this.syntaxTree.Size = new System.Drawing.Size(459, 182);
this.syntaxTree.TabIndex = 2;
this.syntaxTree.Unit = null;
//
// generateCodeButton
//
this.generateCodeButton.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.generateCodeButton.Location = new System.Drawing.Point(119, 2);
this.generateCodeButton.Location = new System.Drawing.Point(225, 2);
this.generateCodeButton.Name = "generateCodeButton";
this.generateCodeButton.Size = new System.Drawing.Size(100, 23);
this.generateCodeButton.TabIndex = 1;
this.generateCodeButton.Text = "Generate";
this.generateCodeButton.UseVisualStyleBackColor = true;
this.generateCodeButton.Click += new System.EventHandler(this.GenerateCodeButtonClick);
this.generateCodeButton.Click += new System.EventHandler(this.CSharpGenerateCodeButtonClick);
//
// parseButton
//
this.parseButton.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.parseButton.Location = new System.Drawing.Point(13, 2);
this.parseButton.Location = new System.Drawing.Point(119, 2);
this.parseButton.Name = "parseButton";
this.parseButton.Size = new System.Drawing.Size(100, 23);
this.parseButton.TabIndex = 0;
this.parseButton.Text = "Parse";
this.parseButton.UseVisualStyleBackColor = true;
this.parseButton.Click += new System.EventHandler(this.ParseButtonClick);
this.parseButton.Click += new System.EventHandler(this.CSharpParseButtonClick);
//
// treeView
//
this.treeView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.treeView.Location = new System.Drawing.Point(3, 31);
this.treeView.Name = "treeView";
this.treeView.Size = new System.Drawing.Size(459, 180);
this.treeView.TabIndex = 0;
this.treeView.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.CSharpTreeViewAfterSelect);
//
// VBDemo
//
@ -152,10 +125,8 @@ namespace ICSharpCode.NRefactory.Demo @@ -152,10 +125,8 @@ namespace ICSharpCode.NRefactory.Demo
this.splitContainer1.ResumeLayout(false);
this.ResumeLayout(false);
}
private System.Windows.Forms.Button clearSpecialsButton;
private System.Windows.Forms.Button editNodeButton;
private ICSharpCode.NRefactory.Demo.VBAstView syntaxTree;
private System.Windows.Forms.TextBox codeView;
private System.Windows.Forms.TreeView treeView;
private System.Windows.Forms.Button generateCodeButton;
private System.Windows.Forms.Button parseButton;
private System.Windows.Forms.SplitContainer splitContainer1;

115
ICSharpCode.NRefactory.Demo/VBDemo.cs

@ -7,10 +7,13 @@ using System.ComponentModel; @@ -7,10 +7,13 @@ using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.NRefactory.VB;
using ICSharpCode.NRefactory.VB.Ast;
using ICSharpCode.NRefactory.VB.Parser;
using ICSharpCode.NRefactory.VB.PrettyPrinter;
namespace ICSharpCode.NRefactory.Demo
{
@ -25,30 +28,110 @@ namespace ICSharpCode.NRefactory.Demo @@ -25,30 +28,110 @@ namespace ICSharpCode.NRefactory.Demo
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//ParseButtonClick(null, null);
}
void ParseButtonClick(object sender, EventArgs e)
CompilationUnit compilationUnit;
void CSharpParseButtonClick(object sender, EventArgs e)
{
var parser = new VBParser();
compilationUnit = parser.Parse(new StringReader(codeView.Text));
treeView.Nodes.Clear();
foreach (var element in compilationUnit.Children) {
treeView.Nodes.Add(MakeTreeNode(element));
}
SelectCurrentNode(treeView.Nodes);
}
TreeNode MakeTreeNode(AstNode node)
{
TreeNode t = new TreeNode(GetNodeTitle(node));
t.Tag = node;
foreach (AstNode child in node.Children) {
t.Nodes.Add(MakeTreeNode(child));
}
return t;
}
string GetNodeTitle(AstNode node)
{
using (VBParser parser = new VBParser(new VBLexer(new StringReader(codeView.Text)))) {
parser.Parse();
// this retrieves the root node of the result DOM
if (parser.Errors.Count > 0) {
MessageBox.Show(parser.Errors.ErrorOutput);
StringBuilder b = new StringBuilder();
b.Append(node.Role.ToString());
b.Append(": ");
b.Append(node.GetType().Name);
bool hasProperties = false;
foreach (PropertyInfo p in node.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) {
if (p.Name == "NodeType" || p.Name == "IsNull")
continue;
if (p.PropertyType == typeof(string) || p.PropertyType.IsEnum || p.PropertyType == typeof(bool)) {
if (!hasProperties) {
hasProperties = true;
b.Append(" (");
} else {
b.Append(", ");
}
b.Append(p.Name);
b.Append(" = ");
try {
object val = p.GetValue(node, null);
b.Append(val != null ? val.ToString() : "**null**");
} catch (TargetInvocationException ex) {
b.Append("**" + ex.InnerException.GetType().Name + "**");
}
}
syntaxTree.Unit = parser.CompilationUnit;
}
if (hasProperties)
b.Append(")");
return b.ToString();
}
void GenerateCodeButtonClick(object sender, EventArgs e)
bool SelectCurrentNode(TreeNodeCollection c)
{
if (syntaxTree.Unit != null) {
VBNetOutputVisitor visitor = new VBNetOutputVisitor();
// re-insert the comments we saved from the parser into the output
using (SpecialNodesInserter.Install(savedSpecials, visitor)) {
syntaxTree.Unit.AcceptVisitor(visitor, null);
int selectionStart = codeView.SelectionStart;
int selectionEnd = selectionStart + codeView.SelectionLength;
foreach (TreeNode t in c) {
AstNode node = t.Tag as AstNode;
if (node != null
&& selectionStart >= GetOffset(codeView, node.StartLocation)
&& selectionEnd <= GetOffset(codeView, node.EndLocation))
{
if (selectionStart == selectionEnd
&& (selectionStart == GetOffset(codeView, node.StartLocation)
|| selectionStart == GetOffset(codeView, node.EndLocation)))
{
// caret is on border of this node; don't expand
treeView.SelectedNode = t;
} else {
t.Expand();
if (!SelectCurrentNode(t.Nodes))
treeView.SelectedNode = t;
}
return true;
}
codeView.Text = visitor.Text.Replace("\t", " ");
}
return false;
}
void CSharpGenerateCodeButtonClick(object sender, EventArgs e)
{
StringWriter w = new StringWriter();
OutputVisitor output = new OutputVisitor(w, new VBFormattingOptions());
compilationUnit.AcceptVisitor(output, null);
codeView.Text = w.ToString();
}
int GetOffset(TextBox textBox, AstLocation location)
{
return textBox.GetFirstCharIndexFromLine(location.Line - 1) + location.Column - 1;
}
void CSharpTreeViewAfterSelect(object sender, TreeViewEventArgs e)
{
AstNode node = e.Node.Tag as AstNode;
if (node != null) {
int startOffset = GetOffset(codeView, node.StartLocation);
int endOffset = GetOffset(codeView, node.EndLocation);
codeView.Select(startOffset, endOffset - startOffset);
}
}
}

5
ICSharpCode.NRefactory.VB/OutputVisitor/OutputVisitor.cs

@ -52,7 +52,10 @@ namespace ICSharpCode.NRefactory.VB @@ -52,7 +52,10 @@ namespace ICSharpCode.NRefactory.VB
public object VisitCompilationUnit(ICSharpCode.NRefactory.VB.Ast.CompilationUnit compilationUnit, object data)
{
throw new NotImplementedException();
// don't do node tracking as we visit all children directly
foreach (AstNode node in compilationUnit.Children)
node.AcceptVisitor(this, data);
return null;
}
public object VisitBlockStatement(ICSharpCode.NRefactory.VB.Ast.BlockStatement blockStatement, object data)

Loading…
Cancel
Save