📄 smc.java
字号:
// For some strange reason I get the wrong // line separator character when I use Java // on Windows. Set the line separator to "\n" // and all is well. System.setProperty("line.separator", "\n"); // Strip away any preceding directories from // the source file name. endIndex = srcFileBase.lastIndexOf(File.separatorChar); if (endIndex >= 0) { srcFilePath = srcFileBase.substring( 0, (endIndex + 1)); // Strip the ".sm" from the source file's name. srcFileBase = srcFileBase.substring( endIndex + 1); } // If -d was specified, then use place generated file // there. if (_outputDirectory != null) { srcFilePath = _outputDirectory; } // Create the header file name and generator - // if the language uses a header file. if (_targetLanguage.hasHeaderFile() == true) { String headerPath = srcFilePath; // If -headerd was specified, then place the file // there. -headerd takes precedence over -d. if (_headerDirectory != null) { headerPath = _headerDirectory; } headerGenerator = _targetLanguage.headerGenerator(srcFileBase); headerFileName = headerGenerator.sourceFile( headerPath, srcFileBase, null); headerFileStream = new FileOutputStream(headerFileName); headerStream = new PrintStream(headerFileStream); headerGenerator.setSource(headerStream); } // Create the language-specific source code generator. generator = _targetLanguage.generator(srcFileBase); srcFileName = generator.sourceFile( srcFilePath, srcFileBase, _suffix); sourceFileStream = new FileOutputStream(srcFileName); sourceStream = new PrintStream(sourceFileStream); generator.setSource(sourceStream); // Generate the header file first. if (headerGenerator != null) { fsm.accept(headerGenerator); headerFileStream.flush(); headerFileStream.close(); if (_verbose == true) { System.out.print("[wrote "); System.out.print(headerFileName); System.out.println("]"); } } // Now output the FSM in the target language. if (generator != null) { fsm.accept(generator); sourceFileStream.flush(); sourceFileStream.close(); if (_verbose == true) { System.out.print("[wrote "); System.out.print(srcFileName); System.out.println("]"); } } return; }//---------------------------------------------------------------// Inner classes// // The Language class explicitly stores each target // language's properties: // + The *start* of the command line option. // + The language's full name. // + The language's SmcCodeGenerator subclass. // + Whether the language also generates a header file and // that header file SmcCodeGenerator subclass. /* package */ static final class Language { //----------------------------------------------------------- // Member methods. // // Constructor. public Language(int index, String optionFlag, String name, Class generator, Class headerGenerator) { Class[] params = new Class[1]; Constructor sourceCtor = null; Constructor headerCtor = null; _index = index; _optionFlag = optionFlag; _name = name; params[0] = String.class; if (generator != null) { try { sourceCtor = generator.getDeclaredConstructor(params); } catch (NoSuchMethodException methoex) {} } if (headerGenerator != null) { try { headerCtor = headerGenerator.getDeclaredConstructor( params); } catch (NoSuchMethodException methoex) {} } _generator = sourceCtor; _headerGenerator = headerCtor; } //------------------------------------------------------- // Get methods. // public int index() { return (_index); } public String optionFlag() { return (_optionFlag); } public String name() { return (_name); } public SmcCodeGenerator generator(String basename) { SmcCodeGenerator retval = null; try { Object[] args = new Object[1]; args[0] = basename; retval = (SmcCodeGenerator) _generator.newInstance(args); } catch (Exception jex) { // Ignore. Return null. } return (retval); } public boolean hasHeaderFile() { return (_headerGenerator != null); } public SmcCodeGenerator headerGenerator(String basename) { SmcCodeGenerator retval = null; try { Object[] args = new Object[1]; args[0] = basename; retval = (SmcCodeGenerator) _headerGenerator.newInstance(args); } catch (Exception jex) { // Ignore. Return null. } return (retval); } public String toString() { return (_name); } // // end of Get methods. //------------------------------------------------------- //----------------------------------------------------------- // Member data. // private final int _index; private final String _optionFlag; private final String _name; private final Constructor _generator; private final Constructor _headerGenerator; }//---------------------------------------------------------------// Member Data// //----------------------------------------------------------- // Statics. // // The source file currently being compiled. private static String _sourceFileName; // The state map source code to be compiled. private static List<String> _sourceFileList; // Append this suffix to the end of the output file. private static String _suffix; // Place the output files in this directory. May be null. private static String _outputDirectory; // Place header files in this directory. May be null. private static String _headerDirectory; // If true, then generate verbose information. private static boolean _debug; // If true, then do not use C++ iostreams for debugging. // Application code must provide a TRACE macro to output // the debug messages. private static boolean _nostreams; // If true, then generate thread-safe Java code. private static boolean _sync; // If true, then do *not* generate C++ exception throws. private static boolean _noex; // If true, then do *not* generate try/catch/rethrow code. private static boolean _nocatch; // If true, then generate unique integer IDs for each state. private static boolean _serial; // If true, then generate getTransitions() method for each // state. private static boolean _reflection; // If true, then generate compiler verbose messages. private static boolean _verbose; // If true, then generate FSM messages. private static boolean _fsmVerbose; // The details placed into the GraphViz DOT file. private static int _graphLevel; // When generating C++ code, use this cast type. private static String _castType; // Have Smc.main() return rather than exit. private static boolean _return; // Store command line error messages here. private static String _errorMsg; // The app's version ID. private static String _version; // The list of all supported languages. private static Language[] _languages; // Map each command line option flag to the target languages // supporting the flag. // private static Map<String, List<Language>> _optionMap; private static Map<String, List<Language>> _optionMap; //----------------------------------------------------------- // Constants. // // Specifies target programming language. /* package */ static Language _targetLanguage; /* package */ static final int LANG_NOT_SET = 0; /* package */ static final int C_PLUS_PLUS = 1; /* package */ static final int JAVA = 2; /* package */ static final int TCL = 3; /* package */ static final int VB = 4; /* package */ static final int C_SHARP = 5; /* package */ static final int PYTHON = 6; /* package */ static final int TABLE = 7; /* package */ static final int GRAPH = 8; /* package */ static final int PERL = 9; /* package */ static final int RUBY = 10; /* package */ static final int C = 11; /* package */ static final int OBJECTIVE_C = 12; /* package */ static final int LUA = 13; /* package */ static final int GROOVY = 14; /* package */ static final int SCALA = 15; /* package */ static final int PHP = 16; /* package */ static final int LANGUAGE_COUNT = 17; // GraphViz detail level. /* package */ static final int NO_GRAPH_LEVEL = -1; /* package */ static final int GRAPH_LEVEL_0 = 0; /* package */ static final int GRAPH_LEVEL_1 = 1; /* package */ static final int GRAPH_LEVEL_2 = 2; // Specifies the transition's type. /* package */ static final int TRANS_NOT_SET = 0; /* package */ static final int TRANS_SET = 1; /* package */ static final int TRANS_PUSH = 2; /* package */ static final int TRANS_POP = 3; private static final String APP_NAME = "smc"; private static final String VERSION = "v. 5.1.0"; // Command line option flags. private static final String CAST_FLAG = "-cast"; private static final String DIRECTORY_FLAG = "-d"; private static final String DEBUG_FLAG = "-g"; private static final String GLEVEL_FLAG = "-glevel"; private static final String HEADER_FLAG = "-headerd"; private static final String HELP_FLAG = "-help"; private static final String NO_CATCH_FLAG = "-nocatch"; private static final String NO_EXCEPTIONS_FLAG = "-noex"; private static final String NO_STREAMS_FLAG = "-nostreams"; private static final String REFLECT_FLAG = "-reflect"; private static final String RETURN_FLAG = "-return"; private static final String SERIAL_FLAG = "-serial"; private static final String SUFFIX_FLAG = "-suffix"; private static final String SYNC_FLAG = "-sync"; private static final String VERBOSE_FLAG = "-verbose"; private static final String VERSION_FLAG = "-version"; private static final String VVERBOSE_FLAG = "-vverbose"; static { // Fill in the static languages array. _languages = new Language[LANGUAGE_COUNT]; _languages[LANG_NOT_SET] = new Language(LANG_NOT_SET, "", null, null, null); _languages[C] = new Language( C,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -