SieveToken.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php namespace Sieve;
  2. include_once('SieveDumpable.php');
  3. class SieveToken implements SieveDumpable
  4. {
  5. const Unknown = 0x0000;
  6. const ScriptEnd = 0x0001;
  7. const LeftBracket = 0x0002;
  8. const RightBracket = 0x0004;
  9. const BlockStart = 0x0008;
  10. const BlockEnd = 0x0010;
  11. const LeftParenthesis = 0x0020;
  12. const RightParenthesis = 0x0040;
  13. const Comma = 0x0080;
  14. const Semicolon = 0x0100;
  15. const Whitespace = 0x0200;
  16. const Tag = 0x0400;
  17. const QuotedString = 0x0800;
  18. const Number = 0x1000;
  19. const Comment = 0x2000;
  20. const MultilineString = 0x4000;
  21. const Identifier = 0x8000;
  22. const String = 0x4800; // Quoted | Multiline
  23. const StringList = 0x4802; // Quoted | Multiline | LeftBracket
  24. const StringListSep = 0x0084; // Comma | RightBracket
  25. const Unparsed = 0x2200; // Comment | Whitespace
  26. const TestList = 0x8020; // Identifier | LeftParenthesis
  27. public $type;
  28. public $text;
  29. public $line;
  30. public function __construct($type, $text, $line)
  31. {
  32. $this->text = $text;
  33. $this->type = $type;
  34. $this->line = intval($line);
  35. }
  36. public function dump()
  37. {
  38. return '<'. SieveToken::escape($this->text) .'> type:'. SieveToken::typeString($this->type) .' line:'. $this->line;
  39. }
  40. public function text()
  41. {
  42. return $this->text;
  43. }
  44. public function is($type)
  45. {
  46. return (bool)($this->type & $type);
  47. }
  48. public static function typeString($type)
  49. {
  50. switch ($type)
  51. {
  52. case SieveToken::Identifier: return 'identifier';
  53. case SieveToken::Whitespace: return 'whitespace';
  54. case SieveToken::QuotedString: return 'quoted string';
  55. case SieveToken::Tag: return 'tag';
  56. case SieveToken::Semicolon: return 'semicolon';
  57. case SieveToken::LeftBracket: return 'left bracket';
  58. case SieveToken::RightBracket: return 'right bracket';
  59. case SieveToken::BlockStart: return 'block start';
  60. case SieveToken::BlockEnd: return 'block end';
  61. case SieveToken::LeftParenthesis: return 'left parenthesis';
  62. case SieveToken::RightParenthesis: return 'right parenthesis';
  63. case SieveToken::Comma: return 'comma';
  64. case SieveToken::Number: return 'number';
  65. case SieveToken::Comment: return 'comment';
  66. case SieveToken::MultilineString: return 'multiline string';
  67. case SieveToken::ScriptEnd: return 'script end';
  68. case SieveToken::String: return 'string';
  69. case SieveToken::StringList: return 'string list';
  70. default: return 'unknown token';
  71. }
  72. }
  73. protected static $tr_ = array("\r" => '\r', "\n" => '\n', "\t" => '\t');
  74. public static function escape($val)
  75. {
  76. return strtr($val, self::$tr_);
  77. }
  78. }