simple-parser-example.rst 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. Simple Parser Example
  2. =====================
  3. Extend the ``Doctrine\Common\Lexer\AbstractLexer`` class and implement
  4. the ``getCatchablePatterns``, ``getNonCatchablePatterns``, and ``getType``
  5. methods. Here is a very simple example lexer implementation named ``CharacterTypeLexer``.
  6. It tokenizes a string to ``T_UPPER``, ``T_LOWER`` and``T_NUMBER`` tokens:
  7. .. code-block:: php
  8. <?php
  9. use Doctrine\Common\Lexer\AbstractLexer;
  10. class CharacterTypeLexer extends AbstractLexer
  11. {
  12. const T_UPPER = 1;
  13. const T_LOWER = 2;
  14. const T_NUMBER = 3;
  15. protected function getCatchablePatterns()
  16. {
  17. return array(
  18. '[a-bA-Z0-9]',
  19. );
  20. }
  21. protected function getNonCatchablePatterns()
  22. {
  23. return array();
  24. }
  25. protected function getType(&$value)
  26. {
  27. if (is_numeric($value)) {
  28. return self::T_NUMBER;
  29. }
  30. if (strtoupper($value) === $value) {
  31. return self::T_UPPER;
  32. }
  33. if (strtolower($value) === $value) {
  34. return self::T_LOWER;
  35. }
  36. }
  37. }
  38. Use ``CharacterTypeLexer`` to extract an array of upper case characters:
  39. .. code-block:: php
  40. <?php
  41. class UpperCaseCharacterExtracter
  42. {
  43. private $lexer;
  44. public function __construct(CharacterTypeLexer $lexer)
  45. {
  46. $this->lexer = $lexer;
  47. }
  48. public function getUpperCaseCharacters($string)
  49. {
  50. $this->lexer->setInput($string);
  51. $this->lexer->moveNext();
  52. $upperCaseChars = array();
  53. while (true) {
  54. if (!$this->lexer->lookahead) {
  55. break;
  56. }
  57. $this->lexer->moveNext();
  58. if ($this->lexer->token['type'] === CharacterTypeLexer::T_UPPER) {
  59. $upperCaseChars[] = $this->lexer->token['value'];
  60. }
  61. }
  62. return $upperCaseChars;
  63. }
  64. }
  65. $upperCaseCharacterExtractor = new UpperCaseCharacterExtracter(new CharacterTypeLexer());
  66. $upperCaseCharacters = $upperCaseCharacterExtractor->getUpperCaseCharacters('1aBcdEfgHiJ12');
  67. print_r($upperCaseCharacters);
  68. The variable ``$upperCaseCharacters`` contains all of the upper case
  69. characters:
  70. .. code-block:: php
  71. Array
  72. (
  73. [0] => B
  74. [1] => E
  75. [2] => H
  76. [3] => J
  77. )
  78. This is a simple example but it should demonstrate the low level API
  79. that can be used to build more complex parsers.