pub mod config;
mod decode;
mod grammar;
mod scan;
mod types;
pub use config::{Config, FlagValue};
pub use types::{
BookmarkRange, CrossFileReference, CrossReference, Diagnostic, FileReference, Fragment,
InterJotResource, Loc, ParseResult, Reference, Resource, Todo, Url,
};
use scan::{CandKind, Candidate};
use std::collections::HashSet;
pub fn parse(text: &str, cfg: &Config) -> ParseResult {
let mut diagnostics: Vec<Diagnostic> = Vec::new();
validate_config_flags(cfg, &mut diagnostics);
let scan_out = scan::scan(text, cfg);
let emit_loc = cfg.enabled("emit_loc", true);
let mut top = Bucket::new();
let mut todos: Vec<Todo> = Vec::new();
let mut bookmarks: Vec<String> = Vec::new();
let mut bookmark_seen: HashSet<String> = HashSet::new();
for cand in scan_out.winners {
match cand.kind {
CandKind::Reject { code, ref message } => {
diagnostics.push(Diagnostic {
code: code.to_string(),
severity: "parse_error",
message: message.clone(),
loc: loc_for(cand.start, cand.end),
});
}
CandKind::Bookmark { ref name } => {
if !bookmark_seen.insert(name.clone()) {
diagnostics.push(Diagnostic {
code: "Jots/Validation/DuplicateBookmark".to_string(),
severity: "validation_error",
message: format!("duplicate bookmark name '{}'", name),
loc: loc_for(cand.start, cand.end),
});
}
bookmarks.push(name.clone());
}
CandKind::Todo { ref body } => {
let Some(mut todo) = decode::decode_todo_body(body) else {
diagnostics.push(Diagnostic {
code: "Jots/Parsing/TodoMissingAction".to_string(),
severity: "parse_error",
message: "todo '??' is missing an action".to_string(),
loc: loc_for(cand.start, cand.end),
});
continue;
};
if let Some(due) = &todo.due
&& !decode::is_valid_iso_date(due)
{
diagnostics.push(Diagnostic {
code: "Jots/Validation/InvalidDate".to_string(),
severity: "validation_error",
message: format!(
"todo due date '{}' is not a valid calendar date",
due
),
loc: loc_for(cand.start, cand.end),
});
}
if emit_loc {
todo.loc = Some(loc_for(cand.start, cand.end));
todo.raw = Some(text[cand.start..cand.end].to_string());
}
let mut tb = Bucket::new();
for t in &todo.tags {
tb.push_tag(t);
}
for c in &todo.categories {
tb.push_category(c);
}
for child in cand.children {
emit_to_bucket(child, text, &mut tb, emit_loc, &mut diagnostics);
}
todo.tags = tb.tags;
todo.categories = tb.categories;
todo.references = tb.references;
todo.cross_references = tb.cross_references;
todo.file_references = tb.file_references;
todo.cross_file_references = tb.cross_file_references;
todo.inter_jot_resources = tb.inter_jot_resources;
todo.resources = tb.resources;
todo.urls = tb.urls;
todos.push(todo);
}
_ => {
emit_to_bucket(cand, text, &mut top, emit_loc, &mut diagnostics);
}
}
}
for bm in scan_out.dropped_bookmarks {
if let CandKind::Bookmark { name } = &bm.kind {
diagnostics.push(Diagnostic {
code: "Jots/Validation/BookmarkInTodoSpan".to_string(),
severity: "validation_error",
message: format!("bookmark declaration '!!{}' inside todo span", name),
loc: loc_for(bm.start, bm.end),
});
}
}
diagnostics.sort_by(|a, b| {
a.loc.start
.cmp(&b.loc.start)
.then_with(|| a.loc.end.cmp(&b.loc.end))
.then_with(|| a.code.cmp(&b.code))
});
ParseResult {
tags: top.tags,
categories: top.categories,
references: top.references,
cross_references: top.cross_references,
file_references: top.file_references,
cross_file_references: top.cross_file_references,
inter_jot_resources: top.inter_jot_resources,
resources: top.resources,
todos,
urls: top.urls,
bookmarks,
diagnostics,
}
}
fn loc_for(start: usize, end: usize) -> Loc {
Loc {
unit: "byte",
start,
end,
}
}
fn validate_config_flags(cfg: &Config, diagnostics: &mut Vec<Diagnostic>) {
let zero_loc = Loc {
unit: "byte",
start: 0,
end: 0,
};
for key in cfg.flags.keys() {
if !config::FLAG_KEYS.contains(&key.as_str()) {
continue;
}
let val = &cfg.flags[key];
if config::is_valid_flag_value(val) {
continue;
}
let display = match val {
FlagValue::Bool(b) => b.to_string(),
FlagValue::Raw(s) => s.clone(),
};
diagnostics.push(Diagnostic {
code: "Jots/Validation/InvalidFlagValue".to_string(),
severity: "validation_error",
message: format!(
"config flag '{}' has invalid value '{}'; expected true or false",
key, display
),
loc: zero_loc.clone(),
});
}
}
#[derive(Default)]
struct Bucket {
tags: Vec<String>,
categories: Vec<String>,
references: Vec<Reference>,
cross_references: Vec<CrossReference>,
file_references: Vec<FileReference>,
cross_file_references: Vec<CrossFileReference>,
inter_jot_resources: Vec<InterJotResource>,
resources: Vec<Resource>,
urls: Vec<Url>,
tags_seen: HashSet<String>,
cats_seen: HashSet<String>,
}
impl Bucket {
fn new() -> Self {
Self::default()
}
fn push_tag(&mut self, t: &str) {
if self.tags_seen.insert(t.to_string()) {
self.tags.push(t.to_string());
}
}
fn push_category(&mut self, c: &str) {
if self.cats_seen.insert(c.to_string()) {
self.categories.push(c.to_string());
}
}
}
fn emit_to_bucket(
cand: Candidate,
text: &str,
bucket: &mut Bucket,
emit_loc: bool,
diagnostics: &mut Vec<Diagnostic>,
) {
let loc = loc_for(cand.start, cand.end);
let raw = text[cand.start..cand.end].to_string();
let opt_loc = if emit_loc { Some(loc.clone()) } else { None };
let opt_raw = if emit_loc { Some(raw) } else { None };
match cand.kind {
CandKind::Tag { name } => bucket.push_tag(&name),
CandKind::Category { path } => bucket.push_category(&path),
CandKind::Reference {
id,
specifier,
annotations,
} => {
let dec = decode::decode_specifier(specifier.as_deref());
validate_specifier(specifier.as_deref(), &dec, &loc, diagnostics);
let (category, tags) = decode::decode_annotations(&annotations);
bucket.references.push(Reference {
id,
fragment: dec.fragment,
bookmark: dec.bookmark,
bookmark_range: dec.bookmark_range,
category,
tags,
loc: opt_loc,
raw: opt_raw,
});
}
CandKind::CrossReference {
space,
id,
specifier,
annotations,
} => {
let dec = decode::decode_specifier(specifier.as_deref());
validate_specifier(specifier.as_deref(), &dec, &loc, diagnostics);
let (category, tags) = decode::decode_annotations(&annotations);
bucket.cross_references.push(CrossReference {
space,
id,
fragment: dec.fragment,
bookmark: dec.bookmark,
bookmark_range: dec.bookmark_range,
category,
tags,
loc: opt_loc,
raw: opt_raw,
});
}
CandKind::FileReference {
filepath,
specifier,
annotations,
} => {
let dec = decode::decode_specifier(specifier.as_deref());
validate_specifier(specifier.as_deref(), &dec, &loc, diagnostics);
let (category, tags) = decode::decode_annotations(&annotations);
bucket.file_references.push(FileReference {
filepath,
fragment: dec.fragment,
bookmark: dec.bookmark,
bookmark_range: dec.bookmark_range,
category,
tags,
loc: opt_loc,
raw: opt_raw,
});
}
CandKind::CrossFileReference {
space,
filepath,
specifier,
annotations,
} => {
let dec = decode::decode_specifier(specifier.as_deref());
validate_specifier(specifier.as_deref(), &dec, &loc, diagnostics);
let (category, tags) = decode::decode_annotations(&annotations);
bucket.cross_file_references.push(CrossFileReference {
space,
filepath,
fragment: dec.fragment,
bookmark: dec.bookmark,
bookmark_range: dec.bookmark_range,
category,
tags,
loc: opt_loc,
raw: opt_raw,
});
}
CandKind::Resource {
filename,
specifier,
annotations,
} => {
let dec = decode::decode_specifier(specifier.as_deref());
validate_specifier(specifier.as_deref(), &dec, &loc, diagnostics);
let (category, tags) = decode::decode_annotations(&annotations);
bucket.resources.push(Resource {
filename,
fragment: dec.fragment,
bookmark: dec.bookmark,
bookmark_range: dec.bookmark_range,
category,
tags,
loc: opt_loc,
raw: opt_raw,
});
}
CandKind::InterJotResource {
space,
id,
filepath,
filename,
specifier,
annotations,
} => {
let dec = decode::decode_specifier(specifier.as_deref());
validate_specifier(specifier.as_deref(), &dec, &loc, diagnostics);
let (category, tags) = decode::decode_annotations(&annotations);
bucket.inter_jot_resources.push(InterJotResource {
space,
id,
filepath,
filename,
resource_fragment: dec.fragment,
resource_bookmark: dec.bookmark,
resource_bookmark_range: dec.bookmark_range,
category,
tags,
loc: opt_loc,
raw: opt_raw,
});
}
CandKind::Url { url, label } => {
bucket.urls.push(Url {
url,
label,
loc: opt_loc,
raw: opt_raw,
});
}
CandKind::Bookmark { .. } | CandKind::Todo { .. } | CandKind::Reject { .. } => {
}
}
}
fn validate_specifier(
raw: Option<&str>,
dec: &decode::DecodedSpec,
loc: &Loc,
diagnostics: &mut Vec<Diagnostic>,
) {
if let Some(frag) = &dec.fragment {
if let Some(raw) = raw {
for token in raw.split(|c: char| !c.is_ascii_digit()) {
if token.len() > 10 {
diagnostics.push(Diagnostic {
code: "Jots/Validation/FragmentIntegerOverflow".to_string(),
severity: "validation_error",
message: format!(
"fragment line number {} exceeds implementation limit",
token
),
loc: loc.clone(),
});
return;
}
}
}
if frag.start == 0 || frag.end == 0 {
diagnostics.push(Diagnostic {
code: "Jots/Validation/ZeroLineNumber".to_string(),
severity: "validation_error",
message: "line numbers are 1-based; 0 is not allowed".to_string(),
loc: loc.clone(),
});
} else if frag.start > frag.end {
diagnostics.push(Diagnostic {
code: "Jots/Validation/RangeStartAfterEnd".to_string(),
severity: "validation_error",
message: format!(
"fragment range start ({}) is greater than end ({})",
frag.start, frag.end
),
loc: loc.clone(),
});
}
}
if let Some(bmr) = &dec.bookmark_range
&& let (Some(a), Some(b)) = (&bmr.start_bookmark, &bmr.end_bookmark)
&& a == b
{
diagnostics.push(Diagnostic {
code: "Jots/Validation/SameBookmarkInRange".to_string(),
severity: "validation_error",
message: format!("bookmark '{}' appears as both start and end of range", a),
loc: loc.clone(),
});
}
}