Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes several bugs with string macros, implements all pronoun/article ones #777

Merged
merged 14 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from 13 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
4 changes: 4 additions & 0 deletions Content.Tests/DMProject/Tests/Text/StringInterpolation1.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

/proc/RunTest()
var/t = "0.5 seconds"
ASSERT("World finished in [t]!" == "World finished in 0.5 seconds!")
8 changes: 8 additions & 0 deletions Content.Tests/DMProject/Tests/Text/StringInterpolation2.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

/obj/blombo
name = "Blombo"

/proc/RunTest()
var/obj/blombo/b = new
var/result_text = "Nobody likes [b]!"
ASSERT(result_text == "Nobody likes Blombo!")
14 changes: 14 additions & 0 deletions Content.Tests/DMProject/Tests/Text/StringInterpolation3.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

/obj/blombo
name = "Blombo"
gender = FEMALE

/obj/blorpo
name = "Blorpo"
gender = MALE

/proc/RunTest()
var/obj/blombo/b = new
var/obj/blorpo/b2 = new
var/result_text = "[b]? Nobody likes \him. \He is awful! Unlike [b2]. \He is pretty cool!"
ASSERT(result_text == "Blombo? Nobody likes her. She is awful! Unlike Blorpo. He is pretty cool!")
21 changes: 21 additions & 0 deletions Content.Tests/DMProject/Tests/Text/StringInterpolation4.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/obj/test1
name = ""
/obj/test2
name = " " // 3 spaces
/obj/test3
name = "\t"

/proc/RunTest()
var/list/correct = list(
"/obj/test1: ",
"/obj/test2: ",
"/obj/test3: \t"
)
var/i = 1
for (var/T in typesof(/obj))
if(T == /obj)
continue
var/obj/O = new T()
var/true_text = correct[i]
ASSERT(true_text == "[T]: \the [O]")
++i
11 changes: 11 additions & 0 deletions Content.Tests/DMProject/Tests/Text/StringInterpolation5.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@


/proc/RunTest()
var/atom/O = new()
O.name = "foo"
O.gender = FEMALE
var/atom/O2 = new
O2.name = "foob"
O2.gender = MALE
var/text = "[O2], \ref[O], \his"
ASSERT(findtextEx(text,", his") != 0)
241 changes: 1 addition & 240 deletions DMCompiler/Compiler/DM/DMParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

