Crates.io | makefile_parser_rs |
lib.rs | makefile_parser_rs |
version | 0.1.0 |
source | src |
created_at | 2024-11-20 06:31:34.088007 |
updated_at | 2024-11-20 06:31:34.088007 |
description | Rust library to parse Makefiles, with variables supported. |
homepage | |
repository | |
max_upload_size | |
id | 1454412 |
size | 34,458 |
Built with Rust, makefile_parser_rs
is a library main task of which is to parse Makefiles.
It supports detecting declared variables and substituting them to commands where they are used.
The parser accepts a valid Makefile and splits it as follows:
makefile = { line* }
line = { (variable_assignment | comment | rule)? ~ NEWLINE }
comment = { whitespace* ~ "#" ~ (ASCII_ALPHANUMERIC | (!NEWLINE ~ ANY))* }
variable = { (ASCII_ALPHANUMERIC | "_" | "." | "-" | "+" | "%" | "^" | "&")+ }
value = @{ ("-" | "--")? ~ (string | variable)? }
variable_assignment = {
variable
~
whitespace*
~
("=" | ":=" | "?=")
~
whitespace*
~
value
}
rule = {
target ~ ":" ~ (" " ~ dependencies)? ~ NEWLINE ~ commands
}
target = { (ASCII_ALPHANUMERIC | "_" | ".")+ }
dependencies = { target ~ (" " ~ target)* }
commands = { (command ~ NEWLINE)* ~ command }
command_variable = @{ "$(" ~ variable ~ ")" }
command_flag = { "-" ~ (ASCII_ALPHANUMERIC | "-")+ }
command_arg = { !"-" ~ variable+ }
command = {
whitespace+
~
(whitespace+ | command_arg | string | command_variable | command_flag)+
}
string = @{ "\"" ~ (!"\"" ~ ANY)* ~ "\"" }
whitespace = _{ " " | "\t" | "\r" }
makefile_parser_rs parse Makefile
or
make run args="parse Makefile"
Given a Makefile:
# This is a simple Makefile
CC = gcc
CFLAGS = -Wall
main: main.cpp my_class.cpp
clang++ -Wall -o main main.cpp my_class.cpp
$(CC) $(CFLAGS) -o some_target dependency1 dependency2
another_target: dependency3
@echo "Building another target"
Running makefile_parser_rs parse example_make
, the output is:
Makefile:
| Variables:
| | CC: gcc
| | CFLAGS: -Wall
| Comments:
| | # This is a simple Makefile
| Rule:
| | Target: main
| | Dependencies:
| | | main.cpp
| | | my_class.cpp
| | Commands:
| | | clang++ -Wall -o main main.cpp my_class.cpp
| | | $(CC) $(CFLAGS) -o some_target dependency1 dependency2
| | | gcc -Wall -o some_target dependency1 dependency2