Skip to content

Commit 11ae0cb

Browse files
committed
Replace some Regex usage with nom.
1 parent f308868 commit 11ae0cb

File tree

3 files changed

+67
-37
lines changed

3 files changed

+67
-37
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samply-symbols/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ debugid = "0.8.0"
5151
flate2 = "1"
5252
yoke = { version = "0.6.2", features = ["derive"] }
5353
nom = "7.1.1"
54-
lazy_static = "1.4.0"
5554

5655
[dev-dependencies]
5756
memmap2 = "0.5.0"

samply-symbols/src/mapped_path.rs

+67-35
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
use lazy_static::lazy_static;
21
use nom::branch::alt;
32
use nom::bytes::complete::{tag, take_until1};
43
use nom::combinator::{eof, map};
54
use nom::error::ErrorKind;
65
use nom::sequence::terminated;
76
use nom::{Err, IResult};
8-
use regex::Regex;
97

108
/// A special source file path for source files which are hosted online.
119
///
@@ -72,38 +70,9 @@ impl MappedPath {
7270

7371
/// Detect some URLs of plain text files and convert them to a `MappedPath`.
7472
pub fn from_url(url: &str) -> Option<Self> {
75-
lazy_static! {
76-
static ref GITHUB_REGEX: Regex = Regex::new(r"^https://raw\.githubusercontent\.com/(?P<repo>[^/]+/[^/]+)/(?P<rev>[^/]+)/(?P<path>.*)$").unwrap();
77-
static ref HG_REGEX: Regex = Regex::new(r"^https://(?P<repo>hg\..+)/raw-file/(?P<rev>[0-9a-f]+)/(?P<path>.*)$").unwrap();
78-
static ref S3_REGEX: Regex = Regex::new(r"^https://(?P<bucket>[^/]+).s3.amazonaws.com/(?P<digest>[^/]+)/(?P<path>.*)$").unwrap();
79-
}
80-
if let Some(captures) = GITHUB_REGEX.captures(url) {
81-
// https://raw.githubusercontent.com/baldurk/renderdoc/v1.15/renderdoc/data/glsl/gl_texsample.h
82-
// -> "git:github.com/baldurk/renderdoc:renderdoc/data/glsl/gl_texsample.h:v1.15"
83-
let repo = format!("github.com/{}", captures.name("repo").unwrap().as_str());
84-
let path = captures.name("path").unwrap().as_str().to_owned();
85-
let rev = captures.name("rev").unwrap().as_str().to_owned();
86-
Some(MappedPath::Git { repo, path, rev })
87-
} else if let Some(captures) = HG_REGEX.captures(url) {
88-
// "https://hg.mozilla.org/mozilla-central/raw-file/1706d4d54ec68fae1280305b70a02cb24c16ff68/mozglue/baseprofiler/core/ProfilerBacktrace.cpp"
89-
// -> "hg:hg.mozilla.org/mozilla-central:mozglue/baseprofiler/core/ProfilerBacktrace.cpp:1706d4d54ec68fae1280305b70a02cb24c16ff68"
90-
let repo = captures.name("repo").unwrap().as_str().to_owned();
91-
let path = captures.name("path").unwrap().as_str().to_owned();
92-
let rev = captures.name("rev").unwrap().as_str().to_owned();
93-
Some(MappedPath::Hg { repo, path, rev })
94-
} else if let Some(captures) = S3_REGEX.captures(url) {
95-
// "https://gecko-generated-sources.s3.amazonaws.com/7a1db5dfd0061d0e0bcca227effb419a20439aef4f6c4e9cd391a9f136c6283e89043d62e63e7edbd63ad81c339c401092bcfeff80f74f9cae8217e072f0c6f3/x86_64-pc-windows-msvc/release/build/swgl-59e3a0e09f56f4ea/out/brush_solid_DEBUG_OVERDRAW.h"
96-
// -> "s3:gecko-generated-sources:7a1db5dfd0061d0e0bcca227effb419a20439aef4f6c4e9cd391a9f136c6283e89043d62e63e7edbd63ad81c339c401092bcfeff80f74f9cae8217e072f0c6f3/x86_64-pc-windows-msvc/release/build/swgl-59e3a0e09f56f4ea/out/brush_solid_DEBUG_OVERDRAW.h:"
97-
let bucket = captures.name("bucket").unwrap().as_str().to_owned();
98-
let digest = captures.name("digest").unwrap().as_str().to_owned();
99-
let path = captures.name("path").unwrap().as_str().to_owned();
100-
Some(MappedPath::S3 {
101-
bucket,
102-
digest,
103-
path,
104-
})
105-
} else {
106-
None
73+
match parse_url(url) {
74+
Ok((_, mapped_path)) => Some(mapped_path),
75+
Err(_) => None,
10776
}
10877
}
10978

@@ -191,7 +160,6 @@ fn cargo_path(input: &str) -> IResult<&str, (String, String, String, String)> {
191160
))
192161
}
193162

