diff options
author | Nao Pross <naopross@thearcway.org> | 2020-02-05 05:25:43 +0100 |
---|---|---|
committer | Nao Pross <naopross@thearcway.org> | 2020-02-05 05:25:43 +0100 |
commit | 5ecfc36226e9d823ef4de16aca120964141be4b3 (patch) | |
tree | 2d43234e8697149eb796da84440cfae261dd4270 | |
download | rlg-5ecfc36226e9d823ef4de16aca120964141be4b3.tar.gz rlg-5ecfc36226e9d823ef4de16aca120964141be4b3.zip |
Initial commit
Diffstat (limited to '')
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Cargo.lock | 113 | ||||
-rw-r--r-- | Cargo.toml | 11 | ||||
-rw-r--r-- | src/journal.rs | 41 | ||||
-rw-r--r-- | src/main.rs | 11 | ||||
-rw-r--r-- | src/parser.rs | 124 |
6 files changed, 301 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..c30c4e2 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,113 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "either" +version = "1.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" + +[[package]] +name = "itertools" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f56a2d0bc861f9165be4eb3442afd3c236d8a98afd426f65d92324ae1091a484" +dependencies = [ + "either", +] + +[[package]] +name = "proc-macro-hack" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecd45702f76d6d3c75a80564378ae228a85f0b59d2f3ed43c91b4a69eb2ebfc5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acb317c6ff86a4e579dfa00fc5e6cca91ecbb4e7eb2df0468805b674eb88548" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rlg" +version = "0.1.0" +dependencies = [ + "itertools", + "time", +] + +[[package]] +name = "rustversion" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3bba175698996010c4f6dce5e7f173b6eb781fce25d2cfc45e27091ce0b79f6" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af6f3550d8dff9ef7dc34d384ac6f107e5d31c8f57d9f28e0081503f547ac8f5" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "time" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcb8742444475438c500d017736dd7cf3d9c9dd1f0153cfa4af428692484e3ef" +dependencies = [ + "rustversion", + "time-macros", +] + +[[package]] +name = "time-macros" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ae9b6e9f095bc105e183e3cd493d72579be3181ad4004fceb01adbe9eecab2d" +dependencies = [ + "proc-macro-hack", + "time-macros-impl", +] + +[[package]] +name = "time-macros-impl" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e987cfe0537f575b5fc99909de6185f6c19c3ad8889e2275e686a873d0869ba1" +dependencies = [ + "proc-macro-hack", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-xid" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..5afadfb --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "rlg" +version = "0.1.0" +authors = ["naopross"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +time = "0.2" +itertools = "0.8" diff --git a/src/journal.rs b/src/journal.rs new file mode 100644 index 0000000..d608bd0 --- /dev/null +++ b/src/journal.rs @@ -0,0 +1,41 @@ +#[allow(dead_code)] +pub struct Commodity { + name: String, +} + +#[allow(dead_code)] +pub struct Account { + name: String, + commodity: Commodity, +} + +#[allow(dead_code)] +pub struct Posting { + account: Account, + amount: i64, + commodity: Commodity, +} + +#[allow(dead_code)] +pub struct Transaction { + text: String, + payee: String, + postings: Vec<Posting>, +} + +#[allow(dead_code)] +pub struct Journal { + pub commodities: Vec<Commodity>, + pub accounts: Vec<Account>, + pub transactions: Vec<Transaction>, +} + + + +impl Commodity { + fn new(name: &str) -> Commodity { + Commodity { + name: name.to_string() + } + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..884cbe5 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,11 @@ +mod journal; +mod parser; + +use std::env; + + +fn main() { + if let Some(fname) = env::args().nth(1) { + parser::parse(&fname); + } +} diff --git a/src/parser.rs b/src/parser.rs new file mode 100644 index 0000000..c57fe14 --- /dev/null +++ b/src/parser.rs @@ -0,0 +1,124 @@ +use crate::journal::Journal; + +use std::fs; +use std::iter::Peekable; + +extern crate itertools; +extern crate time; + +use itertools::Itertools; +use time::Date; + + +#[derive(Debug, Eq, PartialEq, Clone)] +pub enum Token { + Word(String), + DateSep, + AccountSep, + DecimalSep, + Newline, + Space, + Indent, + Marker(char), + Comment(char), + Numeric(String), +} + +pub struct Lexer<I: Iterator<Item=char>> { + iter: Peekable<I> +} + +impl<I: Iterator<Item=char>> Lexer<I> { + pub fn new(iter: I) -> Lexer<I> { + Lexer { + iter: iter.peekable() + } + } +} + +impl<I: Iterator<Item=char>> Iterator for Lexer<I> { + type Item = Token; + + fn next(&mut self) -> Option<Token> { + // let ch = *self.iter.peek().unwrap_or(&'`'); + let ch = self.iter.peek() + match ch { + /* alphanumeric */ + c if c.is_alphabetic() => { + Some(Token::Word(self.iter.by_ref() + .peeking_take_while(|&c| c.is_alphabetic()).collect())) }, + c if c.is_numeric() => { + Some(Token::Numeric(self.iter.by_ref() + .peeking_take_while(|&c| c.is_numeric()).collect())) + }, + /* whitespace */ + ' ' => { + self.iter.next(); + Some(Token::Space) + }, + '\n' => { + self.iter.next(); + Some(Token::Newline) + }, + '\t' => { + self.iter.next(); + Some(Token::Indent) + }, + /* separators */ + '/' => { + self.iter.next(); + Some(Token::DateSep) + }, + ':' => { + self.iter.next(); + Some(Token::AccountSep) + }, + ',' | '.' => { + self.iter.next(); + Some(Token::DecimalSep) + }, + /* comments */ + ';' | '#' | '%' => { + self.iter.next(); + Some(Token::Comment(ch)) + }, + /* markers */ + '*' | '!' | '@' | '-' => { + self.iter.next(); + Some(Token::Marker(ch)) + }, + '`' => { + println!("--"); + None + }, + _ => self.next(), + } + } +} + + +pub fn lex(text: &str) -> Vec<Token> { + Lexer::new(text.chars()).collect() +} + + + +struct Parser { + +} + +pub fn parse(name: &str) -> Journal { + let text = fs::read_to_string(name).expect("Cannot open file"); + + println!("{:?}", text); + + for token in lex(&text) { + println!("{:?}", token); + } + + Journal { + accounts: vec![], + commodities: vec![], + transactions: vec![], + } +} |