pub mod config;
mod decode;
mod grammar;
mod scan;
mod types;
pub use config::{CategoryRoot, Config, FlagValue};
pub use types::{
Blocker, Bookmark, BookmarkRange, CrossFileReference, CrossReference, Diagnostic,
FileReference, Fragment, InterJotResource, Loc, ParseResult, Reference, Resource, Todo, Url,
};
use scan::{CandKind, Candidate};
use std::collections::HashSet;
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub fn parse(text: &str, cfg: &Config) -> ParseResult {
let g = grammar::grammar(cfg.unicode);
let mut diagnostics: Vec<Diagnostic> = Vec::new();
validate_config_flags(cfg, &mut diagnostics);
if let config::CategoryRoot::Invalid(v) = cfg.category_root() {
diagnostics.push(Diagnostic {
code: "Jots/Validation/InvalidCategoryRoot".to_string(),
severity: "validation_error",
message: format!(
"config flag 'category_root' has invalid value '{}'; expected a single category segment",
v
),
loc: None,
});
}
let suffix_policy = decode::SuffixPolicy::from_config(cfg);
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 done_todos: Vec<Todo> = Vec::new();
let mut bookmarks: Vec<Bookmark> = 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: Some(loc_for(cand.start, cand.end)),
});
}
CandKind::Bookmark { ref name } => {
if bookmark_seen.insert(name.clone()) {
bookmarks.push(Bookmark {
name: name.clone(),
loc: if emit_loc {
Some(loc_for(cand.start, cand.end))
} else {
None
},
raw: if emit_loc {
Some(text[cand.start..cand.end].to_string())
} else {
None
},
});
} else {
diagnostics.push(Diagnostic {
code: "Jots/Validation/DuplicateBookmark".to_string(),
severity: "validation_error",
message: format!("duplicate bookmark name '{}'", name),
loc: Some(loc_for(cand.start, cand.end)),
});
}
}
CandKind::Todo { ref body } => {
let mut todo = match decode::decode_todo_body(g, body, &suffix_policy) {
Ok(t) => t,
Err(e) => {
diagnostics.push(Diagnostic {
code: e.code.to_string(),
severity: "parse_error",
message: e.message,
loc: Some(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: Some(loc_for(cand.start, cand.end)),
});
}
if todo.done {
for caps in grammar::COMPLETION_TIME
.captures_iter(&todo.description)
.flatten()
{
let date = caps[2].to_string();
if todo.completed.is_none() {
if !decode::is_valid_iso_date(&date) {
diagnostics.push(Diagnostic {
code: "Jots/Validation/InvalidCompletionDate".to_string(),
severity: "validation_error",
message: format!(
"completion date '{}' is not a valid calendar date",
date
),
loc: Some(loc_for(cand.start, cand.end)),
});
}
todo.completed = Some(date);
} else {
diagnostics.push(Diagnostic {
code: "Jots/Validation/DuplicateCompletionTime".to_string(),
severity: "validation_error",
message: "more than one '.@<iso-date>' on a done todo"
.to_string(),
loc: Some(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(g, 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;
if todo.done {
done_todos.push(todo);
} else {
todos.push(todo);
}
}
_ => {
emit_to_bucket(g, 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: Some(loc_for(bm.start, bm.end)),
});
}
}
diagnostics.sort_by(|a, b| match (&a.loc, &b.loc) {
(None, None) => a.code.cmp(&b.code),
(None, Some(_)) => std::cmp::Ordering::Less,
(Some(_), None) => std::cmp::Ordering::Greater,
(Some(x), Some(y)) => x
.start
.cmp(&y.start)
.then_with(|| x.end.cmp(&y.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,
done_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>) {
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: None,
});
}
}
#[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(
g: &grammar::Grammar,
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(g, specifier.as_deref());
validate_specifier(specifier.as_deref(), &dec, &loc, diagnostics);
let (category, tags) = decode::decode_annotations(g, &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(g, specifier.as_deref());
validate_specifier(specifier.as_deref(), &dec, &loc, diagnostics);
let (category, tags) = decode::decode_annotations(g, &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(g, specifier.as_deref());
validate_specifier(specifier.as_deref(), &dec, &loc, diagnostics);
let (category, tags) = decode::decode_annotations(g, &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(g, specifier.as_deref());
validate_specifier(specifier.as_deref(), &dec, &loc, diagnostics);
let (category, tags) = decode::decode_annotations(g, &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(g, specifier.as_deref());
validate_specifier(specifier.as_deref(), &dec, &loc, diagnostics);
let (category, tags) = decode::decode_annotations(g, &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(g, specifier.as_deref());
validate_specifier(specifier.as_deref(), &dec, &loc, diagnostics);
let (category, tags) = decode::decode_annotations(g, &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: Some(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: Some(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: Some(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: Some(loc.clone()),
});
}
}