PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/source/library/Interlace.Pinch/Interlace.Pinch/Languages/Python/PythonLanguage.cs

https://bitbucket.org/VahidN/interlace
C# | 135 lines | 87 code | 23 blank | 25 comment | 4 complexity | 80ab44e764d8016a274d86dfe55405c8 MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Computer Consultancy Pty Ltd nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Text;
  31. using Interlace.Pinch.Dom;
  32. using Interlace.Pinch.Generation;
  33. using Interlace.PropertyLists;
  34. using System.Text.RegularExpressions;
  35. #endregion
  36. namespace Interlace.Pinch.Languages.Python
  37. {
  38. public class PythonLanguage : Language
  39. {
  40. static Dictionary<IntrinsicType, PythonType> _intrinsics;
  41. static PythonLanguage()
  42. {
  43. _intrinsics = new Dictionary<IntrinsicType, PythonType>();
  44. _intrinsics.Add(IntrinsicType.Float32, new PythonType("float", "0.0"));
  45. _intrinsics.Add(IntrinsicType.Float64, new PythonType("double", "0.0"));
  46. _intrinsics.Add(IntrinsicType.Int8, new PythonType("int", "0"));
  47. _intrinsics.Add(IntrinsicType.Int16, new PythonType("int", "0"));
  48. _intrinsics.Add(IntrinsicType.Int32, new PythonType("int", "0"));
  49. _intrinsics.Add(IntrinsicType.Int64, new PythonType("long", "0"));
  50. _intrinsics.Add(IntrinsicType.Decimal, new PythonType("PinchDecimal", "pinch.PinchDecimal(False, 0, 0)"));
  51. _intrinsics.Add(IntrinsicType.Bool, new PythonType("bool", "False"));
  52. _intrinsics.Add(IntrinsicType.String, new PythonType("unicode", "None"));
  53. _intrinsics.Add(IntrinsicType.Bytes, new PythonType("str", "None"));
  54. }
  55. public PythonLanguage()
  56. : base("py", "Python")
  57. {
  58. }
  59. static Regex _identifierRegex = new Regex(@"\G([A-Z]?[^A-Z]+|[A-Z])");
  60. internal static string ToPrivateIdentifier(string identifier)
  61. {
  62. return "_" + ToPublicIdentifier(identifier);
  63. }
  64. internal static string ToPublicIdentifier(string identifier)
  65. {
  66. Match match = _identifierRegex.Match(identifier);
  67. string publicIdentifier = "";
  68. while (match.Success)
  69. {
  70. if (publicIdentifier != "") publicIdentifier += "_";
  71. publicIdentifier += match.Value.ToLower();
  72. match = match.NextMatch();
  73. }
  74. return publicIdentifier;
  75. }
  76. public override object CreateStructureImplementationHelper(Structure structure, PropertyDictionary options)
  77. {
  78. return new PythonStructure(structure, options);
  79. }
  80. public override object CreateStructureMemberImplementationHelper(StructureMember member)
  81. {
  82. if (member.FieldTypeReference is IntrinsicTypeReference)
  83. {
  84. IntrinsicTypeReference reference = member.FieldTypeReference as IntrinsicTypeReference;
  85. return new PythonStructureMember(member, _intrinsics[reference.Type], null);
  86. }
  87. else
  88. {
  89. DeclarationTypeReference reference = member.FieldTypeReference as DeclarationTypeReference;
  90. return new PythonStructureMember(member, new PythonType("object", "None"), reference.Declaration.Implementation as PythonStructure);
  91. }
  92. }
  93. public override object CreateEnumerationMemberImplementationHelper(EnumerationMember member)
  94. {
  95. return new PythonEnumerationMember(member);
  96. }
  97. public override object CreateProtocolImplementationHelper(Protocol protocol, PropertyDictionary options)
  98. {
  99. return new PythonProtocol(protocol, options);
  100. }
  101. public override void GenerateFiles(Generator generator, Document document)
  102. {
  103. foreach (Protocol protocol in document.Protocols)
  104. {
  105. PythonProtocol pythonProtocol = protocol.Implementation as PythonProtocol;
  106. generator.GenerateFile(
  107. Path.Combine(generator.DestinationPath, pythonProtocol.ModuleName + ".py"),
  108. Templates.PythonTemplate, "file", "Protocol", protocol);
  109. }
  110. }
  111. }
  112. }