WIP: Re-implement parser in pest

Parser implementation is kind of iffy. Let's try to re-implement it
using pest.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-02-11 14:50:09 -08:00
parent abd7f7960a
commit f4699e5e21
6 changed files with 223 additions and 1 deletions

View File

@@ -5,10 +5,10 @@ mod scope;
mod syn;
mod vm;
use pest::Parser;
use std::io::Read;
use std::path::PathBuf;
use structopt::StructOpt;
use syn::parser::Parser;
use vm::{error::RuntimeError, machine::MachineBuilder};
#[derive(Debug, StructOpt)]
@@ -33,11 +33,15 @@ fn main() -> Result {
(input, "<stdin>".to_string())
};
let stmts = syn::peg::parse_file(&text)?;
/*
let mut parser = Parser::new(path, text.as_str());
let mut stmts = Vec::new();
while !parser.is_eof() {
stmts.extend(parser.next_stmt_list()?);
}
*/
let mut machine = MachineBuilder::default()
.max_stack_size(opt.max_stack_size)

View File

@@ -2,5 +2,6 @@ pub mod ast;
pub mod error;
pub mod lexer;
pub mod parser;
pub mod peg;
pub mod span;
pub mod token;

24
src/syn/parser.pest Normal file
View File

@@ -0,0 +1,24 @@
WHITESPACE = _{ " " | "\r" | "\t" | "\n" }
int = @{ ASCII_DIGIT+ }
float = @{ ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ ~ ("e" ~ ("+" | "-")? ~ ASCII_DIGIT+)? }
word_char = @{ ASCII_ALPHA | "_" | "?" | "-" | "+" | "*" | "/" | "=" | "@" | "$" | "%" | "^" | "&" | "|" | "~" }
word = @{ word_char+ }
// meta = ${ "%" ~ word }
str = @{
"\"" ~
(
(!("\"" | "\\") ~ ANY)
| "\\" ~ ("n" | "t" | "r" | "b" | "\\" | "\"" | "\'")
)* ~
"\""
}
apply = @{ "!" }
assign = { ":" ~ word }
atom = { float | int | assign | word | str }
quote = { "[" ~ stmt* ~ "]" }
expr = { atom | quote | apply }
stmt = { expr }
file = { SOI ~ stmt* ~ EOI }

53
src/syn/peg.rs Normal file
View File

@@ -0,0 +1,53 @@
use crate::syn::ast::*;
use crate::syn::token::*;
use pest::{error::Error, iterators::Pair, Parser};
#[derive(pest_derive::Parser)]
#[grammar = "syn/parser.pest"]
pub struct SybilParser;
pub type Result<T, E = Error<Rule>> = std::result::Result<T, E>;
fn parse_atom(pair: Pair<Rule>) -> Result<SpAtom> {
match pair.as_rule() {
Rule::float => todo!(),
Rule::int => todo!(),
Rule::assign => todo!(),
Rule::word => todo!(),
Rule::str => todo!(),
_ => unreachable!(),
}
}
fn parse_expr(pair: Pair<Rule>) -> Result<SpExpr> {
match pair.as_rule() {
Rule::atom => {
todo!()
}
Rule::quote => {
todo!()
}
Rule::apply => {
todo!()
}
_ => unreachable!(),
}
}
fn parse_stmt(pair: Pair<Rule>) -> Result<SpStmt> {
match pair.as_rule() {
Rule::expr => {
todo!()
}
_ => unreachable!(),
}
}
pub fn parse_file(text: &str) -> Result<Vec<SpStmt>> {
let input = SybilParser::parse(Rule::file, text)?.next().unwrap();
let mut stmts = Vec::new();
for pair in input.into_inner() {
stmts.push(parse_stmt(pair)?);
}
Ok(stmts)
}