diff --git a/Cargo.lock b/Cargo.lock index ea2fb20..f5211b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,204 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cpufeatures" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d997bd5e24a5928dd43e46dc529867e207907fe0b239c3477d924f7f2ca320" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "generic-array" +version = "0.14.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bff49e947297f3312447abdca79f45f4738097cc82b06e72054d2223f601f1b9" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "libc" +version = "0.2.140" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c" + +[[package]] +name = "once_cell" +version = "1.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" + +[[package]] +name = "pest" +version = "2.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cbd939b234e95d72bc393d51788aec68aeeb5d51e748ca08ff3aad58cb722f7" +dependencies = [ + "thiserror", + "ucd-trie", +] + +[[package]] +name = "pest_derive" +version = "2.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a81186863f3d0a27340815be8f2078dd8050b14cd71913db9fbda795e5f707d7" +dependencies = [ + "pest", + "pest_generator", +] + +[[package]] +name = "pest_generator" +version = "2.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75a1ef20bf3193c15ac345acb32e26b3dc3223aff4d77ae4fc5359567683796b" +dependencies = [ + "pest", + "pest_meta", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pest_meta" +version = "2.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e3b284b1f13a20dc5ebc90aff59a51b8d7137c221131b52a7260c08cbc1cc80" +dependencies = [ + "once_cell", + "pest", + "sha2", +] + +[[package]] +name = "proc-macro2" +version = "1.0.51" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "sha2" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "skill-oxide" version = "0.1.0" +dependencies = [ + "pest", + "pest_derive", +] + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "1.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5ab016db510546d856297882807df8da66a16fb8c4101cb8b30054b0d5b2d9c" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5420d42e90af0c38c3290abcca25b9b3bdf379fc9f55c528f53a269d9c9a267e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "typenum" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" + +[[package]] +name = "ucd-trie" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e79c4d996edb816c91e4308506774452e55e95c3c9de07b6729e17e15a5ef81" + +[[package]] +name = "unicode-ident" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" diff --git a/Cargo.toml b/Cargo.toml index 09a537d..7feca70 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +pest = "2.0" +pest_derive = "2.0" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index ac84ca8..628d684 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,21 @@ -use std::{env, fs, io::Read}; +use std::{env, fs}; +extern crate pest; +#[macro_use] +extern crate pest_derive; + +use pest::Parser; + +#[derive(Parser)] +#[grammar = "skill.pest"] +pub struct SkillParser; fn main() { - let args: Vec = env::args().collect(); - let mut file = fs::File::open(args[1].as_str()).expect("msg"); - let mut data = String::new(); + let mut args: Vec = env::args().collect(); + let data = fs::read_to_string(args[1].as_mut_str()).expect("could not read from file"); + + let parse = SkillParser::parse(Rule::skill, data.as_str()).expect("ha").next().unwrap(); + for inner in parse.into_inner() { + println!("{:?}: {:?}", inner.as_rule(), inner); + } } diff --git a/src/skill.pest b/src/skill.pest new file mode 100644 index 0000000..48cf40d --- /dev/null +++ b/src/skill.pest @@ -0,0 +1,28 @@ +WHITESPACE = _{ (" " | NEWLINE) } + +token_char = _{(NUMBER | LETTER | "_")} +token = @{ LETTER ~ token_char* } + +string = @{"\"" ~ (!"\"" ~ ANY)* ~ "\""} +number = @{NUMBER+ ~ ("." ~ NUMBER+)? ~ ("e" ~ "-"? ~ NUMBER+)?} +bool = {"t" | "nil"} +literal = _{string | number | bool} + +COMMENT = _{";" ~ (!NEWLINE ~ ANY)* ~ NEWLINE} + +lisp_list = _{ "(" ~ expr* ~ ")" } +lazy_list = _{ "'(" ~ expr* ~ ")" } +cstyle_list = @{ token ~ lisp_list} + +list = { (cstyle_list | lisp_list | lazy_list) } + +assign = {token ~ "=" ~ (inline_expr | list | token)} +inline_operand = _{(list | token | literal)} +inline_operator = {("-" | "+" | "/" | "*")} +inline_expr = {inline_operand ~ (inline_operator ~ inline_operand)+} + +get_operator = {("->" | "~>")} +get = {token ~ get_operator ~ (list | token)} + +expr = _{(get | assign | list | token | literal)} +skill = { SOI ~ expr* ~ EOI } \ No newline at end of file diff --git a/test/data/test.il b/test/data/test.il index 35517e6..128afa7 100644 --- a/test/data/test.il +++ b/test/data/test.il @@ -1,11 +1,22 @@ ;comment with some words -(function1 arg1 arg2 - (a b c) - cstyle( arg3) +1 +1.3 +1e9 +3.5e-4 +"string" +nil +t - var1 = "string" - var2 = 1 - var3 = nil - var4 = t +token +(list with items) +'(lazy list) +cstyle(list with args) + +var = arg1 + arg2 - " A" / 12 +obj->op() + +(let (a (b 3)) + expr1 + expr2 ) \ No newline at end of file