Crate vmf_parser_nom

source ·
Expand description

A parser for the Valve map format. Also a provided convience macro for iterating over subblocks using the traversal crate.

Vmf Format

See parse() for the implementation. Read more about the vmf format on Valve Developer Community

// This is a comment.
ClassName_1
{
	"Property_1" "Value_1"
	"Property_2" "Value_2"
	ClassName_2
	{
		"Property_1" "Value_1"
	}
	ClassName_3
	{
	}
}

Example

use vmf_parser_nom::ast::{Block};
use vmf_parser_nom::parse;
use vmf_parser_nom::error::{VerboseError, SimpleError, ErrorKind};

let input = "ClassName_1
{
\t\"Property_1\" \"Value_1\"
\t\"Property_2\" \"Value_2\"
\tClassName_2
\t{
\t\t\"Property_1\" \"Value_1\"
\t}
\tClassName_3
\t{
\t}
}";

// parse the input to a vmf, borrowing from input
let vmf = parse::<&str, ()>(input).unwrap();
println!("vmf:\n{vmf}");
assert_eq!(input, vmf.to_string());
 
// handy method to generate new ids so you don't have to deal with them
// same as display with alternate flag
assert_eq!(vmf.to_string_new_ids(), format!("{vmf:#}"));

// parse to owned strings instead
let vmf_owned = parse::<String, ()>(input).unwrap();

// All valid error types
let invalid_input = "block{\"property_with_no_value\"}";
let err_verbose = parse::<&str, VerboseError<_>>(invalid_input).unwrap_err();
let err_simple = parse::<&str, SimpleError<_>>(invalid_input).unwrap_err();
let err_tuple = parse::<&str, (_, ErrorKind)>(invalid_input).unwrap_err();
let err_unit = parse::<&str, ()>(invalid_input).unwrap_err();

println!("verbose: {err_verbose:?}");
println!("simple: {err_simple:?}");
println!("tuple: {err_tuple:?}");
println!("unit: {err_unit:?}");

// implements Deref
let block: &Block<String> = &vmf_owned;
assert_eq!(vmf_owned.inner, *block);

// inner value is simply a block with no properties
assert_eq!(vmf_owned.inner.name, "root");
assert_eq!(vmf_owned.inner.props, vec![]);
assert!(!vmf_owned.inner.blocks.is_empty());

Modules

  • Abstract syntax tree representing a vmf file.
  • Re-exports of nom errors for conveinience.
  • Parse Vmf from a str

Macros

  • Macro for making an iterator over all the children of a block using the traversal crate. Calls .as_ref() on input so works for Vmfs and Blocks. Usage is traverse!(<traverse struct>, block).

Functions

  • Parse a &str into a Vmf, completely ignoring whitespace. You can specify the output string type to be any type that implements From<&str>.