Skip to content

Commit

Permalink
一些语法报错时将会带上源码位置, 方便查找
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Jan 24, 2025
1 parent e174b47 commit c50e1b0
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 55 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

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

2 changes: 1 addition & 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.17.18"
version = "0.17.19"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
9 changes: 6 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::{
fmt::Display,
cell::RefCell,
borrow::Cow,
rc::Rc,
};

use display_source::{
Expand Down Expand Up @@ -442,11 +443,11 @@ fn unwrap_parse_err(result: ParseResult<'_>, src: &str) -> Expand {
}

struct CompileMetaExtender {
source: String,
source: Rc<String>,
display_meta: RefCell<DisplaySourceMeta>,
}
impl CompileMetaExtender {
fn new(source: String, display_meta: RefCell<DisplaySourceMeta>) -> Self {
fn new(source: Rc<String>, display_meta: RefCell<DisplaySourceMeta>) -> Self {
Self {
source,
display_meta,
Expand All @@ -471,10 +472,12 @@ impl CompileMetaExtends for CompileMetaExtender {

fn compile_ast(ast: Expand, src: String) -> CompileMeta {
let mut meta = CompileMeta::new();
let src = Rc::new(src);
meta.set_extender(Box::new(CompileMetaExtender::new(
src,
src.clone(),
DisplaySourceMeta::new().into(),
)));
meta.set_source(src);
meta.compile_res_self(ast)
}

Expand Down
6 changes: 2 additions & 4 deletions tools/parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "parser"
version = "0.3.25"
version = "0.3.26"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -11,6 +11,7 @@ lalrpop = "=0.20.0"
[dependencies]
syntax = { path = "../syntax", version = "*" }
var_utils = { path = "../var_utils", version = "*" }
tag_code = { path = "../tag_code", version = "*" }

[dependencies.lalrpop-util]
version = "=0.20.0"
Expand All @@ -20,6 +21,3 @@ features = [
"regex",
"lexer"
]

[dev-dependencies]
tag_code = { path = "../tag_code", version = "*" }
28 changes: 17 additions & 11 deletions tools/parser/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use ::syntax::{
},
Var,
DExp,
Cmper,
ValueBind,
ValueBindRef,
ValueBindRefTarget,
Expand Down Expand Up @@ -53,6 +54,7 @@ use ::syntax::{
FALSE_VAR,
};
use ::var_utils::string_escape;
use tag_code::logic_parser::IdxBox;

grammar(meta: &mut Meta);

Expand Down Expand Up @@ -104,6 +106,9 @@ MakeDExpBody<T>: DExp = {
#[inline]
Boxed<T>: Box<T> = T => Box::new(<>);

#[inline]
Loc<T>: IdxBox<T> = @L T => IdxBox::new(<>);

CtrlBreakStart: () = () => meta.add_control_break_level(None);
CtrlContinueStart: () = () => meta.add_control_continue_level(None);
CtrlStart: () = () => meta.add_control_level(None, None);
Expand Down Expand Up @@ -168,7 +173,7 @@ NonConstRangeValue: Value = {
LogicLine::SetResultHandle(value),
].into()).into()
},
"goto" <MTuple<JumpCmpOnce>> => Value::Cmper(<>.into()),
<l:Loc<"goto">> <v:MTuple<Boxed<JumpCmpOnce>>> => Cmper(l.new_value(v)).into(),
".." => Value::Binder,
FEDExp => <>.into(),
MFTuple<Value>,
Expand Down Expand Up @@ -381,7 +386,7 @@ QuickTake: LogicLine = <value:NonConstRangeValue> "!" <args:ParamArgs> LEnd => {
};

Match: Match = {
"match" <args:ParamArgs> <cases:MBlock<(
"match" <args:Loc<ParamArgs>> <cases:MBlock<(
<MatchPat*> <("@" <MatchPat*>)?> <MBlock<LogicLine*>>
)*>> => {
Match::new(args, cases.into_iter()
Expand All @@ -395,7 +400,7 @@ Match: Match = {
.collect()
)
},
"match" <args:ParamArgs> "=>" <case:(
"match" <args:Loc<ParamArgs>> "=>" <case:(
<MatchPat*> <("@" <MatchPat*>)?> <MBlock<LogicLine*>>
)> => {
Match::new(args, vec![case].into_iter()
Expand All @@ -419,12 +424,12 @@ MatchPat: MatchPatAtom = {
}

ConstMatch: ConstMatch = {
"const" "match" <args:ParamArgs> <cases:MBlock<(
"const" "match" <args:Loc<ParamArgs>> <cases:MBlock<(
ConstMatchPat NoPrefixInlineBlock
)*>> => {
ConstMatch::new(args, cases)
},
"const" "match" <args:ParamArgs> "=>" <case:(
"const" "match" <args:Loc<ParamArgs>> "=>" <case:(
ConstMatchPat NoPrefixInlineBlock
)> => {
ConstMatch::new(args, vec![case])
Expand All @@ -449,10 +454,10 @@ ConstMatchPatAtom: ConstMatchPatAtom = {
sr,
)
},
<sr:Opt<"$">> <dotake:Opt<"*">> <name:(<Var> ":")?> <pat:MList<("*" <Args0>)>> => {
<sr:Opt<"$">> <dotake:Loc<Opt<"*">>> <name:(<Var> ":")?> <pat:MList<("*" <Args0>)>> => {
let dexp = DExp::new(meta.unnamed_var(), vec![
LogicLine::from(Match::new(
Args::GLOB_ONLY,
dotake.new_value(Args::GLOB_ONLY),
vec![
(
vec![MatchPatAtom::new_unamed(pat, false)].into(),
Expand All @@ -474,7 +479,7 @@ ConstMatchPatAtom: ConstMatchPatAtom = {
)),
].into());
ConstMatchPatAtom::new_guard(
dotake,
dotake.value,
name.unwrap_or_default(),
dexp.into(),
sr,
Expand Down Expand Up @@ -521,9 +526,10 @@ ArgsRepeatBlock: ArgsRepeat = {
}
Ok(ArgsRepeat::new(chunk.unwrap_or(1), block))
},
"@" <pats:ConstMatchPatAtom+> <block:NoPrefixInlineBlock> => {
<l:Loc<"@">> <pats:ConstMatchPatAtom+> <block:NoPrefixInlineBlock> => {
let glob = l.new_value(Args::GLOB_ONLY);
ArgsRepeat::new(pats.len(), vec![
ConstMatch::new(Args::GLOB_ONLY, vec![
ConstMatch::new(glob, vec![
(pats.into(), block),
]).into()
].into())
Expand Down Expand Up @@ -1104,7 +1110,7 @@ ControlBody: LogicLine = {
Block,
}
GSwitchCase: GSwitchCase = {
<skip_extra:Opt<"*">> <ids:Args?> <guard:("if" <JumpCmp>)?> => GSwitchCase::Normal { skip_extra, ids: ids.unwrap_or_default(), guard },
<skip_extra:Opt<"*">> <ids:Loc<Args?>> <guard:("if" <JumpCmp>)?> => GSwitchCase::Normal { skip_extra, ids: ids.map(Option::unwrap_or_default), guard },
<skip_extra:Opt<"*">> "<" <to:ConstKey?> => GSwitchCase::Catch { skip_extra, underflow: true, missed: false, overflow: false, to },
<skip_extra:Opt<"*">> "!" <to:ConstKey?> => GSwitchCase::Catch { skip_extra, underflow: false, missed: true, overflow: false, to },
<skip_extra:Opt<"*">> ">" <to:ConstKey?> => GSwitchCase::Catch { skip_extra, underflow: false, missed: false, overflow: true, to },
Expand Down
2 changes: 1 addition & 1 deletion tools/parser/tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "parser-tests"
version = "0.1.44"
version = "0.1.45"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
1 change: 1 addition & 0 deletions tools/parser/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5278,6 +5278,7 @@ fn match_test() {

assert_eq!(
parse!(parser, r#"
# align.............
inline@ A B *C {
print A B C;
}
Expand Down
2 changes: 1 addition & 1 deletion tools/syntax/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "syntax"
version = "0.2.48"
version = "0.2.49"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
Loading

0 comments on commit c50e1b0

Please sign in to comment.