MCPcopy Index your code
hub / github.com/dalance/nom-tracable

github.com/dalance/nom-tracable @v0.9.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.9.1 ↗ · + Follow
84 symbols 180 edges 6 files 14 documented · 17%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

nom-tracable

Extension of nom to trace parser.

Actions Status Crates.io Docs.rs

Feature

  • Tracing parser by colored format
  • Forward/backward call count
  • Folding the specific parsers
  • Histogram/cumulative histogram of parser call count
  • Zero-overhead when trace is disabled

nom-tracable

Requirement

nom must be 5.0.0 or later. nom-tracable can be applied to function-style parser only.

The input type of nom parser must implement Tracable trait. Therefore &str and &[u8] can't be used. You can define a wrapper type of &str or &[u8] and implement Tracable.

nom-tracable is integrated with nom_locate. You can use nom_locate::LocatedSpan<T, TracableInfo> as input type. This implements Tracable in this crate.

Note: T in nom_locate::LocatedSpan<T, TracableInfo> must implement FragmentDisplay. &str and &[u8] implement it in this crate. If you want to use another type as T, you should implement FragmentDisplay for it.

Usage

[features]
default = []
trace   = ["nom-tracable/trace"]

[dependencies]
nom-tracable = "0.9.1"

nom-tracable provides trace feature, and the crate using nom-tracable must provide the feature too. When trace is enabled, trace dump is enabled. If not, there is no additional cost.

Example

You can try examples by the following command.

$ cargo run --manifest-path=nom-tracable/Cargo.toml --example str_parser --features trace
$ cargo run --manifest-path=nom-tracable/Cargo.toml --example u8_parser --features trace

str_parser is below:

use nom::branch::*;
use nom::character::complete::*;
use nom::IResult;
use nom_locate::LocatedSpan;
use nom_tracable::{cumulative_histogram, histogram, tracable_parser, TracableInfo};

// Input type must implement trait Tracable
// nom_locate::LocatedSpan<T, TracableInfo> implements it.
type Span<'a> = LocatedSpan<&'a str, TracableInfo>;

// Apply tracable_parser by custom attribute
#[tracable_parser]
pub fn expr(s: Span) -> IResult<Span, String> {
    alt((expr_plus, expr_minus, term))(s)
}

#[tracable_parser]
pub fn expr_plus(s: Span) -> IResult<Span, String> {
    let (s, x) = term(s)?;
    let (s, y) = char('+')(s)?;
    let (s, z) = expr(s)?;
    let ret = format!("{}{}{}", x, y, z);
    Ok((s, ret))
}

#[tracable_parser]
pub fn expr_minus(s: Span) -> IResult<Span, String> {
    let (s, x) = term(s)?;
    let (s, y) = char('-')(s)?;
    let (s, z) = expr(s)?;
    let ret = format!("{}{}{}", x, y, z);
    Ok((s, ret))
}

#[tracable_parser]
pub fn term(s: Span) -> IResult<Span, String> {
    let (s, x) = term_internal(s)?;
    Ok((s, x))
}

#[tracable_parser]
pub fn term_internal(s: Span) -> IResult<Span, String> {
    let (s, x) = char('1')(s)?;
    Ok((s, x.to_string()))
}

fn main() {
    // Configure trace setting
    let info = TracableInfo::new().parser_width(64).fold("term");
    let ret = expr(LocatedSpan::new_extra("1-1+1+1-1", info));

    // Show histogram
    histogram();
    cumulative_histogram();
}

License

Licensed under either of

  • Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
  • MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Extension points exported contracts — how you extend this code

HasTracableInfo (Interface)
Trait to indicate `TracableInfo` is provided. [3 implementers]
nom-tracable/src/lib.rs
FragmentDisplay (Interface)
Trait to indicate the type can display as fragment. [2 implementers]
nom-tracable/src/lib.rs
Tracable (Interface)
Trait to indicate the type has information for tracing. [2 implementers]
nom-tracable/src/lib.rs

Core symbols most depended-on inside this repo

get_tracable_info
called by 6
nom-tracable/src/lib.rs
fold
called by 5
nom-tracable/src/lib.rs
folded
called by 4
nom-tracable/src/lib.rs
set_tracable_info
called by 4
nom-tracable/src/lib.rs
histogram
called by 4
nom-tracable/src/lib.rs
cumulative_histogram
called by 4
nom-tracable/src/lib.rs
parser_width
called by 3
nom-tracable/src/lib.rs
expr
called by 3
nom-tracable/examples/u8_custom_parser.rs

Shape

Method 42
Function 36
Class 3
Interface 3

Languages

Rust100%

Modules by API surface

nom-tracable/src/lib.rs44 symbols
nom-tracable/examples/u8_custom_parser.rs18 symbols
nom-tracable/tests/test.rs6 symbols
nom-tracable/examples/u8_parser.rs6 symbols
nom-tracable/examples/str_parser.rs6 symbols
nom-tracable-macros/src/lib.rs4 symbols

For agents

$ claude mcp add nom-tracable \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page