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.millscript.expr.Expr;
24  import org.millscript.millscript.expr.IndexExpr;
25  import org.millscript.millscript.expr.SubrangeExpr;
26  
27  /**
28   * This class implements index syntax. The index operator <code>[</code> is used
29   * for indexing lists and maps, or objects that can be viewed as a list. The
30   * index operator is also used for subranging a list, or an object that can be
31   * viewed as a list.
32   *
33   * <p>
34   * <dl>
35   * <dt>Indexing:</dt>
36   * <dd><code>&lt;expr&gt;[ &lt;expr&gt; ]</code></dd>
37   * <dt>Subranging:</dt>
38   * <dd><code>&lt;expr&gt;[ &lt;expr&gt; .. &lt;expr&gt; ]</code></dd>
39   * </p>
40   *
41   * <p>
42   * In the case of indexing, the left hand side expression should return a single
43   * result, which must be a list, map, or an object with a list view. The right
44   * hand expression should return a single result, which is used to index the
45   * left hand side. e.g. in the case where the left hand side is a list, the
46   * right hand side should be a number.  in the case where the left hand side is
47   * a map, the right hand side should be any object which is a valid key for the
48   * map.
49   * </p>
50   *
51   * <p>
52   * In the case of subranging, the left hand side expression should return a
53   * single result, which must be a list or an object with a list view. The right
54   * hand side expressions should both return a single result, which is used to
55   * subrage the left hand side. e.g. in the case where the left hand side is a
56   * list, both right hand side expressions should be valid indicies.
57   * </p>
58   *
59   * @see IndexExpr
60   * @see SubrangeExpr
61   */
62  public final class IndexSyntax extends PostfixSyntax {
63  
64      /**
65       * @see org.millscript.millscript.syntax.PostfixSyntaxInterface#postfix(java.lang.String, int, org.millscript.millscript.expr.Expr, org.millscript.millscript.syntax.Parser)
66       */
67      @Override
68      public Expr postfix( final String sym, final int prec, final Expr lhs, final Parser parser ) {
69          Expr rhs = parser.readExpr();
70          if ( parser.tryRead( ".." ) ) {
71              Expr rhs2 = parser.readExprTo( "]" );
72              return SubrangeExpr.make( lhs, rhs, rhs2 );
73          } else {
74              parser.mustRead( "]" );
75              return IndexExpr.make( lhs, rhs );
76          }
77      }
78  
79  }