PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/source/library/Interlace.Pinch/Interlace.Pinch/Generation/Generator.cs

https://bitbucket.org/VahidN/interlace
C# | 152 lines | 99 code | 28 blank | 25 comment | 4 complexity | d444f0a00c9ad268c2b5a7462bd2810f 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 Antlr.StringTemplate;
  32. using Antlr.StringTemplate.Language;
  33. using Interlace.Pinch.Analysis;
  34. using Interlace.Pinch.Dom;
  35. using Interlace.PropertyLists;
  36. using System.Text.RegularExpressions;
  37. #endregion
  38. namespace Interlace.Pinch.Generation
  39. {
  40. public class Generator
  41. {
  42. Language _language;
  43. Document _document;
  44. string _documentPath;
  45. string _destinationPath;
  46. DateTime _mostRecentSourceFile;
  47. static Regex _suffixRegex = new Regex(@"(\.(java|cs|cpp|python))?\.instance$", RegexOptions.IgnoreCase);
  48. protected Generator(Language language, Document document, string documentPath, string destinationPath, DateTime mostRecentSourceFile)
  49. {
  50. _language = language;
  51. _document = document;
  52. _documentPath = documentPath;
  53. _destinationPath = destinationPath;
  54. _mostRecentSourceFile = mostRecentSourceFile;
  55. }
  56. public static void Generate(Language language, string documentPath, string destinationPath)
  57. {
  58. if (!File.Exists(documentPath))
  59. {
  60. throw new ApplicationException(string.Format(
  61. "The specified input file \"{0}\" does not exist.", documentPath));
  62. }
  63. PropertyDictionary documentOptions;
  64. string actualDocumentPath;
  65. if (_suffixRegex.IsMatch(documentPath))
  66. {
  67. documentOptions = PropertyDictionary.FromFile(documentPath);
  68. actualDocumentPath = _suffixRegex.Replace(documentPath, ".pinch");
  69. if (!File.Exists(actualDocumentPath))
  70. {
  71. throw new ApplicationException(string.Format(
  72. "The instance configuration \"{0}\" exists, but not the expected corresponding specification file \"{1}\".",
  73. documentPath, actualDocumentPath));
  74. }
  75. }
  76. else
  77. {
  78. actualDocumentPath = documentPath;
  79. documentOptions = PropertyDictionary.EmptyDictionary();
  80. actualDocumentPath = documentPath;
  81. }
  82. DateTime documentModified = File.GetLastWriteTimeUtc(documentPath);
  83. DateTime actualDocumentModified = File.GetLastWriteTimeUtc(actualDocumentPath);
  84. DateTime mostRecent = documentModified > actualDocumentModified ? documentModified : actualDocumentModified;
  85. Document document = Document.Parse(actualDocumentPath);
  86. Compilation compilation = new Compilation();
  87. compilation.AddDocument(document);
  88. compilation.Resolve();
  89. compilation.Number();
  90. language.CreateDomImplementationHelpers(document, documentOptions);
  91. Generator generator = new Generator(language, document, actualDocumentPath, destinationPath, mostRecent);
  92. language.GenerateFiles(generator, document);
  93. }
  94. public string BaseName
  95. {
  96. get
  97. {
  98. return Path.GetFileNameWithoutExtension(_documentPath);
  99. }
  100. }
  101. public string DestinationPath
  102. {
  103. get
  104. {
  105. return _destinationPath;
  106. }
  107. }
  108. public void GenerateFile(string outputFilePath, string templateString, string templateName, string rootName, object root)
  109. {
  110. if (_mostRecentSourceFile < File.GetLastWriteTimeUtc(outputFilePath)) return;
  111. StringTemplateGroup group;
  112. using (StringReader reader = new StringReader(templateString))
  113. {
  114. group = new StringTemplateGroup(reader, typeof(AngleBracketTemplateLexer));
  115. }
  116. StringTemplate template = group.GetInstanceOf(templateName);
  117. template.SetAttribute(rootName, root);
  118. using (StreamWriter writer = new StreamWriter(outputFilePath, false))
  119. {
  120. template.Write(new AutoIndentWriter(writer));
  121. }
  122. }
  123. }
  124. }