pub use fancy_regex::Regex;
use std::sync::LazyLock;
const UINT: &str = r"[0-9]+";
const ISO_DATE: &str = r"\d{4}-\d{2}-\d{2} (?: T \d{2}:\d{2} (?: :\d{2} )?
(?: Z | [+\-]\d{2}:\d{2} )? )?";
fn compile(pattern: &str) -> Regex {
let body = format!("(?x){}", pattern);
Regex::new(&body).unwrap_or_else(|e| panic!("regex compile failed: {e}\n---\n{body}\n---"))
}
pub fn is_left_boundary(text: &str, start: usize) -> bool {
if start == 0 {
return true;
}
let bytes = text.as_bytes();
matches!(bytes[start - 1], b' ' | b'\t' | b'\r' | b'\n')
}
pub struct Grammar {
pub inter_jot_cross_id: Regex,
pub inter_jot_same_id: Regex,
pub inter_jot_cross_file: Regex,
pub inter_jot_same_file: Regex,
pub cross_file_ref: Regex,
pub file_ref: Regex,
pub cross_id_ref: Regex,
pub id_ref: Regex,
pub resource: Regex,
pub bookmark_decl: Regex,
pub tagstring: Regex,
pub tag: Regex,
pub hashtag: Regex,
pub category: Regex,
pub word_full: Regex,
pub rej_annot_before_sep_id: Regex,
pub rej_annot_before_sep_file_same: Regex,
pub rej_annot_before_sep_file_cross: Regex,
pub rej_spec_on_loc_id: Regex,
pub rej_spec_on_loc_file_same: Regex,
pub rej_spec_on_loc_file_cross: Regex,
pub rej_cat_run: Regex,
pub rej_filename_doubledot: Regex,
pub rej_filename_start_file_cross: Regex,
pub rej_frag_nonnum_id: Regex,
pub rej_frag_nonnum_file_same: Regex,
pub rej_frag_nonnum_file_cross: Regex,
pub rej_frag_nonnum_res: Regex,
pub rej_mutex_id: Regex,
pub rej_mutex_file_same: Regex,
pub rej_mutex_file_cross: Regex,
pub rej_mutex_res: Regex,
pub rej_mutex_inter_id: Regex,
pub rej_mutex_inter_file_same: Regex,
pub rej_mutex_inter_file_cross: Regex,
pub rej_empty_bmr_id: Regex,
pub rej_empty_bmr_file_same: Regex,
pub rej_empty_bmr_file_cross: Regex,
pub rej_empty_bmr_res: Regex,
pub rej_bmr_nocomma_id: Regex,
pub rej_bmr_nocomma_file_same: Regex,
pub rej_bmr_nocomma_file_cross: Regex,
pub rej_bmr_nocomma_res: Regex,
pub rej_empty_bm_spec_id: Regex,
pub rej_empty_bm_spec_file_same: Regex,
pub rej_empty_bm_spec_file_cross: Regex,
pub rej_empty_bm_spec_res: Regex,
pub rej_tags_before_cat_id: Regex,
pub rej_tags_before_cat_file_same: Regex,
pub rej_tags_before_cat_file_cross: Regex,
pub rej_tags_before_cat_res: Regex,
pub rej_tags_before_cat_todo: Regex,
pub rej_invalid_filename_chars: Regex,
pub valid_res_token: Regex,
pub decode_bmr_range: Regex,
pub decode_bm: Regex,
pub decode_category_prefix: Regex,
pub decode_tag_prefix: Regex,
pub decode_todo_body: Regex,
pub todo_malformed_due: Regex,
pub blocker_entry_word: Regex,
pub blocker_entry_wildcard: Regex,
}
impl Grammar {
fn new(unicode: bool) -> Self {
let wi = if unicode { r"\p{XID_Start}\p{XID_Continue}_" } else { "A-Za-z0-9_" };
let li = if unicode { r"\p{XID_Start}" } else { "A-Za-z" };
let word = format!("[{wi}]+");
let tag_tail = format!(r"(?: \.\. {word} )+");
let fseg = format!(r"[{wi}~,@!()][{wi}~,@!()\-]*");
let filename = format!(r"{fseg} (?: \. {fseg} )*");
let filepath = format!(r"{filename} (?: / {filename} )*");
let specifier = format!(
r"(?: \#\# (?: {word} , (?:{word})? | , {word} )
| : (?=[0-9]) [0-9]+ (?: [-+] [0-9]+ )?
| \# (?!\#) {word} )"
);
let annotations = format!(
r"(?: :: {word} (?: :: {word} )* )?
(?: \.\. {word} )*"
);
let annot_nonempty =
format!(r"(?: \.\. {word} | :: {word} (?: :: {word} )*)+");
let id_locator = format!(r"-- (?: {word} -- )? {word}");
let file_locator_same = format!(r"// {filepath}");
let file_locator_cross = format!(r"/// {word} / {filepath}");
let empty_bmr = format!(r"\#\#, (?![{wi}])");
let empty_bmr_nocomma = r"\#\# (?=[ \t\r\n]|\z)";
let empty_bm_spec = r"\# (?=[ \t\r\n]|\z)";
let w = &word;
let t = &tag_tail;
let f = &filename;
let p = &filepath;
let s = &specifier;
let a = &annotations;
let ane = &annot_nonempty;
let idl = &id_locator;
let fls = &file_locator_same;
let flc = &file_locator_cross;
let d = ISO_DATE;
Grammar {
inter_jot_cross_id: compile(&format!(r"-- ({w}) -- ({w}) ;; ({f}) ({s})? ({a})")),
inter_jot_same_id: compile(&format!(r"-- ({w}) ;; ({f}) ({s})? ({a})")),
inter_jot_cross_file: compile(&format!(r"/// ({w}) / ({p}) ;; ({f}) ({s})? ({a})")),
inter_jot_same_file: compile(&format!(r"// ({p}) ;; ({f}) ({s})? ({a})")),
cross_file_ref: compile(&format!(r"/// ({w}) / ({p}) ({s})? ({a})")),
file_ref: compile(&format!(r"// ({p}) ({s})? ({a})")),
cross_id_ref: compile(&format!(r"-- ({w}) -- ({w}) ({s})? ({a})")),
id_ref: compile(&format!(r"-- ({w}) ({s})? ({a})")),
resource: compile(&format!(r";; ({f}) ({s})? ({a})")),
bookmark_decl: compile(&format!(r"!! ({w})")),
tagstring: compile(&format!(r"(?: \.\. {w} )+")),
tag: compile(&format!(r"\.\. ({w})")),
hashtag: compile(&format!(r"\# (?=[{li}]) ({w})")),
category: compile(&format!(r":: ({w} (?: :: {w} )*)")),
word_full: compile(&format!(r"\A {w} \z")),
rej_annot_before_sep_id: compile(&format!(r"{idl} {ane} ;; {f} (?:{s})? {a}")),
rej_annot_before_sep_file_same: compile(&format!(r"{fls} {ane} ;; {f} (?:{s})? {a}")),
rej_annot_before_sep_file_cross: compile(&format!(r"{flc} {ane} ;; {f} (?:{s})? {a}")),
rej_spec_on_loc_id: compile(&format!(r"{idl} {s} (?:{ane})? ;; {f} (?:{s})? {a}")),
rej_spec_on_loc_file_same: compile(&format!(r"{fls} {s} (?:{ane})? ;; {f} (?:{s})? {a}")),
rej_spec_on_loc_file_cross: compile(&format!(r"{flc} {s} (?:{ane})? ;; {f} (?:{s})? {a}")),
rej_cat_run: compile(&format!(r"(:: [{wi}:]*)")),
rej_filename_doubledot: compile(&format!(r";; ({fseg} \.\. \S*)")),
rej_filename_start_file_cross: compile(&format!(r"/// {w} / ([-.] \S*)")),
rej_frag_nonnum_id: compile(&format!(r"{idl} : (?![0-9:]) [^\s]+")),
rej_frag_nonnum_file_same: compile(&format!(r"{fls} : (?![0-9:]) [^\s]+")),
rej_frag_nonnum_file_cross: compile(&format!(r"{flc} : (?![0-9:]) [^\s]+")),
rej_frag_nonnum_res: compile(&format!(r";; {f} : (?![0-9:]) [^\s]+")),
rej_mutex_id: compile(&format!(r"{idl} {s} {s}")),
rej_mutex_file_same: compile(&format!(r"{fls} {s} {s}")),
rej_mutex_file_cross: compile(&format!(r"{flc} {s} {s}")),
rej_mutex_res: compile(&format!(r";; {f} {s} {s}")),
rej_mutex_inter_id: compile(&format!(r"{idl} ;; {f} {s} {s}")),
rej_mutex_inter_file_same: compile(&format!(r"{fls} ;; {f} {s} {s}")),
rej_mutex_inter_file_cross: compile(&format!(r"{flc} ;; {f} {s} {s}")),
rej_empty_bmr_id: compile(&format!(r"{idl} {empty_bmr}")),
rej_empty_bmr_file_same: compile(&format!(r"{fls} {empty_bmr}")),
rej_empty_bmr_file_cross: compile(&format!(r"{flc} {empty_bmr}")),
rej_empty_bmr_res: compile(&format!(r";; {f} {empty_bmr}")),
rej_bmr_nocomma_id: compile(&format!(r"{idl} {empty_bmr_nocomma}")),
rej_bmr_nocomma_file_same: compile(&format!(r"{fls} {empty_bmr_nocomma}")),
rej_bmr_nocomma_file_cross: compile(&format!(r"{flc} {empty_bmr_nocomma}")),
rej_bmr_nocomma_res: compile(&format!(r";; {f} {empty_bmr_nocomma}")),
rej_empty_bm_spec_id: compile(&format!(r"{idl} {empty_bm_spec}")),
rej_empty_bm_spec_file_same: compile(&format!(r"{fls} {empty_bm_spec}")),
rej_empty_bm_spec_file_cross: compile(&format!(r"{flc} {empty_bm_spec}")),
rej_empty_bm_spec_res: compile(&format!(r";; {f} {empty_bm_spec}")),
rej_tags_before_cat_id: compile(&format!(r"{idl} (?:{s})? {t} ::{w} [^\s]*")),
rej_tags_before_cat_file_same: compile(&format!(r"{fls} (?:{s})? {t} ::{w} [^\s]*")),
rej_tags_before_cat_file_cross: compile(&format!(r"{flc} (?:{s})? {t} ::{w} [^\s]*")),
rej_tags_before_cat_res: compile(&format!(r";; {f} (?:{s})? {t} ::{w} [^\s]*")),
rej_tags_before_cat_todo: compile(&format!(
r"\?\? [ \t]* (?:!{{1,3}})? {w} (?:[ \t]*@{d})? {t} ::{w} [^\r\n]*"
)),
rej_invalid_filename_chars: compile(&format!(r";; ([{wi}~,@!()] \S*)")),
valid_res_token: compile(&format!(r"\A {f} (?:{s})? {a} \z")),
decode_bmr_range: compile(&format!(r"\A \#\# ({w})? , ({w})? \z")),
decode_bm: compile(&format!(r"\A \# ({w}) \z")),
decode_category_prefix: compile(&format!(r"\A :: ({w} (?: :: {w} )*)")),
decode_tag_prefix: compile(&format!(r"\A \.\. ({w})")),
decode_todo_body: compile(&format!(
r"\A
(\.)?
(!{{1,3}})?
(?: \( ([^)\r\n]*) \) )?
({w})
(?: [ \t]* @ ({d}) )?
(
(?: :: {w} (?: :: {w} )* )?
(?: \.\. {w} )*
)
(?:
(?: : [ \t]* | [ \t]+ :? [ \t]* ) (.*)
)?
\z"
)),
todo_malformed_due: compile(&format!(
r"\A \.? !{{0,3}} (?: \( [^)\r\n]* \) )? {w} @ \S*"
)),
blocker_entry_word: compile(&format!(
r"\A ({w}) ( :: {w} (?: :: {w} )* )? ( (?: \.\. {w} )* ) \z"
)),
blocker_entry_wildcard: compile(&format!(
r"\A \* ( :: {w} (?: :: {w} )* ) ( (?: \.\. {w} )* ) \z"
)),
}
}
}
static GRAMMAR_ASCII: LazyLock<Grammar> = LazyLock::new(|| Grammar::new(false));
static GRAMMAR_UNICODE: LazyLock<Grammar> = LazyLock::new(|| Grammar::new(true));
pub fn grammar(unicode: bool) -> &'static Grammar {
if unicode { &GRAMMAR_UNICODE } else { &GRAMMAR_ASCII }
}
pub static URL_RE: LazyLock<Regex> = LazyLock::new(|| {
compile(
r#"==
(?:
\( ([^)]*) \) (https?://\S+)
| \[ ([^\]]*) \] (https?://\S+)
| (https?://\S+)
)"#,
)
});
pub static TODO_LINE: LazyLock<Regex> = LazyLock::new(|| compile(r"\?\? ([^\r\n]*)"));
pub static REJ_URL_BARE: LazyLock<Regex> = LazyLock::new(|| compile(r"== (\S+)"));
pub static REJ_URL_UNMATCHED_PAREN: LazyLock<Regex> =
LazyLock::new(|| compile(r"== \( [^)]* \) (?!https?://) [^\s)]* \) \S*"));
pub static REJ_URL_UNMATCHED_BRACKET: LazyLock<Regex> =
LazyLock::new(|| compile(r"== \[ [^\]]* \] (?!https?://) [^\s\]]* \] \S*"));
pub static REJ_EMPTY_DOT: LazyLock<Regex> = LazyLock::new(|| compile(r"(\.\.) (?:[ \t\r\n]|\z)"));
pub static REJ_EMPTY_COLON: LazyLock<Regex> = LazyLock::new(|| compile(r"(::) (?:[ \t\r\n]|\z)"));
pub static REJ_EMPTY_DASH: LazyLock<Regex> = LazyLock::new(|| compile(r"(--) (?:[ \t\r\n]|\z)"));
pub static REJ_EMPTY_SLASH: LazyLock<Regex> = LazyLock::new(|| compile(r"(//) (?:[ \t\r\n]|\z)"));
pub static REJ_EMPTY_SEMI: LazyLock<Regex> = LazyLock::new(|| compile(r"(;;) (?:[ \t\r\n]|\z)"));
pub static REJ_EMPTY_EQ: LazyLock<Regex> = LazyLock::new(|| compile(r"(==) (?:[ \t\r\n]|\z)"));
pub static REJ_EMPTY_BANG: LazyLock<Regex> = LazyLock::new(|| compile(r"!! (?:[ \t\r\n]|\z)"));
pub static REJ_FILENAME_START_RES: LazyLock<Regex> =
LazyLock::new(|| compile(r";; ([-.] \S*)"));
pub static REJ_FILENAME_START_FILE_SAME: LazyLock<Regex> =
LazyLock::new(|| compile(r"// ([-.] \S*)"));
pub static DECODE_FRAG_RANGE: LazyLock<Regex> =
LazyLock::new(|| compile(&format!(r"\A : ({U}) - ({U}) \z", U = UINT)));
pub static DECODE_FRAG_OFFSET: LazyLock<Regex> =
LazyLock::new(|| compile(&format!(r"\A : ({U}) \+ ({U}) \z", U = UINT)));
pub static DECODE_FRAG_SINGLE: LazyLock<Regex> =
LazyLock::new(|| compile(&format!(r"\A : ({U}) \z", U = UINT)));
pub static TODO_WS_BEFORE_PAREN: LazyLock<Regex> =
LazyLock::new(|| compile(r"\A \.? !{0,3} [ \t]+ \("));
pub static TODO_WS_AFTER_PAREN: LazyLock<Regex> =
LazyLock::new(|| compile(r"\A \.? !{0,3} \( [^)\r\n]* \) [ \t]"));
pub static COMPLETION_TIME: LazyLock<Regex> = LazyLock::new(|| {
compile(&format!(r"(\A | [ \t]) \.@ ({D})", D = ISO_DATE))
});
pub static ISO_DATE_VALIDATOR: LazyLock<Regex> = LazyLock::new(|| {
compile(
r"\A (\d{4}) - (\d{2}) - (\d{2})
(?: T (\d{2}) : (\d{2}) (?: : (\d{2}) )? )?",
)
});