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

📄 jmparse.java

📁 类javaScript脚本解释器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package MultiScriptParse.Parse;

import java.net.*;
import java.util.*;
import java.io.*;

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

//主解释器类负责脚本的载入,对脚本的遍历操作

class Count {

  public int Value = 0;

  public void Inc() {
     Value++;
  }

  public void Dec() {
    Value--;
  }
}

public class JMParse extends JMObject {

  private final int BUFLEN = 65536;

  private char[] Script = new char[BUFLEN];  //存放脚本内容

  private boolean Primary = true;

  static public JMVarParse GlobVarList;   //存放全局变量

  static public JMVarParse LocalVarList;  //存放局部变量

//                                 0        9         9         9         9
  static public String Operator = "+,-,*,/,%,^,<,>,<=,>=,==,!=,!,&&,||,(,),#,";

  static public int[]  COptpriority = {10,2,10,2,12,2,12,2,12,2,
                                      16,2,8,2,8,2,8,2,0,8,
                                      2,0,4,2,0,4,2,0,6,1,
                                      2,2,0,2,2,0,18,0,1,0,
                                      0};

  static public int[]  SOptpriority = {11,2,11,2,13,2,13,2,13,2,
                                      17,2,9,2,9,2,9,2,0,9,
                                      2,0,5,2,0,5,2,0,7,1,
                                      3,2,0,3,2,0,1,0,18,0,
                                      0};


  private JMPropParse objParse;

  private JMExprParse ExprParse;

  private JMKeyWordParse KeyWordParse;

  private JMFunctionParse FunctionParse;

  private URL mainPath;

  private int Length = 0;

  private char ckey;

  static public int CurrPos = 0;   //存放最后一次遍历后 SecondToken 的开始位置

  static public int CurrLine = 1;

  static public StringBuffer CurrFile = new StringBuffer(50);

  public JMParse(boolean Primary) {
    this.Primary = Primary;
    objParse     = new JMPropParse(this);
    GlobVarList  = new JMVarParse(this);
    int[] tmp = new int[1];
    GlobVarList.AddVariable("Array", tmp);
    LocalVarList = new JMVarParse(this);
    ExprParse    = new JMExprParse(this);
    KeyWordParse = new JMKeyWordParse(this, ExprParse, objParse);
    FunctionParse = new JMFunctionParse(this);
    JMParseParamsParse ParseParamsParse = new JMParseParamsParse(ExprParse, objParse, null);
    objParse.setParseParamsParse(ParseParamsParse);
    FunctionParse.setParseParamsParse(ParseParamsParse);
    FunctionParse.setPropParse(objParse);
    ExprParse.setPropParse(objParse);
    ExprParse.setFunctionParse(FunctionParse);
  }

  static public void SkipLess33Char(char[] toParse) {
    while (toParse[CurrPos] < 33) {
      if (toParse[CurrPos] == '\n') CurrLine++;
      CurrPos++;
    }
  }

  static public void SkipRemark(char[] toSkip) {
    if (toSkip[CurrPos] == '#') {
      while (toSkip[CurrPos] != '\n')
        CurrPos++;
      JMParse.CurrLine++;
    }
  }

  static public void LoseEndSymbol() throws JMParseException {
    throw new JMParseException(new StringBuffer("丢失语句的结束符 \";\"").toString());
  }
  public boolean LoadScript(String toLoad)
  throws JMParseException
  {
    try {
      LoadScript(new URL(toLoad));
    } catch (MalformedURLException e) {
      throw new JMParseException(e.getMessage());
    }
    return true;
  }

  public boolean LoadScript(URL toLoad)
  throws JMParseException
  {
    try {
      mainPath = toLoad;
      CurrFile.append(toLoad.getFile());
      Initial(toLoad);
      return true;
    } catch (IOException e){
      throw new JMParseException(e.getMessage());
    }
  }

  public void ParseVar(String VarName, JMVarParse VarList)  throws JMParseException {
    Object oo = null;

    SkipLess33Char(Script);

    if (Script[CurrPos] == '='){
      CurrPos++;
      oo = ExprParse.ParseExpr(Script);
    }

    if (Script[CurrPos] != ';')
      LoseEndSymbol();

    if (oo == null)
      oo = new Object();

    if (!VarList.AddVariable(VarName, oo))
      throw new JMParseException(new StringBuffer("重复定义的变量 ").
                                 append(VarName).toString());
  }

  public Object Next(FuncReturn fr, LoopKeyWord lkw) throws JMParseException
  {
    StringBuffer Token = new StringBuffer(25);
    int symbolType = 0; //0 还未取任何字符 5 已取出元素但未确定 10 已结束Token
    Object oo = null;
    //int Start = CurrPos;
    ckey = Script[CurrPos];

    Token.delete(0, 25);

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

    while (ckey != 0) {

      if (ckey == ';') {
        break;
      }

      if (ckey == '#'){
        SkipRemark(Script);
        ckey = Script[++CurrPos];
        continue;
      }

      if (ckey < 33) {

        if (ckey == '\n')
          CurrLine++;

        if (symbolType != 0){
          symbolType = 10;
        }
        ckey = Script[++CurrPos];
        continue;
      }

      if (ckey == '.') {
        if (symbolType == 0)
          throw new JMParseException("使用不正确的 \".\"");
        oo = objParse.ParseObject(objParse.FindObject(Token.toString()), Script);
        symbolType = 0;
        if (Script[CurrPos] != ';')
          LoseEndSymbol();
        CurrPos++;//跳过语句结束的分号
        return oo;
      }

      if (ckey == '(') {
        if (symbolType == 0)
          throw new JMParseException("使用不正确的 \"(\"");
        if (Token.indexOf("if") > -1) {
          oo = KeyWordParse.Execif(fr, lkw, Script, true);
          return oo;
        } else if (Token.indexOf("for") > -1){
          oo = KeyWordParse.Execfor(fr, Script);
          return oo;
        } else {
          oo = FunctionParse.ExecFunction(Token.toString(), Script, null);
          if (Script[CurrPos] != ';')
            LoseEndSymbol();
        }
        CurrPos++;//跳过语句结束的分号
        return oo;
      }

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

        if (Script[CurrPos] != ';')
          LoseEndSymbol();
        CurrPos++;//跳过语句结束的分号
        return null;
      }

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

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

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

    }

    //判断取得的Token类型执行相应的操作
    if (Token.toString().equals("return")) {
      fr.Return();
      oo = ExprParse.ParseExpr(Script);
    } else if (Token.toString().equals("continue")) {
      if (lkw != null) lkw.setKeyWord(LoopKeyWord.CONTINUE);
    } else if (Token.toString().equals("break")) {
      if (lkw != null) lkw.setKeyWord(LoopKeyWord.BREAK);
    }else
      throw new JMParseException(new StringBuffer("使用不正确的标志符 ").
                                 append(Token).toString());
    if (Script[CurrPos] != ';')
      LoseEndSymbol();
    return oo;

  }



  public void SkipOneLan() throws JMParseException

⌨️ 快捷键说明

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