]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.spreadsheet.common/src/org/simantics/spreadsheet/common/expression/lexer/Lexer.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.spreadsheet.common / src / org / simantics / spreadsheet / common / expression / lexer / Lexer.java
diff --git a/bundles/org.simantics.spreadsheet.common/src/org/simantics/spreadsheet/common/expression/lexer/Lexer.java b/bundles/org.simantics.spreadsheet.common/src/org/simantics/spreadsheet/common/expression/lexer/Lexer.java
new file mode 100644 (file)
index 0000000..332b587
--- /dev/null
@@ -0,0 +1,566 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+/* This file was generated by SableCC (http://www.sablecc.org/). */\r
+\r
+package org.simantics.spreadsheet.common.expression.lexer;\r
+\r
+import java.io.*;\r
+import org.simantics.spreadsheet.common.expression.node.*;\r
+\r
+@SuppressWarnings("nls")\r
+public class Lexer\r
+{\r
+    protected Token token;\r
+    protected State state = State.INITIAL;\r
+\r
+    private PushbackReader in;\r
+    private int line;\r
+    private int pos;\r
+    private boolean cr;\r
+    private boolean eof;\r
+    private final StringBuffer text = new StringBuffer();\r
+\r
+    @SuppressWarnings("unused")\r
+    protected void filter() throws LexerException, IOException\r
+    {\r
+        // Do nothing\r
+    }\r
+\r
+    public Lexer(@SuppressWarnings("hiding") PushbackReader in)\r
+    {\r
+        this.in = in;\r
+    }\r
\r
+    public Token peek() throws LexerException, IOException\r
+    {\r
+        while(this.token == null)\r
+        {\r
+            this.token = getToken();\r
+            filter();\r
+        }\r
+\r
+        return this.token;\r
+    }\r
+\r
+    public Token next() throws LexerException, IOException\r
+    {\r
+        while(this.token == null)\r
+        {\r
+            this.token = getToken();\r
+            filter();\r
+        }\r
+\r
+        Token result = this.token;\r
+        this.token = null;\r
+        return result;\r
+    }\r
+\r
+    protected Token getToken() throws IOException, LexerException\r
+    {\r
+        int dfa_state = 0;\r
+\r
+        int start_pos = this.pos;\r
+        int start_line = this.line;\r
+\r
+        int accept_state = -1;\r
+        int accept_token = -1;\r
+        int accept_length = -1;\r
+        int accept_pos = -1;\r
+        int accept_line = -1;\r
+\r
+        @SuppressWarnings("hiding") int[][][] gotoTable = Lexer.gotoTable[this.state.id()];\r
+        @SuppressWarnings("hiding") int[] accept = Lexer.accept[this.state.id()];\r
+        this.text.setLength(0);\r
+\r
+        while(true)\r
+        {\r
+            int c = getChar();\r
+\r
+            if(c != -1)\r
+            {\r
+                switch(c)\r
+                {\r
+                case 10:\r
+                    if(this.cr)\r
+                    {\r
+                        this.cr = false;\r
+                    }\r
+                    else\r
+                    {\r
+                        this.line++;\r
+                        this.pos = 0;\r
+                    }\r
+                    break;\r
+                case 13:\r
+                    this.line++;\r
+                    this.pos = 0;\r
+                    this.cr = true;\r
+                    break;\r
+                default:\r
+                    this.pos++;\r
+                    this.cr = false;\r
+                    break;\r
+                }\r
+\r
+                this.text.append((char) c);\r
+\r
+                do\r
+                {\r
+                    int oldState = (dfa_state < -1) ? (-2 -dfa_state) : dfa_state;\r
+\r
+                    dfa_state = -1;\r
+\r
+                    int[][] tmp1 =  gotoTable[oldState];\r
+                    int low = 0;\r
+                    int high = tmp1.length - 1;\r
+\r
+                    while(low <= high)\r
+                    {\r
+                        int middle = (low + high) / 2;\r
+                        int[] tmp2 = tmp1[middle];\r
+\r
+                        if(c < tmp2[0])\r
+                        {\r
+                            high = middle - 1;\r
+                        }\r
+                        else if(c > tmp2[1])\r
+                        {\r
+                            low = middle + 1;\r
+                        }\r
+                        else\r
+                        {\r
+                            dfa_state = tmp2[2];\r
+                            break;\r
+                        }\r
+                    }\r
+                }while(dfa_state < -1);\r
+            }\r
+            else\r
+            {\r
+                dfa_state = -1;\r
+            }\r
+\r
+            if(dfa_state >= 0)\r
+            {\r
+                if(accept[dfa_state] != -1)\r
+                {\r
+                    accept_state = dfa_state;\r
+                    accept_token = accept[dfa_state];\r
+                    accept_length = this.text.length();\r
+                    accept_pos = this.pos;\r
+                    accept_line = this.line;\r
+                }\r
+            }\r
+            else\r
+            {\r
+                if(accept_state != -1)\r
+                {\r
+                    switch(accept_token)\r
+                    {\r
+                    case 0:\r
+                        {\r
+                            @SuppressWarnings("hiding") Token token = new0(\r
+                                start_line + 1,\r
+                                start_pos + 1);\r
+                            pushBack(accept_length);\r
+                            this.pos = accept_pos;\r
+                            this.line = accept_line;\r
+                            return token;\r
+                        }\r
+                    case 1:\r
+                        {\r
+                            @SuppressWarnings("hiding") Token token = new1(\r
+                                start_line + 1,\r
+                                start_pos + 1);\r
+                            pushBack(accept_length);\r
+                            this.pos = accept_pos;\r
+                            this.line = accept_line;\r
+                            return token;\r
+                        }\r
+                    case 2:\r
+                        {\r
+                            @SuppressWarnings("hiding") Token token = new2(\r
+                                start_line + 1,\r
+                                start_pos + 1);\r
+                            pushBack(accept_length);\r
+                            this.pos = accept_pos;\r
+                            this.line = accept_line;\r
+                            return token;\r
+                        }\r
+                    case 3:\r
+                        {\r
+                            @SuppressWarnings("hiding") Token token = new3(\r
+                                start_line + 1,\r
+                                start_pos + 1);\r
+                            pushBack(accept_length);\r
+                            this.pos = accept_pos;\r
+                            this.line = accept_line;\r
+                            return token;\r
+                        }\r
+                    case 4:\r
+                        {\r
+                            @SuppressWarnings("hiding") Token token = new4(\r
+                                start_line + 1,\r
+                                start_pos + 1);\r
+                            pushBack(accept_length);\r
+                            this.pos = accept_pos;\r
+                            this.line = accept_line;\r
+                            return token;\r
+                        }\r
+                    case 5:\r
+                        {\r
+                            @SuppressWarnings("hiding") Token token = new5(\r
+                                start_line + 1,\r
+                                start_pos + 1);\r
+                            pushBack(accept_length);\r
+                            this.pos = accept_pos;\r
+                            this.line = accept_line;\r
+                            return token;\r
+                        }\r
+                    case 6:\r
+                        {\r
+                            @SuppressWarnings("hiding") Token token = new6(\r
+                                start_line + 1,\r
+                                start_pos + 1);\r
+                            pushBack(accept_length);\r
+                            this.pos = accept_pos;\r
+                            this.line = accept_line;\r
+                            return token;\r
+                        }\r
+                    case 7:\r
+                        {\r
+                            @SuppressWarnings("hiding") Token token = new7(\r
+                                start_line + 1,\r
+                                start_pos + 1);\r
+                            pushBack(accept_length);\r
+                            this.pos = accept_pos;\r
+                            this.line = accept_line;\r
+                            return token;\r
+                        }\r
+                    case 8:\r
+                        {\r
+                            @SuppressWarnings("hiding") Token token = new8(\r
+                                start_line + 1,\r
+                                start_pos + 1);\r
+                            pushBack(accept_length);\r
+                            this.pos = accept_pos;\r
+                            this.line = accept_line;\r
+                            return token;\r
+                        }\r
+                    case 9:\r
+                        {\r
+                            @SuppressWarnings("hiding") Token token = new9(\r
+                                start_line + 1,\r
+                                start_pos + 1);\r
+                            pushBack(accept_length);\r
+                            this.pos = accept_pos;\r
+                            this.line = accept_line;\r
+                            return token;\r
+                        }\r
+                    case 10:\r
+                        {\r
+                            @SuppressWarnings("hiding") Token token = new10(\r
+                                start_line + 1,\r
+                                start_pos + 1);\r
+                            pushBack(accept_length);\r
+                            this.pos = accept_pos;\r
+                            this.line = accept_line;\r
+                            return token;\r
+                        }\r
+                    case 11:\r
+                        {\r
+                            @SuppressWarnings("hiding") Token token = new11(\r
+                                start_line + 1,\r
+                                start_pos + 1);\r
+                            pushBack(accept_length);\r
+                            this.pos = accept_pos;\r
+                            this.line = accept_line;\r
+                            return token;\r
+                        }\r
+                    case 12:\r
+                        {\r
+                            @SuppressWarnings("hiding") Token token = new12(\r
+                                getText(accept_length),\r
+                                start_line + 1,\r
+                                start_pos + 1);\r
+                            pushBack(accept_length);\r
+                            this.pos = accept_pos;\r
+                            this.line = accept_line;\r
+                            return token;\r
+                        }\r
+                    case 13:\r
+                        {\r
+                            @SuppressWarnings("hiding") Token token = new13(\r
+                                getText(accept_length),\r
+                                start_line + 1,\r
+                                start_pos + 1);\r
+                            pushBack(accept_length);\r
+                            this.pos = accept_pos;\r
+                            this.line = accept_line;\r
+                            return token;\r
+                        }\r
+                    case 14:\r
+                        {\r
+                            @SuppressWarnings("hiding") Token token = new14(\r
+                                getText(accept_length),\r
+                                start_line + 1,\r
+                                start_pos + 1);\r
+                            pushBack(accept_length);\r
+                            this.pos = accept_pos;\r
+                            this.line = accept_line;\r
+                            return token;\r
+                        }\r
+                    case 15:\r
+                        {\r
+                            @SuppressWarnings("hiding") Token token = new15(\r
+                                getText(accept_length),\r
+                                start_line + 1,\r
+                                start_pos + 1);\r
+                            pushBack(accept_length);\r
+                            this.pos = accept_pos;\r
+                            this.line = accept_line;\r
+                            return token;\r
+                        }\r
+                    case 16:\r
+                        {\r
+                            @SuppressWarnings("hiding") Token token = new16(\r
+                                getText(accept_length),\r
+                                start_line + 1,\r
+                                start_pos + 1);\r
+                            pushBack(accept_length);\r
+                            this.pos = accept_pos;\r
+                            this.line = accept_line;\r
+                            return token;\r
+                        }\r
+                    case 17:\r
+                        {\r
+                            @SuppressWarnings("hiding") Token token = new17(\r
+                                getText(accept_length),\r
+                                start_line + 1,\r
+                                start_pos + 1);\r
+                            pushBack(accept_length);\r
+                            this.pos = accept_pos;\r
+                            this.line = accept_line;\r
+                            return token;\r
+                        }\r
+                    }\r
+                }\r
+                else\r
+                {\r
+                    if(this.text.length() > 0)\r
+                    {\r
+                        throw new LexerException(\r
+                            "[" + (start_line + 1) + "," + (start_pos + 1) + "]" +\r
+                            " Unknown token: " + this.text);\r
+                    }\r
+\r
+                    @SuppressWarnings("hiding") EOF token = new EOF(\r
+                        start_line + 1,\r
+                        start_pos + 1);\r
+                    return token;\r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
+    Token new0(@SuppressWarnings("hiding") int line, @SuppressWarnings("hiding") int pos) { return new TLessThan(line, pos); }\r
+    Token new1(@SuppressWarnings("hiding") int line, @SuppressWarnings("hiding") int pos) { return new TGreaterThan(line, pos); }\r
+    Token new2(@SuppressWarnings("hiding") int line, @SuppressWarnings("hiding") int pos) { return new TEqual(line, pos); }\r
+    Token new3(@SuppressWarnings("hiding") int line, @SuppressWarnings("hiding") int pos) { return new TPlus(line, pos); }\r
+    Token new4(@SuppressWarnings("hiding") int line, @SuppressWarnings("hiding") int pos) { return new TMinus(line, pos); }\r
+    Token new5(@SuppressWarnings("hiding") int line, @SuppressWarnings("hiding") int pos) { return new TMult(line, pos); }\r
+    Token new6(@SuppressWarnings("hiding") int line, @SuppressWarnings("hiding") int pos) { return new TDiv(line, pos); }\r
+    Token new7(@SuppressWarnings("hiding") int line, @SuppressWarnings("hiding") int pos) { return new TAmpersand(line, pos); }\r
+    Token new8(@SuppressWarnings("hiding") int line, @SuppressWarnings("hiding") int pos) { return new TComma(line, pos); }\r
+    Token new9(@SuppressWarnings("hiding") int line, @SuppressWarnings("hiding") int pos) { return new TColon(line, pos); }\r
+    Token new10(@SuppressWarnings("hiding") int line, @SuppressWarnings("hiding") int pos) { return new TLPar(line, pos); }\r
+    Token new11(@SuppressWarnings("hiding") int line, @SuppressWarnings("hiding") int pos) { return new TRPar(line, pos); }\r
+    Token new12(@SuppressWarnings("hiding") String text, @SuppressWarnings("hiding") int line, @SuppressWarnings("hiding") int pos) { return new TFunc(text, line, pos); }\r
+    Token new13(@SuppressWarnings("hiding") String text, @SuppressWarnings("hiding") int line, @SuppressWarnings("hiding") int pos) { return new TCell(text, line, pos); }\r
+    Token new14(@SuppressWarnings("hiding") String text, @SuppressWarnings("hiding") int line, @SuppressWarnings("hiding") int pos) { return new TNumber(text, line, pos); }\r
+    Token new15(@SuppressWarnings("hiding") String text, @SuppressWarnings("hiding") int line, @SuppressWarnings("hiding") int pos) { return new TString(text, line, pos); }\r
+    Token new16(@SuppressWarnings("hiding") String text, @SuppressWarnings("hiding") int line, @SuppressWarnings("hiding") int pos) { return new TNewLine(text, line, pos); }\r
+    Token new17(@SuppressWarnings("hiding") String text, @SuppressWarnings("hiding") int line, @SuppressWarnings("hiding") int pos) { return new TBlank(text, line, pos); }\r
+\r
+    private int getChar() throws IOException\r
+    {\r
+        if(this.eof)\r
+        {\r
+            return -1;\r
+        }\r
+\r
+        int result = this.in.read();\r
+\r
+        if(result == -1)\r
+        {\r
+            this.eof = true;\r
+        }\r
+\r
+        return result;\r
+    }\r
+\r
+    private void pushBack(int acceptLength) throws IOException\r
+    {\r
+        int length = this.text.length();\r
+        for(int i = length - 1; i >= acceptLength; i--)\r
+        {\r
+            this.eof = false;\r
+\r
+            this.in.unread(this.text.charAt(i));\r
+        }\r
+    }\r
+\r
+    protected void unread(@SuppressWarnings("hiding") Token token) throws IOException\r
+    {\r
+        @SuppressWarnings("hiding") String text = token.getText();\r
+        int length = text.length();\r
+\r
+        for(int i = length - 1; i >= 0; i--)\r
+        {\r
+            this.eof = false;\r
+\r
+            this.in.unread(text.charAt(i));\r
+        }\r
+\r
+        this.pos = token.getPos() - 1;\r
+        this.line = token.getLine() - 1;\r
+    }\r
+\r
+    private String getText(int acceptLength)\r
+    {\r
+        StringBuffer s = new StringBuffer(acceptLength);\r
+        for(int i = 0; i < acceptLength; i++)\r
+        {\r
+            s.append(this.text.charAt(i));\r
+        }\r
+\r
+        return s.toString();\r
+    }\r
+\r
+    private static int[][][][] gotoTable;\r
+/*  {\r
+        { // INITIAL\r
+            {{10, 10, 1}, {13, 13, 2}, {32, 32, 3}, {34, 34, 4}, {38, 38, 5}, {39, 39, 6}, {40, 40, 7}, {41, 41, 8}, {42, 42, 9}, {43, 43, 10}, {44, 44, 11}, {45, 45, 12}, {47, 47, 13}, {48, 57, 14}, {58, 58, 15}, {60, 60, 16}, {61, 61, 17}, {62, 62, 18}, {65, 90, 19}, {95, 95, 20}, {97, 122, 19}, },\r
+            {},\r
+            {{10, 10, 21}, },\r
+            {{32, 32, 3}, },\r
+            {{32, 33, 22}, {34, 34, 23}, {35, 38, 22}, {39, 39, 24}, {40, 91, 22}, {93, 127, 22}, },\r
+            {},\r
+            {{32, 33, 25}, {34, 34, 26}, {35, 38, 25}, {39, 39, 27}, {40, 91, 25}, {93, 127, 25}, },\r
+            {},\r
+            {},\r
+            {},\r
+            {},\r
+            {},\r
+            {},\r
+            {},\r
+            {{46, 46, 28}, {48, 57, 29}, },\r
+            {},\r
+            {},\r
+            {},\r
+            {},\r
+            {{40, 40, 30}, {48, 57, 31}, {65, 90, 32}, {97, 122, 32}, },\r
+            {{65, 90, 33}, {97, 122, 33}, },\r
+            {},\r
+            {{32, 127, -6}, },\r
+            {},\r
+            {{32, 127, -6}, },\r
+            {{32, 127, -8}, },\r
+            {{32, 127, -8}, },\r
+            {},\r
+            {{46, 46, 28}, {48, 57, 34}, },\r
+            {{46, 57, -16}, },\r
+            {},\r
+            {{40, 57, -21}, {65, 90, 35}, {97, 122, 35}, },\r
+            {{40, 122, -21}, },\r
+            {{48, 57, 36}, {65, 122, -22}, },\r
+            {{48, 57, 34}, },\r
+            {{40, 40, 30}, {48, 57, 37}, {65, 122, -33}, },\r
+            {{48, 57, 36}, },\r
+            {{40, 122, -37}, },\r
+        }\r
+    };*/\r
+\r
+    private static int[][] accept;\r
+/*  {\r
+        // INITIAL\r
+        {17, 16, 16, 17, -1, 7, -1, 10, 11, 5, 3, 8, 4, 6, 14, 9, 0, 2, 1, -1, -1, 16, -1, 15, -1, -1, -1, 15, 14, 14, 12, 13, -1, -1, 14, -1, 13, -1, },\r
+\r
+    };*/\r
+\r
+    public static class State\r
+    {\r
+        public final static State INITIAL = new State(0);\r
+\r
+        private int id;\r
+\r
+        private State(@SuppressWarnings("hiding") int id)\r
+        {\r
+            this.id = id;\r
+        }\r
+\r
+        public int id()\r
+        {\r
+            return this.id;\r
+        }\r
+    }\r
+\r
+    static \r
+    {\r
+        try\r
+        {\r
+            DataInputStream s = new DataInputStream(\r
+                new BufferedInputStream(\r
+                Lexer.class.getResourceAsStream("lexer.dat")));\r
+\r
+            // read gotoTable\r
+            int length = s.readInt();\r
+            gotoTable = new int[length][][][];\r
+            for(int i = 0; i < gotoTable.length; i++)\r
+            {\r
+                length = s.readInt();\r
+                gotoTable[i] = new int[length][][];\r
+                for(int j = 0; j < gotoTable[i].length; j++)\r
+                {\r
+                    length = s.readInt();\r
+                    gotoTable[i][j] = new int[length][3];\r
+                    for(int k = 0; k < gotoTable[i][j].length; k++)\r
+                    {\r
+                        for(int l = 0; l < 3; l++)\r
+                        {\r
+                            gotoTable[i][j][k][l] = s.readInt();\r
+                        }\r
+                    }\r
+                }\r
+            }\r
+\r
+            // read accept\r
+            length = s.readInt();\r
+            accept = new int[length][];\r
+            for(int i = 0; i < accept.length; i++)\r
+            {\r
+                length = s.readInt();\r
+                accept[i] = new int[length];\r
+                for(int j = 0; j < accept[i].length; j++)\r
+                {\r
+                    accept[i][j] = s.readInt();\r
+                }\r
+            }\r
+\r
+            s.close();\r
+        }\r
+        catch(Exception e)\r
+        {\r
+            throw new RuntimeException("The file \"lexer.dat\" is either missing or corrupted.");\r
+        }\r
+    }\r
+}\r