/*
* $Id: EvalStatement.java,v 1.19 2002/09/16 08:05:06 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.script.statements;
import anvil.Location;
import anvil.parser.Tag;
import anvil.codec.Code;
import anvil.ErrorListener;
import anvil.script.compiler.ByteCompiler;
import anvil.script.Context;
import anvil.script.expression.Expression;
import anvil.script.expression.VariableNode;
import anvil.script.parser.StatementParser;
import anvil.script.parser.TemplateParser;
import anvil.script.Grammar;
import java.io.IOException;
/**
* class EvalStatement
*
* @author: Jani Lehtim�ki
*/
public class EvalStatement extends Statement
{
protected Expression _expression = null;
protected boolean _hasEnd;
protected StringBuffer _content = null;
protected boolean _autoreturn = true;
public EvalStatement(Statement parent, Location location)
{
super(parent, location);
_autoreturn = false;
}
public EvalStatement(Statement parent, Location location, Expression expression)
{
super(parent, location);
_expression = expression;
}
public int typeOf()
{
return Statement.ST_EVAL;
}
public String name()
{
return "expression";
}
public String toString()
{
return (_expression != null) ? _expression.toString() + ';' : ";";
}
public boolean hasEnd()
{
return _hasEnd;
}
public Expression getExpression()
{
return _expression;
}
public void parse(TemplateParser parser, Tag tag)
{
String expr = tag.getValue("expr");
if (expr != null) {
_expression = Grammar.parseStandaloneExpression(expr, getLocation(), parser);
_hasEnd = false;
} else {
_hasEnd = true;
}
if (tag.hasEndSlash()) {
_hasEnd = false;
}
if (_hasEnd) {
_content = new StringBuffer();
}
}
public void parseContent(TemplateParser parser)
{
String code = _content.toString();
if (code.startsWith("<!--") && code.endsWith("-->")) {
code = code.substring(4, code.length() - 3);
}
parser.push(getParentStatement().getBlockStatement());
StatementParser p = new StatementParser(parser, getLocation(), code);
p.parseStatements();
parser.pop();
}
public void check(ErrorListener context)
{
if (_expression != null) {
_expression.check(context);
//if (!_expression.isStandalone()) {
// context.error(getLocation(), "Statement is not a standalone expression");
//}
}
}
public void onCharacters(TemplateParser parser, String cdata)
{
_content.append(cdata);
}
public boolean onTag(TemplateParser parser, int type, Tag tag)
{
switch(type) {
case ST_TAG:
_content.append(tag.toString());
break;
case Statement.ST_ENDEVAL:
parser.pop();
parseContent(parser);
break;
default:
return false;
}
return true;
}
public void compile(ByteCompiler context)
{
if (_expression != null) {
Code code = context.getCode();
if (_expression.needLineNumbers()) {
context.location(getLocation());
}
FunctionStatement function = getFunctionStatement();
if (_autoreturn && function != null) {
VariableNode var = function.getReturnVariable();
if (var != null) {
var.compile(context, _expression);
} else {
_expression.compile(context, Expression.GET);
}
} else {
_expression.compile(context, Expression.GET);
}
code.pop();
}
}
}