{"id":13484755,"url":"https://github.com/softdevteam/grmtools","last_synced_at":"2025-05-13T23:09:34.051Z","repository":{"id":37404080,"uuid":"143743108","full_name":"softdevteam/grmtools","owner":"softdevteam","description":"Rust grammar tool libraries and binaries","archived":false,"fork":false,"pushed_at":"2025-05-03T07:02:19.000Z","size":6216,"stargazers_count":542,"open_issues_count":12,"forks_count":33,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-05-03T08:23:54.189Z","etag":null,"topics":["error-recovery","generator","grammar","lex","lexer","lr","parser","rust","yacc"],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/softdevteam.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-08-06T14:50:43.000Z","updated_at":"2025-05-03T07:01:16.000Z","dependencies_parsed_at":"2024-01-02T13:25:30.402Z","dependency_job_id":"1ae5fda0-9229-4041-aef9-abf7b9fffb3b","html_url":"https://github.com/softdevteam/grmtools","commit_stats":{"total_commits":1128,"total_committers":15,"mean_commits":75.2,"dds":0.1365248226950354,"last_synced_commit":"025c592f7bc0b5a3a34717db9d8349356a5954ae"},"previous_names":[],"tags_count":34,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/softdevteam%2Fgrmtools","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/softdevteam%2Fgrmtools/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/softdevteam%2Fgrmtools/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/softdevteam%2Fgrmtools/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/softdevteam","download_url":"https://codeload.github.com/softdevteam/grmtools/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254042142,"owners_count":22004852,"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":["error-recovery","generator","grammar","lex","lexer","lr","parser","rust","yacc"],"created_at":"2024-07-31T17:01:32.587Z","updated_at":"2025-05-13T23:09:29.031Z","avatar_url":"https://github.com/softdevteam.png","language":"Rust","readme":"# Grammar and parsing libraries for Rust\n\n[![Bors enabled](https://bors.tech/images/badge_small.svg)](https://app.bors.tech/repositories/22484) [![lrpar on crates.io](https://img.shields.io/crates/v/lrpar.svg?label=lrpar)](https://crates.io/crates/lrpar) [![lrlex on crates.io](https://img.shields.io/crates/v/lrlex.svg?label=lrlex)](https://crates.io/crates/lrlex) [![lrtable on crates.io](https://img.shields.io/crates/v/lrtable.svg?label=lrtable)](https://crates.io/crates/lrtable) [![cfgrammar on crates.io](https://img.shields.io/crates/v/cfgrammar.svg?label=cfgrammar)](https://crates.io/crates/cfgrammar)\n\ngrmtools is a suite of Rust libraries and binaries for parsing text, both at\ncompile-time, and run-time. Most users will probably be interested in the\ncompile-time Yacc feature, which allows traditional `.y` files to be used\n(mostly) unchanged in Rust.\n\n## Quickstart\n\nA minimal example using this library consists of two files (in addition to the\ngrammar and lexing definitions). First we need to create a file `build.rs` in\nthe root of our project with the following content:\n\n```rust\nuse lrlex::CTLexerBuilder;\n\nfn main() {\n    CTLexerBuilder::new()\n        .lrpar_config(|ctp| {\n            ctp.grammar_in_src_dir(\"calc.y\")\n                .unwrap()\n        })\n        .lexer_in_src_dir(\"calc.l\")\n        .unwrap()\n        .build()\n        .unwrap();\n    Ok(())\n}\n```\n\nThis will generate and compile a parser and lexer, where the definitions for the\nlexer can be found in `src/calc.l`:\n\n```rust\n%%\n[0-9]+ \"INT\"\n\\+ \"+\"\n\\* \"*\"\n\\( \"(\"\n\\) \")\"\n[\\t ]+ ;\n```\n\nand where the definitions for the parser can be found in `src/calc.y`:\n\n```rust\n%grmtools{yacckind: Grmtools}\n%start Expr\n%avoid_insert \"INT\"\n%%\nExpr -\u003e Result\u003cu64, ()\u003e:\n      Expr '+' Term { Ok($1? + $3?) }\n    | Term { $1 }\n    ;\n\nTerm -\u003e Result\u003cu64, ()\u003e:\n      Term '*' Factor { Ok($1? * $3?) }\n    | Factor { $1 }\n    ;\n\nFactor -\u003e Result\u003cu64, ()\u003e:\n      '(' Expr ')' { $2 }\n    | 'INT'\n      {\n          let v = $1.map_err(|_| ())?;\n          parse_int($lexer.span_str(v.span()))\n      }\n    ;\n%%\n// Any functions here are in scope for all the grammar actions above.\n\nfn parse_int(s: \u0026str) -\u003e Result\u003cu64, ()\u003e {\n    match s.parse::\u003cu64\u003e() {\n        Ok(val) =\u003e Ok(val),\n        Err(_) =\u003e {\n            eprintln!(\"{} cannot be represented as a u64\", s);\n            Err(())\n        }\n    }\n}\n```\n\nWe can then use the generated lexer and parser within our `src/main.rs` file as\nfollows:\n\n```rust\nuse std::env;\n\nuse lrlex::lrlex_mod;\nuse lrpar::lrpar_mod;\n\n// Using `lrlex_mod!` brings the lexer for `calc.l` into scope. By default the\n// module name will be `calc_l` (i.e. the file name, minus any extensions,\n// with a suffix of `_l`).\nlrlex_mod!(\"calc.l\");\n// Using `lrpar_mod!` brings the parser for `calc.y` into scope. By default the\n// module name will be `calc_y` (i.e. the file name, minus any extensions,\n// with a suffix of `_y`).\nlrpar_mod!(\"calc.y\");\n\nfn main() {\n    // Get the `LexerDef` for the `calc` language.\n    let lexerdef = calc_l::lexerdef();\n    let args: Vec\u003cString\u003e = env::args().collect();\n    // Now we create a lexer with the `lexer` method with which we can lex an\n    // input.\n    let lexer = lexerdef.lexer(\u0026args[1]);\n    // Pass the lexer to the parser and lex and parse the input.\n    let (res, errs) = calc_y::parse(\u0026lexer);\n    for e in errs {\n        println!(\"{}\", e.pp(\u0026lexer, \u0026calc_y::token_epp));\n    }\n    match res {\n        Some(r) =\u003e println!(\"Result: {:?}\", r),\n        _ =\u003e eprintln!(\"Unable to evaluate expression.\")\n    }\n}\n```\n\nFor more information on how to use this library please refer to the [grmtools\nbook](https://softdevteam.github.io/grmtools/master/book/), which also includes\na more detailed [quickstart\nguide](https://softdevteam.github.io/grmtools/master/book/quickstart.html).\n\n## Examples\n\n[lrpar](https://github.com/softdevteam/grmtools/tree/master/lrpar/examples)\ncontains several examples on how to use the `lrpar`/`lrlex` libraries, showing\nhow to generate [parse\ntrees](https://github.com/softdevteam/grmtools/tree/master/lrpar/examples/calc_parsetree)\nand\n[ASTs](https://github.com/softdevteam/grmtools/tree/master/lrpar/examples/calc_ast), use\n[start conditions/states](https://github.com/softdevteam/grmtools/tree/master/lrpar/examples/start_states)\nor [execute\ncode](https://github.com/softdevteam/grmtools/tree/master/lrpar/examples/calc_actions)\nwhile parsing.\n\n## Documentation\n\n| Latest release                          | master |\n|-----------------------------------------|--------|\n| [grmtools book](https://softdevteam.github.io/grmtools/latest_release/book/) | [grmtools book](https://softdevteam.github.io/grmtools/master/book) |\n| [cfgrammar](https://docs.rs/cfgrammar/) | [cfgrammar](https://softdevteam.github.io/grmtools/master/api/cfgrammar/) |\n| [lrpar](https://docs.rs/lrpar/)         | [lrpar](https://softdevteam.github.io/grmtools/master/api/lrpar/)         |\n| [lrlex](https://docs.rs/lrlex/)         | [lrlex](https://softdevteam.github.io/grmtools/master/api/lrlex/)         |\n| [lrtable](https://docs.rs/lrtable/)     | [lrtable](https://softdevteam.github.io/grmtools/master/api/lrtable/)     |\n\n[Documentation for all past and present releases](https://softdevteam.github.io/grmtools/)\n","funding_links":[],"categories":["Rust","Libraries"],"sub_categories":["Parsing"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftdevteam%2Fgrmtools","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoftdevteam%2Fgrmtools","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftdevteam%2Fgrmtools/lists"}