KLL Compiler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

emitter.py 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #!/usr/bin/env python3
  2. '''
  3. KLL Emitter Base Classes
  4. '''
  5. # Copyright (C) 2016 by Jacob Alexander
  6. #
  7. # This file is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This file is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this file. If not, see <http://www.gnu.org/licenses/>.
  19. ### Imports ###
  20. import re
  21. import os
  22. import sys
  23. ### Decorators ###
  24. ## Print Decorator Variables
  25. ERROR = '\033[5;1;31mERROR\033[0m:'
  26. WARNING = '\033[5;1;33mWARNING\033[0m:'
  27. ### Classes ###
  28. class Emitter:
  29. '''
  30. KLL Emitter Base Class
  31. NOTE: Emitter should do as little as possible in the __init__ function.
  32. '''
  33. def __init__( self, control ):
  34. '''
  35. Emitter initialization
  36. @param control: ControlStage object, used to access data from other stages
  37. '''
  38. self.control = control
  39. self.color = False
  40. def command_line_args( self, args ):
  41. '''
  42. Group parser for command line arguments
  43. @param args: Name space of processed arguments
  44. '''
  45. print( "{0} '{1}' '{2}' has not been implemented yet"
  46. .format(
  47. WARNING,
  48. self.command_line_args.__name__,
  49. type( self ).__name__
  50. )
  51. )
  52. def command_line_flags( self, parser ):
  53. '''
  54. Group parser for command line options
  55. @param parser: argparse setup object
  56. '''
  57. print( "{0} '{1}' '{2}' has not been implemented yet"
  58. .format(
  59. WARNING,
  60. self.command_line_flags.__name__,
  61. type( self ).__name__
  62. )
  63. )
  64. def process( self ):
  65. '''
  66. Emitter Processing
  67. '''
  68. print( "{0} '{1}' '{2}' has not been implemented yet"
  69. .format(
  70. WARNING,
  71. self.process.__name__,
  72. type( self ).__name__
  73. )
  74. )
  75. def output( self ):
  76. '''
  77. Final Stage of Emitter
  78. Generate desired outputs
  79. '''
  80. print( "{0} '{1}' '{2}' has not been implemented yet"
  81. .format(
  82. WARNING,
  83. self.output.__name__,
  84. type( self ).__name__
  85. )
  86. )
  87. class FileEmitter:
  88. '''
  89. KLL File Emitter Class
  90. Base class for any emitter that wants to output a file.
  91. Generally, it is recommended to use the TextEmitter as templates are more readable.
  92. '''
  93. def __init__( self ):
  94. '''
  95. FileEmitter Initialization
  96. '''
  97. self.output_files = []
  98. def generate( self, output_path ):
  99. '''
  100. Generate output file
  101. @param contents: String contents of file
  102. @param output_path: Path to output file
  103. '''
  104. for name, contents in self.output_files:
  105. with open( "{0}/{1}".format( output_path, name ), 'w' ) as outputFile:
  106. outputFile.write( contents )
  107. class TextEmitter:
  108. '''
  109. KLL Text Emitter Class
  110. Base class for any text emitter that wants to use the templating functionality
  111. If multiple files need to be generated, call load_template and generate multiple times.
  112. e.g.
  113. load_template('_myfile.h')
  114. generate('/tmp/myfile.h')
  115. load_template('_myfile2.h')
  116. generate('/tmp/myfile2.h')
  117. TODO
  118. - Generate list of unused tags
  119. '''
  120. def __init__( self ):
  121. '''
  122. TextEmitter Initialization
  123. '''
  124. # Dictionary used to do template replacements
  125. self.fill_dict = {}
  126. self.tag_list = []
  127. self.template = None
  128. def load_template( self, template ):
  129. '''
  130. Loads template file
  131. Looks for <|tags|> to replace in the template
  132. @param template: Path to template
  133. '''
  134. # Does template exist?
  135. if not os.path.isfile( template ):
  136. print ( "{0} '{1}' does not exist...".format( ERROR, template ) )
  137. sys.exit( 1 )
  138. self.template = template
  139. # Generate list of fill tags
  140. with open( template, 'r' ) as openFile:
  141. for line in openFile:
  142. match = re.findall( r'<\|([^|>]+)\|>', line )
  143. for item in match:
  144. self.tag_list.append( item )
  145. def generate( self, output_path ):
  146. '''
  147. Generates the output file from the template file
  148. @param output_path: Path to the generated file
  149. '''
  150. # Make sure we've called load_template at least once
  151. if self.template is None:
  152. print ( "{0} TextEmitter template (load_template) has not been called.".format( ERROR ) )
  153. sys.exit( 1 )
  154. # Process each line of the template, outputting to the target path
  155. with open( output_path, 'w' ) as outputFile:
  156. with open( self.template, 'r' ) as templateFile:
  157. for line in templateFile:
  158. # TODO Support multiple replacements per line
  159. # TODO Support replacement with other text inline
  160. match = re.findall( r'<\|([^|>]+)\|>', line )
  161. # If match, replace with processed variable
  162. if match:
  163. try:
  164. outputFile.write( self.fill_dict[ match[0] ] )
  165. except KeyError as err:
  166. print( "{0} '{1}' not found, skipping...".format(
  167. WARNING, match[0]
  168. ) )
  169. outputFile.write("\n")
  170. # Otherwise, just append template to output file
  171. else:
  172. outputFile.write( line )