public TokenInfo(string _text, TK _token, int _pos, TokenInfo _lastToken, int _line) { this.text = _text; this.token = _token; this.pos = _pos; this.lastToken = _lastToken; this.line = _line; }
public VSBlockInfo GetBLOCK(string js) { Stack<TokenInfo> stack = new Stack<TokenInfo>(); VSBlockInfo block = new VSBlockInfo(); TokenInfo beforToken = null; string str; this.sb = new StringBuilder(); this.lastword = ""; this.lasttok = TK.EOF; this._in = IN.BLOCK; this.in_push(this._in); int pos = 0; TK token = TK.UNKNOWN; curreTokenLine = 1; while (true) { str = this.get_next_token(ref js, ref pos, out token); switch (token) { case TK.START_BLOCK: stack.Push(new TokenInfo(str, token, pos, beforToken, curreTokenLine)); break; case TK.END_BLOCK: if (pos == js.Length) { if (stack.Count > 0) { TokenInfo startBlockToken = stack.Peek(); block.startBlock = startBlockToken; TokenInfo startToken = startBlockToken.lastToken; bool isJson = false; if (startBlockToken.lastToken.token == TK.PUNCT && startBlockToken.lastToken.text == "," ) { if (startBlockToken.lastToken.lastToken != null && startBlockToken.lastToken.lastToken.token == TK.END_BLOCK) { isJson = true; } } if (startToken.token == TK.NEW_LINE) { startToken = startToken.lastToken; } int stratLine = startToken.line; block.line = stratLine; startToken = startToken.lastToken; while (startToken.line == stratLine) { block.line = startToken.line; if (startToken.line == 0) { break; } if (startToken.lastToken == null) { break; } startToken = startToken.lastToken; block.line = startToken.line+1; } int lastIndex = 0; int postion = startToken.pos; for (int i = startToken.pos; i >= 0; i--) { postion = i; if (whitespace.Contains(js[i])) { lastIndex++; if (js[i] == '\n') { break; } if (js[i] == '\r') { if (js[i + 1] == '\n') { postion = i + 1; } break; } } } //上一行语句的最后一个字符 string prevStatementLastWord = ""; int prevStatementPostion = postion; if (postion > 0) { int prevForStart = postion - 1; if (js[prevForStart] == '\r') { prevForStart = prevForStart - 1; } for (int i = prevForStart; i >= 0; i--) { prevStatementPostion = i; if (whitespace.Contains(js[i])) { lastIndex++; if (js[i] == '\n') { break; } if (js[i] == '\r') { if (js[i + 1] == '\n') { prevStatementPostion = i + 1; } break; } } } block.prevStatementLastWord = prevStatementLastWord; for (int i = postion; i >= 0; i--) { if (!whitespace.Contains(js[i])) { if (js[i] != '\n' && js[i] != '\r') { prevStatementLastWord = js[i].ToString(); block.prevStatementLastWord = prevStatementLastWord; break; } } } } if (postion == 0) { postion = -1; } string result = js.Substring(postion + 1, js.Length - postion - 1); int spaceCount = 0; if (prevStatementPostion > 0) { for (int i = prevStatementPostion + 1; i >= 0; i++) { if (js[i] == '\t') { spaceCount += 4; } else if (js[i] == ' ') { spaceCount += 1; } else { break; } } } int indent = spaceCount / 4; string endChars = "{["; if (prevStatementLastWord!=""&&endChars.IndexOf(prevStatementLastWord) >= 0) { indent++; } //if (isJson) //{ // indent++; //} block.indent = indent; block.column = lastIndex; block.text = result; if (startBlockToken.lastToken != null) { block.befor = startToken.token.ToString(); } return block; } else { return block; } } else { if (stack.Count > 0) { stack.Pop(); } } break; } if (pos >= js.Length) { break; } beforToken = new TokenInfo(str, token, pos, beforToken, curreTokenLine); this.lasttok = token; this.lastword = str.ToLower(); } return block; }