Crate minparser

Source
Expand description

Simple parsing tools

This crate is a collection of objects and algorithms shared among different crates that needs to implement a parser.

A Position is an object that identifies a (textual) file and a position inside it, represented as a line index and a column index. The main role of a Position object is to uniquely identify a single character or a token inside a file in order to allow the user to easily find it.

A View<'a, D, F> can be seen as a suffix of a larger string with the position of its first character and some data of type D. The match_tool method can be used to match its prefix with any object implementing the ParseTool trait which represents a pattern that can be satisfied or not by a string.

Many useful parsing tools can be found in tools and utils modules.

§Usage example

use minparser::prelude::*;
let view = ViewFile::new_default("My string   value");
let (step, mtc) = view.match_tool_string("My string").unwrap();
assert_eq!(mtc, "My string");
assert_eq!(step.get_view(), "   value"); 
//let step = step.match_tool(minparser::utils::WhiteTool).unwrap();   // Use the WhiteTool tool to
//assert_eq!(step.get_view(), "value");                               //match a sequence of whitespaces
//assert!(step.match_tool('a').is_err()); // A missing match is an error

§Main objects

§Position and Pos<T>.

A Position<F> is an object that holds a location inside a pool of different resources, where each resource can be identified as a string of textual data.

These object holds the following information:

  • a file field of type F that identify a single resource inside your pool. Its type is provided by the user. If you work on a single resource then you should use NoFile as file.
  • a position inside such resource, which is represented as the line number and column number, both of type u32. Lines here can be separated by either \n or \r\n, and it is an error if any \r character in the resource is not followed by the \n character.

The Pos<T, F> is just a pairing of an object of type T and a Position<F>.

§View<'a, F>

A View<'a, F> holds a reference to a str with lifetime 'a and a Position<F> that locates the first character inside the resources pool. The most important method is match_tool that tests if any prefix of the view matches the provided pattern (called here tool) and if it matches then it strips away the matched prefix, or an error if no prefix matches it.

The match_tool_string also provides you the string prefix that matches the tool.

Tip: If you want to evaluate the original view after a missing match then you can clone it (which is possible when F implements Clone).

§Tools

A tool is an object that implements the ParseTool<'a, F> trait. These objects incapsulates patterns that a string prefix may or may not satisfy. the tools submodule provides many primitive tools which you can use to implement more sofisticated ones.

§Parsable objects

The Parsable<'a, F> traits represents an object that may be inizialized by parsing a string prefix. Just like match_tool, the parse and similar methods in View can be used to inizialize Parsable objects.

§Documented features

A list of features you can optionally enable. None of these are enabled by default:

  • alloc: Links the library with the alloc crate and enable associated functions that works with Vec and other containers in alloc;
  • nightly-features: Enable experimental features. Warning: it requires nightly builds and breaking changes may be introduced even at minor releases.

Modules§

parsable
Module for the Parsable trait in order to match and parse custom objects.
pos
Positions.
predicates
Simple predicates for characters.
prelude
Crate prelude
tools
Parsing tools
utils
Other useful parsing tools.
view
View and other associated utilities.