| 1 | Lossless parser for Makefiles |
| 2 | ============================= |
| 3 | |
| 4 | This crate provides a lossless parser for Makefiles, producing a concrete |
| 5 | syntax tree (CST) that preserves all whitespace, comments and formatting. |
| 6 | Because nothing is discarded, a parsed Makefile can be modified and written |
| 7 | back out with only the intended changes applied. |
| 8 | |
| 9 | Parsing |
| 10 | ------- |
| 11 | |
| 12 | ```rust |
| 13 | use makefile_lossless::Makefile; |
| 14 | |
| 15 | let contents = r#"PYTHON = python3 |
| 16 | |
| 17 | .PHONY: all |
| 18 | |
| 19 | all: build |
| 20 | |
| 21 | build: |
| 22 | $(PYTHON) setup.py build |
| 23 | "#; |
| 24 | |
| 25 | let makefile: Makefile = contents.parse().unwrap(); |
| 26 | |
| 27 | assert_eq!(makefile.rules().count(), 3); |
| 28 | for rule in makefile.rules() { |
| 29 | println!("targets: {:?}", rule.targets().collect::<Vec<_>>()); |
| 30 | } |
| 31 | ``` |
| 32 | |
| 33 | You can also read from any `std::io::Read`: |
| 34 | |
| 35 | ```rust,no_run |
| 36 | use std::fs::File; |
| 37 | use makefile_lossless::Makefile; |
| 38 | |
| 39 | let makefile = Makefile::read(File::open("Makefile").unwrap()).unwrap(); |
| 40 | ``` |
| 41 | |
| 42 | Use `Makefile::read_relaxed` to tolerate syntax errors and still get a tree |
| 43 | back. |
| 44 | |
| 45 | Modifying |
| 46 | --------- |
| 47 | |
| 48 | The tree is mutable. Changes are applied in place and the rest of the file is |
| 49 | left untouched: |
| 50 | |
| 51 | ```rust |
| 52 | use makefile_lossless::Makefile; |
| 53 | |
| 54 | let mut makefile: Makefile = "all:\n".parse().unwrap(); |
| 55 | let mut rule = makefile.add_rule("build"); |
| 56 | rule.push_command("$(PYTHON) setup.py build"); |
| 57 | |
| 58 | print!("{}", makefile); |
| 59 | ``` |
| 60 | |
| 61 | License |
| 62 | ------- |
| 63 | |
| 64 | Apache-2.0 |