/CSEdit/CSEdit/Editor/Editor.cs
C# | 308 lines | 254 code | 50 blank | 4 comment | 31 complexity | 7f9542aea9e3329e599f92e6c2b0612b MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using CSParse;
- using CSStore;
- using System.Diagnostics;
-
- namespace CSEdit
- {
- public partial class Editor : Form
- {
- public Editor()
- {
- InitializeComponent();
-
- codeTextBox.Text = @"namespace N
- {
- class C : B
- {
- void Method(C p)
- {
- C d = new C();
- p.DoNothing();
- d.DoNothing();
- this.F2 = p.Prop.Field;
- }
-
- void DoNothing()
- {
-
- }
-
- B F2;
-
- B Prop
- {
- get { return this.F2; }
- }
- }
- class B
- {
- B Field;
- }
- }";
- codeTextBox.TextChanged += new EventHandler(codeTextBox_TextChanged);
- }
-
- string oldParse = string.Empty;
-
- void codeTextBox_TextChanged(object sender, EventArgs e)
- {
- try
- {
- List<Token> tokens = Lexer.Lex(codeTextBox.Text);
- FileNode file = FileParser.Parse(tokens);
-
- Resolver.ResolveReferences(new[] { file });
-
- oldParse = file.ToString();
- tokenTextBox.Text = oldParse;
- }
- catch (InvalidParseSource ex)
- {
- string text = ex.Message + Environment.NewLine + oldParse;
- tokenTextBox.Text = text;
- }
- }
-
- private void openToolStripMenuItem_Click(object sender, EventArgs e)
- {
- OpenFileDialog dialog = new OpenFileDialog();
- dialog.Filter = "CS file (*.cs)|*.cs";
-
- DialogResult result = dialog.ShowDialog();
-
- if (result == DialogResult.OK)
- {
- codeTextBox.Text = File.ReadAllText(dialog.FileName);
-
- //codeTextBox.Highlight(0, codeTextBox.Text.Length);
- }
- }
-
- private void saveToolStripMenuItem_Click(object sender, EventArgs e)
- {
- SaveFileDialog dialog = new SaveFileDialog();
- dialog.DefaultExt = "cs";
- dialog.Filter = "CS file (*.cs)|*.cs";
-
- DialogResult result = dialog.ShowDialog();
-
- if (result == DialogResult.OK)
- {
- File.WriteAllText(dialog.FileName, codeTextBox.Text);
- }
- }
-
- private void tokenizeToolStripMenuItem_Click(object sender, EventArgs e)
- {
- List<Token> tokens = Lexer.Lex(codeTextBox.Text);
-
- StringBuilder sb = new StringBuilder();
- foreach (Token token in tokens)
- {
- sb.AppendLine(string.Format("Id = {0},\t\tValue = {1}", token.Id, token.Value));
- }
-
- tokenTextBox.Text = sb.ToString();
- }
-
- private void parseToolStripMenuItem_Click(object sender, EventArgs e)
- {
- List<Token> tokens = Lexer.Lex(codeTextBox.Text);
- FileNode file = FileParser.Parse(tokens);
-
- tokenTextBox.Text = file.ToString();
- }
-
- private IEnumerable<FileNode> ParseDirectory(DirectoryInfo dir, List<string> errors)
- {
- foreach (DirectoryInfo childDir in dir.GetDirectories())
- {
- foreach (FileNode fileNode in ParseDirectory(childDir, errors))
- {
- yield return fileNode;
- }
- }
-
- foreach (FileInfo file in dir.GetFiles("*.cs"))
- {
- FileNode fileNode = null;
- string error = null;
- try
- {
- fileNode = ParseFile(file);
- }
- catch (InvalidParseSource ex)
- {
- error = "Failed - " + file.FullName + " - " + ex.Message;
- }
-
- if (error != null)
- {
- errors.Add(error);
- }
-
- if (fileNode != null)
- {
- yield return fileNode;
- }
- }
- }
-
- private FileNode ParseFile(FileInfo file)
- {
- string data = File.ReadAllText(file.FullName);
-
- FileNode fileNode = FileParser.Parse(Lexer.Lex(data), false);
- fileNode.Path = file.FullName;
-
- return fileNode;
- }
-
- private void resolveDirToolStripMenuItem_Click(object sender, EventArgs e)
- {
- FolderBrowserDialog dialog = new FolderBrowserDialog();
- DialogResult result = dialog.ShowDialog();
-
- if (result == System.Windows.Forms.DialogResult.OK)
- {
- string root = dialog.SelectedPath;
- DirectoryInfo dir = new DirectoryInfo(root);
-
- List<string> errors = new List<string>();
- DateTime start = DateTime.Now;
- List<FileNode> files = ParseDirectory(dir, errors).ToList();
- DateTime end = DateTime.Now;
- GC.Collect();
-
- string text = string.Empty;
- text += string.Format("{0} files took {1:0.00} to parse{2}", files.Count, (end - start).TotalSeconds, Environment.NewLine);
-
- start = DateTime.Now;
- Resolver.ResolveReferences(files);
- end = DateTime.Now;
-
- ICollection<TypeNode> types = TypeVisitor.GetTypes(files);
-
- text += string.Format("{0} types took {1:0.00} to resolve{2}", types.Count, (end - start).TotalSeconds, Environment.NewLine);
-
- text += string.Join(Environment.NewLine, errors.ToArray());
-
- //text += Environment.NewLine;
- //text += string.Join(Environment.NewLine, types.Keys.ToArray());
-
- tokenTextBox.Text = text;
-
- GC.Collect();
- }
- }
-
- private void listDirToolStripMenuItem_Click(object sender, EventArgs e)
- {
- FolderBrowserDialog dialog = new FolderBrowserDialog();
- DialogResult result = dialog.ShowDialog();
-
- if (result == System.Windows.Forms.DialogResult.OK)
- {
- GetFileMappings(dialog.SelectedPath);
- }
- }
-
- private Dictionary<string, string> GetFileMappings(string root)
- {
- DirectoryInfo dir = new DirectoryInfo(root);
-
- Dictionary<string, string> elementFileMapping = new Dictionary<string, string>();
-
- List<string> errors = new List<string>();
- foreach (FileNode file in ParseDirectory(dir, errors).Where(f => !string.IsNullOrEmpty(f.Path)))
- {
- foreach (TypeNode type in TypeVisitor.GetTypes(file))
- {
- elementFileMapping[type.Name.ToString()] = file.Path;
-
- foreach (MemberNode member in type.Members)
- {
- elementFileMapping[member.Name.ToString()] = file.Path;
- }
- }
- }
-
- return elementFileMapping;
- }
-
- private void codeTextBox_KeyPress(object sender, KeyPressEventArgs e)
- {
-
- }
-
- private void codeTextBox_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.F12)
- {
- try
- {
- FileNode file = FileParser.Parse(Lexer.Lex(codeTextBox.Text));
- Resolver.ResolveReferences(new[] { file });
-
- int caret = codeTextBox.SelectionStart;
- FinderVisitor finder = new FinderVisitor(caret);
- finder.Visit(file);
- if (finder.FoundReference != null && file.DeclarationLocations.ContainsKey(finder.FoundReference.Id))
- {
- codeTextBox.SelectionStart = file.DeclarationLocations[finder.FoundReference.Id].StartPos;
- }
- else if (finder.FoundExpression != null)
- {
- MemberExpression memberExpression = finder.FoundExpression as MemberExpression;
- LocalExpression localExpression = finder.FoundExpression as LocalExpression;
- if (memberExpression != null)
- {
- codeTextBox.SelectionStart = file.DeclarationLocations[memberExpression.Member.Name.Id].StartPos;
- }
- else if (localExpression != null)
- {
- if (localExpression.Declaration != null)
- {
- IdentifierExpression variable = localExpression.Declaration.Variables.First(v => v.Id.Name == localExpression.Variable);
- codeTextBox.SelectionStart = variable.StartToken.StartPos;
- }
- else if (localExpression.Parameter != null)
- {
- IdentifierExpression variable = localExpression.Parameter.Variable;
- codeTextBox.SelectionStart = variable.StartToken.StartPos;
- }
- }
- else
- {
- Debug.Fail("Finder found unknown expression.");
- }
- }
- }
- catch (InvalidParseSource)
- { }
- }
- }
-
- CodeStore store = new CodeStore();
-
- private void storeDirToolStripMenuItem_Click(object sender, EventArgs args)
- {
- throw new NotImplementedException();
- }
-
- private void gCToolStripMenuItem_Click(object sender, EventArgs e)
- {
- //MessageBox.Show(store.inMemoryStore.Count.ToString());
- GC.Collect();
- }
- }
- }