diff --git a/ConsoleDemo/Program.cs b/ConsoleDemo/Program.cs index 92fd6da..488d29a 100644 --- a/ConsoleDemo/Program.cs +++ b/ConsoleDemo/Program.cs @@ -8,42 +8,56 @@ using Semantics; namespace ConsoleDemo { + internal class TreeRenderer + { + private static void RenderToken(SyntaxToken token, string indent, bool isLast) + { + Console.Write(indent + (isLast ? "└── " : "├── ")); + Console.Write($"<{token.Kind}>"); + Console.Write($" {token.Text}"); + Console.WriteLine(); + } + + private static void RenderNode(SyntaxNode node, string indent, bool isLast) + { + Console.Write(indent); + Console.Write(isLast ? "└── " : "├── "); + Console.Write($"<{node.Kind}>"); + Console.WriteLine(); + var children = node.GetChildNodesAndTokens(); + var last = children.Count - 1; + indent += isLast ? " " : "│ "; + for (var index = 0; index <= last; index++) + { + var child = children[index]; + if (child.IsNode) + { + RenderNode(child.AsNode(), indent, index == last); + } else if (child.IsToken) + { + RenderToken(child.AsToken(), indent, index == last); + } + } + } + + public static void Render(SyntaxNode node) + { + RenderNode(node, "", true); + } + } + class Program { private static readonly string BaseDirectory; private const string BaseDirectoryMacOs = @"/Applications/MATLAB_R2017b.app/toolbox/matlab/"; private const string BaseDirectoryWindows = @"D:\Program Files\MATLAB\R2018a\toolbox\matlab\"; - private static readonly HashSet SkipFiles = new HashSet - { - @"codetools\private\template.m", // this is a template, so it contains '$' characters. - @"plottools\+matlab\+graphics\+internal\+propertyinspector\+views\CategoricalHistogramPropertyView.m", // this one contains a 0xA0 character (probably it's 'non-breakable space' in Win-1252). - @"plottools\+matlab\+graphics\+internal\+propertyinspector\+views\PrimitiveHistogram2PropertyView.m", // same - @"plottools\+matlab\+graphics\+internal\+propertyinspector\+views\PrimitiveHistogramPropertyView.m", // same - @"codetools/private/template.m", // this is a template, so it contains '$' characters. - @"plottools/+matlab/+graphics/+internal/+propertyinspector/+views/CategoricalHistogramPropertyView.m", // this one contains a 0xA0 character (probably it's 'non-breakable space' in Win-1252). - @"plottools/+matlab/+graphics/+internal/+propertyinspector/+views/PrimitiveHistogram2PropertyView.m", // same - @"plottools/+matlab/+graphics/+internal/+propertyinspector/+views/PrimitiveHistogramPropertyView.m", // same - }; private static MParser CreateParser(ITextWindow window) { return new MParser(window); } - private static void ProcessFile(string fileName) - { - var text = File.ReadAllText(fileName); - var window = new TextWindowWithNull(text, fileName); - var parser = CreateParser(window); - var tree = parser.Parse(); - var actual = tree.FullText; - if (actual != text) - { - throw new ApplicationException(); - } - } - static Program() { switch (Environment.OSVersion.Platform) @@ -57,41 +71,19 @@ namespace ConsoleDemo break; } } - - private static int ProcessDirectory(string directory) - { - var counter = 0; - var files = Directory.GetFiles(directory, "*.m"); - foreach (var file in files) - { - var relativePath = Path.GetRelativePath(BaseDirectory, file); - if (SkipFiles.Contains(relativePath)) - { - continue; - } - ProcessFile(file); - counter++; - } - - var subDirectories = Directory.GetDirectories(directory); - foreach (var subDirectory in subDirectories) - { - counter += ProcessDirectory(subDirectory); - } - - return counter; - } private static void ParserDemo() { Console.WriteLine("Hello World!"); - var sw = new Stopwatch(); - sw.Start(); - var processed = ProcessDirectory(BaseDirectory); - sw.Stop(); - Console.WriteLine($"{processed} files parsed. Elapsed: {sw.Elapsed}."); - //AfterFunctionFinish(); - //FirstTokenFinish(); + var text = @" + function [a, b c] = functionName(d, e, f) + a = d + e; + end + "; + var window = new TextWindowWithNull(text, "noname"); + var parser = CreateParser(window); + var tree = parser.Parse(); + TreeRenderer.Render(tree); Console.ReadKey(); } @@ -161,11 +153,11 @@ namespace ConsoleDemo public static void Main(string[] args) { - //ParserDemo(); + ParserDemo(); //SemanticsDemo(); //ContextDemo(); //DumbPrinterDemo(); - UsageDemo(); + //UsageDemo(); Console.ReadKey(); } }