Skip to content

Commit ca37cac

Browse files
committed
Basic LuaTableTextWriter implementation.
1 parent c271e0f commit ca37cac

File tree

10 files changed

+866
-51
lines changed

10 files changed

+866
-51
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,6 @@ paket-files/
258258

259259
# Python Tools for Visual Studio (PTVS)
260260
__pycache__/
261-
*.pyc
261+
*.pyc
262+
263+
_private

Luaon.NET/Exceptions.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.Serialization;
4+
using System.Security;
5+
using System.Text;
6+
7+
namespace Luaon
8+
{
9+
/// <summary>
10+
/// Base type of Luaon.NET exceptions.
11+
/// </summary>
12+
[Serializable]
13+
public class LuaonException : Exception
14+
{
15+
16+
public LuaonException()
17+
{
18+
}
19+
20+
public LuaonException(string message) : base(message)
21+
{
22+
}
23+
24+
public LuaonException(string message, Exception inner) : base(message, inner)
25+
{
26+
}
27+
28+
[SecurityCritical]
29+
protected LuaonException(
30+
SerializationInfo info,
31+
StreamingContext context) : base(info, context)
32+
{
33+
}
34+
}
35+
36+
[Serializable]
37+
public class LuaTableWriterException : LuaonException
38+
{
39+
40+
public LuaTableWriterException()
41+
{
42+
}
43+
44+
public LuaTableWriterException(string message) : base(message)
45+
{
46+
}
47+
48+
public LuaTableWriterException(string message, string path) : base(message)
49+
{
50+
Path = path;
51+
}
52+
53+
public LuaTableWriterException(string message, Exception inner) : base(message, inner)
54+
{
55+
}
56+
57+
[SecurityCritical]
58+
protected LuaTableWriterException(
59+
SerializationInfo info,
60+
StreamingContext context) : base(info, context)
61+
{
62+
Path = info.GetString("Path");
63+
}
64+
65+
/// <inheritdoc />
66+
public override void GetObjectData(SerializationInfo info, StreamingContext context)
67+
{
68+
base.GetObjectData(info, context);
69+
info.AddValue("Path", Path);
70+
}
71+
72+
/// <summary>
73+
/// Gets the Lua property path where the exception happens.
74+
/// </summary>
75+
public virtual string Path { get; }
76+
77+
/// <inheritdoc />
78+
public override string Message
79+
{
80+
get
81+
{
82+
if (!string.IsNullOrEmpty(Path))
83+
return base.Message + "\nPath: " + Path;
84+
return base.Message;
85+
}
86+
}
87+
}
88+
}

Luaon.NET/Formatting.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Luaon
6+
{
7+
/// <summary>
8+
/// Specifies formatting options for <see cref="LuaTableTextWriter"/>
9+
/// </summary>
10+
[Flags]
11+
public enum Formatting
12+
{
13+
14+
/// <summary>
15+
/// Compact Lua code formatting.
16+
/// </summary>
17+
None = 0,
18+
19+
/// <summary>
20+
/// Prettified Lua code formatting.
21+
/// </summary>
22+
Prettified = 1,
23+
}
24+
}

Luaon.NET/LuaContainerContext.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Luaon
6+
{
7+
8+
internal enum LuaContainerType
9+
{
10+
None = 0,
11+
Table,
12+
Key
13+
}
14+
15+
internal struct LuaContainerContext
16+
{
17+
18+
public readonly LuaContainerType ContainerType;
19+
20+
// Key != null: Named field; Key == null: Indexed field.
21+
public string Key;
22+
23+
public bool KeyIsExpression;
24+
25+
public int CurrentIndex;
26+
27+
public LuaContainerContext(LuaContainerType containerType)
28+
{
29+
ContainerType = containerType;
30+
Key = null;
31+
KeyIsExpression = true;
32+
CurrentIndex = 1;
33+
}
34+
35+
/// <inheritdoc />
36+
public override string ToString()
37+
{
38+
var sb = new StringBuilder();
39+
ToString(sb);
40+
return sb.ToString();
41+
}
42+
43+
private void ToString(StringBuilder sb)
44+
{
45+
if (Key == null)
46+
{
47+
sb.Append('[');
48+
sb.Append(CurrentIndex);
49+
sb.Append(']');
50+
}
51+
else if (KeyIsExpression)
52+
{
53+
sb.Append(Key);
54+
}
55+
else if (Key.IndexOfAny(specialChars) != -1)
56+
{
57+
sb.Append('.');
58+
sb.Append(Key);
59+
}
60+
else
61+
{
62+
sb.Append('[');
63+
sb.Append(Key);
64+
sb.Append(']');
65+
}
66+
}
67+
68+
private static readonly char[] specialChars = {' ', '\t', '.', '[', ']', '{', '}'};
69+
70+
public static string ToString(IEnumerable<LuaContainerContext> stack, LuaContainerContext current)
71+
{
72+
var sb = new StringBuilder();
73+
foreach (var c in stack)
74+
{
75+
c.ToString(sb);
76+
}
77+
current.ToString(sb);
78+
return sb.ToString();
79+
}
80+
81+
}
82+
}

0 commit comments

Comments
 (0)