Crate mf2_parser

source
Expand description

A parser for the MessageFormat 2 syntax.

The parser is able to successfully parse any string into an AST, even if the string contains invalid syntax. The diagnostics will contain any errors that were found during parsing.

Use the mf2_printer to pretty-print the AST back into the human-readable MessageFormat 2 syntax.

§Example

use mf2_parser::parse;

let (ast, diagnostics, source_text_info) = parse("Hello, {$name}!");
if !diagnostics.is_empty() {
  panic!("Failed to parse message: {:?}", diagnostics);
}

println!("AST: {:?}", ast);

Modules§

Structs§

  • A line and column index pair, both 0-based, for the UTF-8 encoding of the source text.
  • A line and column index pair, both 0-based, for the UTF-16 encoding of the source text.
  • A location is an opaque value that is used to represent a position in the source text. It can be mapped to UTF-8 byte indices, UTF-8 line and column, or UTF-16 line and column indices in the source text using the SourceTextInfo struct.
  • A view onto the source text, with additional information about the source text that was derived during parsing.
  • A span is a pair of Locations that represent a range in the source text.

Enums§

  • Diagnostics that can be produced by the parser. Each diagnostic has a message that describes the error, and a span that indicates the location in the source text where the error occurred.

Traits§

  • The Visit trait is used to traverse the AST. You can implement this trait to visit each node in the AST in source text order.
  • The VisitAny trait is used to visit the AST without having to know the specific shape of each node. There are two methods, VisitAny::before and VisitAny::after, which are called before and after visiting the children of a given node, respectively.
  • The Visitable trait is used to apply a Visitor to an AST node.

Functions§

  • Check if a string is a syntactically valid name in MF2.
  • Parse a message and return the AST, diagnostics, and source text info.