index.rst 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. Introduction
  2. ============
  3. Doctrine Lexer is a library that can be used in Top-Down, Recursive
  4. Descent Parsers. This lexer is used in Doctrine Annotations and in
  5. Doctrine ORM (DQL).
  6. To write your own parser you just need to extend ``Doctrine\Common\Lexer\AbstractLexer``
  7. and implement the following three abstract methods.
  8. .. code-block:: php
  9. /**
  10. * Lexical catchable patterns.
  11. *
  12. * @return array
  13. */
  14. abstract protected function getCatchablePatterns();
  15. /**
  16. * Lexical non-catchable patterns.
  17. *
  18. * @return array
  19. */
  20. abstract protected function getNonCatchablePatterns();
  21. /**
  22. * Retrieve token type. Also processes the token value if necessary.
  23. *
  24. * @param string $value
  25. * @return integer
  26. */
  27. abstract protected function getType(&$value);
  28. These methods define the `lexical <http://en.wikipedia.org/wiki/Lexical_analysis>`_
  29. catchable and non-catchable patterns and a method for returning the
  30. type of a token and filtering the value if necessary.
  31. The Lexer is responsible for giving you an API to walk across a
  32. string one character at a time and analyze the type of each character, value and position of
  33. each token in the string. The low level API of the lexer is pretty simple:
  34. - ``setInput($input)`` - Sets the input data to be tokenized. The Lexer is immediately reset and the new input tokenized.
  35. - ``reset()`` - Resets the lexer.
  36. - ``resetPeek()`` - Resets the peek pointer to 0.
  37. - ``resetPosition($position = 0)`` - Resets the lexer position on the input to the given position.
  38. - ``isNextToken($token)`` - Checks whether a given token matches the current lookahead.
  39. - ``isNextTokenAny(array $tokens)`` - Checks whether any of the given tokens matches the current lookahead.
  40. - ``moveNext()`` - Moves to the next token in the input string.
  41. - ``skipUntil($type)`` - Tells the lexer to skip input tokens until it sees a token with the given value.
  42. - ``isA($value, $token)`` - Checks if given value is identical to the given token.
  43. - ``peek()`` - Moves the lookahead token forward.
  44. - ``glimpse()`` - Peeks at the next token, returns it and immediately resets the peek.