Add tree renderer
This commit is contained in:
parent
64d06ec02e
commit
d2d631ac6c
@ -8,42 +8,56 @@ using Semantics;
|
|||||||
|
|
||||||
namespace ConsoleDemo
|
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
|
class Program
|
||||||
{
|
{
|
||||||
private static readonly string BaseDirectory;
|
private static readonly string BaseDirectory;
|
||||||
private const string BaseDirectoryMacOs = @"/Applications/MATLAB_R2017b.app/toolbox/matlab/";
|
private const string BaseDirectoryMacOs = @"/Applications/MATLAB_R2017b.app/toolbox/matlab/";
|
||||||
private const string BaseDirectoryWindows = @"D:\Program Files\MATLAB\R2018a\toolbox\matlab\";
|
private const string BaseDirectoryWindows = @"D:\Program Files\MATLAB\R2018a\toolbox\matlab\";
|
||||||
|
|
||||||
private static readonly HashSet<string> SkipFiles = new HashSet<string>
|
|
||||||
{
|
|
||||||
@"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)
|
private static MParser CreateParser(ITextWindow window)
|
||||||
{
|
{
|
||||||
return new MParser(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()
|
static Program()
|
||||||
{
|
{
|
||||||
switch (Environment.OSVersion.Platform)
|
switch (Environment.OSVersion.Platform)
|
||||||
@ -58,40 +72,18 @@ namespace ConsoleDemo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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()
|
private static void ParserDemo()
|
||||||
{
|
{
|
||||||
Console.WriteLine("Hello World!");
|
Console.WriteLine("Hello World!");
|
||||||
var sw = new Stopwatch();
|
var text = @"
|
||||||
sw.Start();
|
function [a, b c] = functionName(d, e, f)
|
||||||
var processed = ProcessDirectory(BaseDirectory);
|
a = d + e;
|
||||||
sw.Stop();
|
end
|
||||||
Console.WriteLine($"{processed} files parsed. Elapsed: {sw.Elapsed}.");
|
";
|
||||||
//AfterFunctionFinish();
|
var window = new TextWindowWithNull(text, "noname");
|
||||||
//FirstTokenFinish();
|
var parser = CreateParser(window);
|
||||||
|
var tree = parser.Parse();
|
||||||
|
TreeRenderer.Render(tree);
|
||||||
Console.ReadKey();
|
Console.ReadKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,11 +153,11 @@ namespace ConsoleDemo
|
|||||||
|
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
//ParserDemo();
|
ParserDemo();
|
||||||
//SemanticsDemo();
|
//SemanticsDemo();
|
||||||
//ContextDemo();
|
//ContextDemo();
|
||||||
//DumbPrinterDemo();
|
//DumbPrinterDemo();
|
||||||
UsageDemo();
|
//UsageDemo();
|
||||||
Console.ReadKey();
|
Console.ReadKey();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user