Crate untwine

source ·
Expand description

A declarative parsing library for extremely simple, concise parsing. See parser! for usage instructions.

Example JSON parser using Untwine:

untwine::parser! {
    [error = ParseJSONError]
    sep = #{char::is_ascii_whitespace}*;
    comma = sep "," sep;
    int: num=<"-"? '0'-'9'+> -> JSONValue { JSONValue::Int(num.parse()?) }
    float: num=<"-"? '0'-'9'+ "." '0'-'9'+> -> JSONValue { JSONValue::Float(num.parse()?) }
    str_char = ("\\" . | [^"\""]) -> char;
    str: "\"" chars=str_char* "\"" -> JSONValue { JSONValue::String(chars.into_iter().collect()) }
    null: "null" -> JSONValue { JSONValue::Null }
    bool: bool=<"true" | "false"> -> JSONValue { JSONValue::Bool(bool == "true") }
    list: "[" sep values=json$comma* sep "]" -> JSONValue { JSONValue::List(values) }
    map_entry: key=str sep ":" sep value=json -> (String, JSONValue) { (key.string().unwrap(), value) }
    map: "{" sep values=map_entry$comma* sep "}" -> JSONValue { JSONValue::Map(values.into_iter().collect()) }
    pub json = (bool | null | str | float | int | list | map) -> JSONValue;
}

Re-exports§

Modules§

Macros§

  • Generate parsers using a series of declarative patterns and blocks evaluating their values.

Functions§

  • Parse a value with a parser function created by the parser! block.
  • Parse a value with a parser function created by the parser! block, and convert the error to a pretty string if there is one.
  • Launches a (very) simple REPL where you can enter individual lines and see the parser output, useful for testing. Multiline inputs are not supported, so literal \n in the input will be replaced with a newline.