{"id":13822797,"url":"https://github.com/fflorent/nom_locate","last_synced_at":"2025-05-15T23:04:25.180Z","repository":{"id":44694059,"uuid":"92660094","full_name":"fflorent/nom_locate","owner":"fflorent","description":"A special input type for nom to locate tokens","archived":false,"fork":false,"pushed_at":"2025-02-03T18:30:52.000Z","size":127,"stargazers_count":232,"open_issues_count":15,"forks_count":39,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-05-15T10:43:48.697Z","etag":null,"topics":["nom","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fflorent.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-05-28T12:33:04.000Z","updated_at":"2025-05-12T16:02:30.000Z","dependencies_parsed_at":"2023-12-22T16:01:15.465Z","dependency_job_id":null,"html_url":"https://github.com/fflorent/nom_locate","commit_stats":{"total_commits":97,"total_committers":28,"mean_commits":"3.4642857142857144","dds":0.7010309278350515,"last_synced_commit":"c61618312d96a51cd7b957831b03dfbbcc5f58c7"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fflorent%2Fnom_locate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fflorent%2Fnom_locate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fflorent%2Fnom_locate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fflorent%2Fnom_locate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fflorent","download_url":"https://codeload.github.com/fflorent/nom_locate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254436944,"owners_count":22070946,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nom","rust"],"created_at":"2024-08-04T08:02:18.086Z","updated_at":"2025-05-15T23:04:25.140Z","avatar_url":"https://github.com/fflorent.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"# nom_locate\n\n[![Coverage Status](https://coveralls.io/repos/github/fflorent/nom_locate/badge.svg?branch=master)](https://coveralls.io/github/fflorent/nom_locate?branch=master)\n[![](https://img.shields.io/crates/v/nom_locate.svg)](https://crates.io/crates/nom_locate)\n\nA special input type for [nom](https://github.com/geal/nom) to locate tokens\n\n## Documentation\n\nThe documentation of the crate is available [here](https://docs.rs/nom_locate/).\n\n## How to use it\nThe crate provide the [`LocatedSpan` struct](https://docs.rs/nom_locate/latest/nom_locate/struct.LocatedSpan.html) that encapsulates the data. Look at the below example and the explanations:\n\n````rust\nextern crate nom;\nextern crate nom_locate;\n\nuse nom::bytes::complete::{tag, take_until};\nuse nom::IResult;\nuse nom_locate::{position, LocatedSpan};\n\ntype Span\u003c'a\u003e = LocatedSpan\u003c\u0026'a str\u003e;\nstruct Token\u003c'a\u003e {\n    pub position: Span\u003c'a\u003e,\n    pub _foo: \u0026'a str,\n    pub _bar: \u0026'a str,\n}\n\nfn parse_foobar(s: Span) -\u003e IResult\u003cSpan, Token\u003e {\n    let (s, _) = take_until(\"foo\")(s)?;\n    let (s, pos) = position(s)?;\n    let (s, foo) = tag(\"foo\")(s)?;\n    let (s, bar) = tag(\"bar\")(s)?;\n\n    Ok((\n        s,\n        Token {\n            position: pos,\n            _foo: foo.fragment(),\n            _bar: bar.fragment(),\n        },\n    ))\n}\n\nfn main() {\n    let input = Span::new(\"Lorem ipsum \\n foobar\");\n    let output = parse_foobar(input);\n    let position = output.unwrap().1.position;\n    assert_eq!(position, unsafe {\n        Span::new_from_raw_offset(\n            14, // offset\n            2,  // line\n            \"\", // fragment\n            (), // extra\n        )\n    });\n    assert_eq!(position.get_column(), 2);\n}\n````\n\n### Import\n\nImport [nom](https://github.com/geal/nom) and nom_locate.\n\n````rust\nextern crate nom;\nextern crate nom_locate;\n\nuse nom::bytes::complete::{tag, take_until};\nuse nom::IResult;\nuse nom_locate::{position, LocatedSpan};\n````\n\nAlso you'd probably create [type alias](https://doc.rust-lang.org/book/type-aliases.html) for convenience so you don't have to specify the `fragment` type every time:\n\n````rust\ntype Span\u003c'a\u003e = LocatedSpan\u003c\u0026'a str\u003e;\n````\n\n### Define the output structure\n\nThe output structure of your parser may contain the position as a `Span` (which provides the `index`, `line` and `column` information to locate your token).\n\n````rust\nstruct Token\u003c'a\u003e {\n    pub position: Span\u003c'a\u003e,\n    pub _foo: \u0026'a str,\n    pub _bar: \u0026'a str,\n}\n````\n\n### Create the parser\n\nThe parser has to accept a `Span` as an input. You may use `position()` in your nom parser, in order to capture the location of your token:\n\n````rust\nfn parse_foobar(s: Span) -\u003e IResult\u003cSpan, Token\u003e {\n    let (s, _) = take_until(\"foo\")(s)?;\n    let (s, pos) = position(s)?;\n    let (s, foo) = tag(\"foo\")(s)?;\n    let (s, bar) = tag(\"bar\")(s)?;\n\n    Ok((\n        s,\n        Token {\n            position: pos,\n            _foo: foo.fragment(),\n            _bar: bar.fragment(),\n        },\n    ))\n}\n````\n\n### Call the parser\n\nThe parser returns a `nom::IResult\u003cToken, _\u003e` (hence the `unwrap().1`). The `position` property contains the `offset`, `line` and `column`.\n\n````rust\nfn main() {\n    let input = Span::new(\"Lorem ipsum \\n foobar\");\n    let output = parse_foobar(input);\n    let position = output.unwrap().1.position;\n    assert_eq!(position, unsafe {\n        Span::new_from_raw_offset(\n            14, // offset\n            2,  // line\n            \"\", // fragment\n            (), // extra\n        )\n    });\n    assert_eq!(position.get_column(), 2);\n}\n````\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffflorent%2Fnom_locate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffflorent%2Fnom_locate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffflorent%2Fnom_locate/lists"}