📄 parser.java
字号:
if (!then){ if (val.intValue()!=0) { getNextToken(); while((tok.ttype!=LexAnn.TT_EIF)&& (tok.ttype!=LexAnn.TT_ELSE)&& (tok.ttype!=LexAnn.TT_EOF)) { //run the body of the if parseStmt(); getNextToken(); } if (tok.ttype==LexAnn.TT_ELSE) { //skip else clause - //have to do this taking into acount nesting depth=1; do { getNextToken(); if (tok.ttype==LexAnn.TT_IF) depth++; if (tok.ttype==LexAnn.TT_EOF) parseError("can't find endif"); if (tok.ttype==LexAnn.TT_EIF) depth--; //A then could indicate a one line //if - then construct, then we don't increment //depth if (tok.ttype==LexAnn.TT_THEN) { getNextToken(); if (tok.ttype!=LexAnn.TT_EOL){ depth--; } tok.pushBack(); } } while(depth>0); getNextToken(); } else { getNextToken(); } } else { //skip to else clause depth=1; do { getNextToken(); if (tok.ttype==LexAnn.TT_IF )depth++; if (tok.ttype==LexAnn.TT_EOF) parseError("can't find endif"); if ((tok.ttype==LexAnn.TT_EIF)) depth--; if (tok.ttype==LexAnn.TT_ELSE && depth==1) depth--; //A then could indicate a one line //if - then construct, then we don't increment //depth if (tok.ttype==LexAnn.TT_THEN) { getNextToken(); if (tok.ttype!=LexAnn.TT_EOF){ depth--; } tok.pushBack(); } } while(depth>0); if (tok.ttype==LexAnn.TT_ELSE) { getNextToken(); getNextToken(); //run else clause while(tok.ttype!=LexAnn.TT_EIF) { parseStmt(); getNextToken(); } getNextToken(); } else { getNextToken(); } } } } private void parseWhile() throws FSException,RetException { //parses the while statement Integer val; int startLine; int endPos; int depth; startLine=code.getCurLine(); getNextToken(); val=(Integer)parseExpr(); getNextToken(); while (val.intValue()!=0) { while((tok.ttype!=LexAnn.TT_EWHILE)&& (tok.ttype!=LexAnn.TT_EOF)){ parseStmt(); getNextToken(); } //reset to start of while loop.... code.setCurLine(startLine); resetTokens(); getNextToken(); //a 'while' you would imagine. val=(Integer)parseExpr(); getNextToken(); } //skip to endwhile depth=1; do { getNextToken(); if (tok.ttype==LexAnn.TT_WHILE) depth++; if (tok.ttype==LexAnn.TT_EWHILE) depth--; if (tok.ttype==LexAnn.TT_EOF) parseError("can't find endwhile"); } while (depth>0); getNextToken(); } private void parseVarDef() throws FSException { Object val; int type=0; String name; val=null; if (tok.ttype==LexAnn.TT_DEFINT) { type=LexAnn.TT_DEFINT; } else if (tok.ttype==LexAnn.TT_DEFSTRING) { type=LexAnn.TT_DEFSTRING; } else if (tok.ttype==LexAnn.TT_DEFDOUBLE) { type=LexAnn.TT_DEFDOUBLE; } else { parseError("Expected 'int','string' or 'double'"); } do { getNextToken(); if (tok.ttype!=LexAnn.TT_WORD) { parseError("Expected variable name identifier,"); } name=(String)tok.value; switch (type){ case LexAnn.TT_DEFINT: { addVar(name,new Integer(0)); break; } case LexAnn.TT_DEFSTRING:{ addVar(name,new String("")); break; } } getNextToken(); if (tok.ttype==LexAnn.TT_EQ){ getNextToken(); setVar(name,parseExpr()); } else if (tok.ttype!=',' && tok.ttype!=LexAnn.TT_EOL) { parseError("Expected ','"); } } while (tok.ttype!=LexAnn.TT_EOL); } //format an error message and throw FSException private void parseError(String s) throws FSException { String t; error=new String[6]; t=tok.toString(); //set up our error block error[0]=s; error[1]=(new Integer(code.getCurLine())).toString(); error[2]=code.getLine(); error[3]=t; error[4]=vars.toString(); if (gVars!=null) error[5]=gVars.toString(); //then build the display string s="\n\t"+s; int l=code.getCurLine(); s=s+ "\n\t\t at line:" + l + " "; s+="\n\t\t\t "+code.getLine(l-2); s+="\n\t\t\t "+code.getLine(l-1); s+="\n\t\t\t> "+code.getLine(l)+" <"; s+="\n\t\t\t "+code.getLine(l+1); s+="\n\t\t\t "+code.getLine(l+2); s=s+ "\n\t\t current token:" + t; s=s+ "\n\t\t Variable dump:" + vars; if (gVars!=null) { s=s+ "\n\t\t Globals:" + gVars; } throw new FSException(s); } //return the error block public String[] getError() { return error; } //Other non SM related routines private boolean pComp(String s) { //a compare for (String)tok.value strings - that avoids the null problem if (tok.value!=null) { return ((String)tok.value).equals(s); } else { return false; } } //misc token access routines private void getNextToken() { if((tok.ttype==LexAnn.TT_EOL) && (code.getCurLine() < maxLine) ) { code.setCurLine(code.getCurLine()+1); tok.setString(code.getLine()); tok.nextToken(); } else if ((tok.ttype==LexAnn.TT_EOL) && (code.getCurLine()>=maxLine)) { tok.ttype=LexAnn.TT_EOF; //the only place this gets set } else { tok.nextToken(); } } private void resetTokens() { tok.setString(code.getLine()); tok.nextToken(); } //variable access routines void addVar(String name,Object value) throws FSException { if (vars.containsKey(name)) { parseError("Already defined in this scope: " + name ); } vars.put(name,value); } Object getVar(String name) { if (vars.containsKey(name)) { return vars.get(name); } else { if (gVars!=null) { if (gVars.containsKey(name)) { return gVars.get(name); } } } return null; //shouldn't get here } void setVar(String name,Object val) throws FSException { Object obj; if (val==null) parseError("set variable "+name+" with null value"); if (vars.containsKey(name)) { obj=vars.get(name); if (val.getClass() != obj.getClass()) { parseError("Incompatible types"); } vars.remove(name); vars.put(name,val); } else if (gVars.containsKey(name)) { obj=gVars.get(name); if (val.getClass() != obj.getClass()) { parseError("Incompatible types"); } gVars.remove(name); gVars.put(name,val); } } boolean hasVar(String name) { if (gVars==null) { return vars.containsKey(name); } else { return vars.containsKey(name) || gVars.containsKey(name); } } //Checks line for correctly formed ( ) and " //this is a little crude (i.e. the rdp should really pick it up) //but it's not all that good about it, hence somewhat kludgy fix private void checkLine(String line) throws FSException{ boolean inQuotes=false; int brCount=0; char chars[]; int n; if (line!=null){ if (!line.trim().startsWith("#")){ chars=line.toCharArray(); for (n=0;n<chars.length;n++){ if (inQuotes){ if (chars[n]=='"'){ if (n>=1){ if (chars[n-1]!='\\'){ inQuotes=false; } } } } else { if (chars[n]=='('){ brCount++; } else if (chars[n]==')'){ brCount--; } else if (chars[n]=='"'){ if (n>=1){ if (chars[n-1]!='\\'){ inQuotes=true; } } } } } if (inQuotes){ parseError("Mismatched quotes"); } if (brCount!=0){ parseError("Mismatched brackets"); } } } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -