Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

Some stuff??

Elijah Potter57b90e330aa0
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 000000000..eb5a316cb
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+target
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 000000000..e77eba45d
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,82 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "Inflector"
+version = "0.11.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3"
+
+[[package]]
+name = "is-macro"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f4467ed1321b310c2625c5aa6c1b1ffc5de4d9e42668cf697a08fb033ee8265e"
+dependencies = [
+ "Inflector",
+ "pmutil",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "lt-cli"
+version = "0.1.0"
+dependencies = [
+ "lt-core",
+]
+
+[[package]]
+name = "lt-core"
+version = "0.1.0"
+dependencies = [
+ "is-macro",
+]
+
+[[package]]
+name = "pmutil"
+version = "0.6.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "52a40bc70c2c58040d2d8b167ba9a5ff59fc9dab7ad44771cfde3dcfde7a09c6"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.66"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.32"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "50f3b39ccfb720540debaa0164757101c08ecb8d326b15358ce76a62c7e85965"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "syn"
+version = "2.0.28"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "04361975b3f5e348b2189d8dc55bc942f278b2d482a6a0365de5bdd62d351567"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
+[[package]]
+name = "unicode-ident"
+version = "1.0.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c"
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 000000000..58ffe290d
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,2 @@
+[workspace]
+members = [ "lt-core", "lt-cli"]
diff --git a/alice.txt b/alice.txt
new file mode 100644
index 000000000..a9054835a
--- /dev/null
+++ b/alice.txt
@@ -0,0 +1,20 @@
+After a time she heard a little pattering of feet in the distance, and
+she hastily dried her eyes to see what was coming. It was the White
+Rabbit returning, splendidly dressed, with a pair of white kid gloves in
+one hand and a large fan in the other: he came trotting along in a great
+hurry, muttering to himself as he came, "Oh! the Duchess, the Duchess!
+Oh! won't she be savage if I've kept her 100 waiting!" Alice felt so
+desperate that she was ready to ask help of any one; so, when the
+Rabbit came near her, she began, in a low, timid voice, "If you please,
+sir----" The Rabbit started violently, dropped the white kid gloves and
+the fan, and scurried away into the darkness as hard as he could go.
+
+Alice took up the fan and gloves, and, as the hall was very hot, she
+kept fanning herself all the time she went on talking! "Dear, dear! How
+queer everything is to-day! And yesterday things went on just as usual.
+I wonder if I've been changed during the night? Let me think: _was_ I
+the same when I got up this morning? I almost think I can remember
+feeling a little different. But if I'm not the same, the next question
+is, who in the world am I? Ah, _that's_ the great puzzle!" And she began
+thinking over all the children she knew that were of the same age as
+herself, to see if she could have been changed for any of them.ubscribe to our email newsletter to hear about new eBooks.
diff --git a/lt-cli/Cargo.toml b/lt-cli/Cargo.toml
new file mode 100644
index 000000000..1bc8e3c49
--- /dev/null
+++ b/lt-cli/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "lt-cli"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+lt-core = { path = "../lt-core" }
diff --git a/lt-cli/src/main.rs b/lt-cli/src/main.rs
new file mode 100644
index 000000000..d172f322f
--- /dev/null
+++ b/lt-cli/src/main.rs
@@ -0,0 +1,29 @@
+use lt_core::{lex_to_end, Lint, TokenKind};
+
+fn main() {
+ let txt = include_str!("../../alice.txt");
+ let chars: Vec<_> = txt.chars().collect();
+
+ let tokens = lex_to_end(&chars);
+
+ for token in tokens.iter() {
+ match token.kind {
+ TokenKind::Number(n) => println!("{}", Lint::new(token.span).display(&chars)),
+ _ => (),
+ }
+ }
+
+ // for token in tokens {
+ // match token.kind {
+ // TokenKind::Word => println!(
+ // "\"{}\"",
+ // token.span.get_content(&chars).iter().collect::<String>()
+ // ),
+ // TokenKind::Number(n) => println!("${}$", n),
+ // TokenKind::Punctuation(_) => println!(
+ // "_{}_",
+ // token.span.get_content(&chars).iter().collect::<String>()
+ // ),
+ // }
+ // }
+}
diff --git a/lt-core/Cargo.toml b/lt-core/Cargo.toml
new file mode 100644
index 000000000..86dd482e7
--- /dev/null
+++ b/lt-core/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "lt-core"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+is-macro = "0.3.0"
diff --git a/lt-core/src/lexer.rs b/lt-core/src/lexer.rs
new file mode 100644
index 000000000..ab3c2973a
--- /dev/null
+++ b/lt-core/src/lexer.rs
@@ -0,0 +1,169 @@
+use crate::span::Span;
+
+use super::token::{Punctuation, Token, TokenKind};
+
+#[derive(Debug)]
+pub struct FoundToken {
+ /// The index of the character __after__ the lexed token
+ pub next_index: usize,
+ /// Token lexed
+ pub token: TokenKind,
+}
+
+/// Lex all tokens, if possible.
+pub fn lex_to_end(source: &[char]) -> Vec<Token> {
+ let mut cursor = 0;
+ let mut tokens = Vec::new();
+
+ loop {
+ cursor += lex_ignorables(&source[cursor..]);
+
+ if cursor == source.len() {
+ return tokens;
+ }
+
+ if let Some(FoundToken { token, next_index }) = lex_token(&source[cursor..]) {
+ tokens.push(Token {
+ span: Span::new(cursor, cursor + next_index),
+ kind: token,
+ });
+ cursor += next_index;
+ } else {
+ cursor += 1;
+ }
+ }
+}
+
+fn lex_token(source: &[char]) -> Option<FoundToken> {
+ let lexers = [lex_punctuation, lex_number, lex_word];
+
+ for lexer in lexers {
+ if let Some(f) = lexer(source) {
+ return Some(f);
+ }
+ }
+ None
+}
+
+fn lex_word(source: &[char]) -> Option<FoundToken> {
+ let mut end = 0;
+
+ while end < source.len() {
+ if lex_punctuation(&source[end + 1..]).is_none() && lex_ignorables(&source[end + 1..]) == 0
+ {
+ end += 1;
+ } else {
+ return Some(FoundToken {
+ next_index: end + 1,
+ token: TokenKind::Word,
+ });
+ }
+ }
+
+ None
+}
+
+pub fn lex_number(source: &[char]) -> Option<FoundToken> {
+ if source.is_empty() {
+ return None;
+ }
+
+ if !source[0].is_numeric() {
+ return None;
+ }
+
+ let Some(end) = source
+ .iter()
+ .enumerate()
+ .rev()
+ .find_map(|(i, v)| v.is_numeric().then_some(i))
+ else {
+ return None;
+ };
+
+ {
+ let s: String = source[0..end + 1].iter().collect();
+
+ if let Ok(n) = s.parse::<f64>() {
+ return Some(FoundToken {
+ token: TokenKind::Number(n),
+ next_index: end + 1,
+ });
+ }
+ }
+
+ lex_number(&source[0..end])
+}
+
+/// Find the first token _after_ any characters that can be ignored (whitespace, mostly).
+fn lex_ignorables(source: &[char]) -> usize {
+ let mut cursor = 0;
+
+ loop {
+ let last_cursor = cursor;
+
+ cursor += lex_whitespace(&source[cursor..]);
+
+ if last_cursor == cursor {
+ break;
+ }
+ }
+
+ cursor
+}
+
+/// Find the first token _after_ whitespace.
+fn lex_whitespace(source: &[char]) -> usize {
+ for (index, c) in source.iter().enumerate() {
+ if !c.is_whitespace() {
+ return index;
+ }
+ }
+
+ source.len()
+}
+
+fn lex_characters(source: &[char], cs: &str, token: TokenKind) -> Option<FoundToken> {
+ let sep: Vec<_> = cs.chars().collect();
+
+ if source.get(0..cs.len())? == sep {
+ Some(FoundToken {
+ token,
+ next_index: cs.len(),
+ })
+ } else {
+ None
+ }
+}
+
+macro_rules! lex_punctuation {
+ ($($text:literal => $res:ident),*) => {
+ fn lex_punctuation(source: &[char]) -> Option<FoundToken> {
+ $(
+ if let Some(found) = lex_characters(source, $text, TokenKind::Punctuation(Punctuation::$res)){
+ return Some(found);
+ }
+ )*
+
+ None
+ }
+ };
+}
+
+lex_punctuation! {
+ "." => Period,
+ "!" => Bang,
+ "?" => Question,
+ ":" => Colon,
+ ";" => Semicolon,
+ "\"" => Quote,
+ "," => Comma,
+ "-" => Hyphen,
+ "’" => Apostrophe,
+ "'" => Apostrophe,
+ "[" => OpenSquare,
+ "]" => CloseSquare,
+ "(" => OpenRound,
+ ")" => CloseRound,
+ "#" => Hash
+}
diff --git a/lt-core/src/lib.rs b/lt-core/src/lib.rs
new file mode 100644
index 000000000..f64464001
--- /dev/null
+++ b/lt-core/src/lib.rs
@@ -0,0 +1,9 @@
+mod lexer;
+mod line_col;
+mod lint;
+mod span;
+mod token;
+
+pub use lexer::lex_to_end;
+pub use lint::{Lint, LintKind};
+pub use token::{Token, TokenKind};
diff --git a/lt-core/src/line_col.rs b/lt-core/src/line_col.rs
new file mode 100644
index 000000000..cd2700934
--- /dev/null
+++ b/lt-core/src/line_col.rs
@@ -0,0 +1,28 @@
+/// Represents a position in a given text document
+pub struct LineCol {
+ pub line: usize,
+ pub col: usize,
+}
+
+impl LineCol {
+ pub fn new(line: usize, col: usize) -> Self {
+ Self { line, col }
+ }
+
+ pub fn from_index(index: usize, source: &[char]) -> LineCol {
+ let mut lines = 0;
+ let mut last_newline_index = 0;
+
+ for (index, c) in source[0..index].iter().enumerate() {
+ if *c == '\n' {
+ lines += 1;
+ last_newline_index = index;
+ }
+ }
+
+ LineCol {
+ line: lines,
+ col: index - last_newline_index - 1,
+ }
+ }
+}
diff --git a/lt-core/src/lint.rs b/lt-core/src/lint.rs
new file mode 100644
index 000000000..fb873cee9
--- /dev/null
+++ b/lt-core/src/lint.rs
@@ -0,0 +1,98 @@
+use std::iter::repeat;
+
+use crate::{line_col::LineCol, span::Span};
+
+pub struct Lint {
+ span: Span,
+ lint_kind: LintKind,
+}
+
+impl Lint {
+ pub fn new(span: Span) -> Self {
+ Self {
+ span,
+ lint_kind: LintKind::MissingWhiteSpace,
+ }
+ }
+
+ pub fn display(&self, source: &[char]) -> String {
+ let mut rect = to_rect(source);
+
+ let start_coord = LineCol::from_index(self.span.start, source);
+ crop(start_coord, LineCol::new(3, 3), &mut rect);
+ outline_text(&mut rect);
+
+ rect_to_string(&mut rect)
+ }
+}
+
+pub enum LintKind {
+ MissingWhiteSpace,
+}
+
+/// Converts a flat (1 dimensional) array of characters to a plane (2 dimensional) format.
+fn to_rect<'a>(source: impl IntoIterator<Item = &'a char>) -> Vec<Vec<char>> {
+ let mut output = Vec::new();
+ let mut current_line = Vec::new();
+
+ for c in source {
+ if *c == '\n' {
+ output.push(current_line.clone());
+ current_line.clear()
+ } else {
+ current_line.push(*c);
+ }
+ }
+
+ output
+}
+
+/// Add spaces on all lines whose length is less than the longest line until all are equal length.
+fn pad_lines(source: &mut Vec<Vec<char>>) {
+ let longest_line = source.iter().map(|line| line.len()).max().unwrap_or(0);
+
+ for line in source {
+ line.extend(repeat(' ').take(longest_line - line.len()));
+ }
+}
+
+/// Wrap text in visual box
+fn outline_text(source: &mut Vec<Vec<char>>) {
+ pad_lines(source);
+
+ source.insert(0, vec!['─'; source[0].len()]);
+ source.iter_mut().for_each(|line| {
+ line.insert(0, '┃');
+ line.push('┃');
+ });
+ source.push(vec!['─'; source[0].len()])
+}
+
+fn rect_to_string(source: &mut Vec<Vec<char>>) -> String {
+ let mut output = String::new();
+
+ for line in source {
+ output.extend(line.iter());
+ output.push('\n');
+ }
+
+ output
+}
+
+fn crop(top_left: LineCol, size: LineCol, source: &mut Vec<Vec<char>>) {
+ source.drain(0..top_left.line);
+ source.drain(top_left.line + size.line..);
+
+ source.retain(|item| item.len() > top_left.col);
+
+ for item in source.iter_mut() {
+ if item.len() > top_left.col {
+ item.drain(0..top_left.col);
+
+ let end = top_left.col + size.col;
+ if item.len() > end {
+ item.drain(end..);
+ }
+ }
+ }
+}
diff --git a/lt-core/src/span.rs b/lt-core/src/span.rs
new file mode 100644
index 000000000..736eac82e
--- /dev/null
+++ b/lt-core/src/span.rs
@@ -0,0 +1,18 @@
+use crate::line_col::LineCol;
+
+/// A window in an [char].
+#[derive(Debug, Clone, Copy)]
+pub struct Span {
+ pub start: usize,
+ pub end: usize,
+}
+
+impl Span {
+ pub fn new(start: usize, end: usize) -> Self {
+ Self { start, end }
+ }
+
+ pub fn get_content<'a>(&self, source: &'a [char]) -> &'a [char] {
+ &source[self.start..self.end]
+ }
+}
diff --git a/lt-core/src/token.rs b/lt-core/src/token.rs
new file mode 100644
index 000000000..7a9748047
--- /dev/null
+++ b/lt-core/src/token.rs
@@ -0,0 +1,48 @@
+use is_macro::Is;
+
+use crate::span::Span;
+
+#[derive(Debug, Clone)]
+pub struct Token {
+ pub span: Span,
+ pub kind: TokenKind,
+}
+
+#[derive(Debug, Is, Clone)]
+pub enum TokenKind {
+ Word,
+ Punctuation(Punctuation),
+ Number(f64),
+}
+
+#[derive(Debug, Is, Clone)]
+pub enum Punctuation {
+ /// .
+ Period,
+ /// !
+ Bang,
+ /// ?
+ Question,
+ /// :
+ Colon,
+ /// ;
+ Semicolon,
+ /// "
+ Quote,
+ /// ,
+ Comma,
+ /// -
+ Hyphen,
+ /// ' or ’
+ Apostrophe,
+ /// [
+ OpenSquare,
+ /// ]
+ CloseSquare,
+ /// (
+ OpenRound,
+ /// )
+ CloseRound,
+ /// "
+ Hash,
+}