/*
 * Created on 2004-12-21
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2004
 */
package pl.zalewski.mikolaj.symbdev.core.internal.mmpparser;

import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IMarker;

/**
 * Parses one token and gives information about it's content, errors
 * etc.
 * 
 * @author mikolaj
 */
abstract public class AbstractTokenParser {

    /**
     * @param it An iterator through an MMPToken list
     */
    public AbstractTokenParser(Iterator it)
    {
        if (it.hasNext())
        {
            fToken=(MMPToken)it.next();
            fContent=fToken.fContent;
        } else
        {
            setError(IMarker.SEVERITY_ERROR, "Unexpected end of line");
            fContent="";
        }
    }
    
    /**
     * Parses the token
     * @param l The method will add itself to this list if
     * l!=<code>null</code> 
     * @return <code>this</code>
     */
    public AbstractTokenParser parseAndAddTo(List l)
    {
        if (fToken!=null)
            parse(fContent);
        if (l!=null)
            l.add(this);
        return this;
    }
    
    /**
     * @return If this token should have an error/warning marker and
     * annotation 
     */
    public boolean hasMarker()
    {
        return fMessage!=null;
    }
    
    /**
     * @return The severity (IMarker.SEVERITY_*) of the
     * marker/annotation associated with this token 
     */
    public int getMarkerSeverity()
    {
        return fSeverity;
    }
    
    /**
     * @return The human-=readable message of the marker/annotation
     * associated with this token 
     */
    public String getMarkerMessage()
    {
        return fMessage;
    }
    
    /**
     * Sets an error for this token. It will be returned through the
     * getMarker* methods to the object owner
     * @param severity The IMarker.SEVERITY_* severity
     * @param message The message
     */
    protected void setError(int severity, String message)
    {
        fSeverity=severity;
        fMessage=message;
    }
    
    protected abstract void parse(String token);

    int fSeverity;
    String fMessage;
    public String fContent;
    public MMPToken fToken;
}
