1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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><expr>[ <expr> ]</code></dd>
37 * <dt>Subranging:</dt>
38 * <dd><code><expr>[ <expr> .. <expr> ]</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 }