PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Scripting/Parser/Construct/Expressions/Object.cs

http://github.com/polyethene/IronAHK
C# | 118 lines | 84 code | 34 blank | 0 comment | 43 complexity | 09b9f601d13c618be7cb6697ddb81b34 MD5 | raw file
  1. using System.CodeDom;
  2. using System.Collections.Generic;
  3. namespace IronAHK.Scripting
  4. {
  5. partial class Parser
  6. {
  7. void ParseObject(List<object> parts, out CodePrimitiveExpression[] keys, out CodeExpression[] values)
  8. {
  9. var names = new List<CodePrimitiveExpression>();
  10. var entries = new List<CodeExpression>();
  11. for (int i = 0; i < parts.Count; i++)
  12. {
  13. CodeExpression value = null;
  14. #region Name
  15. if (!(parts[i] is string))
  16. throw new ParseException(ExUnexpected);
  17. var name = (string)parts[i];
  18. if (name.Length > 2 && name[0] == StringBound && name[name.Length - 1] == StringBound)
  19. name = name.Substring(1, name.Length - 2);
  20. if (name.Length == 0 || !IsIdentifier(name))
  21. throw new ParseException(ExInvalidVarName);
  22. i++;
  23. if (i == parts.Count)
  24. goto collect;
  25. #endregion
  26. #region Assign
  27. if (!(parts[i] is string))
  28. throw new ParseException(ExUnexpected);
  29. var assign = (string)parts[i];
  30. if (assign.Length == 1 && assign[0] == Multicast)
  31. goto collect;
  32. if (!(assign.Length == 1 && (assign[0] == Equal || assign[0] == HotkeyBound)))
  33. throw new ParseException(ExUnexpected);
  34. i++;
  35. if (i == parts.Count)
  36. goto collect;
  37. #endregion
  38. #region Value
  39. var sub = new List<object>();
  40. int next = Set(parts, i);
  41. if (next == 0) // no enclosing set (...){...}[...] so scan until next bounary
  42. {
  43. for (next = i; next < parts.Count; next++)
  44. {
  45. if (parts[next] is string && ((string)parts[next])[0] == Multicast)
  46. break;
  47. }
  48. }
  49. else
  50. next++; // set function returns n-1 index
  51. for (; i < next; i++)
  52. sub.Add(parts[i]);
  53. i--;
  54. value = ParseExpression(sub);
  55. i++;
  56. if (i == parts.Count)
  57. goto collect;
  58. #endregion
  59. #region Delimiter
  60. if (!(parts[i] is string))
  61. throw new ParseException(ExUnexpected);
  62. var delim = (string)parts[i];
  63. if (!(delim.Length == 1 && delim[0] == Multicast))
  64. throw new ParseException(ExUnexpected);
  65. #endregion
  66. #region Collect
  67. collect:
  68. names.Add(new CodePrimitiveExpression(name));
  69. entries.Add(value ?? new CodePrimitiveExpression(null));
  70. #endregion
  71. }
  72. keys = names.ToArray();
  73. values = entries.ToArray();
  74. }
  75. bool IsJsonObject(object item)
  76. {
  77. return item is CodeMethodInvokeExpression && ((CodeMethodInvokeExpression)item).Method.MethodName == InternalMethods.Index.MethodName;
  78. }
  79. bool IsArrayExtension(object item)
  80. {
  81. return item is CodeMethodInvokeExpression && ((CodeMethodInvokeExpression)item).Method.MethodName == InternalMethods.ExtendArray.MethodName;
  82. }
  83. }
  84. }