Add tokens and started with the parsing

pull/1/head
trivernis 5 years ago
commit e9e7dca783

2
.gitignore vendored

@ -0,0 +1,2 @@
/target
**/*.rs.bk

8
.idea/.gitignore vendored

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

@ -0,0 +1,7 @@
<component name="ProjectDictionaryState">
<dictionary name="trivernis">
<words>
<w>striked</w>
</words>
</dictionary>
</component>

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="CPP_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/markdown-rs.iml" filepath="$PROJECT_DIR$/.idea/markdown-rs.iml" />
</modules>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

6
Cargo.lock generated

@ -0,0 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "markdown-rs"
version = "0.1.0"

@ -0,0 +1,9 @@
[package]
name = "markdown-rs"
version = "0.1.0"
authors = ["trivernis <trivernis@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

@ -0,0 +1,2 @@
pub mod tokens;
pub mod parser;

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

@ -0,0 +1,130 @@
use crate::tokens::*;
use std::error::Error;
use std::fmt;
use std::fmt::{Display, Formatter};
#[derive(Debug)]
pub struct ParseError {
index: usize,
}
impl Display for ParseError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "parse error at index {}", self.index)
}
}
impl Error for ParseError {}
impl ParseError {
pub fn new(index: usize) -> Self {
Self { index }
}
}
pub struct Parser {
index: usize,
text: Vec<char>,
current_char: char,
section_nesting: u8,
}
impl Parser {
pub fn next_char(&mut self) -> Option<char> {
self.index += 1;
self.current_char = self.text.get(self.index)?.clone();
Some(self.current_char)
}
pub fn revert_to(&mut self, index: usize) -> Result<(), ParseError> {
self.index = index;
if let Some(_) = self.next_char() {
Ok(())
} else {
Err(ParseError::new(index))
}
}
pub fn parse(&mut self) {
let mut document = Document::new();
while self.index < self.text.len() {
if let Ok(token) = self.parse_block() {
document.add_element(token);
}
}
}
pub fn parse_block(&mut self) -> Result<Block, ParseError> {
if let Some(_) = self.next_char() {
let token = if let Ok(section) = self.parse_section() {
Block::Section(section)
} else if let Ok(list) = self.parse_list() {
Block::List(list)
} else if let Ok(table) = self.parse_table() {
Block::Table(table)
} else if let Ok(paragraph) = self.parse_paragraph() {
Block::Paragraph(paragraph)
} else {
return Err(ParseError::new(self.index));
};
Ok(token)
} else {
Err(ParseError::new(self.index))
}
}
pub fn parse_section(&mut self) -> Result<Section, ParseError> {
let start_index = self.index;
if self.current_char == '#' {
let mut size = 1;
while let Some(next_char) = self.next_char() {
if next_char == '#' {
size += 1;
} else {
break;
}
}
if size <= self.section_nesting || !self.current_char.is_whitespace() {
let index = self.index;
self.revert_to(start_index)?;
return Err(ParseError::new(index));
}
let _ = self.next_char();
let mut header = self.parse_header()?;
header.size = size;
self.section_nesting = size;
let mut section = Section::new(header);
while let Ok(block) = self.parse_block() {
section.add_element(block);
}
Ok(section)
} else {
Err(ParseError::new(self.index))
}
}
pub fn parse_paragraph(&mut self) -> Result<Paragraph, ParseError> {
unimplemented!()
}
pub fn parse_list(&mut self) -> Result<List, ParseError> {
unimplemented!()
}
pub fn parse_table(&mut self) -> Result<Table, ParseError> {
unimplemented!()
}
pub fn parse_header(&mut self) -> Result<Header, ParseError> {
Ok(Header {
size: 0,
text: self.parse_text()?,
})
}
pub fn parse_text(&mut self) -> Result<Text, ParseError> {
unimplemented!()
}
}

@ -0,0 +1,97 @@
pub enum Block {
Section(Section),
Paragraph(Paragraph),
List(List),
Table(Table),
}
pub enum Inline {
Text(Text),
Code(Code),
}
pub struct Document {
elements: Vec<Block>,
}
pub struct Section {
header: Header,
elements: Vec<Block>,
}
pub struct Header {
pub(crate) size: u8,
pub(crate) text: Text,
}
pub struct BlockQuote {
paragraph: Paragraph,
}
pub struct Paragraph {
elements: Vec<Inline>,
}
pub struct List {
ordered: bool,
items: Vec<ListItem>,
}
pub struct ListItem {
text: Vec<Inline>,
}
pub struct Table {
header: Row,
rows: Vec<Row>,
}
pub struct Row {
text: Vec<Cell>,
}
pub struct Cell {
text: Vec<Inline>,
}
pub struct CodeBlock {
language: String,
code: String,
}
pub struct Code {
code: String,
}
pub struct Text {
bold: bool,
italic: bool,
underlined: bool,
striked: bool,
value: String,
}
impl Document {
pub fn new() -> Self {
Self {
elements: Vec::new(),
}
}
pub fn add_element(&mut self, element: Block) {
self.elements.push(element)
}
}
impl Section {
pub fn new(header: Header) -> Self {
Self {
header,
elements: Vec::new(),
}
}
pub fn add_element(&mut self, element: Block) {
self.elements.push(element)
}
}
Loading…
Cancel
Save