// Copyright Epic Games, Inc. All Rights Reserved.
using EpicGames.UHT.Utils;
namespace EpicGames.UHT.Tokenizer
{
///
/// Collection of token reader extensions for float values
///
public static class UhtTokenReaderFloatExtensions
{
///
/// Get the next token as a float. If the next token is not a float, no token is consumed.
///
/// Token reader
/// The float value of the token
/// True if the next token was an float, false if not.
public static bool TryOptionalConstFloat(this IUhtTokenReader tokenReader, out float value)
{
ref UhtToken token = ref tokenReader.PeekToken();
if (token.IsConstFloat() && token.GetConstFloat(out value)) // NOTE: This is restricted to only float values
{
tokenReader.ConsumeToken();
return true;
}
value = 0;
return false;
}
///
/// Get the next token as a float. If the next token is not a float, no token is consumed.
///
///
/// The token reader
public static IUhtTokenReader OptionalConstFloat(this IUhtTokenReader tokenReader)
{
tokenReader.TryOptionalConstFloat(out float _);
return tokenReader;
}
///
/// Get the next token as a float. If the next token is not a float, no token is consumed.
///
/// Token reader
/// Delegate to invoke with the float value
/// The token reader
public static IUhtTokenReader OptionalConstFloat(this IUhtTokenReader tokenReader, UhtTokenConstFloatDelegate floatDelegate)
{
if (tokenReader.TryOptionalConstFloat(out float value))
{
floatDelegate(value);
}
return tokenReader;
}
///
/// Get the next token as a float. If the next token is not a float, an exception is thrown
///
/// 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 an float, false if not.
public static IUhtTokenReader RequireConstFloat(this IUhtTokenReader tokenReader, object? exceptionContext = null)
{
if (!tokenReader.TryOptionalConstFloat(out float _))
{
throw new UhtTokenException(tokenReader, tokenReader.PeekToken(), "constant float", exceptionContext);
}
return tokenReader;
}
///
/// Get the next token as a float. If the next token is not a float, an exception is thrown
///
/// Token reader
/// If not null, an exception will be thrown with the given text as part of the message.
/// The floating point value of the token
public static float GetConstFloat(this IUhtTokenReader tokenReader, object? exceptionContext = null)
{
if (!tokenReader.TryOptionalConstFloat(out float value))
{
throw new UhtTokenException(tokenReader, tokenReader.PeekToken(), "constant float", exceptionContext);
}
return value;
}
///
/// Get the next float. It also handles [+/-] token followed by an float.
///
/// Source tokens
/// The float value of the token
/// True if the next token was an float, false if not.
public static bool TryOptionalLeadingSignConstFloat(this IUhtTokenReader tokenReader, out float value)
{
float localValue = 0;
bool results = tokenReader.TryOptionalLeadingSignConstNumeric((ref UhtToken token) =>
{
return (token.IsConstInt() || token.IsConstFloat()) && token.GetConstFloat(out localValue);
});
value = localValue;
return results;
}
///
/// Get the next float. It also handles [+/-] token followed by an float.
///
/// Source tokens
/// The float value of the token
/// True if the next token was an float, false if not.
public static bool TryOptionalConstFloatExpression(this IUhtTokenReader tokenReader, out float value)
{
float localValue = 0;
bool results = tokenReader.TryOptionalLeadingSignConstNumeric((ref UhtToken token) =>
{
return (token.IsConstInt() || token.IsConstFloat()) && token.GetConstFloat(out localValue);
});
value = localValue;
return results;
}
///
/// Get the next float. It also handles [+/-] token followed by an float.
///
/// Source tokens
/// The double value
public static float GetConstFloatExpression(this IUhtTokenReader tokenReader)
{
if (!tokenReader.TryOptionalConstFloatExpression(out float localValue))
{
throw new UhtTokenException(tokenReader, tokenReader.PeekToken(), "constant float", null);
}
return localValue;
}
///
/// Get the next double. It also handles [+/-] token followed by an double.
///
/// Source tokens
/// The double value of the token
/// True if the next token was an double, false if not.
public static bool TryOptionalConstDoubleExpression(this IUhtTokenReader tokenReader, out double value)
{
double localValue = 0;
bool results = tokenReader.TryOptionalLeadingSignConstNumeric((ref UhtToken token) =>
{
return (token.IsConstInt() || token.IsConstFloat()) && token.GetConstDouble(out localValue);
});
value = localValue;
return results;
}
///
/// Get the next double. It also handles [+/-] token followed by an double.
///
/// Source tokens
/// Delegate to invoke if the double is parsed
/// The supplied token reader
public static IUhtTokenReader RequireConstDoubleExpression(this IUhtTokenReader tokenReader, UhtTokenConstDoubleDelegate doubleDelegate)
{
if (!tokenReader.TryOptionalConstDoubleExpression(out double localValue))
{
throw new UhtTokenException(tokenReader, tokenReader.PeekToken(), "constant double", null);
}
doubleDelegate(localValue);
return tokenReader;
}
///
/// Get the next double. It also handles [+/-] token followed by an double.
///
/// Source tokens
/// The double value
public static double GetConstDoubleExpression(this IUhtTokenReader tokenReader)
{
if (!tokenReader.TryOptionalConstDoubleExpression(out double localValue))
{
throw new UhtTokenException(tokenReader, tokenReader.PeekToken(), "constant double", null);
}
return localValue;
}
}
}