/*
* $Id: ScriptParser.java,v 1.15 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.parser;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import anvil.ErrorListener;
import anvil.ErrorListenerImpl;
import anvil.ForgingException;
import anvil.Location;
import anvil.parser.InputSource;
import anvil.script.ModuleEnvelope;
import anvil.script.statements.ModuleStatement;
import anvil.script.statements.Statement;
/**
* class ScriptParser
*
* @author: Jani Lehtim�ki
*/
public class ScriptParser extends ParserBase
{
private ErrorListenerImpl _listener = null;
private ModuleEnvelope _envelope;
private byte[] _content;
private URL _url;
private InputSource _source;
public ScriptParser(ModuleEnvelope envelope, URL url, byte[] content) throws IOException
{
super(new Tokenizer(content));
_envelope = envelope;
_url = url;
_content = content;
((Tokenizer)token_source).setParser(this);
}
public ModuleEnvelope getEnvelope()
{
return _envelope;
}
public Location toLocation(Token token)
{
if (token != null) {
return new Location(_url, token.beginLine, token.beginColumn);
} else {
return new Location(_url, 0, 0);
}
}
public Location toLocation(int line, int column)
{
return new Location(_url, line, column);
}
public void error(Location location, Throwable throwable)
{
if (_listener == null) {
_listener = new ErrorListenerImpl();
}
_listener.error(location, throwable);
}
public void error(Location location, String message)
{
if (_listener == null) {
_listener = new ErrorListenerImpl();
}
_listener.error(location, message);
}
public int errors()
{
if (_listener == null) {
return 0;
} else {
return _listener.errors();
}
}
public Enumeration getEvents()
{
if (_listener == null) {
_listener = new ErrorListenerImpl();
}
return _listener.getEvents();
}
public void merge(ErrorListener listener)
{
if (_listener == null) {
_listener = new ErrorListenerImpl();
}
_listener.merge(listener);
}
public ModuleStatement parseScript() throws ForgingException
{
try {
long started = System.currentTimeMillis();
Module();
long ended = System.currentTimeMillis();
Statement stmt = flowPeek();
if (stmt == null || stmt.typeOf() != Statement.ST_MODULE) {
error(new Location(_url), "Unexcepted end of file");
throw new ForgingException(_listener);
}
ModuleStatement script = (ModuleStatement)stmt;
if (_listener != null) {
throw new ForgingException(_listener);
}
return script;
} catch (ParseException e) {
error(toLocation(e.currentToken), e.getMessage());
throw new ForgingException(_listener);
}
}
}