Expand description
§mf2_parser
The mf2_parser
crate provides a parser for the Message Format 2 syntax. It can
parse any sequence of Unicode scalar values (valid UTF-8) into an AST
representing the Message Format 2 syntax. The parser has very strong error
recovery, so it can parse even very broken or incomplete input (like is common
in editors).
Use the mf2_printer
crate to pretty-print the AST back into the human-readable
MessageFormat 2 syntax.
§Usage
Add this to your Cargo.toml
:
[dependencies]
mf2_parser = "0.1"
Then you can parse a string like this:
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);
§License
This project is licensed under GPL-3.0-or-later.
Modules§
Structs§
- Line
ColUtf8 - A line and column index pair, both 0-based, for the UTF-8 encoding of the source text.
- Line
ColUtf16 - A line and column index pair, both 0-based, for the UTF-16 encoding of the source text.
- Location
- 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.
- Source
Text Info - A view onto the source text, with additional information about the source text that was derived during parsing.
- Span
- A span is a pair of Locations that represent a range in the source text.
Enums§
- Diagnostic
- 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§
- Spanned
- Visit
- 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.
- Visit
Any - 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.
- Visitable
- The Visitable trait is used to apply a Visitor to an AST node.
Functions§
- is_
valid_ name - Check if a string is a syntactically valid name in MF2.
- parse
- Parse a message and return the AST, diagnostics, and source text info.