Back to index

wims  3.65+svn20090927
Package Functions | Static Package Attributes | Private Attributes | Static Private Attributes
org.javia.arity.Lexer Class Reference
Collaboration diagram for org.javia.arity.Lexer:
Collaboration graph
[legend]

List of all members.

Package Functions

 Lexer (SyntaxException exception)
void scan (String str, TokenConsumer consumer) throws SyntaxException
void init (String str)
Token nextToken () throws SyntaxException
 Lexer (SyntaxException exception)
void scan (String str, TokenConsumer consumer) throws SyntaxException
void init (String str)
Token nextToken () throws SyntaxException

Static Package Attributes

static final int ADD = 1
static final int SUB = 2
static final int MUL = 3
static final int DIV = 4
static final int MOD = 5
static final int UMIN = 6
static final int POWER = 7
static final int FACT = 8
static final int NUMBER = 9
static final int CONST = 10
static final int CALL = 11
static final int COMMA = 12
static final int LPAREN = 13
static final int RPAREN = 14
static final int END = 15
static final int SQRT = 16
static final Token TOK_ADD = new Token(ADD, 3, Token.LEFT, VM.ADD)
static final Token TOK_SUB = new Token(SUB, 3, Token.LEFT, VM.SUB)
static final Token TOK_MUL = new Token(MUL, 4, Token.LEFT, VM.MUL)
static final Token TOK_DIV = new Token(DIV, 4, Token.LEFT, VM.DIV)
static final Token TOK_MOD = new Token(MOD, 4, Token.LEFT, VM.MOD)
static final Token TOK_UMIN = new Token(UMIN, 5, Token.PREFIX, VM.UMIN)
static final Token TOK_POWER = new Token(POWER, 6, Token.RIGHT, VM.POWER)
static final Token TOK_FACT = new Token(FACT, 7, Token.SUFIX, VM.FACT)
static final Token TOK_SQRT = new Token(SQRT, 8, Token.PREFIX, VM.SQRT)
static final Token TOK_LPAREN = new Token(LPAREN, 1, Token.PREFIX, 0)
static final Token TOK_RPAREN = new Token(RPAREN, 2, 0, 0)
static final Token TOK_COMMA = new Token(COMMA, 1, 0, 0)
static final Token TOK_END = new Token(END, 0, 0, 0)
static final Token TOK_NUMBER = new Token(NUMBER, 20, 0, 0)
static final Token TOK_CONST = new Token(CONST, 20, 0, 0)

Private Attributes

char[] input = new char[32]
int pos
SyntaxException exception

Static Private Attributes

static final String WHITESPACE = " \n\r\t"
static final char END_MARKER = '$'

Detailed Description

Definition at line 19 of file Lexer.java.


Constructor & Destructor Documentation

org.javia.arity.Lexer.Lexer ( SyntaxException  exception) [inline, package]

Definition at line 66 of file Lexer.java.

                                     {
        this.exception = exception;
        init("");
    }

Here is the call graph for this function:

org.javia.arity.Lexer.Lexer ( SyntaxException  exception) [inline, package]

Definition at line 66 of file Lexer.java.

                                     {
        this.exception = exception;
        init("");
    }

Here is the call graph for this function:


Member Function Documentation

void org.javia.arity.Lexer.init ( String  str) [inline, package]

Definition at line 83 of file Lexer.java.

                          {
        exception.expression = str;
        int len = str.length();
        if (input.length < len + 1) {
            input = new char[len+1];
        }
        str.getChars(0, len, input, 0);
        input[len] = END_MARKER;
        pos = 0;
    }

Here is the caller graph for this function:

void org.javia.arity.Lexer.init ( String  str) [inline, package]

Definition at line 83 of file Lexer.java.

                          {
        exception.expression = str;
        int len = str.length();
        if (input.length < len + 1) {
            input = new char[len+1];
        }
        str.getChars(0, len, input, 0);
        input[len] = END_MARKER;
        pos = 0;
    }

