Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/language/macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,29 @@ all overloads for that name at once.
```goboscript
CONCAT(prefix, suffix) # becomes prefixsuffix
```

## Stringify Tokens

`STRINGIFY` is a built-in macro that converts its argument tokens into a string literal.
All tokens inside the parentheses are joined with spaces and produced as a single string value.

```goboscript
STRINGIFY(hello world) # becomes "hello world"
```

This is useful when you need to turn a macro expansion or a sequence of tokens into a
string at compile time:

```goboscript
%define VERSION 1 2 3

onflag {
say STRINGIFY(VERSION); # says "1 2 3"
}
```

Nested parentheses are supported and are included verbatim in the resulting string:

```goboscript
STRINGIFY(foo(bar, baz)) # becomes "foo ( bar , baz )"
```
4 changes: 3 additions & 1 deletion src/lexer/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ pub fn name(lex: &mut Lexer<Token>) -> SmolStr {
}

pub fn string(lex: &mut Lexer<Token>) -> Option<SmolStr> {
serde_json::from_str::<String>(lex.slice()).ok().map(SmolStr::from)
serde_json::from_str::<String>(lex.slice())
.ok()
.map(SmolStr::from)
}

pub fn arg(lex: &mut Lexer<Token>) -> SmolStr {
Expand Down
14 changes: 7 additions & 7 deletions src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,18 +241,18 @@ pub enum Token {
impl Display for Token {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Token::Name(name) => write!(f, "name{}", name),
Token::Name(name) => write!(f, "{}", name),
Token::Define => write!(f, "%define"),
Token::Undef => write!(f, "%undef"),
Token::Newline => writeln!(f),
Token::Backslash => write!(f, "\\"),
Token::Arg(name) => write!(f, "${}", name),
Token::Bin(value) => write!(f, "bin{}", value),
Token::Oct(value) => write!(f, "oct{}", value),
Token::Int(value) => write!(f, "int{}", value),
Token::Hex(value) => write!(f, "hex{}", value),
Token::Float(value) => write!(f, "float{}", value),
Token::Str(value) => write!(f, "str{}", value),
Token::Bin(value) => write!(f, "{}", value),
Token::Oct(value) => write!(f, "{}", value),
Token::Int(value) => write!(f, "{}", value),
Token::Hex(value) => write!(f, "{}", value),
Token::Float(value) => write!(f, "{}", value),
Token::Str(value) => write!(f, "\"{}\"", value),
Token::Costumes => write!(f, "costumes"),
Token::Sounds => write!(f, "sounds"),
Token::Local => write!(f, "local"),
Expand Down
Loading
Loading