|
3 | 3 | using System.Diagnostics; |
4 | 4 | using System.Globalization; |
5 | 5 | using System.IO; |
6 | | -using System.Numerics; |
7 | | -using System.Reflection.Metadata; |
8 | 6 | using System.Text; |
9 | 7 |
|
10 | 8 | namespace Luaon |
@@ -42,6 +40,53 @@ public static class LuaConvert |
42 | 40 | /// </summary> |
43 | 41 | public static readonly string NaN = "0/0"; |
44 | 42 |
|
| 43 | + private static readonly HashSet<string> reservedKeywords = new HashSet<string> |
| 44 | + { |
| 45 | + "and", "break", "do", "else", "elseif", |
| 46 | + "end", "false", "for", "function", "if", |
| 47 | + "in", "local", "nil", "not", "or", |
| 48 | + "repeat", "return", "then", "true", "until", "while" |
| 49 | + }; |
| 50 | + |
| 51 | + private const int MIN_KEYWORD_LENGTH = 2; |
| 52 | + private const int MAX_KEYWORD_LENGTH = 8; |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Determines whether the specified expression can be a valid identifier. |
| 56 | + /// </summary> |
| 57 | + /// <param name="expression">The name expression to be checked.</param> |
| 58 | + /// <returns> |
| 59 | + /// <c>true</c> if <paramref name="expression"/> can be a valid Name |
| 60 | + /// (as defined in <a href="http://www.lua.org/manual/5.1/manual.html#2.1">Chapter 2.1, Lua Manual</a>) |
| 61 | + /// and does not conflict with any reserved keywords. |
| 62 | + /// </returns> |
| 63 | + public static bool IsValidIdentifier(string expression) |
| 64 | + { |
| 65 | + if (expression == null) throw new ArgumentNullException(nameof(expression)); |
| 66 | + if (expression.Length == 0) return false; |
| 67 | + if (expression.Length >= MIN_KEYWORD_LENGTH && expression.Length <= MAX_KEYWORD_LENGTH |
| 68 | + && expression[0] > 'a' && expression[0] < 'z') |
| 69 | + { |
| 70 | + // Check for keyword |
| 71 | + if (reservedKeywords.Contains(expression)) return false; |
| 72 | + } |
| 73 | + // Lua only allows ASCII characters as identifiers |
| 74 | + if (!(expression[0] == '_' |
| 75 | + || expression[0] >= 'A' && expression[0] <= 'Z' |
| 76 | + || expression[0] >= 'a' && expression[0] <= 'z')) return false; |
| 77 | + for (int i = 0; i < expression.Length; i++) |
| 78 | + { |
| 79 | + if (!(expression[i] == '_' |
| 80 | + || expression[i] >= 'A' && expression[i] <= 'Z' |
| 81 | + || expression[i] >= 'a' && expression[i] <= 'z' |
| 82 | + || expression[i] >= '0' && expression[i] <= '9')) return false; |
| 83 | + } |
| 84 | + |
| 85 | + return true; |
| 86 | + } |
| 87 | + |
| 88 | + #region ToString methods |
| 89 | + |
45 | 90 | /// <summary> |
46 | 91 | /// Converts the <see cref="bool"/> value into Lua representation. |
47 | 92 | /// </summary> |
@@ -360,6 +405,8 @@ public static string ToString(object value) |
360 | 405 | throw new ArgumentException($"Unsupported type: {ut}."); |
361 | 406 | } |
362 | 407 |
|
| 408 | + #endregion |
| 409 | + |
363 | 410 | internal static void WriteEscapedString(string value, StringDelimiterInfo delimiter, TextWriter writer) |
364 | 411 | { |
365 | 412 | Debug.Assert(delimiter != null); |
|
0 commit comments