Skip to content

Commit

Permalink
refactor: switched to rust
Browse files Browse the repository at this point in the history
  • Loading branch information
xNaCly committed Apr 2, 2023
1 parent 050ebb0 commit 76a1b4c
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 50 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
fleck


# Added by cargo

/target
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
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]
20 changes: 0 additions & 20 deletions Makefile

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# fleck - Markdown-parser

Fleck is intended for outputting standalone html. Fleck is german for mark. Fleck is as minimal as possible and requires no dependencies.
_Fleck_ **is** intended for outputting standalone html. Fleck is german for mark. Fleck is as minimal as possible and requires no dependencies.

## Install

Expand Down
23 changes: 0 additions & 23 deletions fleck.c

This file was deleted.

6 changes: 0 additions & 6 deletions fleck.h

This file was deleted.

22 changes: 22 additions & 0 deletions src/main.rs
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);
}
99 changes: 99 additions & 0 deletions src/parser.rs
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
}
}
24 changes: 24 additions & 0 deletions src/token.rs
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 added src/transformer.rs
Empty file.

0 comments on commit 76a1b4c

Please sign in to comment.