Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions harper-core/src/linting/irregular_verbs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
[
"// comments can appear in the line before an entry",
"// or in place of an entry",
["arise", "arose", "arisen"],
["awake", "awoke", "awoken"],
"// be/am/are/is -- was/were -- been",
["become", "became", "become"],
["begin", "began", "begun"],
["bend", "bent", "bent"],
["bet", "bet", "bet"],
["bid", "bade", "bidden"],
["bind", "bound", "bound"],
["bite", "bit", "bitten"],
["bleed", "bled", "bled"],
["blow", "blew", "blown"],
["break", "broke", "broken"],
["breed", "bred", "bred"],
["bring", "brought", "brought"],
["build", "built", "built"],
["burst", "burst", "burst"],
["buy", "bought", "bought"],
["catch", "caught", "caught"],
["choose", "chose", "chosen"],
["come", "came", "come"],
["cost", "cost", "cost"],
["cut", "cut", "cut"],
["dive", "dove", "dove"],
["do", "did", "done"],
["drink", "drank", "drunk"],
["drive", "drove", "driven"],
["eat", "ate", "eaten"],
["fall", "fell", "fallen"],
["feed", "fed", "fed"],
["feel", "felt", "felt"],
["fight", "fought", "fought"],
["find", "found", "found"],
["fly", "flew", "flown"],
["forget", "forgot", "forgotten"],
["forgo", "forwent", "forgone"],
["freeze", "froze", "frozen"],
"// get -- got -- gotten",
["get", "got", "got"],
["give", "gave", "given"],
["go", "went", "gone"],
["grow", "grew", "grown"],
["have", "had", "had"],
["hear", "heard", "heard"],
["hit", "hit", "hit"],
["hold", "held", "held"],
["hurt", "hurt", "hurt"],
["input", "input", "input"],
["keep", "kept", "kept"],
["know", "knew", "known"],
["lay", "laid", "lain"],
["lead", "led", "led"],
["light", "lit", "lit"],
["lose", "lost", "lost"],
["make", "made", "made"],
["mistake", "mistook", "mistaken"],
["output", "output", "output"],
["overtake", "overtook", "overtaken"],
["overthrow", "overthrew", "overthrown"],
["overwrite", "overwrote", "overwritten"],
["partake", "partook", "partaken"],
["pay", "paid", "paid"],
["put", "put", "put"],
["read", "read", "read"],
["redo", "redid", "redone"],
["remake", "remade", "remade"],
["reread", "reread", "reread"],
["reset", "reset", "reset"],
["ride", "rode", "ridden"],
["ring", "rang", "rung"],
["rise", "rose", "risen"],
["run", "ran", "run"],
["see", "saw", "seen"],
["sell", "sold", "sold"],
["send", "sent", "sent"],
["set", "set", "set"],
["shake", "shook", "shaken"],
["shed", "shed", "shed"],
["shine", "shone", "shone"],
["shoe", "shod", "shod"],
["shoot", "shot", "shot"],
["show", "showed", "shown"],
["shrink", "shrank", "shrunk"],
["shut", "shut", "shut"],
["sing", "sang", "sung"],
"// sink -- sank -- sunken??",
["sink", "sank", "sunk"],
["sit", "sat", "sat"],
["slay", "slew", "slain"],
["sleep", "slept", "slept"],
["slide", "slid", "slid"],
["slit", "slit", "slit"],
"// sneak -- sneaked/snuck -- sneaked/snuck",
["speak", "spoke", "spoken"],
["spin", "spun", "spun"],
["spit", "spat", "spat"],
["split", "split", "split"],
["spread", "spread", "spread"],
["spring", "sprang", "sprung"],
["stand", "stood", "stood"],
["steal", "stole", "stolen"],
["stick", "stuck", "stuck"],
["sting", "stung", "stung"],
["stink", "stank", "stunk"],
["stride", "strode", "stridden"],
["strike", "struck", "stricken"],
["string", "strung", "strung"],
["sew", "sewed", "sewn"],
["swear", "swore", "sworn"],
["swim", "swam", "swum"],
["swing", "swung", "swung"],
["take", "took", "taken"],
["teach", "taught", "taught"],
["tear", "tore", "torn"],
["think", "thought", "thought"],
["throw", "threw", "thrown"],
["tread", "trod", "trodden"],
["undo", "undid", "undone"],
["wake", "woke", "woken"],
["wear", "wore", "worn"],
["weave", "wove", "woven"],
["wind", "wound", "wound"],
["write", "wrote", "written"]
]
120 changes: 120 additions & 0 deletions harper-core/src/linting/irregular_verbs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
use lazy_static::lazy_static;
use serde::Deserialize;
use std::sync::Arc;

