Giter VIP home page Giter VIP logo

antlr4parsetreevisualizer's Introduction

ANTLR4 Parse Tree Visualizer

GitHub Release AppVeyor build status

Screenshot

Features

  • List of tokens (error tokens are highlighted in red)

  • Treeview of rule contexts and terminal nodes (error nodes in red)

  • Properties of selected treeview node (properties not declared in the Antlr namespace are checked)

  • Input text, or the text from the current channel positioned as in the input

  • Selection sync, when selecting in the token list, the tree view, or the source text.

    Selection sync

  • Filtering the token list, by text, whitespace, or error; or by specific token types:

    Token filtering

  • Filtering the parse tree nodes by text, whitespace, or error nodes; or by specific rule types.

    Parse tree filtering

  • Set a specific node as the root node, either in the current window, or in a new window

    Set node as root, in current or new window

  • You can also embed the same UI in your own applications.

Requirements

  • Tested with Visual Studio 2019 or 2017 (may work with older versions as well)
  • Supports the current Antlr.Runtime.Standard.DLL (4.7.2) as well as the older Antlr.Runtime.DLL (4.6.6).

Installation

Before installing, you need to determine two things:

  1. There are two .NET variants of the ANTLR4 runtime library: the "standard" version -- Antlr4.Runtime.Standard -- and the "legacy" Antlr4.Runtime at version 4.6.6. Which variant is your code using?
  2. Are you using Visual Studio 2019 or 2017?

Then:

  1. Go to the releases page. Choose the appropriate ZIP file(s) based on the Antlr4 variant and your version of Visual Studio.

    For example, if you're in Visual Studio 2019 and debugging code written against Antlr4.Runtime.Standard, download the ParseTreeVisualizer.Standard.2019.zip file.

    Note: if you need to, you can download and install all four visualizer variants; they won't overwrite each other.

  2. You may need to unblock the file.

  3. Unzip the contents of the ZIP file into one of Visual Studio's recognized visualizer folders:

    • VisualStudioInstallPath\Common7\Packages\Debugger\Visualizers
    • My Documents\Visual Studio Version\Visualizers

    You don't have to restart VS, just make sure VS is not currently in a debugging session

You can also compile the source yourself (ANTLR4ParseTreeVisualizer.sln) and place the output DLLs in one of the appropriate visualizer folders.

For VS 2017, the DLLs will end up in either the Visualizers parent folder, or a single subfolder. For VS 2019, there are additional subfolders for debugging various target frameworks.

Usage

  1. Begin a debugging session, and break at some point.
  2. Hover over an instance of one of the visualizer target types (Antlr4.Runtime.RuleContext, Antlr4.Runtime.BufferedTokenStream, or string), in the code editor, or the Watch or Locals window. This instance can be exposed by any variable, or any expression; the type of the expression doesn't matter.
  3. Click on the magnifying glass to the right of the expression.
  4. You may need to choose a lexer class if you are visualizing a string and there are multiple lexers in the debugged assemblies.
  5. You may need to choose a parser class and a parser method from the settings, if you are visualizing a BufferedTokenStream or a string, and the debugged assemblies have multiple parser classes.

Note that these choices -- selected lexer, parser and parser method -- persist between sessions, so if you've already chosen, there's no need to do so again.

Visualizing a string

Troubleshooting

Contributing

  • Test the visualizer. (The significance of this kind of contribution cannot be overestimated.)
  • Suggest ideas and enhancements (issues)
  • Notify about bugs (issues)
  • Provide screenshots which could be used to demonstrate different features of the visualizer (also via (issues))
  • Star the project
  • Spread the word
  • Provide feedback to Microsoft about these limitations to the visualizer API

Roadmap

antlr4parsetreevisualizer's People

Contributors

zspitz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

antlr4parsetreevisualizer's Issues

Use XAML Behaviors for WPF

Currently fails with an exception:

System.Windows.Markup.XamlParseException: 'Could not load file or assembly 'Microsoft.Xaml.Behaviors, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.'

Inner Exception:
FileNotFoundException: Could not load file or assembly 'Microsoft.Xaml.Behaviors, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

The DLL is in the bin folder.

https://github.com/Microsoft/XamlBehaviorsWpf/wiki

Token list controls

  • Checkbox -- show/hide whitespace tokens
  • Checkbox -- show/hide error tokens
  • List of token types to filter by

Visualizer should target additional types -- string, and token source

When visualizing a token source, return a VisualizerData instance, but with a null Root. If there's a selected parser class, we can parse the tree using the token source, and return a VisualizerData with the Root filled.

Similar logic applies to the visualization of a string. Once there's a lexer class selected, we can populate the token list. Once there's a parser class, we can populate the root node. The view should be able to handle the token list and root node being empty.

