ItsDeltin / Overwatch-Script-To-Workshop

Converts scripts to Overwatch workshops.
209 stars 26 forks source link

Interpolated strings #224

Closed ItsDeltin closed 3 years ago

ItsDeltin commented 3 years ago

Interpolated strings (aka template literals) allow you to insert values into strings with a format like $'Hello {x}!'

Possible ways to implement:

Implementation 1

Tokens have a new field (ex PreceedingCharacters) in the Token class which contains the preceding whitespace characters leading to the previous token. For example:

String myString = $"Hello {x}!";
This will generate the tokens: Type Text PreceedingCharacters
Identifier String Empty
Identifier myString ' '
Equals = ' '
InterpStart $ ' '
DoubleQuote " Empty
Identifier Hello Empty
CurlyBracketOpen { ' '
Identifier x Empty
CurlyBracketClose } Empty
Exclamation ! Empty
DoubleQuote " Empty

When the InterpStart token is encountered, the string will be built using the proceeding tokens until the curly bracket is reached.

Pros:

Cons:

Implementation 2

Curly brackets are counted during the lexing stage.

For lexing the code below, when the first { is encountered in the string, value starts. Every { after that will add to a counter, } will subtract from it. When } is encountered when the counter is 0, the interpolation head is matched.

String myString = $"Hello {func(() => { })}!";

Pros:

Cons:

Implementation 3

The lexer is changed to only get the tokens when needed, so lexing is done during parsing rather than before. This is how the Typescript compiler handles interpolated strings and parsing in general.

Pros:

Cons:

ItsDeltin commented 3 years ago

Added with v2.0-beta.8

ItsDeltin commented 3 years ago

I went with 3 because 2 did not work well with the incremental lexer.