-
-
Notifications
You must be signed in to change notification settings - Fork 636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[internal] Parse addresses using a generated parser #14346
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was really impressed with how easy this crate was to use: good choice @jsirois!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love PEG's. (first and only placed I've used/seen them is in
Lua
, which I think is where they originated, from Mr Roberto I)