Definition at line 94 of file Lexer.java.

                                             {
        while (WHITESPACE.indexOf(input[pos]) != -1) {
            ++pos;
        }

        char c = input[pos];
        int begin = pos++;

        switch (c) {
        case '!': return TOK_FACT;
        case END_MARKER: return TOK_END;
        case '%': return TOK_MOD;
        case '(': return TOK_LPAREN;
        case ')': return TOK_RPAREN;
        case '*': return TOK_MUL;
        case '+': return TOK_ADD;
        case ',': return TOK_COMMA;
        case '-': return TOK_SUB;
        case '/': return TOK_DIV;
        }
        if (c == '^') { 
            return TOK_POWER;
        }

        int p  = pos;
        if (('0' <= c && c <= '9') || c == '.') {
            while (('0' <= c && c <= '9') || c == '.' || c == 'E' || c == 'e') {
                if ((c == 'E' || c == 'e') && input[p] == '-') { //accept '-' only after E
                    ++p; 
                }
                c = input[p++];
            } 
            pos = p-1;
            String nbStr = String.valueOf(input, begin, p-1-begin);
            try {
                // parse single dot as 0
                if (nbStr.equals(".")) {
                    return TOK_NUMBER.setValue(0);
                } else {
                    double numberValue = Double.parseDouble(nbStr);
                    return TOK_NUMBER.setValue(numberValue);
                }
            } catch (NumberFormatException e) {
                throw exception.set("invalid number '" + nbStr + "'", begin);
            }
        } else if (('a' <= c && c <= 'z') ||
                   ('A' <= c && c <= 'Z')) {
            do {
                c = input[p++];
            } while (('a' <= c && c <= 'z') ||
                     ('A' <= c && c <= 'Z') ||
                     ('0' <= c && c <= '9'));
            String nameValue = String.valueOf(input, begin, p-1-begin);
            while (WHITESPACE.indexOf(c) != -1) {
                c = input[p++];
            }
            if (c == '(') {
                pos = p;
                return (new Token(CALL, 0, Token.PREFIX, 0)).setAlpha(nameValue);
            } else {
                pos = p-1;                
                return TOK_CONST.setAlpha(nameValue);
            }
        } else if ((c >= '\u0391' && c <= '\u03a9') || (c >= '\u03b1' && c <= '\u03c9')) {
            return TOK_CONST.setAlpha(""+c);
        } else { 
            switch (c) {
            case '^':
                return TOK_POWER;
            case '\u00d7':
                return TOK_MUL;
            case '\u00f7':
                return TOK_DIV;
            case '\u2212':
                return TOK_SUB;
            case '\u221a':
                return TOK_SQRT;
            default:
                throw exception.set("invalid character '" + c + "'", begin); 
            }
        }
    }

Here is the call graph for this function:

