📄 smcrubygenerator.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.//// Port to Python by Francois Perrad, francois.perrad@gadz.org// Copyright 2004, Francois Perrad.// 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: SmcRubyGenerator.java,v 1.7 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;import java.util.StringTokenizer;/** * Visits the abstract syntax tree, emitting Ruby code. * @see SmcElement * @see SmcCodeGenerator * @see SmcVisitor * * @author Francois Perrad */public final class SmcRubyGenerator extends SmcCodeGenerator{//---------------------------------------------------------------// Member methods// public SmcRubyGenerator(String srcfileBase) { super (srcfileBase, "{0}{1}_sm.{2}", "rb"); } // end of SmcRubyGenerator(String) public void visit(SmcFSM fsm) { String packageName = fsm.getPackage(); String context = fsm.getContext(); String rawSource = fsm.getSource(); String startState = fsm.getStartState(); List<SmcMap> maps = fsm.getMaps(); List<SmcTransition> transitions; List<SmcParameter> params; String transName; int packageDepth = 0; int index; _source.println("# DO NOT EDIT."); _source.println( "# generated by smc (http://smc.sourceforge.net/)"); _source.print("# from file : "); _source.print(_srcfileBase); _source.println(".sm"); _source.println(); // Dump out the raw source code, if any. if (rawSource != null && rawSource.length () > 0) { _source.println(rawSource); _source.println(); } // If a package has been specified, then output that // module now. If the package name is "a::b::c", then // this must be converted to: // module a // module b // module c // ... // end // end // end _indent = ""; if (packageName != null && packageName.length() > 0) { StringTokenizer tokenizer = new StringTokenizer(packageName, "::"); String token; while (tokenizer.hasMoreTokens() == true) { token = tokenizer.nextToken(); ++packageDepth; _source.print(_indent); _source.print("module "); _source.println(token); _source.println(); _indent += " "; } } _source.print(_indent); _source.println("require 'statemap'"); // Do user-specified imports now. for (String imp: fsm.getImports()) { _source.print(_indent); _source.print("require '"); _source.print(imp); _source.println("'"); } // Declare the inner state class. _source.println(); _source.print(_indent); _source.print("class "); _source.print(context); _source.println("State < Statemap::State"); _source.println(); _source.print(_indent); _source.println(" def Entry(fsm) end"); _source.println(); _source.print(_indent); _source.println(" def Exit(fsm) end"); _source.println(); // Get the transition list. // Generate the default transition definitions. transitions = fsm.getTransitions(); for (SmcTransition trans: transitions) { params = trans.getParameters(); // Don't generate the Default transition here. if (trans.getName().equals("Default") == false) { _source.print(_indent); _source.print(" def "); _source.print(trans.getName()); _source.print("(fsm"); for (SmcParameter param: params) { _source.print(", "); _source.print(param.getName()); } _source.println(")"); // If this method is reached, that means that // this transition was passed to a state which // does not define the transition. Call the // state's default transition method. _source.print(_indent); _source.println(" Default(fsm)"); _source.print(_indent); _source.println(" end"); _source.println(); } } // Generate the overall Default transition for all maps. _source.print(_indent); _source.println(" def Default(fsm)"); if (Smc.isDebug() == true) { _source.print(_indent); _source.println( " if fsm.getDebugFlag then"); _source.print(_indent); _source.println( " fsm.getDebugStream.write(\"TRANSITION : Default\\n\")"); _source.print(_indent); _source.println(" end"); } _source.print(_indent); _source.println( " msg = \"\\nState: \" + fsm.getState.getName +"); _source.print(_indent); _source.println( " \"\\nTransition: \" + fsm.getTransition + \"\\n\""); _source.print(_indent); _source.println( " raise Statemap::TransitionUndefinedException, msg"); _source.print(_indent); _source.println(" end"); // End of context class. _source.println(); _source.print(_indent); _source.println("end"); // Have each map print out its source code now. for (SmcMap map: maps) { map.accept(this); } // The context class contains all the state classes as // inner classes, so generate the context first rather // than last. _source.println(); _source.print(_indent); _source.print("class "); _source.print(context); _source.println("_sm < Statemap::FSMContext"); // Generate the context class' constructor. _source.println(); _source.print(_indent); _source.println(" def initialize(owner)"); _source.print(_indent); _source.println(" super()"); _source.print(_indent); _source.println(" @_owner = owner"); _source.print(_indent); _source.print(" setState("); _source.print(startState); _source.println(")"); // Execute the start state's entry actions. _source.print(_indent); _source.print(" "); _source.print(startState); _source.println(".Entry(self)"); _source.print(_indent); _source.println(" end"); _source.println(); // Generate the transition methods. for (SmcTransition trans: transitions) { transName = trans.getName(); params = trans.getParameters(); if (transName.equals("Default") == false) { _source.print(_indent); _source.print(" def "); _source.print(transName); if (params.size() != 0) { _source.println("(*arglist)"); } else { _source.println("()"); } // Save away the transition name in case it is // need in an UndefinedTransitionException. _source.print(_indent); _source.print(" @_transition = '"); _source.print(transName); _source.println("'"); _source.print(_indent); _source.print(" getState."); _source.print(transName); _source.print("(self"); if (params.size() != 0) { _source.print(", *arglist"); } _source.println(")"); _source.print(_indent); _source.println(" @_transition = nil"); _source.print(_indent); _source.println(" end"); _source.println(); } } // getState() method. _source.print(_indent); _source.println(" def getState()"); _source.print(_indent); _source.println(" if @_state.nil? then"); _source.print(_indent); _source.println( " raise Statemap::StateUndefinedException"); _source.print(_indent); _source.println(" end"); _source.print(_indent); _source.println(" return @_state"); _source.print(_indent); _source.println(" end"); _source.println(); // getOwner() method. _source.print(_indent); _source.println(" def getOwner()"); _source.print(_indent); _source.println(" return @_owner"); _source.print(_indent); _source.println(" end"); _source.println(); _source.print(_indent); _source.println("end"); // If necessary, place an end for the module. if (packageName != null && packageName.length() > 0) { int i; int j; for (i = (packageDepth - 1); i >= 0; --i) { _source.println(); // Output the proper indent. for (j = 0; j < i; ++j) { _source.print(" "); } _source.println("end"); } } return; } // end of visit(SmcFSM) public void visit(SmcMap map) { List<SmcTransition> definedDefaultTransitions; SmcState defaultState = map.getDefaultState(); String context = map.getFSM().getContext(); String mapName = map.getName(); List<SmcState> states = map.getStates(); String indent2; // Initialize the default transition list to all the // default state's transitions. if (defaultState != null) { definedDefaultTransitions = defaultState.getTransitions(); } else { definedDefaultTransitions = new ArrayList<SmcTransition>(); } // Declare the map default state class. _source.println(); _source.print(_indent); _source.print("class "); _source.print(mapName); _source.print("_Default < "); _source.print(context); _source.println("State"); // Declare the user-defined default transitions first. indent2 = _indent; _indent = _indent + " "; for (SmcTransition transition: definedDefaultTransitions) { transition.accept(this); } _indent = indent2; // If -reflect was specified, then generate the // _transitions table. if (Smc.isReflection() == true) { List<SmcTransition> allTransitions = map.getFSM().getTransitions(); String transName; int transDefinition; // Generate the getTransitions() method. _source.println(); _source.print(_indent); _source.println(" def getTransitions()"); _source.print(_indent); _source.println(" return {"); // Now place all transition names and states into the // map. for (SmcTransition transition: allTransitions) { transName = transition.getName(); // If the transition is defined in this map's // default state, then the value is 2. if (definedDefaultTransitions.contains( transition) == true) { transDefinition = 2; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -