home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / fnb101.zip / Lib / site-packages / Fnorb / script / fnidl.py < prev    next >
Text File  |  1999-06-28  |  5KB  |  176 lines

  1. #############################################################################
  2. # Copyright (C) DSTC Pty Ltd (ACN 052 372 577) 1997, 1998, 1999
  3. # All Rights Reserved.
  4. #
  5. # The software contained on this media is the property of the DSTC Pty
  6. # Ltd.  Use of this software is strictly in accordance with the
  7. # license agreement in the accompanying LICENSE.HTML file.  If your
  8. # distribution of this software does not contain a LICENSE.HTML file
  9. # then you have no rights to use this software in any manner and
  10. # should contact DSTC at the address below to determine an appropriate
  11. # licensing arrangement.
  12. #      DSTC Pty Ltd
  13. #      Level 7, GP South
  14. #      Staff House Road,
  15. #      University of Queensland
  16. #      St Lucia, 4072
  17. #      Australia
  18. #      Tel: +61 7 3365 4310
  19. #      Fax: +61 7 3365 4311
  20. #      Email: enquiries@dstc.edu.au
  21. # This software is being provided "AS IS" without warranty of any
  22. # kind.  In no event shall DSTC Pty Ltd be liable for damage of any
  23. # kind arising out of or in connection with the use or performance of
  24. # this software.
  25. #
  26. # Project:      Fnorb
  27. # File:         $Source: /units/arch/src/Fnorb/script/RCS/fnidl.py,v $
  28. # Version:      @(#)$RCSfile: fnidl.py,v $ $Revision: 1.3 $
  29. #
  30. #############################################################################
  31. """ IDL compiler for Fnorb! """
  32.  
  33.  
  34. # Standard/built-in modules.
  35. import commands, os, string, sys
  36.  
  37. # Fnorb modules.
  38. from Fnorb.orb      import CORBA, Util
  39. from Fnorb.parser   import IDLParser
  40. from Fnorb.compiler import IDLCompiler 
  41. from Fnorb.script   import cpp
  42.  
  43. # Interface Repository modules.
  44. from Fnorb.cos.interface_repository import IntRepImpl
  45.  
  46.  
  47. # Default options.
  48. DEFAULT_DIRECTORY = '.' 
  49. DEFAULT_PACKAGE   = Util.PackageName()
  50. DEFAULT_GLOBALS   = '_GlobalIDL'
  51.  
  52.  
  53. def main(argv):
  54.     """ Do it! """
  55.  
  56.     # Default options.
  57.     directory = DEFAULT_DIRECTORY
  58.     package   = DEFAULT_PACKAGE
  59.     globals   = DEFAULT_GLOBALS
  60.  
  61.     # We separate arguments for the pre-processor from IDL files.
  62.     cpp_flags = []
  63.     idl_files = []
  64.  
  65.     for arg in argv[1:]:
  66.     # Output directory option.
  67.     if arg[:12] == '--directory=':
  68.         directory = string.split(arg, '=')[1]
  69.  
  70.     # Package option.
  71.     elif arg[:10] == '--package=':
  72.         package = Util.PackageName(string.split(arg, '=')[1])
  73.  
  74.     # Global IDL package option.
  75.     elif arg[:10] == '--globals=':
  76.         globals = string.split(arg, '=')[1]
  77.  
  78.     # CPP flags.
  79.     elif arg[0] == '-':
  80.         cpp_flags.append(arg)
  81.  
  82.     # IDL files.
  83.     elif arg[-4:] == '.idl':
  84.         idl_files.append(arg)
  85.  
  86.     # Ignore anything else (including the spurious last argument '\n' on
  87.     # Windoows 95 ;^).
  88.     else:
  89.         pass
  90.  
  91.     # Create the compilation context.
  92.     context = IDLCompiler.Context(directory, package, globals)
  93.  
  94.     # If no files were specified on the command line, then parse from stdin!
  95.     if len(idl_files) == 0:
  96.     result = main_interactive(context)
  97.  
  98.     else:
  99.     result = main_batch(context, cpp_flags, idl_files)
  100.  
  101.     return result
  102.  
  103.  
  104. def main_interactive(context):
  105.     """ Parse IDL from stdin! """
  106.  
  107.     # Create the parser.
  108.     parser = IDLParser.IDLParser()
  109.  
  110.     # Create an instance of the 'Repository' implementation class.
  111.     ifr = IntRepImpl.RepositoryImpl()
  112.  
  113.     # Do the parsing!
  114.     print 'Enter IDL (Ctrl-Z to finish)...\n'
  115.     (result, contents) = parser.parse(ifr, 'stdin', sys.stdin)
  116.  
  117.     # If the parsing succeeded.
  118.     if result == 0:
  119.     if len(contents) > 0:
  120.         # Create an IDLCompiler to do the work!
  121.         idl_compiler = IDLCompiler.IDLCompiler()
  122.         
  123.         # Generate code for the objects returned by the parser.
  124.         idl_compiler.compile(context, contents)
  125.  
  126.     return result
  127.  
  128.  
  129. def main_batch(context, cpp_flags, idl_files):
  130.     """ Parse IDL from files! """
  131.  
  132.     # Create the parser.
  133.     parser = IDLParser.IDLParser()
  134.  
  135.     # Parse each file.
  136.     for idl_file in idl_files:
  137.     # Format the command to run the C/C++ pre-processor.
  138.     cmd = cpp.COMMAND % (string.join(cpp_flags), idl_file)
  139.  
  140.     # Run the pre-processor and use its output as the lexer's input stream.
  141.     yyin = os.popen(cmd, 'r')
  142.  
  143.     # Create an instance of the 'Repository' implementation class.
  144.     ifr = IntRepImpl.RepositoryImpl()
  145.  
  146.     # Do the parsing!
  147.     (result, contents) = parser.parse(ifr, idl_file, yyin)
  148.  
  149.     # If the parsing succeeded.
  150.     if result == 0:
  151.         if len(contents) > 0:
  152.         # Create an IDLCompiler to do the work!
  153.         idl_compiler = IDLCompiler.IDLCompiler()
  154.         
  155.         # Generate code for the objects returned by the parser.
  156.         idl_compiler.compile(context, contents)
  157.  
  158.     # If a parsing error occured then bail out.
  159.     else:
  160.         break
  161.  
  162.     # Close the pipe.
  163.     yyin.close()
  164.  
  165.     return result
  166.  
  167. #############################################################################
  168.  
  169. if __name__ == '__main__':
  170.     # Do it!
  171.     sys.exit(main(sys.argv))
  172.  
  173. #############################################################################
  174.