📄 smcvbgenerator.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: SmcVBGenerator.java,v 1.10 2008/03/21 14:03:17 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 VB.Net code. * @see SmcElement * @see SmcCodeGenerator * @see SmcVisitor * * @author <a href="mailto:rapp@acm.org">Charles Rapp</a> */public final class SmcVBGenerator extends SmcCodeGenerator{//---------------------------------------------------------------// Member methods// public SmcVBGenerator(String srcfileBase) { super (srcfileBase, "{0}{1}_sm.{2}", "vb"); } // end of SmcVBGenerator(String) public void visit(SmcFSM fsm) { String rawSource = fsm.getSource(); String packageName = fsm.getPackage(); String context = fsm.getContext(); String startState = fsm.getStartState(); List<SmcMap> maps = fsm.getMaps(); List<SmcTransition> transitions; List<SmcParameter> params; Iterator<SmcParameter> pit; String transName; String vbState; String separator; int index; String indent2; // Dump out the raw source code, if any. if (rawSource != null && rawSource.length () > 0) { _source.println(rawSource); _source.println(); } // If reflection is on, then import the .Net collections // package. if (Smc.isReflection() == true) { _source.println("Imports System.Collections"); } // Do user-specified imports now. for (String imp: fsm.getImports()) { _source.print("Imports "); _source.println(imp); } // If serialization is on, then import the .Net // serialization package. if (Smc.isSerial() == true) { _source.println( "Imports System.Runtime.Serialization"); } _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 = " "; } // If -serial was specified, then prepend the serialize // attribute to the class declaration. if (Smc.isSerial() == true) { _source.print(_indent); _source.print("<Serializable()> "); } // Now declare the context class. _source.print(_indent); _source.print("Public NotInheritable Class "); _source.print(context); _source.println("Context"); _source.print(_indent); _source.println(" Inherits statemap.FSMContext"); // If -serial was specified, then we also implement the // ISerializable interface. if (Smc.isSerial() == true) { _source.print(_indent); _source.println(" Implements ISerializable"); } // Declare the associated application class as a data // member. _source.println(); _source.print(_indent); _source.println( " '------------------------------------------------------------"); _source.print(_indent); _source.println(" ' Member data"); _source.print(_indent); _source.println(" '"); _source.print(_indent); _source.println(); _source.print(_indent); _source.println( " ' The associated application class instance."); _source.print(_indent); _source.print(" Private _owner As "); _source.println(context); _source.println(); // If serialization is on, then the shared state array // must be generated. if (Smc.isSerial() == true) { String mapName; _source.print(_indent); _source.println( " '------------------------------------------------------------"); _source.print(_indent); _source.println(" ' Shared data"); _source.print(_indent); _source.println(" '"); _source.println(); _source.print(_indent); _source.print(" ' State instance array. "); _source.println("Used to deserialize."); _source.print(_indent); _source.print( " Private Shared ReadOnly _States() As "); _source.print(context); _source.println("State = _"); _source.print(_indent); _source.print(" {"); // For each map, ... separator = " _"; for (SmcMap map: maps) { mapName = map.getName(); // and for each map state, ... for (SmcState state: map.getStates()) { // Add its singleton instance to the array. _source.println(separator); _source.print(_indent); _source.print(" "); _source.print(mapName); _source.print("."); _source.print(state.getClassName()); separator = ", _"; } } _source.println(" _"); _source.print(_indent); _source.println(" }"); _source.println(); } // Now declare the current state and owner properties. _source.print(_indent); _source.println( " '------------------------------------------------------------"); _source.print(_indent); _source.println(" ' Properties"); _source.print(_indent); _source.println(" '"); _source.println(); _source.print(_indent); _source.print(" Public Property State() As "); _source.print(context); _source.println("State"); _source.print(_indent); _source.println(" Get"); _source.print(_indent); _source.println(" If _state Is Nothing _"); _source.print(_indent); _source.println(" Then"); _source.print(_indent); _source.print(" Throw "); _source.println( "New statemap.StateUndefinedException()"); _source.print(_indent); _source.println(" End If"); _source.println(); _source.print(_indent); _source.println(" Return _state"); _source.print(_indent); _source.println(" End Get"); _source.println(); _source.print(_indent); _source.print(" Set(ByVal state As "); _source.print(context); _source.println("State)"); _source.println(); _source.print(_indent); _source.println(" _state = state"); _source.print(_indent); _source.println(" End Set"); _source.print(_indent); _source.println(" End Property"); _source.println(); _source.print(_indent); _source.print( " Public Property Owner() As "); _source.println(context); _source.print(_indent); _source.println(" Get"); _source.print(_indent); _source.println(" Return _owner"); _source.print(_indent); _source.println(" End Get"); _source.print(_indent); _source.print(" Set(ByVal owner As "); _source.print(context); _source.println(")"); _source.println(); _source.print(_indent); _source.println(" If owner Is Nothing _"); _source.print(_indent); _source.println(" Then"); _source.print(_indent); _source.println( " Throw New NullReferenceException"); _source.print(_indent); _source.println(" End If"); _source.println(); _source.print(_indent); _source.println(" _owner = owner"); _source.print(_indent); _source.println(" End Set"); _source.print(_indent); _source.println(" End Property"); _source.println(); // Generate the class member methods, starting with the // constructor. _source.print(_indent); _source.println( " '------------------------------------------------------------"); _source.print(_indent); _source.println(" ' Member methods"); _source.print(_indent); _source.println(" '"); _source.println(); _source.print(_indent); _source.print(" Public Sub New(ByRef owner As "); _source.print(context); _source.println(")"); _source.println(); _source.print(_indent); _source.println(" _owner = owner"); // The state name "map::state" must be changed to // "map.state". if ((index = startState.indexOf("::")) >= 0) { vbState = startState.substring(0, index) + "." + startState.substring(index + 2); } else { vbState = startState; } _source.print(_indent); _source.print(" _state = "); _source.println(vbState); // Execute the start state's entry actions. _source.print(_indent); _source.print(" "); _source.print(vbState); _source.println(".Entry(Me)"); _source.print(_indent); _source.println(" End Sub"); _source.println(); // Generate the transition methods. These methods are all // formatted: set transition name, call the current // state's transition method, clear the transition name. transitions = fsm.getTransitions(); for (SmcTransition trans: transitions) { // Ignore the default transition. if (trans.getName().equals("Default") == false) { _source.print(_indent); _source.print(" Public Sub "); _source.print(trans.getName()); _source.print("("); // Now output the transition's parameters. params = trans.getParameters(); for (pit = params.iterator(), separator = ""; pit.hasNext() == true; separator = ", ") { _source.print(separator); (pit.next()).accept(this); } _source.println(")"); _source.println(); // If the -sync flag was specified, then output // "SyncLock Me" to prevent multiple threads from // access this state machine simultaneously. if (Smc.isSynchronized() == true) { _source.print(_indent); _source.println(" SyncLock Me");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -