Skip to content
Open
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
3 changes: 3 additions & 0 deletions src/Lua/CodeAnalysis/Compilation/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public void EnterBlock(bool isLoop)
Assert(FreeRegisterCount == ActiveVariableCount);
}

/// <exception cref="LuaCompileException"></exception>
public void UndefinedGotoError(Label g)
{
if (Scanner.IsReserved(g.Name))
Expand Down Expand Up @@ -306,6 +307,7 @@ public void MoveGotosOut(Block b)
}
}

/// <exception cref="LuaCompileException"></exception>
public void LeaveBlock()
{
var b = Block;
Expand Down Expand Up @@ -354,6 +356,7 @@ public static ExprDesc MakeExpression(Kind kind, int info)
};
}

/// <exception cref="LuaCompileException"></exception>
public void SemanticError(string message)
{
P.Scanner.Token = default;
Expand Down
8 changes: 8 additions & 0 deletions src/Lua/CodeAnalysis/Compilation/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ internal bool TestNext(int token)
return Scanner.TestNext(token);
}

/// <exception cref="LuaCompileException"></exception>
internal void Next()
{
Scanner.Next();
Expand Down Expand Up @@ -119,6 +120,7 @@ public void LeaveLevel()
Scanner.L.CallCount--;
}

/// <exception cref="LuaCompileException"></exception>
public TempBlock EnterLevel()
{
if (!RuntimeHelpers.TryEnsureSufficientExecutionStack())
Expand Down Expand Up @@ -251,6 +253,7 @@ public ExprDesc FunctionArguments(ExprDesc f, int line)
return e;
}

/// <exception cref="LuaCompileException"></exception>
public ExprDesc PrimaryExpression()
{
switch (T)
Expand Down Expand Up @@ -451,6 +454,7 @@ public bool BlockFollow(bool withUntil)
return false;
}

/// <exception cref="LuaCompileException"></exception>
public void StatementList()
{
while (!BlockFollow(true))
Expand Down Expand Up @@ -659,6 +663,7 @@ public void IfStatement(int line)
Function.PatchToHere(escapes);
}

/// <exception cref="LuaCompileException"></exception>
public void Block()
{
Function.EnterBlock(false);
Expand Down Expand Up @@ -884,6 +889,7 @@ public void ReturnStatement()
TestNext(';');
}

/// <exception cref="LuaCompileException"></exception>
public void Statement()
{
var line = Scanner.LineNumber;
Expand Down Expand Up @@ -949,6 +955,7 @@ public void Statement()
Function.FreeRegisterCount = Function.ActiveVariableCount;
}

/// <exception cref="LuaCompileException"></exception>
internal void MainFunction()
{
Function.OpenMainFunction();
Expand All @@ -958,6 +965,7 @@ internal void MainFunction()
Function = Function.CloseMainFunction();
}

/// <exception cref="LuaCompileException"></exception>
public static Prototype Parse(LuaState l, TextReader r, string name)
{
using var internPool = new StringInternPool(4);
Expand Down
6 changes: 6 additions & 0 deletions src/Lua/CodeAnalysis/Compilation/Scanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,19 @@ struct Scanner

public static ReadOnlySpan<string> Tokens => tokens;

/// <exception cref="LuaCompileException">Always</exception>
public void SyntaxError(int position, string message)
{
ScanError(position, message, Token.T);
}

/// <exception cref="LuaCompileException">Always</exception>
public void SyntaxError(string message)
{
ScanError(R.Position, message, Token.T);
}

/// <exception cref="LuaCompileException">Always</exception>
public void ErrorExpected(int position, int t)
{
SyntaxError(position, TokenToString(t) + " expected");
Expand Down Expand Up @@ -213,6 +216,7 @@ string FormatNearToken(int t)
};
}

/// <exception cref="LuaCompileException">Always</exception>
public void ScanError(int pos, string message, int token)
{
var shortSourceBuffer = (stackalloc char[59]);
Expand Down Expand Up @@ -641,6 +645,7 @@ public Token ReservedOrName(int pos, ReadOnlySpan<char> text)
return new(pos, token == 0 ? TkName : token, str, rawLength);
}

/// <exception cref="LuaCompileException"></exception>
public Token Scan()
{
const bool comment = true,
Expand Down Expand Up @@ -799,6 +804,7 @@ public Token Scan()
}
}

/// <exception cref="LuaCompileException"></exception>
public void Next()
{
LastLine = LineNumber;
Expand Down
2 changes: 2 additions & 0 deletions src/Lua/CodeAnalysis/Syntax/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public Lexer() { }

internal readonly SourcePosition Position => position;

/// <exception cref="LuaParseException"></exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void Advance(int count)
{
Expand Down Expand Up @@ -60,6 +61,7 @@ bool TryRead(int offset, out char value)
return true;
}

/// <exception cref="LuaParseException"></exception>
public bool MoveNext()
{
if (Source.Length <= offset)
Expand Down
9 changes: 9 additions & 0 deletions src/Lua/CodeAnalysis/Syntax/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public LuaSyntaxTree Parse()
return tree;
}

/// <exception cref="LuaParseException"></exception>
StatementNode ParseStatement(ref SyntaxTokenEnumerator enumerator)
{
switch (enumerator.Current.Type)
Expand Down Expand Up @@ -655,6 +656,7 @@ out var endToken
);
}

/// <exception cref="LuaParseException"></exception>
TableMethodDeclarationStatementNode ParseTableMethodDeclarationStatement(
ref SyntaxTokenEnumerator enumerator,
SyntaxToken functionToken
Expand Down Expand Up @@ -826,6 +828,7 @@ or SyntaxTokenType.LCurly
return true;
}

/// <exception cref="LuaParseException"></exception>
ExpressionNode ParseExpression(
ref SyntaxTokenEnumerator enumerator,
OperatorPrecedence precedence
Expand Down Expand Up @@ -862,6 +865,7 @@ ExpressionNode ParseMinusNumber(ref SyntaxTokenEnumerator enumerator)
}
}

/// <exception cref="LuaParseException"></exception>
UnaryExpressionNode ParseUnaryExpression(
ref SyntaxTokenEnumerator enumerator,
SyntaxToken operatorToken
Expand All @@ -885,6 +889,7 @@ SyntaxToken operatorToken
return new(operatorType, right, operatorToken.Position);
}

/// <exception cref="LuaParseException"></exception>
BinaryExpressionNode ParseBinaryExpression(
ref SyntaxTokenEnumerator enumerator,
OperatorPrecedence precedence,
Expand Down Expand Up @@ -1028,6 +1033,7 @@ when enumerator.GetNext(true).Type is SyntaxTokenType.Assignment:
return new(items.AsSpan().ToArray(), startToken.Position);
}

/// <exception cref="LuaParseException"></exception>
ExpressionNode ParseTableAccessExpression(
ref SyntaxTokenEnumerator enumerator,
ExpressionNode? parentTable
Expand Down Expand Up @@ -1334,6 +1340,7 @@ SourcePosition GetEndPosition(ref SyntaxTokenEnumerator enumerator)
return EndPosition.Line > 0 ? EndPosition : enumerator.Current.Position;
}

/// <exception cref="LuaParseException"></exception>
void MissingClosingToken(
ref SyntaxTokenEnumerator enumerator,
SyntaxTokenType expectedToken,
Expand Down Expand Up @@ -1390,6 +1397,7 @@ out SyntaxToken token
MoveNextWithValidation(ref enumerator);
}

/// <exception cref="LuaParseException"></exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void CheckCurrent(ref SyntaxTokenEnumerator enumerator, SyntaxTokenType expectedToken)
{
Expand All @@ -1405,6 +1413,7 @@ void CheckCurrent(ref SyntaxTokenEnumerator enumerator, SyntaxTokenType expected
}
}

/// <exception cref="LuaParseException"></exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void MoveNextWithValidation(ref SyntaxTokenEnumerator enumerator)
{
Expand Down
8 changes: 8 additions & 0 deletions src/Lua/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class LuaParseException(string? chunkName, SourcePosition position, strin
public string? ChunkName { get; } = chunkName;
public SourcePosition Position { get; } = position;

/// <exception cref="LuaParseException"></exception>
public static void UnexpectedToken(
string? chunkName,
SourcePosition position,
Expand All @@ -26,6 +27,7 @@ SyntaxToken token
);
}

/// <exception cref="LuaParseException"></exception>
public static void ExpectedToken(
string? chunkName,
SourcePosition position,
Expand All @@ -35,6 +37,7 @@ SyntaxTokenType token
throw new LuaParseException(chunkName, position, $"{GetTokenTypeText(token)} expected");
}

/// <exception cref="LuaParseException"></exception>
internal static void ExpectedToken(
string? chunkName,
SourcePosition position,
Expand Down Expand Up @@ -64,6 +67,7 @@ static string GetTokenTypeText(SyntaxTokenType token)
};
}

/// <exception cref="LuaParseException"></exception>
internal static void ExpectedClosingToken(
string? chunkName,
SourcePosition position,
Expand Down Expand Up @@ -99,6 +103,7 @@ static string GetNearTokenText(SyntaxToken? token)
};
}

