mirror of
https://forge.katzen.cafe/katzen-cafe/iowo.git
synced 2026-11-24 07:50:30 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ccc6d4f532 |
||
|
|
54401d2a21 |
||
|
|
18309ec919 |
||
|
|
0705702d4a |
||
|
|
31a044577a |
||
|
|
911339fc2a |
||
|
|
619b7acf94 |
||
|
|
b7bc0366c2 |
||
|
|
734a734f09 |
||
|
|
dddbcccf72 |
||
|
|
26996fbd00 |
||
|
|
d9a07c8898 |
||
|
|
db9228dec4 |
||
|
|
56ec11e143 |
||
|
|
1e9648966f |
||
|
|
a2695a2a11 |
||
|
|
dc44244e7b |
||
|
|
1e0741e600 |
||
|
|
3eee768ce1 |
||
|
|
eb7806572b |
||
|
|
1c6180aabc |
||
|
|
37651a83bc |
||
|
|
3e2c5946c8 |
||
|
|
1a533eb788 |
||
|
|
7bc603f7e7 |
||
|
|
d6bc644fb6 |
||
|
|
cfefab9fd0 |
||
|
|
0de076ace1 |
||
|
|
946ac879a7 |
||
|
|
f6da90a354 |
||
|
|
ed151c2e3c |
||
|
|
4bcaf945d7 |
||
|
|
29cdcfbe0c |
||
|
|
afd493be16 |
||
|
|
30f17773a8 |
||
|
|
db2643359c |
||
|
|
9af71ed3f4 |
||
|
|
8a541546d9 |
||
|
|
4df0118aa4 |
||
|
|
ba0da33509 |
||
|
|
9510d9254c |
||
|
|
e62b50a51a |
||
|
|
2bea3994c2 |
||
|
|
86b1481943 |
||
|
|
06c9094227 |
||
|
|
381ab45edc |
||
|
|
6d8b79e8f7 |
||
|
|
be637846b1 |
||
|
|
1711d17fa6 |
||
|
|
f7b61f9e0e |
||
|
|
2d59a7f560 |
||
|
|
9da157ff4a |
||
|
|
881a987b2f |
||
|
|
bfd4b3765f |
||
|
|
198c74c7ae |
||
|
|
8d7401531e |
||
|
|
b6e304fa78 |
||
|
|
ace69b0094 |
||
|
|
84448af714 |
||
|
|
ae60db7721 |
||
|
|
de008263ca |
||
|
|
ca84af4e1b |
||
|
|
ae86ae29ab |
||
|
|
02c5e9e159 |
||
|
|
0197df5ee2 |
||
|
|
919a3bb377 |
||
|
|
9ae8c2fbd3 |
||
|
|
9727ef82ca |
||
|
|
c31a158d9b |
||
|
|
aeeee54200 |
||
|
|
dc7d76dc26 |
||
|
|
e17fffb66b |
||
|
|
f59062cf88 |
||
|
|
384fef5a81 |
||
|
|
77bcb54b5e |
||
|
|
d87033d320 |
||
|
|
bf60bdd814 |
||
|
|
5368951254 |
||
|
|
a42ec014e5 |
||
|
|
01b1880089 |
||
|
|
56848a1b05 |
||
|
|
69f0baf425 |
||
|
|
98850ee1e9 |
93 changed files with 5266 additions and 292 deletions
875
Cargo.lock
generated
875
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -2,13 +2,17 @@
|
|||
members = [
|
||||
"crates/app",
|
||||
"crates/eval",
|
||||
"crates/ir",
|
||||
"crates/ir",
|
||||
"crates/lang",
|
||||
"crates/svg-filters",
|
||||
"crates/prowocessing",
|
||||
]
|
||||
resolver = "2"
|
||||
|
||||
[workspace.dependencies]
|
||||
clap = { version = "4", features = ["derive"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
petgraph = "0.6.4"
|
||||
|
||||
# to enable all the lints below, this must be present in a workspace member's Cargo.toml:
|
||||
# [lints]
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
name = "app"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
default-run = "app"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
|
|
@ -11,6 +12,7 @@ clap = { workspace = true, features = [ "derive", "env" ] }
|
|||
dirs = "5"
|
||||
eval = { path = "../eval" }
|
||||
ir = { path = "../ir" }
|
||||
prowocessing = { path = "../prowocessing"}
|
||||
owo-colors = "4"
|
||||
ron = "0.8"
|
||||
serde = { workspace = true, features = [ "derive" ] }
|
||||
|
|
|
|||
|
|
@ -1,18 +1,11 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
use self::{
|
||||
cli::Args,
|
||||
config_file::{find_config_file, Configs},
|
||||
};
|
||||
use self::config_file::{find_config_file, Configs};
|
||||
pub(crate) use cli::CliConfigs;
|
||||
|
||||
mod cli;
|
||||
mod config_file;
|
||||
|
||||
/// this struct may hold all configuration
|
||||
pub struct Config {
|
||||
pub source: PathBuf,
|
||||
pub evaluator: eval::Available,
|
||||
|
||||
pub startup_msg: bool,
|
||||
|
|
@ -20,13 +13,17 @@ pub struct Config {
|
|||
|
||||
impl Config {
|
||||
/// Get the configs from all possible places (args, file, env...)
|
||||
pub fn read() -> Self {
|
||||
let args = Args::parse();
|
||||
let config = if let Some(config) = args.config_path {
|
||||
Ok(config)
|
||||
} else {
|
||||
find_config_file()
|
||||
};
|
||||
pub fn read(args: &CliConfigs) -> Self {
|
||||
// let config = if let Some(config) = &args.config_path {
|
||||
// Ok(config.clone())
|
||||
// } else {
|
||||
// find_config_file()
|
||||
// };
|
||||
let config = args
|
||||
.config_path
|
||||
.clone()
|
||||
.ok_or(())
|
||||
.or_else(|()| find_config_file());
|
||||
|
||||
// try to read a maybe existing config file
|
||||
let config = config.ok().and_then(|path| {
|
||||
|
|
@ -42,7 +39,6 @@ impl Config {
|
|||
|
||||
if let Some(file) = config {
|
||||
Self {
|
||||
source: args.source,
|
||||
evaluator: args.evaluator.and(file.evaluator).unwrap_or_default(),
|
||||
// this is negated because to an outward api, the negative is more intuitive,
|
||||
// while in the source the other way around is more intuitive
|
||||
|
|
@ -50,7 +46,6 @@ impl Config {
|
|||
}
|
||||
} else {
|
||||
Self {
|
||||
source: args.source,
|
||||
startup_msg: !args.no_startup_message,
|
||||
evaluator: args.evaluator.unwrap_or_default(),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,9 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
use clap::{builder::BoolishValueParser, ArgAction, Parser};
|
||||
|
||||
#[derive(Parser)]
|
||||
pub(crate) struct Args {
|
||||
/// What file contains the pipeline to evaluate.
|
||||
pub source: PathBuf,
|
||||
use clap::{builder::BoolishValueParser, ArgAction, Args};
|
||||
|
||||
#[derive(Args)]
|
||||
pub(crate) struct CliConfigs {
|
||||
/// How to actually run the pipeline.
|
||||
/// Overrides the config file. Defaults to the debug evaluator.
|
||||
#[arg(short, long)]
|
||||
|
|
|
|||
|
|
@ -5,7 +5,9 @@ use std::{
|
|||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::error::Config;
|
||||
use crate::error_reporting::{report_serde_json_err, report_serde_ron_err};
|
||||
|
||||
use super::error::{self, Config};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Configs {
|
||||
|
|
@ -40,14 +42,20 @@ pub(super) fn find_config_file() -> Result<PathBuf, Config> {
|
|||
}
|
||||
|
||||
impl Configs {
|
||||
pub fn read(p: PathBuf) -> Result<Self, Config> {
|
||||
pub fn read(p: PathBuf) -> Result<Self, error::Config> {
|
||||
match p
|
||||
.extension()
|
||||
.map(|v| v.to_str().expect("config path to be UTF-8"))
|
||||
{
|
||||
Some("ron") => Ok(serde_json::from_str(&fs::read_to_string(p)?)?),
|
||||
Some("json") => Ok(ron::from_str(&fs::read_to_string(p)?)?),
|
||||
e => Err(Config::UnknownExtension(e.map(str::to_owned))),
|
||||
Some("ron") => {
|
||||
let f = fs::read_to_string(p)?;
|
||||
ron::from_str(&f).or_else(|e| report_serde_ron_err(&f, &e))
|
||||
}
|
||||
Some("json") => {
|
||||
let f = fs::read_to_string(p)?;
|
||||
serde_json::from_str(&f).or_else(|e| report_serde_json_err(&f, &e))
|
||||
}
|
||||
e => Err(error::Config::UnknownExtension(e.map(str::to_owned))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ fn report_serde_err(src: &str, line: usize, col: usize, msg: String) -> ! {
|
|||
.finish()
|
||||
.eprint(("test", Source::from(src)))
|
||||
.expect("writing error to stderr failed");
|
||||
process::exit(1);
|
||||
process::exit(1)
|
||||
}
|
||||
|
||||
/// Reconstruct a byte offset from the line + column numbers typical from serde crates
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
use std::fs;
|
||||
use std::{fs, path::PathBuf};
|
||||
|
||||
use config::Config;
|
||||
use clap::{Parser, Subcommand};
|
||||
use config::{CliConfigs, Config};
|
||||
use dev::DevCommands;
|
||||
use welcome_msg::print_startup_msg;
|
||||
|
||||
mod config;
|
||||
|
|
@ -9,19 +11,60 @@ mod config;
|
|||
mod error_reporting;
|
||||
mod welcome_msg;
|
||||
|
||||
#[derive(Parser)]
|
||||
struct Args {
|
||||
#[command(flatten)]
|
||||
configs: CliConfigs,
|
||||
#[command(subcommand)]
|
||||
command: Commands,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
Run {
|
||||
/// What file contains the pipeline to evaluate.
|
||||
source: PathBuf,
|
||||
},
|
||||
Dev {
|
||||
#[command(subcommand)]
|
||||
command: DevCommands,
|
||||
},
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// TODO: proper error handling across the whole function
|
||||
// don't forget to also look inside `Config`
|
||||
let cfg = Config::read();
|
||||
let args = Args::parse();
|
||||
let cfg = Config::read(&args.configs);
|
||||
|
||||
if cfg.startup_msg {
|
||||
print_startup_msg();
|
||||
}
|
||||
|
||||
let source = fs::read_to_string(cfg.source).expect("can't find source file");
|
||||
let ir = ir::from_ron(&source).expect("failed to parse source to graph ir");
|
||||
match args.command {
|
||||
Commands::Run { source } => {
|
||||
let source = fs::read_to_string(source).expect("can't find source file");
|
||||
let ir = ir::from_ron(&source).expect("failed to parse source to graph ir");
|
||||
|
||||
let mut machine = cfg.evaluator.pick();
|
||||
machine.feed(ir);
|
||||
machine.eval_full();
|
||||
let mut machine = cfg.evaluator.pick();
|
||||
machine.feed(ir);
|
||||
machine.eval_full();
|
||||
}
|
||||
Commands::Dev {
|
||||
command: dev_command,
|
||||
} => dev_command.run(),
|
||||
}
|
||||
}
|
||||
|
||||
mod dev {
|
||||
use clap::Subcommand;
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub(crate) enum DevCommands {}
|
||||
|
||||
impl DevCommands {
|
||||
pub fn run(self) {
|
||||
println!("There are currently no dev commands.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ impl Available {
|
|||
#[must_use]
|
||||
pub fn pick(&self) -> Box<dyn Evaluator> {
|
||||
match self {
|
||||
Self::Debug => Box::new(kind::debug::Evaluator::default()),
|
||||
Self::Debug => Box::<kind::debug::Evaluator>::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
25
crates/lang/Cargo.toml
Normal file
25
crates/lang/Cargo.toml
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
[package]
|
||||
name = "lang"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
logos = "0.14"
|
||||
petgraph = { workspace = true}
|
||||
indexmap = "2.2.6"
|
||||
clap = { version = "4", features = ["derive"] }
|
||||
ariadne = "0.4.0"
|
||||
ego-tree = "0.6.2"
|
||||
rowan = "0.15.15"
|
||||
drop_bomb = "0.1.5"
|
||||
enumset = "1.1.3"
|
||||
indoc = "2"
|
||||
dashmap = "5.5.3"
|
||||
crossbeam = "0.8.4"
|
||||
owo-colors = {version = "4", features = ["supports-colors"]}
|
||||
strip-ansi-escapes = "0.2.0"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
80
crates/lang/src/ast.rs
Normal file
80
crates/lang/src/ast.rs
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
use crate::lst_parser::syntax_kind::SyntaxKind::*;
|
||||
use crate::SyntaxNode;
|
||||
use rowan::Language;
|
||||
|
||||
// Heavily modified version of https://github.com/rust-analyzer/rowan/blob/e2d2e93e16c5104b136d0bc738a0d48346922200/examples/s_expressions.rs#L250-L266
|
||||
macro_rules! ast_nodes {
|
||||
($($ast:ident, $kind:ident);+) => {
|
||||
$(
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
#[repr(transparent)]
|
||||
pub struct $ast(SyntaxNode);
|
||||
impl rowan::ast::AstNode for $ast {
|
||||
type Language = crate::Lang;
|
||||
|
||||
fn can_cast(kind: <Self::Language as Language>::Kind) -> bool {
|
||||
kind == $kind
|
||||
}
|
||||
|
||||
fn cast(node: SyntaxNode) -> Option<Self> {
|
||||
if node.kind() == $kind {
|
||||
Some(Self(node))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn syntax(&self) -> &SyntaxNode {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
)+
|
||||
};
|
||||
}
|
||||
|
||||
ast_nodes!(
|
||||
Def, DEF;
|
||||
DefName, DEF_NAME;
|
||||
DefBody, DEF_BODY;
|
||||
|
||||
Mod, MODULE;
|
||||
ModName, MODULE_NAME;
|
||||
ModBody, MODULE_BODY;
|
||||
|
||||
Use, USE;
|
||||
UsePat, USE_PAT;
|
||||
PatItem, PAT_ITEM;
|
||||
PatGlob, PAT_GLOB;
|
||||
PatGroup, PAT_GROUP;
|
||||
|
||||
Literal, LITERAL;
|
||||
IntLit, INT_NUM;
|
||||
FloatLit, FLOAT_NUM;
|
||||
StringLit, STRING;
|
||||
|
||||
Matrix, MATRIX;
|
||||
MatrixRow, MAT_ROW;
|
||||
Vector, VEC;
|
||||
List, LIST;
|
||||
CollectionItem, COLLECTION_ITEM;
|
||||
|
||||
ParenthesizedExpr, PARENTHESIZED_EXPR;
|
||||
Expression, EXPR;
|
||||
|
||||
Pipeline, PIPELINE;
|
||||
|
||||
Instruction, INSTR;
|
||||
InstructionName, INSTR_NAME;
|
||||
InstructionParams, INSTR_PARAMS;
|
||||
|
||||
AttributeSet, ATTR_SET;
|
||||
Attribute, ATTR;
|
||||
AttributeName, ATTR_NAME;
|
||||
AttributeValue, ATTR_VALUE;
|
||||
|
||||
ParseError, PARSE_ERR;
|
||||
LexError, LEX_ERR;
|
||||
|
||||
Root, ROOT;
|
||||
Eof, EOF
|
||||
);
|
||||
25
crates/lang/src/lib.rs
Normal file
25
crates/lang/src/lib.rs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#![feature(type_alias_impl_trait, lint_reasons, box_into_inner)]
|
||||
|
||||
use crate::lst_parser::syntax_kind::SyntaxKind;
|
||||
|
||||
pub mod ast;
|
||||
pub mod lst_parser;
|
||||
pub mod world;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub enum Lang {}
|
||||
impl rowan::Language for Lang {
|
||||
type Kind = SyntaxKind;
|
||||
#[allow(unsafe_code)]
|
||||
fn kind_from_raw(raw: rowan::SyntaxKind) -> Self::Kind {
|
||||
assert!(raw.0 <= SyntaxKind::ROOT as u16);
|
||||
unsafe { std::mem::transmute::<u16, SyntaxKind>(raw.0) }
|
||||
}
|
||||
fn kind_to_raw(kind: Self::Kind) -> rowan::SyntaxKind {
|
||||
kind.into()
|
||||
}
|
||||
}
|
||||
|
||||
pub type SyntaxNode = rowan::SyntaxNode<Lang>;
|
||||
pub type SyntaxToken = rowan::SyntaxNode<Lang>;
|
||||
pub type SyntaxElement = rowan::NodeOrToken<SyntaxNode, SyntaxToken>;
|
||||
169
crates/lang/src/lst_parser.rs
Normal file
169
crates/lang/src/lst_parser.rs
Normal file
|
|
@ -0,0 +1,169 @@
|
|||
use drop_bomb::DropBomb;
|
||||
|
||||
use self::{
|
||||
error::SyntaxError,
|
||||
events::{Event, NodeKind},
|
||||
input::Input,
|
||||
syntax_kind::SyntaxKind,
|
||||
};
|
||||
use std::cell::Cell;
|
||||
|
||||
pub mod syntax_kind;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
pub mod error;
|
||||
pub mod events;
|
||||
pub mod grammar;
|
||||
pub mod input;
|
||||
pub mod output;
|
||||
|
||||
const PARSER_STEP_LIMIT: u32 = 4096;
|
||||
|
||||
pub struct Parser<'src, 'toks> {
|
||||
input: Input<'src, 'toks>,
|
||||
pos: usize,
|
||||
events: Vec<Event>,
|
||||
steps: Cell<u32>,
|
||||
}
|
||||
|
||||
impl<'src, 'toks> Parser<'src, 'toks> {
|
||||
pub fn new(input: Input<'src, 'toks>) -> Self {
|
||||
Self {
|
||||
input,
|
||||
pos: 0,
|
||||
events: Vec::new(),
|
||||
steps: Cell::new(0),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn finish(self) -> Vec<Event> {
|
||||
self.events
|
||||
}
|
||||
|
||||
pub(crate) fn nth(&self, n: usize) -> SyntaxKind {
|
||||
self.step();
|
||||
self.input.kind(self.pos + n)
|
||||
}
|
||||
|
||||
pub fn eat_succeeding_ws(&mut self) {
|
||||
self.push_ev(Event::Eat {
|
||||
count: self.input.meaningless_tail_len(),
|
||||
});
|
||||
}
|
||||
|
||||
pub(crate) fn current(&self) -> SyntaxKind {
|
||||
self.step();
|
||||
self.input.kind(self.pos)
|
||||
}
|
||||
|
||||
pub(crate) fn start(&mut self, name: &str) -> Marker {
|
||||
let pos = self.events.len();
|
||||
self.push_ev(Event::tombstone());
|
||||
Marker::new(pos, name)
|
||||
}
|
||||
|
||||
pub(crate) fn at(&self, kind: SyntaxKind) -> bool {
|
||||
self.nth_at(0, kind)
|
||||
}
|
||||
|
||||
pub(crate) fn eat(&mut self, kind: SyntaxKind) -> bool {
|
||||
if !self.at(kind) {
|
||||
return false;
|
||||
}
|
||||
|
||||
self.do_bump();
|
||||
true
|
||||
}
|
||||
|
||||
pub(crate) fn nth_at(&self, n: usize, kind: SyntaxKind) -> bool {
|
||||
self.nth(n) == kind
|
||||
}
|
||||
|
||||
fn do_bump(&mut self) {
|
||||
self.push_ev(Event::Eat {
|
||||
count: self.input.preceding_meaningless(self.pos),
|
||||
});
|
||||
self.pos += 1;
|
||||
}
|
||||
|
||||
fn push_ev(&mut self, event: Event) {
|
||||
self.events.push(event)
|
||||
}
|
||||
|
||||
fn step(&self) {
|
||||
let steps = self.steps.get();
|
||||
assert!(steps <= PARSER_STEP_LIMIT, "the parser seems stuck...");
|
||||
self.steps.set(steps + 1);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct Marker {
|
||||
pos: usize,
|
||||
bomb: DropBomb,
|
||||
}
|
||||
|
||||
impl Marker {
|
||||
pub(crate) fn new(pos: usize, name: &str) -> Self {
|
||||
Self {
|
||||
pos,
|
||||
bomb: DropBomb::new(format!("Marker {name} must be completed or abandoned")),
|
||||
}
|
||||
}
|
||||
|
||||
fn close_node(mut self, p: &mut Parser, kind: NodeKind) -> CompletedMarker {
|
||||
self.bomb.defuse();
|
||||
match &mut p.events[self.pos] {
|
||||
Event::Start { kind: slot, .. } => *slot = kind.clone(),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
||||
p.push_ev(Event::Finish);
|
||||
|
||||
CompletedMarker {
|
||||
pos: self.pos,
|
||||
kind,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn complete(self, p: &mut Parser<'_, '_>, kind: SyntaxKind) -> CompletedMarker {
|
||||
self.close_node(p, NodeKind::Syntax(kind))
|
||||
}
|
||||
|
||||
pub(crate) fn error(self, p: &mut Parser, kind: SyntaxError) -> CompletedMarker {
|
||||
self.close_node(p, NodeKind::Error(kind))
|
||||
}
|
||||
|
||||
pub(crate) fn abandon(mut self, p: &mut Parser<'_, '_>) {
|
||||
self.bomb.defuse();
|
||||
if self.pos == p.events.len() - 1 {
|
||||
match p.events.pop() {
|
||||
Some(Event::Start {
|
||||
kind: NodeKind::Syntax(SyntaxKind::TOMBSTONE),
|
||||
forward_parent: None,
|
||||
}) => (),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct CompletedMarker {
|
||||
pos: usize,
|
||||
kind: NodeKind,
|
||||
}
|
||||
|
||||
impl CompletedMarker {
|
||||
pub(crate) fn precede(self, p: &mut Parser<'_, '_>, name: &str) -> Marker {
|
||||
let new_pos = p.start(name);
|
||||
|
||||
match &mut p.events[self.pos] {
|
||||
Event::Start { forward_parent, .. } => {
|
||||
*forward_parent = Some(new_pos.pos - self.pos);
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
||||
new_pos
|
||||
}
|
||||
}
|
||||
15
crates/lang/src/lst_parser/error.rs
Normal file
15
crates/lang/src/lst_parser/error.rs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
use crate::lst_parser::syntax_kind::SyntaxKind;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||
pub enum SyntaxError {
|
||||
Expected(Vec<SyntaxKind>),
|
||||
PipelineNeedsSink,
|
||||
// if there was two space seperated items in a list
|
||||
SpaceSepInList,
|
||||
SemicolonInList,
|
||||
CommaInMatOrVec,
|
||||
UnterminatedTopLevelItem,
|
||||
UnclosedModuleBody,
|
||||
UnfinishedPath,
|
||||
PathSepContainsSemicolon,
|
||||
}
|
||||
70
crates/lang/src/lst_parser/events.rs
Normal file
70
crates/lang/src/lst_parser/events.rs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
use crate::lst_parser::syntax_kind::SyntaxKind;
|
||||
|
||||
use super::error::SyntaxError;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Event {
|
||||
Start {
|
||||
kind: NodeKind,
|
||||
forward_parent: Option<usize>,
|
||||
},
|
||||
Finish,
|
||||
Eat {
|
||||
count: usize,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum NodeKind {
|
||||
Syntax(SyntaxKind),
|
||||
Error(SyntaxError),
|
||||
}
|
||||
|
||||
impl NodeKind {
|
||||
pub fn is_syntax(&self) -> bool {
|
||||
matches!(self, Self::Syntax(_))
|
||||
}
|
||||
|
||||
pub fn is_error(&self) -> bool {
|
||||
matches!(self, Self::Error(_))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SyntaxKind> for NodeKind {
|
||||
fn from(value: SyntaxKind) -> Self {
|
||||
NodeKind::Syntax(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SyntaxError> for NodeKind {
|
||||
fn from(value: SyntaxError) -> Self {
|
||||
NodeKind::Error(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<SyntaxKind> for NodeKind {
|
||||
fn eq(&self, other: &SyntaxKind) -> bool {
|
||||
match self {
|
||||
NodeKind::Syntax(s) => s == other,
|
||||
NodeKind::Error(_) => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<SyntaxError> for NodeKind {
|
||||
fn eq(&self, other: &SyntaxError) -> bool {
|
||||
match self {
|
||||
NodeKind::Syntax(_) => false,
|
||||
NodeKind::Error(e) => e == other,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Event {
|
||||
pub(crate) fn tombstone() -> Self {
|
||||
Self::Start {
|
||||
kind: SyntaxKind::TOMBSTONE.into(),
|
||||
forward_parent: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
38
crates/lang/src/lst_parser/grammar.rs
Normal file
38
crates/lang/src/lst_parser/grammar.rs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
use std::fmt::Debug;
|
||||
|
||||
use crate::lst_parser::syntax_kind::SyntaxKind::*;
|
||||
|
||||
use self::module::{mod_body, top_level_item};
|
||||
|
||||
use super::{
|
||||
input::Input,
|
||||
output::Output,
|
||||
syntax_kind::{self, lex},
|
||||
Parser,
|
||||
};
|
||||
|
||||
mod expression;
|
||||
mod module;
|
||||
|
||||
pub fn source_file(p: &mut Parser) {
|
||||
let root = p.start("root");
|
||||
|
||||
mod_body(p);
|
||||
// expression::expression(p, false);
|
||||
p.eat_succeeding_ws();
|
||||
|
||||
root.complete(p, ROOT);
|
||||
}
|
||||
|
||||
fn check_parser(input: &str, parser_fn: fn(&mut Parser), output: &str) {
|
||||
let toks = lex(input);
|
||||
let mut parser = Parser::new(Input::new(&toks));
|
||||
|
||||
parser_fn(&mut parser);
|
||||
|
||||
let p_out = dbg!(parser.finish());
|
||||
let o = Output::from_parser_output(toks, p_out);
|
||||
|
||||
let s = strip_ansi_escapes::strip_str(format!("{o:?}"));
|
||||
assert_eq!(&s, output);
|
||||
}
|
||||
44
crates/lang/src/lst_parser/grammar/expression.rs
Normal file
44
crates/lang/src/lst_parser/grammar/expression.rs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
use crate::lst_parser::{error::SyntaxError, syntax_kind::SyntaxKind::*, CompletedMarker, Parser};
|
||||
|
||||
use self::{collection::collection, instruction::instr, lit::literal, pipeline::PIPES};
|
||||
|
||||
mod collection;
|
||||
mod instruction;
|
||||
mod lit;
|
||||
mod pipeline;
|
||||
|
||||
pub fn expression(p: &mut Parser, in_pipe: bool) -> Option<CompletedMarker> {
|
||||
let expr = p.start("expr");
|
||||
|
||||
if atom(p).or_else(|| instr(p)).is_none() {
|
||||
expr.abandon(p);
|
||||
return None;
|
||||
}
|
||||
|
||||
let r = expr.complete(p, EXPR);
|
||||
|
||||
if PIPES.contains(p.current()) && !in_pipe {
|
||||
pipeline::pipeline(p, r)
|
||||
} else {
|
||||
Some(r)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn atom(p: &mut Parser) -> Option<CompletedMarker> {
|
||||
literal(p)
|
||||
.or_else(|| collection(p))
|
||||
.or_else(|| parenthesized_expr(p))
|
||||
}
|
||||
|
||||
pub fn parenthesized_expr(p: &mut Parser) -> Option<CompletedMarker> {
|
||||
if p.eat(L_PAREN) {
|
||||
let par_expr = p.start("parenthesized");
|
||||
expression(p, false);
|
||||
if !p.eat(R_PAREN) {
|
||||
return Some(par_expr.error(p, SyntaxError::Expected(vec![R_PAREN])));
|
||||
}
|
||||
|
||||
return Some(par_expr.complete(p, PARENTHESIZED_EXPR));
|
||||
}
|
||||
None
|
||||
}
|
||||
25
crates/lang/src/lst_parser/grammar/expression/collection.rs
Normal file
25
crates/lang/src/lst_parser/grammar/expression/collection.rs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
use enumset::enum_set;
|
||||
|
||||
use crate::lst_parser::{
|
||||
syntax_kind::{SyntaxKind::*, TokenSet},
|
||||
CompletedMarker, Parser,
|
||||
};
|
||||
|
||||
use self::{attr_set::attr_set, vec::vec_matrix_list};
|
||||
|
||||
mod attr_set;
|
||||
mod vec;
|
||||
|
||||
const COLLECTION_START: TokenSet = enum_set!(L_BRACK | L_BRACE);
|
||||
|
||||
pub fn collection(p: &mut Parser) -> Option<CompletedMarker> {
|
||||
if !COLLECTION_START.contains(p.current()) {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(match p.current() {
|
||||
L_BRACK => vec_matrix_list(p),
|
||||
L_BRACE => attr_set(p),
|
||||
_ => unreachable!(),
|
||||
})
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
use crate::lst_parser::{
|
||||
error::SyntaxError,
|
||||
grammar::expression::{atom, expression},
|
||||
CompletedMarker, Marker, Parser,
|
||||
SyntaxKind::*,
|
||||
};
|
||||
|
||||
pub fn attr_set(p: &mut Parser) -> CompletedMarker {
|
||||
let start = p.start("attr_set_start");
|
||||
assert!(p.eat(L_BRACE));
|
||||
|
||||
loop {
|
||||
if attr(p).is_some() {
|
||||
// TODO: handle others
|
||||
if p.eat(COMMA) {
|
||||
continue;
|
||||
} else if p.eat(R_BRACE) {
|
||||
return start.complete(p, ATTR_SET);
|
||||
}
|
||||
// TODO: check for newline and stuff following that for recov of others
|
||||
} else if p.eat(R_BRACE) {
|
||||
return start.complete(p, ATTR_SET);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn attr(p: &mut Parser) -> Option<CompletedMarker> {
|
||||
if p.at(IDENT) {
|
||||
let attr_start = p.start("attr");
|
||||
let attr_name_start = p.start("attr_name");
|
||||
p.do_bump();
|
||||
attr_name_start.complete(p, ATTR_NAME);
|
||||
|
||||
// TODO: handle comma, expr/atom, other
|
||||
p.eat(COLON);
|
||||
|
||||
// TODO: handle failed expr parser too
|
||||
let attr_value = p.start("attr_value");
|
||||
let _ = expression(p, false);
|
||||
attr_value.complete(p, ATTR_VALUE);
|
||||
Some(attr_start.complete(p, ATTR))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,97 @@
|
|||
use crate::lst_parser::{
|
||||
error::SyntaxError, grammar::expression::atom, CompletedMarker, Marker, Parser, SyntaxKind::*,
|
||||
};
|
||||
|
||||
pub fn vec_matrix_list(p: &mut Parser) -> CompletedMarker {
|
||||
let start = p.start("vec_matrix_list_start");
|
||||
assert!(p.eat(L_BRACK));
|
||||
let row_start = p.start("matrix_row_start");
|
||||
if let Some(item) = atom(p) {
|
||||
item.precede(p, "coll_item_start")
|
||||
.complete(p, COLLECTION_ITEM);
|
||||
|
||||
if p.at(COMMA) {
|
||||
row_start.abandon(p);
|
||||
return finish_list(p, start);
|
||||
}
|
||||
|
||||
finish_mat_or_vec(p, start, row_start)
|
||||
} else if p.eat(R_BRACK) {
|
||||
row_start.abandon(p);
|
||||
start.complete(p, LIST)
|
||||
} else {
|
||||
row_start.abandon(p);
|
||||
start.error(p, SyntaxError::Expected(vec![EXPR, R_BRACK]))
|
||||
}
|
||||
}
|
||||
|
||||
fn finish_list(p: &mut Parser, list_start: Marker) -> CompletedMarker {
|
||||
loop {
|
||||
if p.eat(COMMA) {
|
||||
if let Some(item) = atom(p) {
|
||||
item.precede(p, "coll_item_start")
|
||||
.complete(p, COLLECTION_ITEM);
|
||||
} else if p.eat(R_BRACK) {
|
||||
return list_start.complete(p, LIST);
|
||||
}
|
||||
} else if p.eat(R_BRACK) {
|
||||
return list_start.complete(p, LIST);
|
||||
} else if let Some(item) = atom(p) {
|
||||
item.precede(p, "next_item")
|
||||
.complete(p, COLLECTION_ITEM)
|
||||
.precede(p, "err_space_sep")
|
||||
.error(p, SyntaxError::SpaceSepInList);
|
||||
} else if p.at(SEMICOLON) {
|
||||
let semi_err = p.start("semicolon_err");
|
||||
p.eat(SEMICOLON);
|
||||
semi_err.error(p, SyntaxError::SemicolonInList);
|
||||
if let Some(item) = atom(p) {
|
||||
item.precede(p, "coll_item_start")
|
||||
.complete(p, COLLECTION_ITEM);
|
||||
} else if p.eat(R_BRACK) {
|
||||
return list_start.complete(p, LIST);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: handle commas, general other wrong toks
|
||||
fn finish_mat_or_vec(p: &mut Parser, coll_start: Marker, mut row_start: Marker) -> CompletedMarker {
|
||||
let mut is_matrix = false;
|
||||
let mut row_item_count = 1;
|
||||
loop {
|
||||
if let Some(item) = atom(p) {
|
||||
item.precede(p, "coll_item_start")
|
||||
.complete(p, COLLECTION_ITEM);
|
||||
row_item_count += 1;
|
||||
} else if p.at(SEMICOLON) {
|
||||
is_matrix = true;
|
||||
row_start.complete(p, MAT_ROW);
|
||||
p.eat(SEMICOLON);
|
||||
row_start = p.start("matrix_row_start");
|
||||
row_item_count = 0;
|
||||
} else if p.at(R_BRACK) {
|
||||
if is_matrix && row_item_count == 0 {
|
||||
row_start.abandon(p);
|
||||
p.eat(R_BRACK);
|
||||
return coll_start.complete(p, MATRIX);
|
||||
} else if is_matrix {
|
||||
row_start.complete(p, MAT_ROW);
|
||||
p.eat(R_BRACK);
|
||||
return coll_start.complete(p, MATRIX);
|
||||
} else {
|
||||
row_start.abandon(p);
|
||||
p.eat(R_BRACK);
|
||||
return coll_start.complete(p, VEC);
|
||||
}
|
||||
} else if p.at(COMMA) {
|
||||
let err_unexpected_comma = p.start("err_unexpected_comma");
|
||||
p.do_bump();
|
||||
err_unexpected_comma.error(p, SyntaxError::CommaInMatOrVec);
|
||||
} else {
|
||||
let err_unexpected = p.start("err_unexpected_tok");
|
||||
p.do_bump();
|
||||
err_unexpected.error(p, SyntaxError::Expected(vec![EXPR, SEMICOLON, R_BRACK]));
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue