// Copyright Epic Games, Inc. All Rights Reserved.
using EpicGames.Core;
using EpicGames.UHT.Utils;
namespace EpicGames.UHT.Tokenizer
{
///
/// Collection of token reader exceptions for handling strings
///
public static class UhtTokenReaderStringExtensions
{
///
/// Get the next token as a string. If the next token is not a string, no token is consumed.
///
/// Token reader
/// The string value of the token
/// True if the next token was an string, false if not.
public static bool TryOptionalConstString(this IUhtTokenReader tokenReader, out StringView value)
{
ref UhtToken token = ref tokenReader.PeekToken();
if (token.IsConstString())
{
value = token.GetTokenString();
tokenReader.ConsumeToken();
return true;
}
value = "";
return false;
}
///
/// Get the next token as a string. If the next token is not a string, no token is consumed.
///
/// Token reader
/// True if the next token was an string, false if not.
public static IUhtTokenReader OptionalConstString(this IUhtTokenReader tokenReader)
{
ref UhtToken token = ref tokenReader.PeekToken();
if (token.IsConstString())
{
tokenReader.ConsumeToken();
}
return tokenReader;
}
///
/// Verify that the next token is a string.
///
/// Token reader
/// If not null, an exception will be thrown with the given text as part of the message.
/// True if the next token was a string, false if not.
public static IUhtTokenReader RequireConstString(this IUhtTokenReader tokenReader, object? exceptionContext)
{
ref UhtToken token = ref tokenReader.PeekToken();
if (token.IsConstString())
{
tokenReader.ConsumeToken();
return tokenReader;
}
throw new UhtTokenException(tokenReader, token, "constant string", exceptionContext);
}
///
/// Get the next token as a string. If the next token is not a string, an exception is thrown
///
/// Token reader
/// If not null, an exception will be thrown with the given text as part of the message.
/// The value of the string.
public static StringView GetConstString(this IUhtTokenReader tokenReader, object? exceptionContext = null)
{
ref UhtToken token = ref tokenReader.PeekToken();
if (token.IsConstString())
{
StringView output = token.GetTokenString();
tokenReader.ConsumeToken();
return output;
}
throw new UhtTokenException(tokenReader, token, "constant string", exceptionContext);
}
///
/// Get the next token as a quoted string. If the next token is not a string, an exception is thrown.
/// Character constants are not considered strings by this routine.
///
/// Token reader
/// If not null, an exception will be thrown with the given text as part of the message.
/// The value of the string.
public static StringView GetConstQuotedString(this IUhtTokenReader tokenReader, object? exceptionContext = null)
{
ref UhtToken token = ref tokenReader.PeekToken();
if (token.TokenType == UhtTokenType.StringConst)
{
StringView output = token.Value;
tokenReader.ConsumeToken();
return output;
}
throw new UhtTokenException(tokenReader, token, "constant quoted string", exceptionContext);
}
///
/// Get a const string that can optionally be wrapped with a TEXT() macro
///
/// Token reader
/// If not null, an exception will be thrown with the given text as part of the message.
/// The value of the string
public static StringView GetWrappedConstString(this IUhtTokenReader tokenReader, object? exceptionContext = null)
{
ref UhtToken token = ref tokenReader.PeekToken();
if (token.IsIdentifier("TEXT"))
{
tokenReader.ConsumeToken();
tokenReader.Require('(');
StringView output = tokenReader.GetConstString(exceptionContext);
tokenReader.Require(')');
return output;
}
else
{
return tokenReader.GetConstString(exceptionContext);
}
}
}
}