public RTFNode(RTFToken token) { this.strKeyword = token.Key; this.bolHasParameter = token.HasParam; this.intParameter = token.Param; if (token.Type == RTFTokenType.Control) { intType = RTFNodeType.Control; } else if (token.Type == RTFTokenType.Control) { intType = RTFNodeType.Control; } else if (token.Type == RTFTokenType.Keyword) { intType = RTFNodeType.Keyword; } else if (token.Type == RTFTokenType.ExtKeyword) { intType = RTFNodeType.ExtKeyword; } else if (token.Type == RTFTokenType.Text) { intType = RTFNodeType.Text; } else { intType = RTFNodeType.Text; } }
/// <summary> /// read token /// </summary> /// <returns>token readed</returns> public RTFToken ReadToken() { bolFirstTokenInGroup = false; myLastToken = myCurrentToken; if (myLastToken != null && myLastToken.Type == RTFTokenType.GroupStart) { bolFirstTokenInGroup = true; } myCurrentToken = myLex.NextToken( ); if (myCurrentToken == null || myCurrentToken.Type == RTFTokenType.Eof) { myCurrentToken = null; return(null); } intTokenCount++; if (myCurrentToken.Type == RTFTokenType.GroupStart) { intLevel++; } else if (myCurrentToken.Type == RTFTokenType.GroupEnd) { intLevel--; } //if (myTokenStack.Count > 0) //{ // myCurrentToken.Parent = (RTFToken)myTokenStack[myTokenStack.Count - 1]; //} //myTokenStack.Add( myCurrentToken ); return(myCurrentToken); }
/// <summary> /// read next token /// </summary> /// <returns>token</returns> public RTFToken NextToken( ) { int c = 0; RTFToken token = new RTFToken(); //myReader.Read(); c = myReader.Read(); while (c == '\r' || c == '\n' || c == '\t' || c == '\0') { c = myReader.Read(); //c = myReader.Peek(); } // // c = myReader.Read(); // while( c == '\r' // || c == '\n' // || c == '\t' // || c == '\0') // { // c = myReader.Read(); // } if (c != EOF) { switch (c) { case '{': token.Type = RTFTokenType.GroupStart; break; case '}': token.Type = RTFTokenType.GroupEnd; break; case '\\': ParseKeyword(token); break; default: token.Type = RTFTokenType.Text; ParseText(c, token); break; } } else { token.Type = RTFTokenType.Eof; } return(token); }
private void ParseText(int c, RTFToken token) { System.Text.StringBuilder myStr = new System.Text.StringBuilder((( char )c).ToString()); c = ClearWhiteSpace(); while (c != '\\' && c != '}' && c != '{' && c != EOF) { myReader.Read(); myStr.Append(( char )c); c = ClearWhiteSpace(); } token.Key = myStr.ToString(); }
private void ParseKeyword(RTFToken token) { int c = 0; bool ext = false; c = myReader.Peek(); if (char.IsLetter(( char )c) == false) { myReader.Read(); if (c == '*') { // expend keyword token.Type = RTFTokenType.Keyword; myReader.Read(); //myReader.Read(); ext = true; goto ReadKeywrod; } else if (c == '\\' || c == '{' || c == '}') { // special character token.Type = RTFTokenType.Text; token.Key = (( char )c).ToString(); } else { token.Type = RTFTokenType.Control; token.Key = (( char )c).ToString(); if (token.Key == "\'") { // read 2 hex characters System.Text.StringBuilder text = new System.Text.StringBuilder(); text.Append(( char )myReader.Read()); text.Append(( char )myReader.Read()); token.HasParam = true; token.Param = Convert.ToInt32(text.ToString().ToLower(), 16); if (token.Param == 0) { token.Param = 0; } } } return; } //if( char.IsLetter( ( char ) c ) == false ) ReadKeywrod: // read keyword System.Text.StringBuilder Keyword = new System.Text.StringBuilder(); c = myReader.Peek(); while (char.IsLetter(( char )c)) { myReader.Read(); Keyword.Append(( char )c); c = myReader.Peek(); } if (ext) { token.Type = RTFTokenType.ExtKeyword; } else { token.Type = RTFTokenType.Keyword; } token.Key = Keyword.ToString(); // read a interger if (char.IsDigit(( char )c) || c == '-') { token.HasParam = true; bool Negative = false; if (c == '-') { Negative = true; myReader.Read(); } c = myReader.Peek(); System.Text.StringBuilder text = new System.Text.StringBuilder(); while (char.IsDigit(( char )c)) { myReader.Read(); text.Append(( char )c); c = myReader.Peek(); } int p = Convert.ToInt32(text.ToString()); if (Negative) { p = -p; } token.Param = p; } //if( char.IsDigit( ( char ) c ) || c == '-' ) if (c == ' ') { myReader.Read(); } }
/// <summary> /// Accept rtf token /// </summary> /// <param name="token">RTF token</param> /// <returns>Is accept it?</returns> public bool Accept(RTFToken token, RTFReader reader) { if (token == null) { return(false); } if (token.Type == RTFTokenType.Text) { if (reader != null) { if (token.Key[0] == '?') { if (reader.LastToken != null) { if (reader.LastToken.Type == RTFTokenType.Keyword && reader.LastToken.Key == "u" && reader.LastToken.HasParam) { // 紧跟在在“\uN”后面的问号忽略掉 if (token.Key.Length > 0) { CheckBuffer(); myStr.Append(token.Key.Substring(1)); } return(true); } } } } CheckBuffer(); myStr.Append(token.Key); return(true); } else if (token.Type == RTFTokenType.Control && token.Key == "'" && token.HasParam) { myBuffer.Add((byte)token.Param); return(true); } if (token.Key == RTFConsts._u && token.HasParam) { // Unicode char CheckBuffer(); // 忽略 \u 指令 //myStr.Append( (char)token.Param); return(true); } if (token.Key == "tab") { CheckBuffer(); myStr.Append("\t"); return(true); } if (token.Key == "emdash") { CheckBuffer(); myStr.Append('—'); return(true); } if (token.Key == "") { // 提示未识别的字符 CheckBuffer(); myStr.Append('-'); return(true); } return(false); }