lexer.txt

来自「有关编译器的编译器.」· 文本 代码 · 共 424 行

TXT
424
字号
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * This file is part of SableCC.                             * * See the file "LICENSE" for copyright information and the  * * terms and conditions for copying, distribution and        * * modification of SableCC.                                  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */Macro:LexerException/* This file was generated by SableCC (http://www.sablecc.org/). */package $0$;public class LexerException extends Exception{    public LexerException(String message)    {        super(message);    }}$Macro:LexerHeader/* This file was generated by SableCC (http://www.sablecc.org/). */package $0$;import java.io.*;import java.util.*;import $1$.*;public class Lexer{    protected Token token;    protected State state = State.$2$;    private PushbackReader in;    private int line;    private int pos;    private boolean cr;    private boolean eof;    private final StringBuffer text = new StringBuffer();    protected void filter() throws LexerException, IOException    {    }    public Lexer(PushbackReader in)    {        this.in = in;        if(gotoTable == null)        {            try            {                DataInputStream s = new DataInputStream(                    new BufferedInputStream(                    Lexer.class.getResourceAsStream("lexer.dat")));                // read gotoTable                int length = s.readInt();                gotoTable = new int[length][][][];                for(int i = 0; i < gotoTable.length; i++)                {                    length = s.readInt();                    gotoTable[i] = new int[length][][];                    for(int j = 0; j < gotoTable[i].length; j++)                    {                        length = s.readInt();                        gotoTable[i][j] = new int[length][3];                        for(int k = 0; k < gotoTable[i][j].length; k++)                        {                            for(int l = 0; l < 3; l++)                            {                                gotoTable[i][j][k][l] = s.readInt();                            }                        }                    }                }                // read accept                length = s.readInt();                accept = new int[length][];                for(int i = 0; i < accept.length; i++)                {                    length = s.readInt();                    accept[i] = new int[length];                    for(int j = 0; j < accept[i].length; j++)                    {                        accept[i][j] = s.readInt();                    }                }                s.close();            }            catch(Exception e)            {                throw new RuntimeException("The file \"lexer.dat\" is either missing or corrupted.");            }        }    }    public Token peek() throws LexerException, IOException    {        while(token == null)        {            token = getToken();            filter();        }        return token;    }    public Token next() throws LexerException, IOException    {        while(token == null)        {            token = getToken();            filter();        }        Token result = token;        token = null;        return result;    }    protected Token getToken() throws IOException, LexerException    {        int dfa_state = 0;        int start_pos = pos;        int start_line = line;        int accept_state = -1;        int accept_token = -1;        int accept_length = -1;        int accept_pos = -1;        int accept_line = -1;        int[][][] gotoTable = this.gotoTable[state.id()];        int[] accept = this.accept[state.id()];        text.setLength(0);        while(true)        {            int c = getChar();            if(c != -1)            {                switch(c)                {                case 10:                    if(cr)                    {                        cr = false;                    }                    else                    {                        line++;                        pos = 0;                    }                    break;                case 13:                    line++;                    pos = 0;                    cr = true;                    break;                default:                    pos++;                    cr = false;                    break;                };                text.append((char) c);                do                {                    int oldState = (dfa_state < -1) ? (-2 -dfa_state) : dfa_state;                    dfa_state = -1;                    int[][] tmp1 =  gotoTable[oldState];                    int low = 0;                    int high = tmp1.length - 1;                    while(low <= high)                    {                        int middle = (low + high) / 2;                        int[] tmp2 = tmp1[middle];                        if(c < tmp2[0])                        {                            high = middle - 1;                        }                        else if(c > tmp2[1])                        {                            low = middle + 1;                        }                        else                        {                            dfa_state = tmp2[2];                            break;                        }                    }                }while(dfa_state < -1);            }            else            {                dfa_state = -1;            }            if(dfa_state >= 0)            {                if(accept[dfa_state] != -1)                {                    accept_state = dfa_state;                    accept_token = accept[dfa_state];                    accept_length = text.length();                    accept_pos = pos;                    accept_line = line;                }            }            else            {                if(accept_state != -1)                {                    switch(accept_token)                    {$Macro:LexerVariableToken                    case $0$:                        {                            Token token = new$0$(                                getText(accept_length),                                start_line + 1,                                start_pos + 1);                            pushBack(accept_length);                            pos = accept_pos;                            line = accept_line;$Macro:LexerFixedToken                    case $0$:                        {                            Token token = new$0$(                                start_line + 1,                                start_pos + 1);                            pushBack(accept_length);                            pos = accept_pos;                            line = accept_line;$Macro:TokenSwitchHeader                            switch(state.id())                            {$Macro:TokenCase                                case $0$: state = State.$1$; break;$Macro:TokenSwitchTail                            }$Macro:LexerTokenTail                            return token;                        }$Macro:LexerBody1                    }                }                else                {                    if(text.length() > 0)                    {                        throw new LexerException(                            "[" + (start_line + 1) + "," + (start_pos + 1) + "]" +                            " Unknown token: " + text);                    }                    else                    {                        EOF token = new EOF(                            start_line + 1,                            start_pos + 1);                        return token;                    }                }            }        }    }$Macro:LexerNewVariableToken    Token new$0$(String text, int line, int pos) { return new $1$(text, line, pos); }$Macro:LexerNewFixedToken    Token new$0$(int line, int pos) { return new $1$(line, pos); }$Macro:LexerBody2    private int getChar() throws IOException    {        if(eof)        {            return -1;        }        int result = in.read();        if(result == -1)        {            eof = true;        }        return result;    }    private void pushBack(int acceptLength) throws IOException    {        int length = text.length();        for(int i = length - 1; i >= acceptLength; i--)        {            eof = false;            in.unread(text.charAt(i));        }    }    protected void unread(Token token) throws IOException    {        String text = token.getText();        int length = text.length();        for(int i = length - 1; i >= 0; i--)        {            eof = false;            in.unread(text.charAt(i));        }        pos = token.getPos() - 1;        line = token.getLine() - 1;    }    private String getText(int acceptLength)    {        StringBuffer s = new StringBuffer(acceptLength);        for(int i = 0; i < acceptLength; i++)        {            s.append(text.charAt(i));        }        return s.toString();    }    private static int[][][][] gotoTable;/*  {$Macro:LexerAcceptHeader    };*/    private static int[][] accept;/*  {$Macro:LexerAcceptTail    };*/$Macro:LexerStateHeader    public static class State    {$Macro:LexerStateBody        public final static State $0$ = new State($1$);$Macro:LexerStateTail        private int id;        private State(int id)        {            this.id = id;        }        public int id()        {            return id;        }    }$Macro:LexerTail}$

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?