1 ////////////////////////////////////////////////////////////////////////////////
2 // MillScript: an Open Spice interpreter and batch website creation tool
3 // Copyright (C) 2004 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.ApplyExpr;
24 import org.millscript.millscript.expr.CommaExpr;
25 import org.millscript.millscript.expr.Expr;
26 import org.millscript.millscript.expr.NameExpr;
27
28 /**
29 * This class implements <code>this</code> syntax, i.e. the <code>^</code>
30 * operator. The caret is a shorthand for <code>this.</code>.
31 *
32 * @see ApplyExpr
33 */
34 public final class ThisSyntax extends PrefixSyntax {
35
36 /**
37 * @see org.millscript.millscript.syntax.PrefixSyntaxInterface#prefix(java.lang.String, org.millscript.millscript.syntax.Parser)
38 */
39 @Override
40 public ApplyExpr prefix( final String sym, final Parser parser ) {
41 // The caret is a shorthand for "this." so we expect a function
42 // expression to follow it.
43 // NOTE - It is not required to have any arguments on the right hand
44 // side of the function expression, hence we are reading optional
45 // expressions. We read the optional arguments at a higher precedence
46 // as the caret binds more tightly that the dot.
47 // e.g. we might be reading:
48 // ^amethod
49 // ^amethod.afunc
50 // ^amethod( x, y )
51 // the first two cases are actually the same, i.e. you need to read the
52 // second example as:
53 // afunc( ^amethod )
54 Expr f = parser.readExprPrec( Parser.TIGHT_PREC + 210 );
55 // Check if we got a function application expression, e.g. the case of
56 // ^amethod( x, y )
57 if ( f instanceof ApplyExpr ) {
58 // We must build a new apply expression inserting the additional
59 // first argument
60 return (
61 new ApplyExpr(
62 ((ApplyExpr) f).getFun(),
63 CommaExpr.make( new NameExpr( "this" ), ((ApplyExpr) f).getArgs() )
64 )
65 );
66 } else {
67 // We've read another expression, which should be the function name
68 // expression but it could be a little more complex.
69 return new ApplyExpr( f, new NameExpr( "this" ) );
70 }
71 }
72
73 }