Definition at line 94 of file Lexer.java.

                                             {
        while (WHITESPACE.indexOf(input[pos]) != -1) {
            ++pos;
        }

        char c = input[pos];
        int begin = pos++;

        switch (c) {
        case '!': return TOK_FACT;
        case END_MARKER: return TOK_END;
        case '%': return TOK_MOD;
        case '(': return TOK_LPAREN;
        case ')': return TOK_RPAREN;
        case '*': return TOK_MUL;
        case '+': return TOK_ADD;
        case ',': return TOK_COMMA;
        case '-': return TOK_SUB;
        case '/': return TOK_DIV;
        }
        if (c == '^') { 
            return TOK_POWER;
        }

        int p  = pos;
        if (('0' <= c && c <= '9') || c == '.') {
            while (('0' <= c && c <= '9') || c == '.' || c == 'E' || c == 'e') {
                if ((c == 'E' || c == 'e') && input[p] == '-') { //accept '-' only after E
                    ++p; 
                }
                c = input[p++];
            } 
            pos = p-1;
            String nbStr = String.valueOf(input, begin, p-1-begin);
            try {
                // parse single dot as 0
                if (nbStr.equals(".")) {
                    return TOK_NUMBER.setValue(0);
                } else {
                    double numberValue = Double.parseDouble(nbStr);
                    return TOK_NUMBER.setValue(numberValue);
                }
            } catch (NumberFormatException e) {
                throw exception.set("invalid number '" + nbStr + "'", begin);
            }
        } else if (('a' <= c && c <= 'z') ||
                   ('A' <= c && c <= 'Z')) {
            do {
                c = input[p++];
            } while (('a' <= c && c <= 'z') ||
                     ('A' <= c && c <= 'Z') ||
                     ('0' <= c && c <= '9'));
            String nameValue = String.valueOf(input, begin, p-1-begin);
            while (WHITESPACE.indexOf(c) != -1) {
                c = input[p++];
            }
            if (c == '(') {
                pos = p;
                return (new Token(CALL, 0, Token.PREFIX, 0)).setAlpha(nameValue);
            } else {
                pos = p-1;                
                return TOK_CONST.setAlpha(nameValue);
            }
        } else if ((c >= '\u0391' && c <= '\u03a9') || (c >= '\u03b1' && c <= '\u03c9')) {
            return TOK_CONST.setAlpha(""+c);
        } else { 
            switch (c) {
            case '^':
                return TOK_POWER;
            case '\u00d7':
                return TOK_MUL;
            case '\u00f7':
                return TOK_DIV;
            case '\u2212':
                return TOK_SUB;
            case '\u221a':
                return TOK_SQRT;
            default:
                throw exception.set("invalid character '" + c + "'", begin); 
            }
        }
    }

Here is the call graph for this function:

Here is the caller graph for this function:

void org.javia.arity.Lexer.scan ( String  str,
TokenConsumer  consumer 
) throws SyntaxException [inline, package]

Definition at line 71 of file Lexer.java.

                                                                         {
        init(str);
        consumer.start();
        Token token;
        do {
            int savePos = pos;
            token = nextToken();
            token.position = savePos;
            consumer.push(token);
        } while (token != TOK_END);
    }

Here is the call graph for this function:

void org.javia.arity.Lexer.scan ( String  str,
TokenConsumer  consumer 
) throws SyntaxException [inline, package]

Definition at line 71 of file Lexer.java.

                                                                         {
        init(str);
        consumer.start();
        Token token;
        do {
            int savePos = pos;
            token = nextToken();
            token.position = savePos;
            consumer.push(token);
        } while (token != TOK_END);
    }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

static final int org.javia.arity.Lexer.ADD = 1 [static, package]

Definition at line 21 of file Lexer.java.

static final int org.javia.arity.Lexer.CALL = 11 [static, package]

Definition at line 31 of file Lexer.java.

static final int org.javia.arity.Lexer.COMMA = 12 [static, package]

Definition at line 32 of file Lexer.java.

static final int org.javia.arity.Lexer.CONST = 10 [static, package]

Definition at line 30 of file Lexer.java.

static final int org.javia.arity.Lexer.DIV = 4 [static, package]

Definition at line 24 of file Lexer.java.

static final int org.javia.arity.Lexer.END = 15 [static, package]

Definition at line 35 of file Lexer.java.

static final char org.javia.arity.Lexer.END_MARKER = '$' [static, private]

Definition at line 61 of file Lexer.java.

Definition at line 64 of file Lexer.java.

static final int org.javia.arity.Lexer.FACT = 8 [static, package]

Definition at line 28 of file Lexer.java.

char [] org.javia.arity.Lexer.input = new char[32] [private]

Definition at line 62 of file Lexer.java.

static final int org.javia.arity.Lexer.LPAREN = 13 [static, package]

Definition at line 33 of file Lexer.java.

static final int org.javia.arity.Lexer.MOD = 5 [static, package]

