-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose variable scopes tracked by the parser to consumers
- Loading branch information
Showing
13 changed files
with
300 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Xml.Linq; | ||
using Acornima.Ast; | ||
using Acornima.Cli.Helpers; | ||
using Acornima.Jsx; | ||
using McMaster.Extensions.CommandLineUtils; | ||
|
||
namespace Acornima.Cli.Commands; | ||
|
||
|
||
[Command(CommandName, Description = "Parse JS code and print tree of variable scopes.")] | ||
internal sealed class PrintScopesCommand | ||
{ | ||
public const string CommandName = "scopes"; | ||
|
||
private readonly IConsole _console; | ||
|
||
public PrintScopesCommand(IConsole console) | ||
{ | ||
_console = console; | ||
} | ||
|
||
[Option("--type", Description = "Type of the JS code to parse.")] | ||
public JavaScriptCodeType CodeType { get; set; } | ||
|
||
[Option("--jsx", Description = "Allow JSX expressions.")] | ||
public bool AllowJsx { get; set; } | ||
|
||
[Argument(0, Description = "The JS code to parse. If omitted, the code will be read from the standard input.")] | ||
public string? Code { get; } | ||
|
||
private T CreateParserOptions<T>() where T : ParserOptions, new() => new T().RecordScopeInfoInUserData(); | ||
|
||
public int OnExecute() | ||
{ | ||
Console.InputEncoding = System.Text.Encoding.UTF8; | ||
|
||
var code = Code ?? _console.ReadString(); | ||
|
||
IParser parser = AllowJsx | ||
? new JsxParser(CreateParserOptions<JsxParserOptions>()) | ||
: new Parser(CreateParserOptions<ParserOptions>()); | ||
|
||
Node rootNode = CodeType switch | ||
{ | ||
JavaScriptCodeType.Script => parser.ParseScript(code), | ||
JavaScriptCodeType.Module => parser.ParseModule(code), | ||
JavaScriptCodeType.Expression => parser.ParseExpression(code), | ||
_ => throw new InvalidOperationException() | ||
}; | ||
|
||
|
||
var treePrinter = new TreePrinter(_console); | ||
treePrinter.Print(new[] { rootNode }, | ||
node => node | ||
.DescendantNodes(descendIntoChildren: descendantNode => ReferenceEquals(node, descendantNode) || descendantNode.UserData is not ScopeInfo) | ||
.Where(node => node.UserData is ScopeInfo), | ||
node => | ||
{ | ||
var scopeInfo = (ScopeInfo)node.UserData!; | ||
var names = scopeInfo.VarNames.Concat(scopeInfo.LexicalNames).Concat(scopeInfo.FunctionNames); | ||
return $"{node.TypeText} ({string.Join(", ", names)})"; | ||
}); | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Acornima.Ast; | ||
|
||
namespace Acornima; | ||
|
||
public sealed class ScopeInfo | ||
{ | ||
private readonly int _id; | ||
private readonly int _currentVarScopeId; | ||
private readonly int _currentThisScopeId; | ||
|
||
public ScopeInfo(Node node, in Scope scope, ReadOnlySpan<Scope> scopeStack) | ||
{ | ||
AssociatedNode = node; | ||
_id = scope.Id; | ||
_currentVarScopeId = scope.CurrentVarScopeIndex == scopeStack.Length ? scope.Id : scopeStack[scope.CurrentVarScopeIndex].Id; | ||
_currentThisScopeId = scope.CurrentThisScopeIndex == scopeStack.Length ? scope.Id : scopeStack[scope.CurrentThisScopeIndex].Id; | ||
VarNames = scope.VarNames.ToArray(); | ||
LexicalNames = scope.LexicalNames.ToArray(); | ||
FunctionNames = scope.FunctionNames.ToArray(); | ||
} | ||
|
||
public Node AssociatedNode { get; } | ||
public Node? AssociatedNodeParent { get; internal set; } | ||
|
||
public string[] VarNames { get; } | ||
public string[] LexicalNames { get; } | ||
public string[] FunctionNames { get; } | ||
|
||
// These lookups could as well be cached. | ||
public ScopeInfo? Parent => FindAncestor(_ => true); | ||
public ScopeInfo? VarScope => FindAncestor(scope => scope._id == _currentVarScopeId); | ||
public ScopeInfo? ThisScope => FindAncestor(scope => scope._id == _currentThisScopeId); | ||
|
||
private ScopeInfo? FindAncestor(Func<ScopeInfo, bool> predicate) | ||
{ | ||
var node = AssociatedNode; | ||
while ((node = GetParentNode(node!)!) is not null) | ||
{ | ||
if (node.UserData is ScopeInfo scopeInfo && predicate(scopeInfo)) | ||
{ | ||
return scopeInfo; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static Node? GetParentNode(Node node) | ||
=> node.UserData is ScopeInfo scopeInfo ? scopeInfo.AssociatedNodeParent : (Node?)node.UserData; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.