View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
4   //
5   // This file is part of MillScript.
6   //
7   // MillScript is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
20  ////////////////////////////////////////////////////////////////////////////////
21  package org.millscript.millscript.syntax;
22  
23  import org.millscript.commons.util.EList;
24  import org.millscript.commons.util.list.ELinkedList;
25  import org.millscript.millscript.expr.CaseExpr;
26  import org.millscript.millscript.expr.Expr;
27  import org.millscript.millscript.expr.SwitchExpr;
28  
29  /**
30   * This class implements <code>switch</code> syntax. This is used to implement
31   * switch statements in MillScript.
32   *
33   * <p>
34   * <code>switch &lt;expr&gt; [ tests ] endswitch</code>
35   * </p>
36   *
37   * <p>
38   * The switch statement expression must return a single result, which can be
39   * tested by specifying some optional tests. Each test is a {@link CaseExpr},
40   * e.g
41   * </p>
42   *
43   * <p>
44   * <code>case &lt;expr&gt; [ case &lt;expr&gt; ] then &lt;expr&gt;</code>
45   * </p>
46   *
47   * <p>
48   * Each <code>case</code> statement contains an expression whose result will be
49   * compared to the <code>switch</code> statement expression result. The body of
50   * the <code>case</code> statement will be executed if the <code>case</code>
51   * statement expression result matches the switch statement expression result.
52   * If you have several <code>case</code>s that need to share the same body, you
53   * can repeat as many <code>case</code> statements as you require before the
54   * <code>then</code>.
55   * </p>
56   *
57   * @see SwitchExpr
58   */
59  public final class SwitchSyntax extends PrefixSyntax {
60  
61      /**
62       * @see org.millscript.millscript.syntax.PrefixSyntaxInterface#prefix(java.lang.String, org.millscript.millscript.syntax.Parser)
63       */
64      @Override
65      public SwitchExpr prefix( final String sym, final Parser parser ) {
66          // Read the switch expression
67          Expr sexpr = parser.readExpr();
68          // This list will hold the individual case expressions
69          EList< CaseExpr > cases = new ELinkedList< CaseExpr >();
70          // Placeholder for the optional default expression
71          Expr def = null;
72          // Keep reading until the end of the switch statement
73          while ( !parser.tryRead( "endswitch" ) ) {
74              // The switch body can only contain "else" or "case" expressions
75              if ( parser.tryRead( "else" ) ) {
76                  // Read the default expression/statements
77                  def = parser.readStmnts();
78              } else {
79                  // This list will hold individual cases that share the same body
80                  // expression
81                  EList< Expr > c = new ELinkedList< Expr >();
82                  // We must read "case"
83                  parser.mustRead( "case" );
84                  // Read the case expression and add it to the list
85                  c.addLast( parser.readExpr() );
86                  // Continue to try and read any further case expressions that
87                  // will share the same body
88                  while ( parser.tryRead( "case" ) ) {
89                      // There is another "case", so read it and add it to the
90                      // list
91                      c.addLast( parser.readExpr() );
92                  }
93                  // There are no more cases this body, so we must read "then"
94                  parser.mustRead( "then" );
95                  // Read the body statements for this case
96                  Expr dexpr = parser.readStmnts();
97                  // Add this new case expression to the switch statement
98                  cases.addLast( new CaseExpr( c, dexpr ) );
99              }
100         }
101         // Return a new switch expression for the parsed switch, case and
102         // default expressions.
103         return new SwitchExpr( sexpr, cases, def );
104     }
105 
106 }