Definition at line 25 of file Lexer.java.

static final int org.javia.arity.Lexer.MUL = 3 [static, package]

Definition at line 23 of file Lexer.java.

static final int org.javia.arity.Lexer.NUMBER = 9 [static, package]

Definition at line 29 of file Lexer.java.

Definition at line 63 of file Lexer.java.

static final int org.javia.arity.Lexer.POWER = 7 [static, package]

Definition at line 27 of file Lexer.java.

static final int org.javia.arity.Lexer.RPAREN = 14 [static, package]

Definition at line 34 of file Lexer.java.

static final int org.javia.arity.Lexer.SQRT = 16 [static, package]

Definition at line 36 of file Lexer.java.

static final int org.javia.arity.Lexer.SUB = 2 [static, package]

Definition at line 22 of file Lexer.java.

static final Token org.javia.arity.Lexer.TOK_ADD = new Token(ADD, 3, Token.LEFT, VM.ADD) [static, package]

Definition at line 39 of file Lexer.java.

static final Token org.javia.arity.Lexer.TOK_COMMA = new Token(COMMA, 1, 0, 0) [static, package]

Definition at line 54 of file Lexer.java.

static final Token org.javia.arity.Lexer.TOK_CONST = new Token(CONST, 20, 0, 0) [static, package]

Definition at line 58 of file Lexer.java.

static final Token org.javia.arity.Lexer.TOK_DIV = new Token(DIV, 4, Token.LEFT, VM.DIV) [static, package]

Definition at line 43 of file Lexer.java.

static final Token org.javia.arity.Lexer.TOK_END = new Token(END, 0, 0, 0) [static, package]

Definition at line 55 of file Lexer.java.

static final Token org.javia.arity.Lexer.TOK_FACT = new Token(FACT, 7, Token.SUFIX, VM.FACT) [static, package]

Definition at line 49 of file Lexer.java.

static final Token org.javia.arity.Lexer.TOK_LPAREN = new Token(LPAREN, 1, Token.PREFIX, 0) [static, package]

Definition at line 52 of file Lexer.java.

static final Token org.javia.arity.Lexer.TOK_MOD = new Token(MOD, 4, Token.LEFT, VM.MOD) [static, package]

Definition at line 44 of file Lexer.java.

static final Token org.javia.arity.Lexer.TOK_MUL = new Token(MUL, 4, Token.LEFT, VM.MUL) [static, package]

Definition at line 42 of file Lexer.java.

static final Token org.javia.arity.Lexer.TOK_NUMBER = new Token(NUMBER, 20, 0, 0) [static, package]

Definition at line 57 of file Lexer.java.

static final Token org.javia.arity.Lexer.TOK_POWER = new Token(POWER, 6, Token.RIGHT, VM.POWER) [static, package]

Definition at line 48 of file Lexer.java.

static final Token org.javia.arity.Lexer.TOK_RPAREN = new Token(RPAREN, 2, 0, 0) [static, package]

Definition at line 53 of file Lexer.java.

static final Token org.javia.arity.Lexer.TOK_SQRT = new Token(SQRT, 8, Token.PREFIX, VM.SQRT) [static, package]

Definition at line 50 of file Lexer.java.

static final Token org.javia.arity.Lexer.TOK_SUB = new Token(SUB, 3, Token.LEFT, VM.SUB) [static, package]

Definition at line 40 of file Lexer.java.

static final Token org.javia.arity.Lexer.TOK_UMIN = new Token(UMIN, 5, Token.PREFIX, VM.UMIN) [static, package]

Definition at line 46 of file Lexer.java.

static final int org.javia.arity.Lexer.UMIN = 6 [static, package]

Definition at line 26 of file Lexer.java.

static final String org.javia.arity.Lexer.WHITESPACE = " \n\r\t" [static, private]

Definition at line 60 of file Lexer.java.


The documentation for this class was generated from the following files: