📄 smc.java
字号:
//// The contents of this file are subject to the Mozilla Public// License Version 1.1 (the "License"); you may not use this file// except in compliance with the License. You may obtain a copy// of the License at http://www.mozilla.org/MPL///// Software distributed under the License is distributed on an// "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or// implied. See the License for the specific language governing// rights and limitations under the License.//// The Original Code is State Machine Compiler (SMC).//// The Initial Developer of the Original Code is Charles W. Rapp.// Portions created by Charles W. Rapp are// Copyright (C) 2000 - 2007. Charles W. Rapp.// All Rights Reserved.//// Contributor(s):// Eitan Suez contributed examples/Ant.// (Name withheld) contributed the C# code generation and// examples/C#.// Francois Perrad contributed the Python code generation and// examples/Python, Perl code generation and examples/Perl,// Ruby code generation and examples/Ruby, Lua code generation// and examples/Lua, Groovy code generation and examples/Groovy,// Scala code generation and examples/Scala.// Chris Liscio contributed the Objective-C code generation// and examples/ObjC.// Toni Arnold contributed the PHP code generation and// examples/PHP.//// SMC --//// State Map Compiler//// This class parses a state map exception, checks the code// for semantic consistency and then generates object-oriented// code in the user specified target language.//// RCS ID// $Id: Smc.java,v 1.28 2008/04/22 16:05:24 fperrad Exp $//// CHANGE LOG// (See bottom of file.)//package net.sf.smc;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.PrintStream;import java.lang.reflect.Constructor;import java.text.ParseException;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;public final class Smc{//---------------------------------------------------------------// Member Methods// public static void main(String[] args) { int retcode = 0; _errorMsg = new String(); // The default smc output level is 1. _targetLanguage = null; _version = VERSION; _debug = false; _nostreams = false; _sync = false; _noex = false; _nocatch = false; _serial = false; _castType = "dynamic_cast"; _graphLevel = GRAPH_LEVEL_0; _sourceFileList = new ArrayList<String>(); _verbose = false; _fsmVerbose = false; _return = false; _reflection = false; _outputDirectory = null; _headerDirectory = null; _suffix = null; // Process the command line. if (parseArgs(args) == false) { retcode = 1; System.err.println(APP_NAME + ": " + _errorMsg); // _usage(System.err); } // Arguments check out - start compiling.. else { SmcLexer lexer; SmcParser parser; SmcFSM fsm; Iterator<String> sit; boolean checkFlag; long startTime = 0; long finishTime; long totalStartTime = 0; long totalFinishTime; if (_verbose == true) { totalStartTime = System.currentTimeMillis(); } try { for (sit = _sourceFileList.iterator(); sit.hasNext() == true; ) { _sourceFileName = (String) sit.next(); if (_verbose == true) { System.out.print("[parsing started "); System.out.print(_sourceFileName); System.out.println("]"); startTime = System.currentTimeMillis(); } parser = new SmcParser( _getFileName(_sourceFileName), new FileInputStream(_sourceFileName), _fsmVerbose); // First - do the parsing fsm = parser.parse(); if (_verbose == true) { finishTime = System.currentTimeMillis(); System.out.print("[parsing completed "); System.out.print(finishTime - startTime); System.out.println("ms]"); } if (fsm == null) { retcode = 1; // Output the parser's messages. outputMessages(_sourceFileName, System.err, parser.getMessages()); } else { SmcSyntaxChecker checker = new SmcSyntaxChecker( _sourceFileName, _targetLanguage.index()); if (_verbose == true) { System.out.print("[checking "); System.out.print(_sourceFileName); System.out.println("]"); } // Second - do the semantic check. fsm.accept(checker); if (checker.isValid() == false) { retcode = 1; // Output the syntax checker's // messages. outputMessages( _sourceFileName, System.err, checker.getMessages()); } else { // Third - do the code generation. _generateCode(fsm); } } } } // Report an unknown file exception. catch (FileNotFoundException filex) { System.err.print(_sourceFileName); System.err.print(": error - "); System.err.println(filex.getMessage()); } // A parse exception may be thrown by generateCode(). // This is not a problem. catch (ParseException parsex) { System.err.print(_sourceFileName); System.err.print(":"); System.err.print(parsex.getErrorOffset()); System.err.print(": error - "); System.err.println(parsex.getMessage()); } catch (Exception e) { retcode = 1; System.err.println( "SMC has experienced a fatal error. Please e-mail the following error output to rapp@acm.org. Thank you.\n"); System.err.println( "--------------------------------------------------------------------------------"); System.err.println("SMC version: " + _version); System.err.println( "JRE version: v. " + System.getProperty("java.version")); System.err.println( "JRE vender: " + System.getProperty("java.vendor") + " (" + System.getProperty("java.vendor.url") + ")"); System.err.println( "JVM: " + System.getProperty("java.vm.name") + ", v. " + System.getProperty("java.vm.version")); System.err.println( "JVM vender: " + System.getProperty("java.vm.vendor")); System.err.println("Exception:\n"); e.printStackTrace(); System.err.println( "--------------------------------------------------------------------------------"); } if (_verbose == true) { totalFinishTime = System.currentTimeMillis(); System.out.print("[total "); System.out.print( totalFinishTime - totalStartTime); System.out.println("ms]"); } } // Need to return the appropriate exit code in case SMC // is called by make. Just doing a return always results // in a zero return code. // v. 4.0.0: But calling exit when SMC is an ANT task is // problematic. ANT is a Java program and calls Smc.main // directly and not as a forked process. So when Smc.main // exits, it exits the JVM for everyone including ANT. if (_return == false) { System.exit(retcode); } else { return; } } // end of main(String[]) public static String sourceFileName() { return (_sourceFileName); } public static boolean isDebug() { return (_debug); } public static boolean isNoStreams() { return (_nostreams); } public static boolean isSynchronized() { return (_sync); } public static boolean isNoExceptions() { return (_noex); } public static boolean isNoCatch() { return (_nocatch); } public static boolean isSerial() { return (_serial); } public static boolean isReflection() { return (_reflection); } public static String castType() { return (_castType); } public static int graphLevel() { return (_graphLevel); } public static String outputDirectory() { return (_outputDirectory); } public static String headerDirectory() { return (_headerDirectory); } // Merge two lists together, returning an ordered list with // no multiple entries. public static List<SmcTransition> merge(List<SmcTransition> l1, List<SmcTransition> l2, Comparator<SmcTransition> c) { int result; Iterator<SmcTransition> it1; Iterator<SmcTransition> it2; SmcTransition e1; SmcTransition e2; List<SmcTransition> retval = new ArrayList<SmcTransition>(); // First, make certain that both lists are sorted. Collections.sort(l1, c); Collections.sort(l2, c); // Now merge the two lists together. // Continue until the end of either list is reached. for (it1 = l1.iterator(), it2 = l2.iterator(), e1 = null, e2 = null; (it1.hasNext() == true || e1 != null) && (it2.hasNext() == true || e2 != null); ) { if (e1 == null) { e1 = it1.next(); } if (e2 == null) { e2 = it2.next(); } if ((result = c.compare(e1, e2)) < 0) { retval.add(e1); e1 = null; } else if (result > 0) { retval.add(e2); e2 = null; } else { retval.add(e1); e1 = null; e2 = null; } } // Is there any more to add? if (it1.hasNext() == true || e1 != null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -