complete pest grammar and sample il file

This commit is contained in:
2023-03-12 17:12:50 +01:00
parent b37eee1fe1
commit 455e62efd3
5 changed files with 263 additions and 11 deletions
+17 -4
View File
@@ -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<String> = env::args().collect();
let mut file = fs::File::open(args[1].as_str()).expect("msg");
let mut data = String::new();
let mut args: Vec<String> = 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);
}
}
+28
View File
@@ -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 }