et1337

Tokenizer rules

Jan 8th, 2012
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1. // Each TokenClass gets a function that takes the next character in the stream as a parameter,
  2. // and returns true if that character is allowed to start a token of the given TokenClass.
  3. this.startingRules = new Dictionary<TokenClass, Func<TokenClass, char, bool>>
  4. {
  5.     { TokenClass.Whitespace, (lastToken, c) => whitespaceCharacters.Contains(c) },
  6.     { TokenClass.Identifier, (lastToken, c) => char.IsUpper(c) && (lastToken == TokenClass.Whitespace || lastToken == TokenClass.SpecialSymbol) },
  7.     { TokenClass.ReservedWord, (lastToken, c) => char.IsLower(c) && reservedWordStartingCharacters.Contains(c) && (lastToken == TokenClass.Whitespace || lastToken == TokenClass.SpecialSymbol) },
  8.     { TokenClass.SpecialSymbol, (lastToken, c) => specialSymbolStartingCharacters.Contains(c) },
  9.     { TokenClass.Integer, (lastToken, c) => char.IsDigit(c) && (lastToken == TokenClass.Whitespace || lastToken == TokenClass.SpecialSymbol) }
  10. };
  11.  
  12. // Each TokenClass gets a function that takes the current state of a token and the next
  13. // character in the stream as parameters, and returns true if the next character is also
  14. // part of the current token.
  15. this.continuationRules = new Dictionary<TokenClass, Func<StringBuilder, char, bool>>
  16. {
  17.     { TokenClass.Whitespace, (builder, c) => whitespaceCharacters.Contains(c) },
  18.     { TokenClass.Identifier, (builder, c) => char.IsUpper(c) || char.IsDigit(c) },
  19.     { TokenClass.ReservedWord, (builder, c) => char.IsLower(c) && reservedWordContinuationCharacters.Contains(c) },
  20.     { TokenClass.SpecialSymbol, (builder, c) => builder.Length <= 1 && specialSymbolContinuationCharacters.Contains(c) },
  21.     { TokenClass.Integer, (builder, c) => char.IsDigit(c) }
  22. };
Advertisement
Add Comment
Please, Sign In to add comment