Introduction

edit

The Meziantou.Framework.Language.* packages parse text into an immutable syntax tree that keeps every character of the input: whitespace, comments, quotes, line breaks, and even the mistakes. Write the tree back and you get the input exactly; change one node and only that part of the text changes. They are made for tools that work on the text itself, such as a formatter, a linter, an editor feature, or a script that updates a configuration file without rewriting it.

What you can do with them

  • Read a document as it is written: walk its nodes, find the node at a position, read the comments. See Nodes and tokens and Trivia.
  • Report problems with their position: parsing never throws, whatever the input, and every problem is a diagnostic with a line and a column. See Diagnostics.
  • Edit without reformatting: replace, insert or remove a node, and keep the formatting and the comments of everything else. See Editing trees.
  • Inspect a tree from the command line: dump it as JSON without writing any C#. See Command-line tool.

Packages

Every language is a package of its own. Each one references Meziantou.Framework.Language, the package that holds what they share, so a span, a text change, or a diagnostic means the same thing whichever language produced it.

PackageParsesEntry point
Meziantou.Framework.Language.CssStyle sheets and declaration lists, with nesting, selectors and at-rulesCssSyntaxTree. See CSS.
Meziantou.Framework.Language.IniINI files, in the dialect you chooseIniSyntaxTree. See INI.
Meziantou.Framework.Language.JsonJSON, with comments and trailing commasJsonSyntaxTree. See JSON.
Meziantou.Framework.Language.RegexRegular expression patterns: .NET, JavaScript, PCRE, POSIXRegexSyntaxTree. See Regular expressions.
Meziantou.Framework.Language.ShellScripts: sh, bash, zsh, PowerShell, cmdShellSyntaxTree. See Shell scripts.
Meziantou.Framework.Language.TomlTOML 1.0 and 1.1TomlSyntaxTree. See TOML.
Meziantou.Framework.Language.XmlXML, with XPath queries and a formatterXmlSyntaxTree. See XML.
Meziantou.Framework.Language.ToolAll of the above, from the command lineMeziantou.Framework.Language.Tool syntax. See Command-line tool.

Meziantou.Framework.Language is not useful on its own: install the package of the language you parse, and it comes with it.

Choose the right tool

A syntax tree is the right model when the text matters: its layout, its comments, the position of each part. When you only need the data, a serializer is simpler and uses less memory.

You want toUse
Update a value in a package.json, a .csproj, an .editorconfig or a Cargo.toml, and keep the rest of the file as it isThe language package: Editing trees
Report the problems of a file with their line and columnThe language package: Diagnostics
Find which group of a regular expression, or which command of a script, is at a positionRegexSyntaxTree or ShellSyntaxTree with FindToken
Load a TOML file into your own types, or save themMeziantou.Framework.Toml: see TOML
Highlight code in a web pageMeziantou.Framework.SyntaxHighlighting: see Syntax highlighting

Meziantou.Framework.Language.Toml and Meziantou.Framework.Toml both parse TOML, for different purposes: the first gives you a tree to read and edit the text, with the same model as the other languages here; the second maps documents to .NET objects. See Two TOML packages.

Namespaces

NamespaceContains
Meziantou.Framework.LanguageWhat every language shares: SyntaxTree, SyntaxNode, SyntaxToken, SyntaxTrivia, the lists, SourceText, TextSpan, TextChange, Location, Diagnostic, SyntaxAnnotation, and the editing extension methods (ReplaceNode, RemoveNode, …)
Meziantou.Framework.Language.<Language>The tree, the nodes, the options, the visitors, SyntaxKind, SyntaxFactory and SyntaxFacts of one language, such as Meziantou.Framework.Language.Json

Each language namespace is a child of Meziantou.Framework.Language, so a file that has using Meziantou.Framework.Language.Json; can use the shared types without a second using directive. The examples of this documentation omit the using directives.

Next steps