194-
// Parse any of the line data that can occur in the body of a symbol file.
195163
fn parse_special_path(input: &str) -> IResult<&str, MappedPath> {
196164
alt((
197165
map(git_path, |(repo, path, rev)| MappedPath::Git {
@@ -220,6 +188,70 @@ fn parse_special_path(input: &str) -> IResult<&str, MappedPath> {
220188
))(input)
221189
}
222190

191+
fn github_url(input: &str) -> IResult<&str, (String, String, String)> {
192+
// Example: "https://raw.githubusercontent.com/baldurk/renderdoc/v1.15/renderdoc/data/glsl/gl_texsample.h"
193+
let (input, _) = tag("https://raw.githubusercontent.com/")(input)?;
194+
let (input, org) = terminated(take_until1("/"), tag("/"))(input)?;
195+
let (input, repo_name) = terminated(take_until1("/"), tag("/"))(input)?;
196+
let (input, rev) = terminated(take_until1("/"), tag("/"))(input)?;
197+
let path = input;
198+
Ok((
199+
"",
200+
(
201+
format!("github.com/{org}/{repo_name}"),
202+
path.to_owned(),
203+
rev.to_owned(),
204+
),
205+
))
206+
}
207+
208+
fn hg_url(input: &str) -> IResult<&str, (String, String, String)> {
209+
// Example: "https://hg.mozilla.org/mozilla-central/raw-file/1706d4d54ec68fae1280305b70a02cb24c16ff68/mozglue/baseprofiler/core/ProfilerBacktrace.cpp"
210+
let (input, _) = tag("https://hg.")(input)?;
211+
let (input, host_rest) = terminated(take_until1("/"), tag("/"))(input)?;
212+
let (input, repo) = terminated(take_until1("/raw-file/"), tag("/raw-file/"))(input)?;
213+
let (input, rev) = terminated(take_until1("/"), tag("/"))(input)?;
214+
let path = input;
215+
Ok((
216+
"",
217+
(
218+
format!("hg.{host_rest}/{repo}"),
219+
path.to_owned(),
220+
rev.to_owned(),
221+
),
222+
))
223+
}
224+
225+
fn s3_url(input: &str) -> IResult<&str, (String, String, String)> {
226+
// Example: "https://gecko-generated-sources.s3.amazonaws.com/7a1db5dfd0061d0e0bcca227effb419a20439aef4f6c4e9cd391a9f136c6283e89043d62e63e7edbd63ad81c339c401092bcfeff80f74f9cae8217e072f0c6f3/x86_64-pc-windows-msvc/release/build/swgl-59e3a0e09f56f4ea/out/brush_solid_DEBUG_OVERDRAW.h"
227+
let (input, _) = tag("https://")(input)?;
228+
let (input, bucket) =
229+
terminated(take_until1(".s3.amazonaws.com/"), tag(".s3.amazonaws.com/"))(input)?;
230+
let (input, digest) = terminated(take_until1("/"), tag("/"))(input)?;
231+
let path = input;
232+
Ok(("", (bucket.to_owned(), digest.to_owned(), path.to_owned())))
233+
}
234+
235+
fn parse_url(input: &str) -> IResult<&str, MappedPath> {
236+
alt((
237+
map(github_url, |(repo, path, rev)| MappedPath::Git {
238+
repo,
239+
path,
240+
rev,
241+
}),
242+
map(hg_url, |(repo, path, rev)| MappedPath::Hg {
243+
repo,
244+
path,
245+
rev,
246+
}),
247+
map(s3_url, |(bucket, digest, path)| MappedPath::S3 {
248+
bucket,
249+
digest,
250+
path,
251+
}),
252+
))(input)
253+
}
254+
223255
#[cfg(test)]
224256
mod test {
225257
use super::*;

0 commit comments

Comments
 (0)