-
-
Notifications
You must be signed in to change notification settings - Fork 645
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[internal] Parse addresses using a generated parser (#14346)
To prepare to make the address syntax more complicated as part of #13882, move to parsing addresses using a generated `peg` parser (using the same parser crate that @jsirois introduced in #11922). Additionally, simplify `Address.spec` and `Address.path_safe_spec` slightly by removing non-trivial early returns and reducing the total number of f-strings. [ci skip-build-wheels]
- Loading branch information
Showing
10 changed files
with
177 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
version = "0.0.1" | ||
edition = "2021" | ||
name = "address" | ||
authors = [ "Pants Build <pantsbuild@gmail.com>" ] | ||
publish = false | ||
|
||
[dependencies] | ||
peg = "0.7" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2021 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
#![deny(warnings)] | ||
// Enable all clippy lints except for many of the pedantic ones. It's a shame this needs to be copied and pasted across crates, but there doesn't appear to be a way to include inner attributes from a common source. | ||
#![deny( | ||
clippy::all, | ||
clippy::default_trait_access, | ||
clippy::expl_impl_clone_on_copy, | ||
clippy::if_not_else, | ||
clippy::needless_continue, | ||
clippy::unseparated_literal_suffix, | ||
clippy::used_underscore_binding | ||
)] | ||
// It is often more clear to show that nothing is being moved. | ||
#![allow(clippy::match_ref_pats)] | ||
// Subjective style. | ||
#![allow( | ||
clippy::len_without_is_empty, | ||
clippy::redundant_field_names, | ||
clippy::too_many_arguments | ||
)] | ||
// Default isn't as big a deal as people seem to think it is. | ||
#![allow(clippy::new_without_default, clippy::new_ret_no_self)] | ||
// Arc<Mutex> can be more clear than needing to grok Orderings: | ||
#![allow(clippy::mutex_atomic)] | ||
|
||
pub struct AddressInput<'a> { | ||
pub path: &'a str, | ||
pub target: Option<&'a str>, | ||
pub generated: Option<&'a str>, | ||
} | ||
|
||
peg::parser! { | ||
grammar relative_address_parser() for str { | ||
rule path() -> &'input str = s:$([^':' | '#']*) {s} | ||
|
||
rule target_name() -> &'input str | ||
= quiet!{ s:$([^'#' | '@']+) { s } } | ||
/ expected!("a non-empty target name to follow a `:`.") | ||
|
||
rule target() -> &'input str = ":" s:target_name() { s } | ||
|
||
rule generated_name() -> &'input str | ||
= quiet!{ s:$([_]+) { s } } | ||
/ expected!("a non-empty generated target name to follow a `#`.") | ||
|
||
rule generated() -> &'input str = "#" s:generated_name() { s } | ||
|
||
pub(crate) rule relative_address() -> AddressInput<'input> | ||
= path:path() target:target()? generated:generated()? { | ||
AddressInput { | ||
path, | ||
target, | ||
generated, | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn parse_address(value: &str) -> Result<AddressInput, String> { | ||
let relative_address = relative_address_parser::relative_address(value) | ||
.map_err(|e| format!("Failed to parse Address `{value}`: {e}"))?; | ||
|
||
Ok(relative_address) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2022 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
use pyo3::create_exception; | ||
use pyo3::exceptions::PyException; | ||
use pyo3::prelude::*; | ||
|
||
use address::parse_address; | ||
|
||
create_exception!(native_engine, AddressParseException, PyException); | ||
|
||
pub fn register(py: Python, m: &PyModule) -> PyResult<()> { | ||
m.add( | ||
"AddressParseException", | ||
py.get_type::<AddressParseException>(), | ||
)?; | ||
m.add_function(wrap_pyfunction!(address_parse, m)?)?; | ||
Ok(()) | ||
} | ||
|
||
/// Parses an Address spec into: | ||
/// 1. a path component | ||
/// 2. a target component | ||
/// 3. a generated component | ||
/// | ||
#[pyfunction] | ||
fn address_parse(spec: &str) -> PyResult<(&str, Option<&str>, Option<&str>)> { | ||
let address = parse_address(spec).map_err(AddressParseException::new_err)?; | ||
Ok((address.path, address.target, address.generated)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters