⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parse.java

📁 Hecl编程语言是一个高层次的脚本语言的Java实现。其用意是要小
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    parseWord(state);                    break;            }	    addCurrent();        }    }    /**     * The <code>parseComment</code> method keeps reading until a newline,     * this 'eating' the comment.     *     * @param state a <code>ParseState</code> value     */    private void parseComment(ParseState state) {        char ch;        while (true) {            ch = state.nextchar();	    if (ch == '\n') {		state.lineno ++;		return;	    }            if ((ch == '\r') || state.done()) {                return;            }        }    }    /* Various bits and pieces utilized by parseDollar, below.  */    private static String allowed = "_/@:-";    private static String xchars="0123456789ABCDEFabcdef";    private static boolean isLetter(char ch) {	return ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'));    }    private static boolean isDigit(char ch) {	return (ch >= '0' && ch <= '9');    }    private static boolean isXDigit(char ch) {	return xchars.indexOf(ch) >= 0;    }    private static boolean isValidVarPunct(char ch) {	return allowed.indexOf(ch) >= 0;    }    /**     * The <code>parseDollar</code> method parses a $\ foo     * variable. These can also be of the form $\ {foo} so that we can     * separate them from any surrounding text.     *     * @param state a <code>ParseState</code> value     * @exception HeclException if an error occurs     */    private void parseDollar(ParseState state)	throws HeclException {	char ch;	ch = state.nextchar();	if (ch == '{') {	    parseVarBlock(state);	} else {	    /* Variable names use this range here. */	    while((isLetter(ch) || isDigit(ch) || isValidVarPunct(ch))) {		appendToCurrent(ch);		ch = state.nextchar();	    }	    if (!state.done()) {		state.rewind();	    }	}// 	System.out.println("parser vvvv");// 	PrintThing.printThing(new Thing(outBuf));// 	System.out.println("parser ^^^^");	outGroup.setElementAt(new SubstThing(outBuf.getStringRep()),			      outGroup.size() - 1);    }    /**     * <code>parseBlock</code> parses a {} block.     *     * @param state a <code>ParseState</code> value     * @exception HeclException if an error occurs     */    protected void parseBlock(ParseState state) throws HeclException {        parseBlockOrCommand(state, true, false);    }    protected void parseVarBlock(ParseState state) throws HeclException {	parseBlockOrCommand(state, true, true);    }    /**     * <code>parseCommand</code> parses a [] command.     *     * @param state a <code>ParseState</code> value     * @exception HeclException if an error occurs     */    protected void parseCommand(ParseState state) throws HeclException {        parseBlockOrCommand(state, false, false);    }    /**     * <code>parseBlockOrCommand</code> is what parseCommand and parseBlock     * use internally.     *     * @param state a <code>ParseState</code> value     * @param block a <code>boolean</code> value     * @exception HeclException if an error occurs     */    protected void parseBlockOrCommand(ParseState state, boolean block, boolean invar)            throws HeclException {        int level = 1;        char ldelim, rdelim;        char ch;	char lastchar = 0;        if (block == true) {            ldelim = '{';            rdelim = '}';        } else {            ldelim = '[';            rdelim = ']';        }        while (true) {            ch = state.nextchar();            if (state.done()) {		throw new HeclException("Unbalanced " +					(block ? "{}" : "[]"), "PARSE_ERROR");	    }	    	    // || ch == '\r'	    if (ch == '\n') {		state.lineno ++;	    }	    if (block || lastchar != '\\') { 		if (ch == ldelim) {		    level++;		} else if (ch == rdelim) {		    level--;		}	    }            if (level == 0) {                /* It's just a block, return it. */                if (block || parselist) {		    ch = state.nextchar();		    /* If we are not dealing with a variable parse		     * such as $\ {foo}, and the next character		     * isn't a space, we have a problem. */		    if (!invar && ch != ' ' && ch != '	' &&			ch != '\n' && ch != '\r' && ch != ';' && ch != 0) {			throw new HeclException("Extra characters after close-brace");		    }		    state.rewind();                    return;                } else {                    /* We parse it up for later consumption. */		    Parse hp = new Parse(interp, outBuf.getStringRep());                    CodeThing code = hp.parseToCode();                    code.marksubst = true;		    /* Replace outBuf in the vector. */		    outGroup.setElementAt(code, outGroup.size() - 1);                    return;                }            } else {                appendToCurrent(ch);            }	    /* Save the last character viewed. */	    lastchar = ch;        }    }    /**     * <code>parseText</code> parses a "string in quotes".     *     * @param state a <code>ParseState</code> value     * @exception HeclException if an error occurs     */    protected void parseText(ParseState state) throws HeclException {        char ch;        while (true) {            ch = state.nextchar();            if (state.done()) {                return;            }            switch (ch) {                case '"' :		    /* If it's quoted, it must be text. */		    outBufNumeric = false;                    return;                case '\\' :		    parseEscape(state);                    break;                case '[' :                    addCommand();                    break;                case '$' :                    addDollar();                    break;                default :                    appendToCurrent(ch);                    break;            }        }    }    /**     * <code>parseWord</code> parses a regular word not in quotes.     *     * @param state a <code>ParseState</code> value     * @exception HeclException if an error occurs     */    protected void parseWord(ParseState state) throws HeclException {        char ch;        while (true) {            ch = state.nextchar();            if (state.done()) {		return;            }            switch (ch) {	      case '[' :		addCommand();		break;	      case '$' :		addDollar();		break;	      case ' ' :	      case '	' :		return;	      case '\n' :		state.lineno ++;		/* Fall through on purpose. */	      case '\r' :	      case ';' :		state.eoc = true;		return;	      case '\\' :		if (parseEscape(state)) {		    return;		}		break;	      default :		appendToCurrent(ch);		break;            }        }    }    /**     * The <code>parseEscape</code> method parses \n \t style escapes     * - or just prints the next character.     *     * @param state a <code>ParseState</code> value     * @return a <code>boolean</code> value     * @exception HeclException if an error occurs     */    protected boolean parseEscape(ParseState state) throws HeclException {	char ch = state.nextchar();	if (state.done()) {	    return true;	}	/* \n style escapes */	switch (ch) {	  case '\r':	    char ch2 = state.nextchar();	    if (ch2 == '\n') {		return true;	    } else {		state.rewind();	    }	  case '\n':	    return true;	  case 'r':	    appendToCurrent((char)0x0d);	    break;	  case 'n':	    appendToCurrent(eol[0]);//#ifdef j2se	    if (eol.length > 1) {		appendToCurrent(eol[1]);	    }//#endif	    break;	  case 't':	    appendToCurrent('\t');	    break;	  case 'u':	    /* Add unicode sequences. */	    StringBuffer num = new StringBuffer("");	    char nextc;	    for (int i = 0; i < 4; i++) {		nextc = state.nextchar();		if (state.done()) {		    return true;		}		if (!isXDigit(nextc)) {		    state.rewind();		    break;		}		num.append(nextc);	    }	    try {		appendToCurrent((char)Integer.parseInt(num.toString(), 16));	    } catch (NumberFormatException e) {		throw new HeclException("illegal unicode escape: \\u" + num);	    }	    num = null;	    break;	  default:	    appendToCurrent(ch);	    break;	}	return false;    }}

⌨️ 快捷键说明

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