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