// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) space 
// Source File Name:   Parser.java

package lt.monarch.function.parser;

import java.io.InputStream;
import java.io.Reader;
import java.util.Vector;
import lt.monarch.function.*;
import lt.monarch.function.node.*;

// Referenced classes of package lt.monarch.function.parser:
//            ParserConstants, ParseException, Token, ASCII_UCodeESC_CharStream, 
//            ParserTokenManager

public class Parser
    implements ParserConstants
{

    public final Function functionDefinition(Environment environment)
        throws ParseException
    {
        Vector vector = new Vector();
        Token token1 = jj_consume_token(9);
        jj_consume_token(16);
        jj_consume_token(9);
        vector.addElement(token);
label0:
        do
            switch (jj_ntk != -1 ? jj_ntk : jj_ntk())
            {
            default:
                jj_la1[0] = jj_gen;
                break label0;

            case 17: // '\021'
                jj_consume_token(17);
                jj_consume_token(9);
                vector.addElement(token);
                break;
            }
        while (true);
        jj_consume_token(18);
        jj_consume_token(19);
        Variable avariable[] = new Variable[vector.size()];
        for (int i = 0; i < vector.size(); i++)
            avariable[i] = new Variable(((Token)vector.elementAt(i)).image);

        Node node = sum(new LocalEnvironment(environment, avariable));
        jj_consume_token(0);
        return new Function(token1.image, avariable, node);
    }

    public final Node functionBody(Environment environment)
        throws ParseException
    {
        Node node = sum(environment);
        jj_consume_token(0);
        return node;
    }

    public final Node sum(Environment environment)
        throws ParseException
    {
        Object obj = term(environment);
label0:
        do
            switch (jj_ntk != -1 ? jj_ntk : jj_ntk())
            {
            default:
                jj_la1[1] = jj_gen;
                break label0;

            case 4: // '\004'
            case 5: // '\005'
                switch (jj_ntk != -1 ? jj_ntk : jj_ntk())
                {
                case 4: // '\004'
                    jj_consume_token(4);
                    Node node = term(environment);
                    obj = new PlusNode(((Node) (obj)), node);
                    break;

                case 5: // '\005'
                    jj_consume_token(5);
                    Node node1 = term(environment);
                    obj = new MinusNode(((Node) (obj)), node1);
                    break;

                default:
                    jj_la1[2] = jj_gen;
                    jj_consume_token(-1);
                    throw new ParseException();
                }
                break;
            }
        while (true);
        return ((Node) (obj));
    }

    public final Node term(Environment environment)
        throws ParseException
    {
        Object obj = expr(environment);
label0:
        do
            switch (jj_ntk != -1 ? jj_ntk : jj_ntk())
            {
            default:
                jj_la1[3] = jj_gen;
                break label0;

            case 6: // '\006'
            case 7: // '\007'
                switch (jj_ntk != -1 ? jj_ntk : jj_ntk())
                {
                case 6: // '\006'
                    jj_consume_token(6);
                    Node node = expr(environment);
                    obj = new MulNode(((Node) (obj)), node);
                    break;

                case 7: // '\007'
                    jj_consume_token(7);
                    Node node1 = expr(environment);
                    obj = new DivNode(((Node) (obj)), node1);
                    break;

                default:
                    jj_la1[4] = jj_gen;
                    jj_consume_token(-1);
                    throw new ParseException();
                }
                break;
            }
        while (true);
        return ((Node) (obj));
    }

    public final Node expr(Environment environment)
        throws ParseException
    {
        Object obj = unary(environment);
        switch (jj_ntk != -1 ? jj_ntk : jj_ntk())
        {
        case 8: // '\b'
            jj_consume_token(8);
            Node node = element(environment);
            obj = new PowerNode(((Node) (obj)), node);
            break;

        default:
            jj_la1[5] = jj_gen;
            break;
        }
        return ((Node) (obj));
    }

    public final Node unary(Environment environment)
        throws ParseException
    {
        switch (jj_ntk != -1 ? jj_ntk : jj_ntk())
        {
        case 5: // '\005'
            jj_consume_token(5);
            Node node = element(environment);
            return (Node)new NegativeNode(node);

        case 9: // '\t'
        case 10: // '\n'
        case 16: // '\020'
            Node node1 = element(environment);
            return node1;

        case 6: // '\006'
        case 7: // '\007'
        case 8: // '\b'
        case 11: // '\013'
        case 12: // '\f'
        case 13: // '\r'
        case 14: // '\016'
        case 15: // '\017'
        default:
            jj_la1[6] = jj_gen;
            jj_consume_token(-1);
            throw new ParseException();
        }
    }

    public final Node element(Environment environment)
        throws ParseException
    {
        switch (jj_ntk != -1 ? jj_ntk : jj_ntk())
        {
        case 10: // '\n'
            jj_consume_token(10);
            double d = (0.0D / 0.0D);
            try
            {
                d = Double.valueOf(token.image).doubleValue();
            }
            catch (NumberFormatException numberformatexception) { }
            return (Node)new ConstantNode(d);

        case 16: // '\020'
            jj_consume_token(16);
            Node node = sum(environment);
            jj_consume_token(18);
            return node;

        case 9: // '\t'
            Node node1 = identifier(environment);
            return node1;

        case 11: // '\013'
        case 12: // '\f'
        case 13: // '\r'
        case 14: // '\016'
        case 15: // '\017'
        default:
            jj_la1[7] = jj_gen;
            jj_consume_token(-1);
            throw new ParseException();
        }
    }

    public final Node identifier(Environment environment)
        throws ParseException
    {
        Vector vector = new Vector();
        Token token1 = jj_consume_token(9);
        switch (jj_ntk != -1 ? jj_ntk : jj_ntk())
        {
        case 16: // '\020'
            jj_consume_token(16);
            Node node = sum(environment);
            vector.addElement(node);
label0:
            do
                switch (jj_ntk != -1 ? jj_ntk : jj_ntk())
                {
                default:
                    jj_la1[8] = jj_gen;
                    break label0;

                case 17: // '\021'
                    jj_consume_token(17);
                    Node node1 = sum(environment);
                    vector.addElement(node1);
                    break;
                }
            while (true);
            jj_consume_token(18);
            break;

        default:
            jj_la1[9] = jj_gen;
            break;
        }
        Identifier identifier1 = environment.identifierWithName(token1.image);
        if (identifier1 == null)
        {
            ParseException parseexception = new ParseException("Unknown identifier \"" + token1.image + "\"");
            parseexception.currentToken = token1;
            throw parseexception;
        }
        if (identifier1.parameterCount() != vector.size())
        {
            ParseException parseexception1 = identifier1.parameterCount() != 0 ? new ParseException("Expecting " + identifier1.parameterCount() + " parameter(s) in '" + token1.image + "'") : new ParseException("'" + token1.image + "' is a has no parameters");
            parseexception1.currentToken = token1;
            throw parseexception1;
        }
        if (vector.size() == 0)
        {
            return (Node)new IdentifierNode(identifier1);
        } else
        {
            Node anode[] = new Node[vector.size()];
            vector.copyInto(anode);
            return (Node)new IdentifierNode(identifier1, anode);
        }
    }

    public void ReInit(InputStream inputstream)
    {
        jj_input_stream.ReInit(inputstream, 1, 1);
        token_source.ReInit(jj_input_stream);
        token = new Token();
        jj_ntk = -1;
        jj_gen = 0;
        for (int i = 0; i < 10; i++)
            jj_la1[i] = -1;

    }

    public void ReInit(Reader reader)
    {
        jj_input_stream.ReInit(reader, 1, 1);
        token_source.ReInit(jj_input_stream);
        token = new Token();
        jj_ntk = -1;
        jj_gen = 0;
        for (int i = 0; i < 10; i++)
            jj_la1[i] = -1;

    }

    public void ReInit(ParserTokenManager parsertokenmanager)
    {
        token_source = parsertokenmanager;
        token = new Token();
        jj_ntk = -1;
        jj_gen = 0;
        for (int i = 0; i < 10; i++)
            jj_la1[i] = -1;

    }

    private final Token jj_consume_token(int i)
        throws ParseException
    {
        Token token1;
        if ((token1 = token).next != null)
            token = token.next;
        else
            token = token.next = token_source.getNextToken();
        jj_ntk = -1;
        if (token.kind == i)
        {
            jj_gen++;
            return token;
        } else
        {
            token = token1;
            jj_kind = i;
            throw generateParseException();
        }
    }

    public final Token getNextToken()
    {
        if (token.next != null)
            token = token.next;
        else
            token = token.next = token_source.getNextToken();
        jj_ntk = -1;
        jj_gen++;
        return token;
    }

    public final Token getToken(int i)
    {
        Token token1 = token;
        for (int j = 0; j < i; j++)
            if (token1.next != null)
                token1 = token1.next;
            else
                token1 = token1.next = token_source.getNextToken();

        return token1;
    }

    private final int jj_ntk()
    {
        if ((jj_nt = token.next) == null)
            return jj_ntk = (token.next = token_source.getNextToken()).kind;
        else
            return jj_ntk = jj_nt.kind;
    }

    public final ParseException generateParseException()
    {
        jj_expentries.removeAllElements();
        boolean aflag[] = new boolean[20];
        for (int i = 0; i < 20; i++)
            aflag[i] = false;

        if (jj_kind >= 0)
        {
            aflag[jj_kind] = true;
            jj_kind = -1;
        }
        for (int j = 0; j < 10; j++)
            if (jj_la1[j] == jj_gen)
            {
                for (int l = 0; l < 32; l++)
                    if ((jj_la1_0[j] & 1 << l) != 0)
                        aflag[l] = true;

            }

        for (int k = 0; k < 20; k++)
            if (aflag[k])
            {
                jj_expentry = new int[1];
                jj_expentry[0] = k;
                jj_expentries.addElement(jj_expentry);
            }

        int ai[][] = new int[jj_expentries.size()][];
        for (int i1 = 0; i1 < jj_expentries.size(); i1++)
            ai[i1] = (int[])jj_expentries.elementAt(i1);

        return new ParseException(token, ai, ParserConstants.tokenImage);
    }

    public final void enable_tracing()
    {
    }

    public final void disable_tracing()
    {
    }

    public Parser(InputStream inputstream)
    {
        jj_la1 = new int[10];
        jj_expentries = new Vector();
        jj_kind = -1;
        jj_input_stream = new ASCII_UCodeESC_CharStream(inputstream, 1, 1);
        token_source = new ParserTokenManager(jj_input_stream);
        token = new Token();
        jj_ntk = -1;
        jj_gen = 0;
        for (int i = 0; i < 10; i++)
            jj_la1[i] = -1;

    }

    public Parser(Reader reader)
    {
        jj_la1 = new int[10];
        jj_expentries = new Vector();
        jj_kind = -1;
        jj_input_stream = new ASCII_UCodeESC_CharStream(reader, 1, 1);
        token_source = new ParserTokenManager(jj_input_stream);
        token = new Token();
        jj_ntk = -1;
        jj_gen = 0;
        for (int i = 0; i < 10; i++)
            jj_la1[i] = -1;

    }

    public Parser(ParserTokenManager parsertokenmanager)
    {
        jj_la1 = new int[10];
        jj_expentries = new Vector();
        jj_kind = -1;
        token_source = parsertokenmanager;
        token = new Token();
        jj_ntk = -1;
        jj_gen = 0;
        for (int i = 0; i < 10; i++)
            jj_la1[i] = -1;

    }

    public ParserTokenManager token_source;
    ASCII_UCodeESC_CharStream jj_input_stream;
    public Token token;
    public Token jj_nt;
    private int jj_ntk;
    private int jj_gen;
    private final int jj_la1[];
    private final int jj_la1_0[] = {
        0x20000, 48, 48, 192, 192, 256, 0x10620, 0x10600, 0x20000, 0x10000
    };
    private Vector jj_expentries;
    private int jj_expentry[];
    private int jj_kind;
}
