-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
166 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
fleck | ||
|
||
|
||
# Added by cargo | ||
|
||
/target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "fleck" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#![allow(dead_code)] | ||
|
||
const VERSION: &str = "v0.1.0"; | ||
|
||
mod parser; | ||
mod token; | ||
mod transformer; | ||
|
||
fn main() { | ||
println!("fleck - {}", VERSION); | ||
|
||
let args = std::env::args().collect::<Vec<String>>(); | ||
if args.len() < 2 { | ||
panic!("not enough arguments"); | ||
} | ||
|
||
let file_name = args.get(1).expect("not enough arguments"); | ||
let mut p = parser::Parser::new(&file_name); | ||
let tokens = p.parse(); | ||
|
||
dbg!(tokens); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use std::fs; | ||
|
||
use crate::token::{self, Token}; | ||
|
||
pub struct Parser { | ||
current_char: char, | ||
input: String, | ||
pos: usize, | ||
line: usize, | ||
line_pos: usize, | ||
} | ||
|
||
impl Parser { | ||
pub fn new(file_path: &str) -> Parser { | ||
let input = fs::read_to_string(file_path).expect("could not read file"); | ||
let current_char = input | ||
.chars() | ||
.nth(1) | ||
.expect("could not get first character of input"); | ||
Parser { | ||
input, | ||
current_char, | ||
pos: 0, | ||
line: 0, | ||
line_pos: 0, | ||
} | ||
} | ||
|
||
fn at_end(&self) -> bool { | ||
if self.current_char == '\0' || self.pos >= self.input.len() { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
fn peek(&self) -> char { | ||
if !self.at_end() { | ||
return self.input.chars().nth(self.pos + 1).expect("couldn't peek"); | ||
} | ||
'\0' | ||
} | ||
|
||
fn peek_equals(&self, character: char) -> bool { | ||
self.peek() == character | ||
} | ||
|
||
fn advance(&mut self) { | ||
if !self.at_end() || self.pos + 1 >= self.input.len() { | ||
self.pos += 1; | ||
self.current_char = self | ||
.input | ||
.chars() | ||
.nth(self.pos) | ||
.expect("could not get next character"); | ||
self.line_pos += 1; | ||
} | ||
} | ||
|
||
pub fn parse(&mut self) -> Vec<Token> { | ||
let mut res: Vec<Token> = vec![]; | ||
while !self.at_end() { | ||
let mut token_value = String::new(); | ||
let mut token_kind = token::TokenKind::Paragraph; | ||
|
||
match self.current_char { | ||
'#' => { | ||
// skip over '#' with a counter: | ||
let mut heading_id = 1; | ||
while self.peek_equals('#') { | ||
heading_id += 1; | ||
self.advance(); | ||
} | ||
while self.peek() != '\n' { | ||
token_value.push(self.current_char); | ||
self.advance(); | ||
} | ||
token_kind = match heading_id { | ||
1 => token::TokenKind::Heading1, | ||
2 => token::TokenKind::Heading2, | ||
3 => token::TokenKind::Heading3, | ||
4 => token::TokenKind::Heading4, | ||
5 => token::TokenKind::Heading5, | ||
6 => token::TokenKind::Heading6, | ||
_ => token::TokenKind::Paragraph, | ||
} | ||
} | ||
_ => {} | ||
} | ||
|
||
res.push(Token { | ||
pos: self.pos, | ||
kind: token_kind, | ||
content: token_value.to_owned(), | ||
}); | ||
self.advance(); | ||
} | ||
res | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#[derive(Debug)] | ||
pub enum TokenKind { | ||
Paragraph, | ||
Heading1, | ||
Heading2, | ||
Heading3, | ||
Heading4, | ||
Heading5, | ||
Heading6, | ||
Quote, | ||
Listitem, | ||
CodeInline, | ||
CodeBlock, | ||
Ruler, | ||
Emphasis, | ||
Image, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Token { | ||
pub pos: usize, | ||
pub kind: TokenKind, | ||
pub content: String, | ||
} |
Empty file.