Proof of concept parsing: print all labels
This commit is contained in:
+19
-2
@@ -1,8 +1,25 @@
|
||||
use crate::{
|
||||
preprocess::peg::{Alt, And, Dot, EOF, Not, Opt, Rule, Star},
|
||||
preprocess::peg::{Alt, And, Dot, EOF, Not, Opt, Parser, Rule, Star, Tag},
|
||||
star,
|
||||
world::Stat,
|
||||
};
|
||||
|
||||
pub fn print_labels(b: &Stat) {
|
||||
let mut parser = Parser::new(&b.code);
|
||||
if !parse_program().parse(&mut parser) {
|
||||
eprintln!("Couldn't parse stat's code: {:?}", b.code);
|
||||
return;
|
||||
}
|
||||
|
||||
for group in parser.iter() {
|
||||
if group.event.kind == "label" {
|
||||
let span = group.event.span.clone();
|
||||
let s = &b.code[span];
|
||||
println!("- {}", s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_program() -> impl Rule {
|
||||
(Opt((parse_line, star!(("\n", parse_line)))), EOF)
|
||||
}
|
||||
@@ -17,7 +34,7 @@ fn parse_any_line() -> impl Rule {
|
||||
|
||||
fn parse_label_line() -> impl Rule {
|
||||
let allowed = Alt(('A'..='Z', 'a'..='z', '0'..='9', "_", "@", "~"));
|
||||
(":", star!(allowed), eol)
|
||||
(":", Tag("label", star!(allowed)), eol)
|
||||
}
|
||||
|
||||
fn eol() -> impl Rule {
|
||||
|
||||
@@ -4,6 +4,7 @@ mod preprocess;
|
||||
mod world;
|
||||
|
||||
use anyhow::anyhow;
|
||||
use labels::labels::print_labels;
|
||||
use preprocess::eval::Context;
|
||||
use std::{env, error::Error, fs, path::PathBuf, process::exit};
|
||||
use world::World;
|
||||
@@ -46,6 +47,13 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
board.name.push_str(" (♪)");
|
||||
}
|
||||
|
||||
for board in &world.boards {
|
||||
println!("# Board: {}", board.name);
|
||||
for stat in &board.stats {
|
||||
print_labels(stat);
|
||||
}
|
||||
}
|
||||
|
||||
// Try to write a modified world file
|
||||
fs::write("tmp.zzt", world.to_bytes()?)?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user