SyntaxEditor for WPF, our syntax-highlighting code editor control, has just made some big improvements to the Oslo Dataflow (MGrammar) Add-on. The enhancements include the ability to asynchronously parse text and return AST and syntax error results. These updates appear in the latest WPF Studio build.
What is the Oslo Dataflow (MGrammar) Add-on?
The add-on is a free component that any SyntaxEditor for WPF customer can use. It allows you to easily integrate parsers created via MGrammar with SyntaxEditor. Previously, the add-on supported tokenization (lexing) and was able to drive syntax highlighting within the editor, all with just a few lines of code, as described in this previous post.
Parsing, AST construction, and error reporting
The add-on now includes a new DataflowParser class that implements IParser. When registered with your language, documents using the language that are changed (generally by user typing) automatically log a request to have parsing done. The parsing requests are queued and call back to the IParser on a worker thread. At this point the IParser performs a parsing operation. In the case of DataflowParser, the parsing operation is a call to Oslo’s DynamicParser. Our DataflowParser then returns an object of type IDataflowParseData, which has a property containing the AST graph node result along with a list of IParseError objects, if any. The IParseError objects indicate syntax errors that occurred during the parse.
|

|
The screenshot above shows the updated sample included with WPF Studio. It now listens to the document’s ParseDataChanged event, which is an event that fires whenever the document’s ParseData property is updated. From this event, we write out the AST in the pane on the right and list the errors, if any, on the bottom.
Again, the parsing operation takes place on a worker thread, courtesy of our advanced parsing framework, and the result is returned asynchronously to the document’s ParseData property. This ensures that the parsing doesn’t block the UI thread.
Enabling the new parsing features
One line of code added to the code described in this previous post enables all the multi-threaded functionality to parse your documents and update the ParseData property asynchronously:
1: language.RegisterService<IParser>(new DataflowParser(parser));
What this does is register a new DataflowParser object as an IParser service with the language being used. The parser variable in the code above is assumed to contain a DynamicParser instance, which is what is loaded from your MGrammar image.
Summary
With these recent updates, your MGrammar-based parsers can completely drive SyntaxEditor’s lexing, syntax highlighting, parsing, AST construction, and error reporting, all with just a few lines of code. It’s now easier than ever to add a code editor to your WPF applications that integrate with your MGrammar DSLs.