type Verb = (String, String, String);

#[derive(Debug, Deserialize)]
pub struct IrregularVerbs {
verbs: Vec<Verb>,
}

/// The uncached function that is used to produce the original copy of the
/// irregular verb table.
fn uncached_inner_new() -> Arc<IrregularVerbs> {
IrregularVerbs::from_json_file(include_str!("irregular_verbs.json"))
.map(Arc::new)
.unwrap_or_else(|e| panic!("Failed to load irregular verb table: {}", e))
}

lazy_static! {
static ref VERBS: Arc<IrregularVerbs> = uncached_inner_new();
}

impl IrregularVerbs {
pub fn new() -> Self {
Self { verbs: vec![] }
}

pub fn from_json_file(json: &str) -> Result<Self, serde_json::Error> {
// Deserialize into Vec<serde_json::Value> to handle mixed types
let values: Vec<serde_json::Value> =
serde_json::from_str(json).expect("Failed to parse irregular verbs JSON");

let mut verbs = Vec::new();

for value in values {
match value {
serde_json::Value::Array(arr) if arr.len() == 3 => {
// Handle array of 3 strings
if let (Some(lemma), Some(preterite), Some(past_participle)) =
(arr[0].as_str(), arr[1].as_str(), arr[2].as_str())
{
verbs.push((
lemma.to_string(),
preterite.to_string(),
past_participle.to_string(),
));
}
}
// Strings are used for comments to guide contributors editing the file
serde_json::Value::String(_) => {}
_ => {}
}
}

Ok(Self { verbs })
}

pub fn get() -> Arc<Self> {
(*VERBS).clone()
}

pub fn get_past_participle_for_preterite(&self, preterite: &str) -> Option<&str> {
self.verbs
.iter()
.find(|(_, pt, _)| pt.eq_ignore_ascii_case(preterite))
.map(|(_, _, pp)| pp.as_str())
}
}

impl Default for IrregularVerbs {
fn default() -> Self {
Self::new()
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn can_find_irregular_past_participle_for_preterite_lowercase() {
assert_eq!(
IrregularVerbs::get().get_past_participle_for_preterite("arose"),
Some("arisen")
);
}

#[test]
fn can_find_irregular_past_participle_for_preterite_uppercase() {
assert_eq!(
IrregularVerbs::get().get_past_participle_for_preterite("WENT"),
Some("gone")
);
}

#[test]
fn can_find_irregular_past_participle_same_as_past_tense() {
assert_eq!(
IrregularVerbs::get().get_past_participle_for_preterite("taught"),
Some("taught")
);
}

#[test]
fn cant_find_regular_past_participle() {
assert_eq!(
IrregularVerbs::get().get_past_participle_for_preterite("walked"),
None
);
}

#[test]
fn cant_find_non_verb() {
assert_eq!(
IrregularVerbs::get().get_past_participle_for_preterite("the"),
None
);
}
}
2 changes: 2 additions & 0 deletions harper-core/src/linting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ mod inflected_verb_after_to;
mod initialism_linter;
mod initialisms;
mod interested_in;
mod irregular_verbs;
mod it_is;
mod it_looks_like_that;
mod it_would_be;
Expand Down Expand Up @@ -201,6 +202,7 @@ mod would_never_have;

pub use expr_linter::ExprLinter;
pub use initialism_linter::InitialismLinter;
pub use irregular_verbs::IrregularVerbs;
pub use lint::Lint;
pub use lint_group::{LintGroup, LintGroupConfig};
pub use lint_kind::LintKind;
Expand Down
Loading
Loading