This involves the following parts:

  • Define the DebuggerVisualizer attribute on both string and CommonTokenStream types
  • Handle in VisualizerData.cs
  • The config has to be able to specify whether the selected lexer can be changed (true for string), and whether the selected parser can be changed (true for string and CommonTokenStream).
  • The UI has to respect the above
  • When choosing a parser class, the user also needs to choose a parse method (we can try startRule by default, if it exists).
  • Additionaly, if no lexer has been selected, replace the datagrid in the UI with a message "Select a lexer class from settings to see tokens"
  • Same for parser class and treeview

Algorithm for identifying the parser class

This is important for getting rule and token names (#7) and for storing settings per class/assembly.

On the debugger side, iterate through all the assemblies, find all the classes that inherit from Antlr4.Runtime.Parser and are marked with the GeneratedCode attribute by ANTLR. If there is only one, that is the parser class.

On the debuggee side, present the user with all the available parser classes, and pass it along in the settings object.

Settings persistence

Store JSON in %ProgramData%.

File should be read when visualizer is first started.

File should be written at two points:

  1. after TransferObject returns (as the debuggee might have changed settings)
  2. after PropertyChanged is triggered on settings object

How can we persist parser specific settings? If we have a single settings file, we'll have to find some way of merging settings from multiple parser/lexer classes into a single object.

Alternatively, we could store each lexer / parser settings in its own file. We'd need some sort of naming scheme, as we couldn't use \ from the path as the file name.

We could simply use numbers and store the corresponding assembly / classname in the global settings file.

Selection sync

The visualizer currently has three areas whose selections should be synced:

  • the source
  • the treeview
  • the token list
Starting control Results
Source Determine the start and end tokens covered by the selection (a selection could include partial tokens).
Select those tokens in the token list.
Find the deepest tree node whose tokens encompass the selected tokens, and select that node in the treeview
Token list Select the text in the source based on the first and last selected tokens. (It is possible to select non-contiguous tokens, but in order to reflect that in the source selection, we'd need a multi-selection textbox, which the built-in WPF textbox is not. Perhaps later.)
As above, find the deepest tree node whose tokens encompass the selected tokens, and select that node in the treeview.
Treeview Select the tokens in the token list covered by the tree node.
Select the source covered by the tokens.

ConditionalMessageContainer control

The token list and treeview both have the same behavior: the control is only shown if the ItemsSource is not null. Otherwise a text message is displayed.

This behavior should be written into a custom control. Not sure how to implement this.

Crash with large grammars and parse trees

Attached is a zip file of a solution/project containing a Java parser example. Displaying the tree with ANTLR4ParseTreeVisualizer crashes, presumably because it is a large parse tree, on release 0.1.0.63. It's good to be able to visualize non-trivial large trees, especially for nodes deep in the tree in a tree visitor. GetText() of a node doesn't work well, and I need to visualize where in the tree I am, e.g., with a variableDeclarator in what block.
AntlrConsole3.zip

Allow expanding collection properties

Properties that return collections should expand to a full datagrid of those items, if they have multiple properties; or a single column if a collection of scalar values.

Filter tree nodes

There should be two filtering options.

  • based on node type - rulecontext, token or trivia
  • provide a list of reflection types to be filtered on

Settings

We have to figure out how to persist settings. If we can we read/write from the debugger side to %ProgramData% that would be best. Otherwise, perhaps a local JSON file?

Some settings are global:

  • Maximum window dimensions, as a percentage of entire monitor; or absolute pixels

Some settings relate to a given parser class; the key should be the class name + the executing assembly location. But if we haven't found the parser class, they should be set globally; or perhaps using a default key

  • Last used parser class and assembly, based on the executing assembly location?
  • Pane locations, once we use some sort of docking control (AvalonDock?)

GridSplitter / DockSplitter

Currently the user can't change the layout. It would be expected that the user could drag the boundaries of each pane.

The standard way to do this is with a Grid and a GridSplitter, which I don't like, because different parts of the layout come out wrong.

I'd prefer to have a DockPanel, with some kind of splitter to adjust.

Alternatively, there are docking controls for WPF; perhaps I could use one of them. I'm wary of doing so, because it means users have to download and copy another DLL into the visualizers subfolder.

Allow viewing entire tree from node in middle of tree

When visualizing a node in the middle of a tree, the current behavior is to only display the subtree of that particular node.

Alternative behavior might be to show the entire tree, but the visualized node should be selected.

Perhaps the second behavior should be the default, and the first could be gotten to using the "Scope to this" context menu option?

@kaby76, would that cover all the bases?

Databinding in listboxes

Find some way to do databinding from listboxes and other multiselect controls, and their SelectedItems and SelectedValues properties..

Include VS 2017 debuggers DLL as third-party reference

Currently, the AppVeyor build runs against both the VS 2019 image and the VS 2017 image, in order to get hold of the VS 2017 debugger DLL.

It would be far simpler to add a reference to the DLL without having a complete install of VS 2017.

This would also allow using later C# features that are unsupported in VS 2017.

Update notification

Global setting - "Check for new release on visualizer load"

New release check: check latest release version against current version. If there's a new release, display a messagebox and ask about opening the releases page. If yes, open both the releases page and the destination folder (based on the current path of the assembly).


If the setting is true, then each time the visualizer loads, checks if there's a new release, as above.

If the setting is false, then show an explicit "Check now" button next to the setting UI. On click, check as above.

Selection management control

An independent control, for managing the selection of another control.

Can be used with DataGrid, ListBox among others.

  • Text filter
  • Select all
  • Clear selection
  • Invert selection

Properties:

  • Control (wire up in code, for now)'
  • Filter member path - path on which to apply filter

Simplify CI build

It seems the 2019 dll works just fine with VS 2017.

  • Remove 2017.sln and projects
  • Remove the 2017 image an job from appveyor.yml

MVVM testing

  • Create a token stream, then a parse tree from the input
  • successfully construct visualizer data from string, token stream, and parse tree
  • successfully construct a viewmodel from visualizer data
  • Pass an invalid/missing config to VisualizerData constructor
    • invalid lexer
    • invalid parser
    • invalid method name
  • deserialize visualizerdata, configviewmodel, visualizerdataviewmodel from stored JSON; compare against constructed
  • change selection on viewmodel; verify changes to selection
    • from viewmodel selection source properties
    • from root treeview model
    • from token list
  • Config.Clone

Relevant: https://www.pluralsight.com/courses/wpf-mvvm-test-driven-development-viewmodels

Questions:

  • Should we test autofill of lexer and parser when there is no selected? Currently we can test this, because there is only one lexer and parser in all the loaded assemblies. But we may add more lexers and parsers in the future.

Enforce proper MVVM architecture

Relates to #22

Also important if we want to create a separate visualizer on token streams. We'll have to tease apart the token list control and model into a reusable form.

Package artifacts as a single ZIP per VS version

It's better to have a single download (and unblock) from the Releases page, than to have multiple downloads; each file has to be unblocked, and through the UI it can only be done one file at a time.

We could include relative paths in the ZIP, which might be an alternative to #17 -- the main DLL always needs to be in the root Visualizers folder, but the dependency DLLs could be in a subfolder.

When source is generated from tree, channels which don't exist in the tree are not included

For example, given the following grammar:

// define a grammar called Hello
grammar Hello;
r   : 'hello' ID;
ID  : [a-z]+ ;
WS  : [ \t\r\n]+ -> skip ;

the UI might end up as follows:
image
because the selected node's span is (6, 10) which relates to the source including the skipped whitespace.

A workaround would be to construct the source by iterating over the nodes and inserting a filler character until each node's text would be in the right position.

Related: antlr/antlr4#1302 (comment)

Properties with null value

Currently returns the NRE error message, because we're trying to call ToString on null.

A number of choices:

  • Exclude such properties entirely? Don't like this.
  • Different background color.
  • Special string? -- ?

We could do both of the latter 2.

Visualize strings / standalone visualizer

This is the shared workflow for:

  • a Visual Studio debugging visualizer attached to the string type
  • a standalone application that shows the parse tree for user-input source code, and a selected lexer and parser
User Code
Choose lexer and parser assembly and class
Optionally, choose rule
Either enter source code, or load from a file
(within the debugging visualizer, this would be set by calling code)
Click Parse
Instantiate lexer, common token source, parser
Create instance of VisualizerData class
from returned parser rule context
Show visualized

@bclothier @retailcoder Does the above work for visualizing strings?

Allow scrolling source view

This is important because the text will often not fit in the textbox. Ordinarily it might be less of an issue, because you could scroll the selection to bring various parts into focus. But that doesn't work here, because the selection in the source changes the selection in other parts of the UI.

Antlr4.Runtime.Standard support

There are two versions of the ANTLR runtime for .NET:

  • Sam Harwell's original C# target, which complements his .NET ANTLR code generator (used by Rubberduck). This version is not up to date with the latest version of the Java ANTLR generator.
  • The official ANTLR runtime, called Antlr.Runtime.Standard, which supports the Java ANTLR generator. C# code is generated from the grammar using the ANTLR Java tool.

Currently, we support Sam Harwell's runtime, and we really should support both.

ATM this requires two additional end projects:

  • Standard.2019 -- for VS 2019
  • Standard.2017 -- for VS 2017

which reference the Antlr.Runtime.Standard package instead of the Antlr.Runtime package.

AFAICT there are some API differences between the two runtimes; this will have to be accounted for in the shared code somehow.

How would this play with AppVeyor?

Eventually, it would be nice to incorporate everything into a single .csproj file, and use compiler constants to control the output.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.