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

📄 jmkeywordparse.java

📁 类javaScript脚本解释器
💻 JAVA
字号:
package MultiScriptParse.Parse;

import java.util.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

class LoopKeyWord {

  static final int CONTINUE = 1;

  static final int BREAK = 2;

  static final int NONE = -1;

  private int KeyWord = NONE;

  public int KeyWord() {
    return KeyWord;
  }

  public void setKeyWord(int v) {
    KeyWord = v;
  }
}

public class JMKeyWordParse extends JMObject {

  static final int CONTINUE = 1;

  static final int BREAK = 2;

  private JMExprParse ExprParse;

  private JMPropParse PropParse;

  public JMKeyWordParse() {

  }

  public JMKeyWordParse(JMParse Owner, JMExprParse ExprParse, JMPropParse PropParse) {
    super(Owner);
    this.ExprParse = ExprParse;
    this.PropParse = PropParse;
  }

  public JMKeyWordParse(JMParse Owner) {
    super(Owner);
  }

  private void DoIterative(char[] Script, int Start) throws JMParseException {
    StringBuffer Token = new StringBuffer(25);
    int symbolType = 0; //0 还未取任何字符 5 已取出元素但未确定 10 已结束Token
    Object oo = null;
    //int Start = CurrPos;
    char ckey = Script[Owner.CurrPos];

    Token.delete(0, 25);

    //循环中主要完成 if for 等后跟括号的分支语句、一切函数、附值语句

    while (ckey != 0) {

      if (ckey < 33) {
        if (ckey == '\n')
          Owner.CurrLine++;
        ckey = Script[++Owner.CurrPos];
        continue;
      }

      if (ckey == '=') {
        if (symbolType == 0)
          throw new JMParseException("不正确的附值语句");
        Owner.CurrPos++;
        oo = ExprParse.ParseExpr(Script);
        if (!JMParse.GlobVarList.AssignToVariable(Token.toString(), oo))
          if (!JMParse.LocalVarList.AssignToVariable(Token.toString(), oo))
            throw new JMParseException(new StringBuffer("未定义的变量 \"").
                                       append(Token).append(" \"").toString());
        return;
      }

      if ( (ckey > 47) && (ckey < 58)) {
        if (symbolType == 0)
          throw new JMParseException(new StringBuffer("不正确的标志符表示法 ").
                                     append(Token).toString());
        Token.append(ckey);
        ckey = Script[++JMParse.CurrPos];
        continue;
      }

      if ( (ckey == 95) || (ckey > 64 && ckey < 91) ||
          (ckey > 96 && ckey < 123)) {
        if (symbolType == 0)
          symbolType = 5;
        Token.append(ckey);
        ckey = Script[++JMParse.CurrPos];
        continue;
      }

      throw new JMParseException(new StringBuffer("使用不正确的标志符 \"").
                                 append(ckey).append("\"").toString());

    }
  }

  public void SkipIf(char[] toSkip)
  throws JMParseException
  {
    StringBuffer key = new StringBuffer(25);
    int StartPos = 0;
    int Bracket = 0;

    //如果有小于33的字符就跳过
    Owner.SkipLess33Char(toSkip);
    char ckey = toSkip[Owner.CurrPos];
    if (ckey == '(')
      Bracket++;
    else
      throw new JMParseException("期盼条件表达式开始符'('");

    //跳过表达式和表达式结束符“)”
    ckey = toSkip[++Owner.CurrPos];
    while (ckey != 0) {

      if (ckey < 33) {
        if (ckey == '\n') Owner.CurrLine++;
        ckey = toSkip[++Owner.CurrPos];
        continue;
      }

      if (ckey == ';' || ckey == '{') {
        if (Bracket != 0)
          throw new JMParseException("条件表达式错误");
      }
      if (ckey == '(')
        Bracket++;
      if (ckey == ')'){
        Bracket--;
        if (Bracket == 0) {
          Owner.CurrPos++; //跳过“)”
          break;
        }
      }
      ckey = toSkip[++Owner.CurrPos];
    }


    //跳过小于33的字符和注释
    Owner.SkipLess33Char(toSkip);
    Owner.SkipRemark(toSkip);

    if (toSkip[Owner.CurrPos] == '{') {
       while (toSkip[Owner.CurrPos] != '}'){
         Owner.SkipOneLan();
         Owner.SkipLess33Char(toSkip);
       }
       Owner.CurrPos++;  //跳过“}”
    } else Owner.SkipOneLan();

    Owner.SkipLess33Char(toSkip);
    key.delete(0, 25);
    ckey = toSkip[Owner.CurrPos];
    StartPos = Owner.CurrPos;
    while (ckey != 0) {
      if ( (ckey == 95) || (ckey > 64 && ckey < 91) ||
          (ckey > 96 && ckey < 123)) {
        key.append(ckey);
        ckey = toSkip[++JMParse.CurrPos];
        continue;
      }
      break;
    }

    if (key.toString().equals("else")) {
      Owner.SkipLess33Char(toSkip);
      if (toSkip[Owner.CurrPos] == '{') {
        while (toSkip[Owner.CurrPos] != '}'){
          Owner.SkipOneLan();
          Owner.SkipLess33Char(toSkip);
        }
        Owner.CurrPos++;  //跳过“}”
      } else Owner.SkipOneLan();
    } else Owner.CurrPos = StartPos;

  }

