From 856f817dbe00463c5925b36f24d7d95eff8919ae Mon Sep 17 00:00:00 2001 From: Chris Mounce Date: Thu, 22 May 2025 00:07:36 -0700 Subject: [PATCH] Add parsing for label refs, shorthand sends --- src/labels/labels.rs | 49 ++++++++++++++++++- ...labels__labels__test__label_detection.snap | 6 +-- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/labels/labels.rs b/src/labels/labels.rs index 9abfc65..72cc7bb 100644 --- a/src/labels/labels.rs +++ b/src/labels/labels.rs @@ -46,12 +46,45 @@ fn bare_if() -> impl Rule { condition, w, Opt(("then", w)), - Box::new(line) as Box, + Alt((shorthand_send, Box::new(line) as Box)), ) } +/// `send` without a preceding `#` fn bare_send() -> impl Rule { - (NoCase("send"), ww, Tag("ref", label_name), w, eol) + (NoCase("send"), ww, label_reference, w, eol) +} + +/// `send` without a send keyword +fn shorthand_send() -> impl Rule { + let af = ( + And('a'..='f'), + Alt(( + "become", "bind", "change", "char", "clear", "cycle", "die", "end", "endgame", + )), + ); + let gr = ( + And('g'..='r'), + Alt(( + "go", "idle", "if", "lock", "play", "put", "restart", "restore", + )), + ); + let sz = ( + And('s'..='z'), + Alt(( + "send", + "set", + "shoot", + "take", + "throwstar", + "try", + "unlock", + "walk", + "zap", + )), + ); + let command = Alt((af, gr, sz)); + (Not(command), label_reference, w, eol) } fn any_line() -> impl Rule { @@ -138,6 +171,7 @@ fn condition() -> impl Rule { ("blocked", ww, direction), "contact", "energized", + ('a'..='z', star!(word_char)), )); (star!("not", ww), base) } @@ -165,6 +199,16 @@ fn label_name() -> impl Rule { ) } +fn label_reference() -> impl Rule { + Tag( + "ref", + ( + Opt((Tag("dest", plus!(word_char)), ":")), + Tag("name", label_name), + ), + ) +} + fn word_char() -> impl Rule { Alt(('A'..='Z', 'a'..='z', '0'..='9', "_")) } @@ -263,6 +307,7 @@ mod test { let before = &input[last_index..group.span().start]; let inner = match group.kind() { "label" => format!("({})", group.text()), + "ref" => format!("[{}]", group.text()), _ => group.text().into(), }; result.push_str(before); diff --git a/src/labels/snapshots/zasm__labels__labels__test__label_detection.snap b/src/labels/snapshots/zasm__labels__labels__test__label_detection.snap index 745a587..86265c2 100644 --- a/src/labels/snapshots/zasm__labels__labels__test__label_detection.snap +++ b/src/labels/snapshots/zasm__labels__labels__test__label_detection.snap @@ -4,6 +4,6 @@ expression: result --- 'Find all the labels and their references :(foo) -#send foo -#if blocked n #send foo -#if bar foo +#send [foo] +#if blocked n #send [foo] +#if bar [foo]