/// <exception cref="LuaParseException"></exception>
public static void UnfinishedLongComment(string? chunkName, SourcePosition position)
{
throw new LuaParseException(
Expand All @@ -108,6 +113,7 @@ public static void UnfinishedLongComment(string? chunkName, SourcePosition posit
);
}

/// <exception cref="LuaParseException"></exception>
public static void SyntaxError(string? chunkName, SourcePosition position, SyntaxToken? token)
{
throw new LuaParseException(
Expand All @@ -117,11 +123,13 @@ public static void SyntaxError(string? chunkName, SourcePosition position, Synta
);
}

/// <exception cref="LuaParseException"></exception>
public static void NoVisibleLabel(string label, string? chunkName, SourcePosition position)
{
throw new LuaParseException(chunkName, position, $"no visible label '{label}' for <goto>");
}

/// <exception cref="LuaParseException"></exception>
public static void BreakNotInsideALoop(string? chunkName, SourcePosition position)
{
throw new LuaParseException(chunkName, position, "<break> not inside a loop");
Expand Down
5 changes: 5 additions & 0 deletions src/Lua/IO/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public bool IsReadable(string path)
return File.Exists(GetFullPath(path));
}

/// <exception cref="System.IO.PathTooLongException">The specified path, file name, or both exceed the system-defined maximum length.</exception>
/// <exception cref="System.IO.DirectoryNotFoundException">The specified path is invalid, (for example, it is on an unmapped drive).</exception>
/// <exception cref="System.IO.IOException">An I/O error occurred while opening the file.</exception>
/// <exception cref="System.UnauthorizedAccessException">``path`` specified a file that is read-only and ``access`` is not ``Read``. -or- ``path`` specified a directory. -or- The caller does not have the required permission. -or- ``mode`` is ``System.IO.FileMode.Create`` and the specified file is a hidden file.</exception>
/// <exception cref="System.NotSupportedException">``path`` is in an invalid format.</exception>
public ValueTask<ILuaStream> Open(
string path,
LuaFileOpenMode openMode,
Expand Down
8 changes: 6 additions & 2 deletions src/Lua/LuaState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,8 @@ CancellationToken cancellationToken
/// Loads .NET source text and a .NET chunk name. In byte-string mode, text is
/// encoded as UTF-8. Use the byte overload for source that is already encoded.
/// </summary>
/// <exception cref="LuaCompileException"></exception>
/// <exception cref="LuaParseException">If the parsing is unsuccessful</exception>
public LuaClosure Load(ReadOnlySpan<char> chunk, string chunkName, LuaTable? environment = null)
{
var name = Platform.EncodeText(chunkName);
Expand Down Expand Up @@ -859,6 +861,8 @@ internal unsafe LuaClosure LoadCore(
/// Loads encoded source bytes with a .NET chunk name. Byte-string mode preserves
/// the source bytes, except for an initial UTF-8 BOM.
/// </summary>
/// <exception cref="LuaCompileException">The script contains invalid semantics</exception>
/// <exception cref="LuaParseException">If the parsing is unsuccessful</exception>
public LuaClosure Load(
ReadOnlySpan<byte> chunk,
string? chunkName = null,
Expand Down Expand Up @@ -893,7 +897,7 @@ static bool AllowsMode(string mode, char chunkMode)
{
if (!AllowsMode(mode, 'b'))
{
throw new Exception("attempt to load a binary chunk (mode is 't')");
throw new InvalidOperationException("attempt to load a binary chunk (mode is 't')");
}

return new(this, Parser.Undump(chunk, chunkName, Platform.StringMode), environment);
Expand All @@ -902,7 +906,7 @@ static bool AllowsMode(string mode, char chunkMode)

if (!AllowsMode(mode, 't'))
{
throw new Exception("attempt to load a text chunk (mode is 'b')");
throw new InvalidOperationException("attempt to load a text chunk (mode is 'b')");
}

Encoding encoding;
Expand Down
8 changes: 8 additions & 0 deletions src/Lua/LuaStateExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ public static LuaClosure Load(this LuaState state, LuaModule module)
: state.Load(module.ReadText(), module.Name);
}

// It *could* throw multiple exceptions due to `state.GlobalState.Platform.FileSystem.Open`.
// However, these exceptions vary based on the implementing interface, so probably best to
// omit them here.
/// <summary>Loads a file whose name is .NET text, or standard input when null.</summary>
/// <exception cref="LuaCompileException"></exception>
/// <exception cref="LuaParseException">If the parsing is unsuccessful</exception>
public static async ValueTask<LuaClosure> LoadFileAsync(
this LuaState state,
string? fileName,
Expand Down Expand Up @@ -173,6 +178,8 @@ public static ValueTask<LuaValue[]> ExecuteAsync(
}

/// <summary>Executes a file whose path is .NET text, including in byte-string mode.</summary>
/// <exception cref="LuaCompileException">The script contains invalid semantics</exception>
/// <exception cref="LuaParseException">If the parsing is unsuccessful</exception>
[AsyncMethodBuilder(typeof(LightAsyncValueTaskMethodBuilder<>))]
public static async ValueTask<int> DoFileAsync(
this LuaState state,
Expand All @@ -189,6 +196,7 @@ public static async ValueTask<int> DoFileAsync(
}

/// <summary>Executes a file whose path is .NET text, including in byte-string mode.</summary>
/// <exception cref=""></exception>
[AsyncMethodBuilder(typeof(LightAsyncValueTaskMethodBuilder<>))]
public static async ValueTask<LuaValue[]> DoFileAsync(
this LuaState state,
Expand Down
1 change: 1 addition & 0 deletions src/Lua/LuaValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ out result
}
}

/// <exception cref="InvalidOperationException">If the conversion is not possible.</exception>
public T Read<T>()
{
if (!TryRead<T>(out var result))
Expand Down