  public void SkipFor(char[] toSkip) throws JMParseException {
    int Semicolon = 0;
    int Bracket = 1;
    Owner.CurrPos++; //跳过左括号
    char Ckey = toSkip[Owner.CurrPos];

    while (Ckey != 0) {

      if (Ckey == '(') {
        Bracket++;
        Ckey = toSkip[++Owner.CurrPos];
        continue;
      }

      if (Ckey == ';') {
        Semicolon++;
        if (Semicolon > 2 && Bracket > 0)
          throw new JMParseException("丢失循环定义结束符)");
        Ckey = toSkip[++Owner.CurrPos];
        continue;
      }

      if (Ckey == ')') {
        Bracket--;
        if (Bracket == 0)
          break;
        Ckey = toSkip[++Owner.CurrPos];
        continue;
      }

      if (Ckey == '{' || Ckey == '}') {
        if (Bracket != 0 && Semicolon != 2)
          throw new JMParseException("错误的循环定义符)");
      }
      Ckey = toSkip[++Owner.CurrPos];
    }// (Ckey != 0) {

    Owner.CurrPos++;//跳过循环定义结束符)
    Owner.SkipLess33Char(toSkip);

    if (toSkip[Owner.CurrPos] == '{') {
       while (toSkip[Owner.CurrPos] != '}'){
         Owner.SkipOneLan();
         Owner.SkipLess33Char(toSkip);
       }
       Owner.CurrPos++;  //跳过“}”
    } else Owner.SkipOneLan();
  }

  public Object Execif(FuncReturn fr, LoopKeyWord lkw, char[] toParse, boolean ExecJudge)
  throws JMParseException
  {
    Object oo = null;
    int StartPos = 0;
    boolean ExecTrue = false;
    StringBuffer key = new StringBuffer(25);

    //如果有小于33的字符就跳过
    Owner.SkipLess33Char(toParse);

    JMParse.CurrPos++; //跳过(
    oo = ExprParse.ParseExpr(toParse);

    //跳过表达式末尾的)、小于33的字符和注释
    JMParse.CurrPos++; //跳过)
    Owner.SkipLess33Char(toParse);
    Owner.SkipRemark(toParse);

    if (((Boolean)oo).booleanValue()) {
      if (toParse[Owner.CurrPos] == '{') {
        Owner.CurrPos++;//跳过‘{’
        while (toParse[Owner.CurrPos] != '}'){
          oo = Owner.Next(fr, lkw);
          if (fr.Returned())
            return oo;
          Owner.SkipLess33Char(toParse);
        }
        JMParse.CurrPos++; //跳过末尾的}
      } else oo = Owner.Next(fr, lkw);
      if (fr.Returned())
        return oo;
      ExecTrue = true;
    }
    else {
      if (toParse[Owner.CurrPos] == '{') {
        Owner.CurrPos++;//跳过‘{’
        while (toParse[Owner.CurrPos] != '}') {
          Owner.SkipOneLan();
          Owner.SkipLess33Char(toParse);
        }
        JMParse.CurrPos++; //跳过末尾的}
      }
      else
        Owner.SkipOneLan();
    }

    Owner.SkipLess33Char(toParse);
    char ckey = toParse[Owner.CurrPos];
    key.delete(0, 25);
    StartPos = Owner.CurrPos;
    while (ckey != 0) {
      if ( (ckey == 95) || (ckey > 64 && ckey < 91) ||
          (ckey > 96 && ckey < 123)) {
        key.append(ckey);
        ckey = toParse[++JMParse.CurrPos];
        continue;
      }
      break;
    }
    if (key.toString().equals("else")) {
      Owner.SkipLess33Char(toParse);
      if (toParse[Owner.CurrPos] == '{') {
        Owner.CurrPos++;//跳过‘{’
        if (ExecTrue) {
          while (toParse[Owner.CurrPos] != '}') {
            Owner.SkipOneLan();
            Owner.SkipLess33Char(toParse);
          }
        } else {
          while (toParse[Owner.CurrPos] != '}'){
            oo = Owner.Next(fr, lkw);
            if (fr.Returned())
              return oo;
            Owner.SkipLess33Char(toParse);
          }
        }
        JMParse.CurrPos++; //跳过末尾的}
      } else {
        if (ExecTrue)
          Owner.SkipOneLan();
        else
          return Owner.Next(fr, lkw);
      }
    } else Owner.CurrPos = StartPos;

    return null;
  }

