001: /*
002: * $Id: ExpressionParser.java,v 1.17 2002/09/16 08:05:05 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.script.parser;
011:
012: import anvil.ErrorListener;
013: import anvil.Location;
014: import anvil.script.Context;
015: import anvil.script.expression.Node;
016: import anvil.script.expression.Expression;
017: import anvil.script.expression.ExpressionList;
018: import anvil.script.expression.Parent;
019: import anvil.script.ModuleEnvelope;
020: import anvil.script.ParameterListDeclaration;
021: import java.util.Enumeration;
022:
023: /**
024: * class ExpressionParser
025: *
026: * @author: Jani Lehtimäki
027: */
028: public class ExpressionParser extends ParserBase {
029:
030: public static final int TYPE_VALUE = 0;
031: public static final int TYPE_STANDALONE = 1;
032: public static final int TYPE_ASSIGNMENT = 2;
033: public static final int TYPE_ASSIGNABLE = 3;
034:
035: private TemplateParser _parser;
036: private Location _location = null;
037:
038: public ExpressionParser(TemplateParser parser, Location location,
039: String expression) {
040: super (new Tokenizer(expression));
041: _parser = parser;
042: _location = location;
043: _current = parser.peek();
044: ((Tokenizer) token_source).setParser(this );
045: }
046:
047: public ModuleEnvelope getEnvelope() {
048: return _parser.getEnvelope();
049: }
050:
051: public Location toLocation(Token token) {
052: return _location;
053: }
054:
055: public Location toLocation(int line, int column) {
056: return _location;
057: }
058:
059: public void error(Location location, Throwable throwable) {
060: _parser.error(location, throwable);
061: }
062:
063: public void error(Location location, String message) {
064: _parser.error(location, message);
065: }
066:
067: public int errors() {
068: return _parser.errors();
069: }
070:
071: public Enumeration getEvents() {
072: return _parser.getEvents();
073: }
074:
075: public void merge(ErrorListener listener) {
076: _parser.merge(listener);
077: }
078:
079: public void parseParameterListDeclaration(
080: ParameterListDeclaration parameters) {
081: try {
082: TerminalFunctionParameterList(parameters);
083: } catch (ParseException e) {
084: error(_location, e.getMessage());
085: }
086: }
087:
088: public Parent parseParameterList() {
089: try {
090: TerminalArgumentList();
091: return (Parent) pop();
092: } catch (ParseException e) {
093: error(_location, e.getMessage());
094: return new ExpressionList(0);
095: }
096: }
097:
098: public ExpressionList parseExpressionList() {
099: try {
100: TerminalList();
101: return (ExpressionList) pop();
102: } catch (ParseException e) {
103: error(_location, e.getMessage());
104: return new ExpressionList(0);
105: }
106: }
107:
108: public Object[] parseForExpressionList() {
109: try {
110: return TerminalForExpressionList();
111: } catch (ParseException e) {
112: error(_location, e.getMessage());
113: return new Object[3];
114: }
115: }
116:
117: public Expression parseExpression(int type) {
118: try {
119:
120: switch (type) {
121: case TYPE_VALUE:
122: TerminalValueExpression();
123: return (Expression) pop();
124:
125: case TYPE_STANDALONE:
126: TerminalExpression();
127: return (Expression) pop();
128:
129: case TYPE_ASSIGNMENT:
130: TerminalAssignmentExpression();
131: return (Expression) pop();
132:
133: case TYPE_ASSIGNABLE:
134: TerminalAssignableExpression();
135: return (Expression) pop();
136: }
137:
138: } catch (ParseException e) {
139: error(_location, e.getMessage());
140: }
141:
142: return Expression.NULL;
143: }
144:
145: public Expression[] parseForeachExpression() {
146: try {
147: return TerminalForeachAssignmentExpression();
148: } catch (ParseException e) {
149: error(_location, e.getMessage());
150: }
151: return new Expression[0];
152: }
153:
154: }
|