View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2005 Kevin Rogers
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.expr.Expr;
24  
25  /**
26   * This class is a convenience for implementing procedure synyax elements. e.g.
27   * functions, fun's, methods and memoes.
28   */
29  public abstract class ProcedureSyntax extends PrefixSyntax {
30  
31      /**
32       * The closing character sequence for this proceedure.
33       */
34      private final String closingKeyword;
35  
36      /**
37       * Indicates if this proceedure requires a name. e.g. for use in lambda
38       * functions which have no name.
39       */
40      private boolean proceedureNeedsName;
41  
42      /**
43       * Creates a proceedure syntax parser instance, with the specified closing
44       * character sequence.
45       *
46       * @param closing   the closing character seqeunce for this procedure
47       * syntax parser instance
48       * @param nameNeeded    a boolean indicating if this function must have a
49       * name or not.
50       */
51      protected ProcedureSyntax( final String closing, final boolean nameNeeded ) {
52          this.closingKeyword = closing;
53          this.proceedureNeedsName = nameNeeded;
54      }
55  
56      /**
57       * @see org.millscript.millscript.syntax.PrefixSyntaxInterface#prefix(java.lang.String, org.millscript.millscript.syntax.Parser)
58       */
59      @Override
60      public Expr prefix( final String sym, final Parser parser ) {
61          // Read the function header
62          Expr h = parser.readExprTo( "=>" );
63          // Make a new function header object for this parser
64          FunctionHeader header = new FunctionHeader();
65          // Flatten the parsed header into the header object
66          header.flatten( h, this.proceedureNeedsName );
67          // Read the body of the function, up to the closing keyword for this
68          // function syntax instance
69          Expr body = parser.readStmntsTo( this.closingKeyword );
70          // Return an expression for this proceedure
71          return procedure( header, body, parser );
72      }
73  
74      /**
75       * Returns the expression for this type of proceedure syntax.
76       *
77       * @param header    the procedure header
78       * @param body  the procedure body
79       * @param parser    the current parser
80       * @return  an Expr for this procedure
81       */
82      public abstract Expr procedure( final FunctionHeader header, final Expr body, final Parser parser );
83  
84  }