private RtfToken ParseWord() { RtfToken token = new RtfToken(); Read(); // consume / int c = Peek(); if (IsAsciiLetter(c)) { token = ParseControlWord(); } else if (c == '{' || c == '}' || c == '\\') { // escaped character token.Type = RtfTokenType.Text; token.Value = ((char)c).ToString(); Read(); // consume character } else { token.Type = RtfTokenType.ControlSymbol; token.Value = ((char)c).ToString(); Read(); // consume symbol } return(token); }
private RtfToken ParseText() { StringBuilder text = new StringBuilder(); text.Append((char)Read()); while (true) { while (IsInRange(IgnoredChars)) { Read(); } int c = Peek(); if (c == '{' || c == '}' || c == '\\' || c == EOF) { break; } text.Append((char)Read()); } RtfToken token = new RtfToken(); token.Type = RtfTokenType.Text; token.Value = text.ToString(); return(token); }
private RtfToken Match(RtfTokenType type, string value) { RtfToken token = (RtfToken)Read(); if (token.Type != type || token.Value != value) { throw new ParseException("Unexpected token"); } return(token); }
private void ParseColorTable() { RtfToken token; do { token = (RtfToken)Read(); if (token.Type == RtfTokenType.ControlWord && token.Value == "red") { RtfToken tokenG = Match(RtfTokenType.ControlWord, "green"); RtfToken tokenB = Match(RtfTokenType.ControlWord, "blue"); Match(RtfTokenType.Text, ";"); this.colorTable.Add(Color.FromArgb(0xff, (byte)token.Parameter, (byte)tokenG.Parameter, (byte)tokenB.Parameter)); } }while (!token.IsEof && token.Type != RtfTokenType.GroupEnd); }
/// <summary> /// Gets the next token. /// </summary> /// <returns></returns> public override IToken NextToken() { RtfToken token = new RtfToken(); while (IsInRange(IgnoredChars)) { Read(); } int c = Peek(); if (c == '{') { token.Type = RtfTokenType.GroupStart; Read(); } else if (c == '}') { token.Type = RtfTokenType.GroupEnd; Read(); } else if (c == '\\') { token = ParseWord(); } else if (c == EOF) { token.Type = RtfTokenType.Eof; } else { token = ParseText(); } return(token); }
private RtfToken ParseControlWord() { StringBuilder word = new StringBuilder(); int? parameter = null; bool negative = false; int c; while ((c = Peek()) != EOF) { if (c == ' ') { Read(); break; } if (this.State == RtfLexerState.ControlWordParameter) { if (IsDigit(c)) { parameter = parameter * 10 + (c - '0'); } else { break; } } else { if (IsAsciiLetter(c)) { word.Append((char)c); } else if (IsDigit(c)) { PushState(RtfLexerState.ControlWordParameter); parameter = c - '0'; } else if (c == '-') { PushState(RtfLexerState.ControlWordParameter); negative = true; } else { break; } } Read(); } if (this.State == RtfLexerState.ControlWordParameter) { PopState(); } RtfToken token = new RtfToken(); token.Type = RtfTokenType.ControlWord; token.Value = word.ToString(); if (parameter.HasValue) { token.Parameter = negative ? -parameter : parameter; } return(token); }