001: /*=============================================================================
002: * Copyright Texas Instruments 2004. All Rights Reserved.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * $ProjectHeader: OSCRIPT 0.155 Fri, 20 Dec 2002 18:34:22 -0800 rclark $
019: */
020:
021: package oscript.translator;
022:
023: import oscript.syntaxtree.*;
024:
025: /**
026: * A translator to implement one part of the language in terms of another.
027: * This simplifies the compiler/interpreter implementation. This translates
028: * from:
029: * <pre>
030: * "'{" Program "}"
031: * </pre>
032: * to
033: * <pre>
034: * "function" "(" "args..." ")" "{" Program "}"
035: * </pre>
036: *
037: * <!--
038: * Note that current implementation substitutes NodeToken-s for the most
039: * similar NodeToken in the input. This makes row/col #'s the most sane,
040: * but the string representation may be wrong, for example "function"
041: * instead of "var". I did it this way since it is the least overhead,
042: * which matters for the interpreter. That should work fine with the
043: * current interpreter and compiler implementations
044: * -->
045: *
046: * @author Rob Clark
047: * @version 0.1
048: */
049: public class ShorthandFunctionPrimaryPrefixTranslator {
050: private final static oscript.parser.Token ARGS_TOKEN = new oscript.parser.Token();
051: private final static oscript.parser.Token DOTDOTDOT_TOKEN = new oscript.parser.Token();
052: static {
053: ARGS_TOKEN.kind = oscript.parser.OscriptParserConstants.IDENTIFIER;
054: DOTDOTDOT_TOKEN.kind = oscript.parser.OscriptParserConstants.DOTDOTDOT;
055: }
056: private final static NodeOptional ARGLIST = new NodeOptional(
057: new Arglist(new Permissions(new NodeListOptional()),
058: new NodeToken("args", oscript.data.OString
059: .makeString("args"), ARGS_TOKEN, null),
060: new NodeListOptional(), new NodeOptional(
061: new NodeToken("...", oscript.data.OString
062: .makeString("..."),
063: DOTDOTDOT_TOKEN, null))));
064: private final static NodeOptional EXTENDS = new NodeOptional();
065:
066: /**
067: * Convert a {@link ShorthandFunctionPrimaryPrefix} production in the syntaxtree
068: * into an equivalent production.
069: * <pre>
070: * f0 -> "'{"
071: * f1 -> Program(true)
072: * f2 -> "}"
073: * </pre>
074: */
075: public static Node translate(ShorthandFunctionPrimaryPrefix n) {
076: if (n.translated == null) {
077: n.translated = new FunctionPrimaryPrefix(n.f0, // "function"
078: n.f0, // "("
079: ARGLIST, // (Arglist())?
080: n.f0, // ")"
081: EXTENDS, // ("extends" ...)?
082: n.f0, // "{"
083: n.f1, // Program()
084: n.f2, // "}"
085: n.hasVarInScope, n.hasFxnInScope);
086: }
087: return n.translated;
088: }
089: }
090:
091: /*
092: * Local Variables:
093: * tab-width: 2
094: * indent-tabs-mode: nil
095: * mode: java
096: * c-indentation-style: java
097: * c-basic-offset: 2
098: * eval: (c-set-offset 'substatement-open '0)
099: * eval: (c-set-offset 'case-label '+)
100: * eval: (c-set-offset 'inclass '+)
101: * eval: (c-set-offset 'inline-open '0)
102: * End:
103: */
|