View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2005 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.millscript.alert.Alerts;
24  import org.millscript.millscript.expr.ApplyExpr;
25  import org.millscript.millscript.expr.CommaExpr;
26  import org.millscript.millscript.expr.Expr;
27  import org.millscript.millscript.expr.InitExpr;
28  import org.millscript.millscript.expr.NameExpr;
29  
30  /**
31   * This class implements <code>init</code> syntax.
32   *
33   * <p>
34   * <code>init &lt;expr&gt;</code>
35   * </p>
36   *
37   * <p>
38   * This type of expression is only valid inside a class initialiser function.
39   * The right hand side expression must be an apply expression, which is
40   * applying a parent class initialiser. It only makes sense to be able to call
41   * an initialiser for each class once, i.e. you can't call different
42   * initialisers for the same parent class inside a given initialiser.
43   * </p>
44   *
45   * @see org.millscript.millscript.expr.InitExpr
46   */
47  public final class InitSyntax extends PrefixSyntax {
48  
49      /**
50       * @see org.millscript.millscript.syntax.PrefixSyntaxInterface#prefix(java.lang.String, org.millscript.millscript.syntax.Parser)
51       */
52      @Override
53      public InitExpr prefix( final String sym, final Parser parser ) {
54          // Check if the user has specified the "this" caret, so we know if we
55          // will need to provide it implicitly.
56          final boolean thisWasSpecified = parser.peekRead( "^" );
57          // Now read the initialisation apply action
58          final Expr initExpr = parser.readExpr();
59          // This expression must be an apply expression
60          if ( initExpr instanceof ApplyExpr ) {
61              final ApplyExpr ae = (ApplyExpr) initExpr;
62              // Did the user specify the "this" parameter?
63              if ( thisWasSpecified ) {
64                  return new InitExpr( ae.getFun(), ae.getArgs() );
65              } else {
66                  // Implicitly supply the "this" parameter
67                  return new InitExpr( ae.getFun(), new CommaExpr( ae.getArgs(), new NameExpr( "this" ) ) );
68              }
69          } else {
70              throw(
71                  Alerts.parse(
72                      "Unexpected expression after 'init'",
73                      "'init' must be followed by a class initialiser application"
74                  ).mishap()
75              );
76          }
77      }
78  
79  }