Skip to content

Commit

Permalink
添加A选项以源码风格显示AST,
Browse files Browse the repository at this point in the history
 - 将read踢出关键字
 - 将功能稍微分箱
 - 小改报错前缀
 - 对生成的一些`Value::Var`改为`Value::ReprVar`
 - 对跨行字符串稍微修改, 使其换行时使用`\n`
  • Loading branch information
A4-Tacks committed Aug 10, 2023
1 parent 2ddae8d commit 4c3565c
Show file tree
Hide file tree
Showing 14 changed files with 733 additions and 45 deletions.
12 changes: 11 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mindustry_logic_bang_lang"
version = "0.8.0"
version = "0.9.0"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand All @@ -15,6 +15,9 @@ lalrpop = "0.20.0"


[dependencies]
tag_code = { path = "./tools/tag_code", version = "*" }
display_source = { path = "./tools/display_source", version = "*" }

[dependencies.lalrpop-util]
version = "0.20.0"

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod syntax;
pub mod syntax_def;
pub mod tag_code;
pub use tag_code;
24 changes: 21 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ use std::{
mem::replace,
};

use display_source::DisplaySource;
use lalrpop_util::{
lexer::Token,
ParseError
};
use mindustry_logic_bang_lang::{
err,
syntax::{
CompileMeta,
Error,
Expand All @@ -27,6 +27,13 @@ use mindustry_logic_bang_lang::{
tag_code::TagCodes,
};

/// 带有错误前缀, 并且文本为红色的eprintln
macro_rules! err {
( $fmtter:expr $(, $args:expr)* $(,)? ) => {
eprintln!(concat!("\x1b[1;31m", "MainError: ", $fmtter, "\x1b[0m"), $($args),*);
};
}

macro_rules! concat_lines {
( $( $( $s:expr ),* ; )* ) => {
concat!( $( $($s ,)* "\n" ),* )
Expand All @@ -41,6 +48,7 @@ pub const HELP_MSG: &str = concat_lines! {
"MODE:";
"\t", "c: compile MdtBangLang to MdtLogicCode";
"\t", "a: compile MdtBangLang to AST Debug";
"\t", "A: compile MdtBangLang to MdtBangLang";
"\t", "t: compile MdtBangLang to MdtTagCode";
"\t", "T: compile MdtBangLang to MdtTagCode (Builded TagDown)";
"\t", "f: compile MdtLogicCode to MdtTagCode";
Expand Down Expand Up @@ -82,6 +90,13 @@ fn main() {
let ast = from_stdin_build_ast();
println!("{:#?}", ast)
},
"A" => {
let ast = from_stdin_build_ast();
let mut meta = Default::default();
ast.display_source(&mut meta);
assert!(meta.pop_lf());
println!("{}", meta.buffer());
},
"t" => {
let ast = from_stdin_build_ast();
let meta = compile_ast(ast);
Expand Down Expand Up @@ -163,10 +178,13 @@ fn build_tag_down(meta: &mut CompileMeta) {
type ParseResult<'a> = Result<Expand, ParseError<usize, Token<'a>, Error>>;

fn from_stdin_build_ast() -> Expand {
build_ast(&read_stdin())
}

fn build_ast(src: &str) -> Expand {
let parser = ExpandParser::new();
let mut meta = Meta::new();
let buf = read_stdin();
unwrap_parse_err(parser.parse(&mut meta, &buf), &buf)
unwrap_parse_err(parser.parse(&mut meta, src), src)
}

fn read_stdin() -> String {
Expand Down
Loading

0 comments on commit 4c3565c

Please sign in to comment.