namespace DMCompiler.Compiler.DM {
public partial class DMParser : Parser<Token> {
public const char StringFormatCharacter = (char)0xFF;

private DreamPath _currentPath = DreamPath.Root;

Expand Down Expand Up @@ -2008,245 +2007,7 @@ public DMASTExpression Constant() {
case TokenType.DM_Resource: Advance(); return new DMASTConstantResource(constantToken.Location, (string)constantToken.Value);
case TokenType.DM_Null: Advance(); return new DMASTConstantNull(constantToken.Location);
case TokenType.DM_RawString: Advance(); return new DMASTConstantString(constantToken.Location, (string)constantToken.Value);
case TokenType.DM_String: {
string tokenValue = (string)constantToken.Value;
StringBuilder stringBuilder = new StringBuilder(tokenValue.Length);
List<DMASTExpression>? interpolationValues = null;
Advance();

int bracketNesting = 0;
StringBuilder? insideBrackets = null;
StringFormatTypes currentInterpolationType = StringFormatTypes.Stringify;
for (int i = 0; i < tokenValue.Length; i++) {
char c = tokenValue[i];


if (bracketNesting > 0) {
insideBrackets?.Append(c); // should never be null
}

switch (c)
{
case '[':
bracketNesting++;
insideBrackets ??= new StringBuilder(tokenValue.Length - stringBuilder.Length);
interpolationValues ??= new List<DMASTExpression>(1);
break;
case ']' when bracketNesting > 0:
{
bracketNesting--;

if (bracketNesting == 0) { //End of expression
insideBrackets.Remove(insideBrackets.Length - 1, 1); //Remove the ending bracket

string insideBracketsText = insideBrackets?.ToString();
if (!String.IsNullOrWhiteSpace(insideBracketsText)) {
DMPreprocessorLexer preprocLexer = new DMPreprocessorLexer(null, constantToken.Location.SourceFile, insideBracketsText);
List<Token> preprocTokens = new();
Token preprocToken;
do {
preprocToken = preprocLexer.GetNextToken();
preprocToken.Location = constantToken.Location;
preprocTokens.Add(preprocToken);
} while (preprocToken.Type != TokenType.EndOfFile);

DMLexer expressionLexer = new DMLexer(constantToken.Location.SourceFile, preprocTokens);
DMParser expressionParser = new DMParser(expressionLexer, _unimplementedWarnings);

DMASTExpression expression = null;
try {
expressionParser.Whitespace(true);
expression = expressionParser.Expression();
if (expression == null) Error("Expected an expression");
if (expressionParser.Current().Type != TokenType.EndOfFile) Error("Expected end of embedded statement");
} catch (CompileErrorException) {
}

if (expressionParser.Errors.Count > 0) Errors.AddRange(expressionParser.Errors);
if (expressionParser.Warnings.Count > 0) Warnings.AddRange(expressionParser.Warnings);
interpolationValues.Add(expression);
} else {
interpolationValues.Add(null);
}

stringBuilder.Append(StringFormatCharacter);
stringBuilder.Append((char)currentInterpolationType);

currentInterpolationType = StringFormatTypes.Stringify;
insideBrackets.Clear();
}

break;
}
case '\\' when bracketNesting == 0:
{
string escapeSequence = String.Empty;

if (i == tokenValue.Length) {
Error("Invalid escape sequence");
}
c = tokenValue[++i];

if (char.IsLetter(c)) {
while (i < tokenValue.Length && char.IsLetter(tokenValue[i])) {
escapeSequence += tokenValue[i++];
}
i--;

bool unimplemented = false;
bool skipSpaces = false;
//TODO: Many of these require [] before the macro instead of after. They should verify that there is one.
switch (escapeSequence) {
case "proper":
case "improper":
if (stringBuilder.Length != 0) {
Error($"Escape sequence \"\\{escapeSequence}\" must come at the beginning of the string");
}

skipSpaces = true;
stringBuilder.Append(StringFormatCharacter);
stringBuilder.Append(escapeSequence == "proper" ? (char)StringFormatTypes.Proper : (char)StringFormatTypes.Improper);
break;

case "ref":
currentInterpolationType = StringFormatTypes.Ref; break;

case "The":
skipSpaces = true;
currentInterpolationType = StringFormatTypes.UpperDefiniteArticle;
break;
case "the":
skipSpaces = true;
currentInterpolationType = StringFormatTypes.LowerDefiniteArticle;
break;

case "A":
case "An":
unimplemented = true;
currentInterpolationType = StringFormatTypes.UpperIndefiniteArticle;
break;
case "a":
case "an":
unimplemented = true;
currentInterpolationType = StringFormatTypes.LowerIndefiniteArticle;
break;

case "He":
case "She":
unimplemented = true;
stringBuilder.Append(StringFormatCharacter);
stringBuilder.Append((char)StringFormatTypes.UpperSubjectPronoun);
break;
case "he":
case "she":
unimplemented = true;
stringBuilder.Append(StringFormatCharacter);
stringBuilder.Append((char)StringFormatTypes.LowerSubjectPronoun);
break;

case "His":
unimplemented = true;
stringBuilder.Append(StringFormatCharacter);
stringBuilder.Append((char)StringFormatTypes.UpperPossessiveAdjective);
break;
case "his":
unimplemented = true;
stringBuilder.Append(StringFormatCharacter);
stringBuilder.Append((char)StringFormatTypes.LowerPossessiveAdjective);
break;

case "him":
unimplemented = true;
stringBuilder.Append(StringFormatCharacter);
stringBuilder.Append((char)StringFormatTypes.ObjectPronoun);
break;

case "himself":
case "herself":
unimplemented = true;
stringBuilder.Append(StringFormatCharacter);
stringBuilder.Append((char)StringFormatTypes.ReflexivePronoun);
break;

case "Hers":
unimplemented = true;
stringBuilder.Append(StringFormatCharacter);
stringBuilder.Append((char)StringFormatTypes.UpperPossessivePronoun);
break;
case "hers":
unimplemented = true;
stringBuilder.Append(StringFormatCharacter);
stringBuilder.Append((char)StringFormatTypes.LowerPossessivePronoun);
break;

default:
if (escapeSequence.StartsWith("n")) {
stringBuilder.Append('\n');
stringBuilder.Append(escapeSequence.Skip(1).ToArray());
} else if (escapeSequence.StartsWith("t")) {
stringBuilder.Append('\t');
stringBuilder.Append(escapeSequence.Skip(1).ToArray());
} else if (!DMLexer.ValidEscapeSequences.Contains(escapeSequence)) {
Error($"Invalid escape sequence \"\\{escapeSequence}\"");
}

break;
}

if (unimplemented) {
DMCompiler.UnimplementedWarning(constantToken.Location, $"Unimplemented escape sequence \"{escapeSequence}\"");
}

if (skipSpaces) {
// Note that some macros in BYOND require a single/zero space between them and the []
// This doesn't replicate that
while (i < tokenValue.Length - 1 && tokenValue[i + 1] == ' ') i++;
}

} else
{
escapeSequence += c;
switch (escapeSequence)
{
case "[":
case "]":
case "<":
case ">":
case "\"":
case "'":
case "\\":
case " ":
case ".":
stringBuilder.Append(escapeSequence);
break;
default: //Unimplemented escape sequence
Error("Invalid escape sequence \"\\" + escapeSequence + "\"");
break;
}
}

break;
}
default:
{
if (bracketNesting == 0) {
stringBuilder.Append(c);
}

break;
}
}
}

if (bracketNesting > 0) Error("Expected ']'");

string stringValue = stringBuilder.ToString();
if (interpolationValues is null) {
return new DMASTConstantString(constantToken.Location, stringValue);
} else {
return new DMASTStringFormat(constantToken.Location, stringValue, interpolationValues.ToArray());
}
}
case TokenType.DM_String: return ExpressionFromString(constantToken);
default: return null;
}
}
Expand Down
Loading