  public Object Execfor(FuncReturn fr, char[] toParse)
  throws JMParseException
  {
    Object oo = null;
    int StartPos = Owner.CurrPos;
    int tmp = 0;
    int ConChk = 1000;
    boolean Enter = false;
    StringBuffer key = new StringBuffer(25);
    int Con = Owner.CurrPos;
    int ConLine = Owner.CurrLine;
    int Iterative = Owner.CurrPos;
    int IterativeLine = Owner.CurrLine;
    char ckey;
    LoopKeyWord lkw = new LoopKeyWord();

    //如果有小于33的字符就跳过
    Owner.SkipLess33Char(toParse);

    if (toParse[Owner.CurrPos] != '(')
      throw new JMParseException("丢失循环定义“(”");

    JMParse.CurrPos++; //跳过(

    //为循环付初值
    Owner.SkipLess33Char(toParse);
    if (toParse[Owner.CurrPos] == ';') { //循环初值为空
      ConChk = 1100;
      Owner.CurrPos++;
    } else
      Owner.Next(fr, lkw);

    //计算条件表达式并记录;
    Owner.SkipLess33Char(toParse);
    if (toParse[Owner.CurrPos] == ';') //条件表达式为空
      ConChk = 1110;
    else {
      if (ConChk == 1100)
        throw new JMParseException("没有给循环付初值");

      Con = Owner.CurrPos;
      ConLine = Owner.CurrLine;

      Enter = ((Boolean)ExprParse.ParseExpr(toParse)).booleanValue();
      if (Enter) {
        Owner.SkipLess33Char(toParse);
        if (toParse[Owner.CurrPos] != ';')
          throw new JMParseException("丢失循环定义分割符;");
      } else {
        Owner.CurrPos = StartPos;
        SkipFor(toParse);
        return null;
      }
    }
    Owner.CurrPos++;//跳过循环定义分隔符;

    //记录跌代语句
    Owner.SkipLess33Char(toParse);
    if (toParse[Owner.CurrPos] == ')')
      ConChk = 1111;
    else {
      if (ConChk == 1100 || ConChk == 1110)
        throw new JMParseException("循环定义不正确");
      Iterative = Owner.CurrPos;
      IterativeLine = Owner.CurrLine;
      while(toParse[Owner.CurrPos] != 0) {

        if (toParse[Owner.CurrPos] == ')') {
          break;
        }

        if (toParse[Owner.CurrPos] == ';' || toParse[Owner.CurrPos] == '{'||
            toParse[Owner.CurrPos] == '}')
          throw new JMParseException("丢失循环定义结束符)");

        Owner.CurrPos++;
      }
    }

    Owner.CurrPos++;//跳过循环定义结束符)

    Owner.SkipLess33Char(toParse);

    if (ConChk == 1111)
      Enter = true;

    int LoopStart = Owner.CurrPos;//记录循环的开始位置
    int LootStartLine = Owner.CurrLine;

    while (Enter) {
      Owner.CurrPos = LoopStart;
      Owner.CurrLine = LootStartLine;
      if (toParse[Owner.CurrPos] == '{') {
        Owner.CurrPos++;
        while (toParse[Owner.CurrPos] != '}') {
          oo = Owner.Next(fr, lkw);
          if (fr.Returned())
            return oo;
          if (lkw.KeyWord() == LoopKeyWord.CONTINUE) {
            lkw.setKeyWord(LoopKeyWord.NONE);
            break;
          }
          if (lkw.KeyWord() == LoopKeyWord.BREAK) {
            lkw.setKeyWord(LoopKeyWord.NONE);
            Owner.CurrPos = StartPos;
            SkipFor(toParse);
            return null;
          }

          Owner.SkipLess33Char(toParse);
        } //while (toParse[Owner.CurrPos] != '}')
      } else {
        oo = Owner.Next(fr, lkw);
        if (fr.Returned())
          return oo;
        if (lkw.KeyWord() == LoopKeyWord.CONTINUE)
          break;
        if (lkw.KeyWord() == LoopKeyWord.BREAK) {
          Owner.CurrPos = StartPos;
          SkipFor(toParse);
          return null;
        }
      }

      if (ConChk != 1111) {
        Owner.CurrPos = Iterative;
        Owner.CurrLine = IterativeLine;
        DoIterative(toParse, Iterative);
        Owner.CurrPos = Con;
        Owner.CurrLine = ConLine;
        Enter = ((Boolean) ExprParse.ParseExpr(toParse)).booleanValue();
      }
    }  //while (Enter) {

    Owner.CurrPos = StartPos;
    SkipFor(toParse);
    return oo;
  }

}

⌨️ 快捷键说明

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