From 11e7aaf6e4841274a8023020943e9458700ae3a3 Mon Sep 17 00:00:00 2001 From: Yotam Ofek Date: Sat, 15 Feb 2025 22:36:11 +0000 Subject: [PATCH 1/3] jsondocck: minor cleanups - replace `OnceLock` with `LazyLock` - use `let..else` where applicable --- src/tools/jsondocck/src/main.rs | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/tools/jsondocck/src/main.rs b/src/tools/jsondocck/src/main.rs index 7bfa7e3355d3f..3f3a524217a09 100644 --- a/src/tools/jsondocck/src/main.rs +++ b/src/tools/jsondocck/src/main.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; use std::process::ExitCode; -use std::sync::OnceLock; +use std::sync::LazyLock; use std::{env, fs}; use regex::{Regex, RegexBuilder}; @@ -151,8 +151,7 @@ impl CommandKind { } } -static LINE_PATTERN: OnceLock = OnceLock::new(); -fn line_pattern() -> Regex { +static LINE_PATTERN: LazyLock = LazyLock::new(|| { RegexBuilder::new( r#" //@\s+ @@ -165,7 +164,7 @@ fn line_pattern() -> Regex { .unicode(true) .build() .unwrap() -} +}); fn print_err(msg: &str, lineno: usize) { eprintln!("Invalid command: {} on line {}", msg, lineno) @@ -184,21 +183,17 @@ fn get_commands(template: &str) -> Result, ()> { for (lineno, line) in file.split('\n').enumerate() { let lineno = lineno + 1; - let cap = match LINE_PATTERN.get_or_init(line_pattern).captures(line) { - Some(c) => c, - None => continue, + let Some(cap) = LINE_PATTERN.captures(line) else { + continue; }; - let negated = cap.name("negated").unwrap().as_str() == "!"; + let negated = &cap["negated"] == "!"; let args_str = &cap["args"]; - let args = match shlex::split(args_str) { - Some(args) => args, - None => { - print_err(&format!("Invalid arguments to shlex::split: `{args_str}`",), lineno); - errors = true; - continue; - } + let Some(args) = shlex::split(args_str) else { + print_err(&format!("Invalid arguments to shlex::split: `{args_str}`",), lineno); + errors = true; + continue; }; if let Some((kind, path)) = CommandKind::parse(&cap["cmd"], negated, &args) { From 94645f6d102b2e7e720ffa1c759069c378bfd219 Mon Sep 17 00:00:00 2001 From: Yotam Ofek Date: Sat, 15 Feb 2025 22:49:00 +0000 Subject: [PATCH 2/3] jsondocck: catch and error on deprecated syntax --- src/tools/jsondocck/src/main.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/tools/jsondocck/src/main.rs b/src/tools/jsondocck/src/main.rs index 3f3a524217a09..54249fbd9ae71 100644 --- a/src/tools/jsondocck/src/main.rs +++ b/src/tools/jsondocck/src/main.rs @@ -166,6 +166,18 @@ static LINE_PATTERN: LazyLock = LazyLock::new(|| { .unwrap() }); +static DEPRECATED_LINE_PATTERN: LazyLock = LazyLock::new(|| { + RegexBuilder::new( + r#" + //\s+@ + "#, + ) + .ignore_whitespace(true) + .unicode(true) + .build() + .unwrap() +}); + fn print_err(msg: &str, lineno: usize) { eprintln!("Invalid command: {} on line {}", msg, lineno) } @@ -183,6 +195,12 @@ fn get_commands(template: &str) -> Result, ()> { for (lineno, line) in file.split('\n').enumerate() { let lineno = lineno + 1; + if DEPRECATED_LINE_PATTERN.is_match(line) { + print_err("Deprecated command syntax, replace `// @` with `//@ `", lineno); + errors = true; + continue; + } + let Some(cap) = LINE_PATTERN.captures(line) else { continue; }; From 797ef6455e782ec0ec4b6dd725c8ec70746e3e2d Mon Sep 17 00:00:00 2001 From: Yotam Ofek Date: Sat, 15 Feb 2025 22:49:21 +0000 Subject: [PATCH 3/3] htmldocck: catch and error on deprecated syntax --- src/etc/htmldocck.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/etc/htmldocck.py b/src/etc/htmldocck.py index d6b594aca71a2..06fc6518e3b1b 100755 --- a/src/etc/htmldocck.py +++ b/src/etc/htmldocck.py @@ -297,10 +297,24 @@ def filter_line(line): re.X | re.UNICODE, ) +DEPRECATED_LINE_PATTERN = re.compile( + r""" + //\s+@ +""", + re.X | re.UNICODE, +) + def get_commands(template): with io.open(template, encoding="utf-8") as f: for lineno, line in concat_multi_lines(f): + if DEPRECATED_LINE_PATTERN.search(line): + print_err( + lineno, + line, + "Deprecated command syntax, replace `// @` with `//@ `", + ) + continue m = LINE_PATTERN.search(line) if not m: continue