📄 smccsharpgenerator.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) 2005. 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.// Chris Liscio contributed the Objective-C code generation// and examples/ObjC.//// RCS ID// $Id: SmcCSharpGenerator.java,v 1.12 2008/03/21 14:03:16 fperrad Exp $//// CHANGE LOG// (See the bottom of this file.)//package net.sf.smc;import java.io.PrintStream;import java.util.ArrayList;import java.util.Iterator;import java.util.List;/** * Visits the abstract syntax tree, emitting C# code. * @see SmcElement * @see SmcCodeGenerator * @see SmcVisitor * * @author <a href="mailto:rapp@acm.org">Charles Rapp</a> */public final class SmcCSharpGenerator extends SmcCodeGenerator{//---------------------------------------------------------------// Member methods// public SmcCSharpGenerator(String srcfileBase) { super (srcfileBase, "{0}{1}_sm.{2}", "cs"); } // end of SmcCSharpGenerator(String) public void visit(SmcFSM fsm) { String rawSource = fsm.getSource(); String packageName = fsm.getPackage(); String context = fsm.getContext(); String startState = fsm.getStartState(); String accessLevel = fsm.getAccessLevel(); List<SmcMap> maps = fsm.getMaps(); List<SmcTransition> transitions; Iterator<SmcParameter> pit; String transName; String csState; String separator; int index; List<SmcParameter> params; String indent2; // If the access level has not been set, then the // default is "public". if (accessLevel == null || accessLevel.length() == 0) { accessLevel = "public"; } // Dump out the raw source code, if any. if (rawSource != null && rawSource.length () > 0) { _source.println(rawSource); _source.println(); } // Always include the system package. _source.println("using System;"); // If debugging code is being generated, then import // system diagnostics package as well. if (Smc.isDebug() == true) { _source.println("using System.Diagnostics;"); } // If serialization is on, then import the .Net // serialization package. if (Smc.isSerial() == true) { _source.println( "using System.Runtime.Serialization;"); _source.println("using System.Security;"); _source.println( "using System.Security.Permissions;"); } // If reflection is on, then import the .Net collections // package. if (Smc.isReflection() == true) { _source.println("using System.Collections;"); } _source.println(); // Do user-specified imports now. for (String imp: fsm.getImports()) { _source.print("using "); _source.print(imp); _source.println(";"); } // If a package has been specified, generate the package // statement now and set the indent. if (packageName != null && packageName.length() > 0) { _source.print("namespace "); _source.println(packageName); _source.println("{"); _indent = " "; } // Does the user want to serialize this FSM? if (Smc.isSerial() == true) { _source.print(_indent); _source.println("[Serializable]"); } // Now declare the FSM context class. _source.print(_indent); _source.print(accessLevel); _source.print(" sealed class "); _source.print(context); _source.println("Context :"); _source.print(_indent); _source.print(" statemap.FSMContext"); if (Smc.isSerial() == false) { _source.println(); } else { _source.println(','); _source.print(_indent); _source.println(" ISerializable"); } _source.print(_indent); _source.println("{"); _source.print(_indent); _source.println( "//---------------------------------------------------------------"); _source.print(_indent); _source.println("// Properties."); _source.print(_indent); _source.println("//"); _source.println(); // State property. _source.print(_indent); _source.print(" public "); _source.print(context); _source.println("State State"); _source.print(_indent); _source.println(" {"); _source.print(_indent); _source.println(" get"); _source.print(_indent); _source.println(" {"); // Again, if synchronization is on, then protect access // to this FSM. if (Smc.isSynchronized() == true) { _source.print(_indent); _source.println(" lock (this)"); _source.print(_indent); _source.println(" {"); indent2 = _indent + " "; } else { indent2 = _indent + " "; } _source.print(indent2); _source.println("if (_state == null)"); _source.print(indent2); _source.println("{"); _source.print(indent2); _source.println(" throw("); _source.print(indent2); _source.println( " new statemap.StateUndefinedException());"); _source.print(indent2); _source.println("}"); _source.println(); _source.print(indent2); _source.print("return (("); _source.print(context); _source.println("State) _state);"); // If we are in a lock block, close it. if (Smc.isSynchronized() == true) { _source.print(_indent); _source.println(" }"); } // Close the State get. _source.print(_indent); _source.println(" }"); // Now generate the State set. _source.print(_indent); _source.println(" set"); _source.print(_indent); _source.println(" {"); // Again, if synchronization is on, then protect access // to this FSM. if (Smc.isSynchronized() == true) { _source.print(_indent); _source.println(" lock(this)"); _source.print(_indent); _source.println(" {"); indent2 = _indent + " "; } else { indent2 = _indent + " "; } _source.print(indent2); _source.println("SetState(value);"); // If we are in a lock block, close it. if (Smc.isSynchronized() == true) { _source.print(_indent); _source.println(" }"); } // Close the State set. _source.print(_indent); _source.println(" }"); // Close the state property. _source.print(_indent); _source.println(" }"); _source.println(); // Generate the Owner property. _source.print(_indent); _source.print(" public "); _source.print(context); _source.println(" Owner"); _source.print(_indent); _source.println(" {"); // Generate the property get method. _source.print(_indent); _source.println(" get"); _source.print(_indent); _source.println(" {"); _source.print(_indent); _source.println(" return (_owner);"); _source.print(_indent); _source.println(" }"); // Generate the property set method. _source.print(_indent); _source.println(" set"); _source.print(_indent); _source.println(" {"); // Again, if synchronization is on, then protect access // to this FSM. if (Smc.isSynchronized() == true) { _source.print(_indent); _source.println(" lock(this)"); _source.print(_indent); _source.println(" {"); indent2 = _indent + " "; } else { indent2 = _indent + " "; } _source.print(indent2); _source.println("_owner = value;"); // If we are in a lock block, close it. if (Smc.isSynchronized() == true) { _source.print(_indent); _source.println(" }"); } // Close the Onwer set. _source.print(_indent); _source.println(" }"); // Close the Owner property. _source.print(_indent); _source.println(" }"); _source.println(); _source.print(_indent); _source.println( "//---------------------------------------------------------------"); _source.print(_indent); _source.println("// Member methods."); _source.print(_indent); _source.println("//"); _source.println(); // Generate the context class' constructor. _source.print(_indent); _source.print(" public "); _source.print(context); _source.print("Context("); _source.print(context); _source.println(" owner) :"); _source.print(_indent); _source.println(" base ()"); _source.print(_indent); _source.println(" {"); _source.println(" _owner = owner;"); // The state name "map::state" must be changed to // "map.state". if ((index = startState.indexOf("::")) >= 0) { csState = startState.substring(0, index) + "." + startState.substring(index + 2); } else { csState = startState; } _source.print(_indent); _source.print(" _state = "); _source.print(csState); _source.println(";"); // Execute the start state's entry actions. _source.print(_indent); _source.print(" "); _source.print(csState); _source.println(".Entry(this);"); // Context class' constructor end. _source.print(_indent); _source.println(" }"); _source.println(); // If -serial was specified, then generate the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -