README.rst 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. ###################################################################
  2. parso - A Python Parser
  3. ###################################################################
  4. .. image:: https://github.com/davidhalter/parso/workflows/Build/badge.svg?branch=master
  5. :target: https://github.com/davidhalter/parso/actions
  6. :alt: GitHub Actions build status
  7. .. image:: https://coveralls.io/repos/github/davidhalter/parso/badge.svg?branch=master
  8. :target: https://coveralls.io/github/davidhalter/parso?branch=master
  9. :alt: Coverage Status
  10. .. image:: https://pepy.tech/badge/parso
  11. :target: https://pepy.tech/project/parso
  12. :alt: PyPI Downloads
  13. .. image:: https://raw.githubusercontent.com/davidhalter/parso/master/docs/_static/logo_characters.png
  14. Parso is a Python parser that supports error recovery and round-trip parsing
  15. for different Python versions (in multiple Python versions). Parso is also able
  16. to list multiple syntax errors in your python file.
  17. Parso has been battle-tested by jedi_. It was pulled out of jedi to be useful
  18. for other projects as well.
  19. Parso consists of a small API to parse Python and analyse the syntax tree.
  20. A simple example:
  21. .. code-block:: python
  22. >>> import parso
  23. >>> module = parso.parse('hello + 1', version="3.9")
  24. >>> expr = module.children[0]
  25. >>> expr
  26. PythonNode(arith_expr, [<Name: hello@1,0>, <Operator: +>, <Number: 1>])
  27. >>> print(expr.get_code())
  28. hello + 1
  29. >>> name = expr.children[0]
  30. >>> name
  31. <Name: hello@1,0>
  32. >>> name.end_pos
  33. (1, 5)
  34. >>> expr.end_pos
  35. (1, 9)
  36. To list multiple issues:
  37. .. code-block:: python
  38. >>> grammar = parso.load_grammar()
  39. >>> module = grammar.parse('foo +\nbar\ncontinue')
  40. >>> error1, error2 = grammar.iter_errors(module)
  41. >>> error1.message
  42. 'SyntaxError: invalid syntax'
  43. >>> error2.message
  44. "SyntaxError: 'continue' not properly in loop"
  45. Resources
  46. =========
  47. - `Testing <https://parso.readthedocs.io/en/latest/docs/development.html#testing>`_
  48. - `PyPI <https://pypi.python.org/pypi/parso>`_
  49. - `Docs <https://parso.readthedocs.org/en/latest/>`_
  50. - Uses `semantic versioning <https://semver.org/>`_
  51. Installation
  52. ============
  53. pip install parso
  54. Future
  55. ======
  56. - There will be better support for refactoring and comments. Stay tuned.
  57. - There's a WIP PEP8 validator. It's however not in a good shape, yet.
  58. Known Issues
  59. ============
  60. - `async`/`await` are already used as keywords in Python3.6.
  61. - `from __future__ import print_function` is not ignored.
  62. Acknowledgements
  63. ================
  64. - Guido van Rossum (@gvanrossum) for creating the parser generator pgen2
  65. (originally used in lib2to3).
  66. - `Salome Schneider <https://www.crepes-schnaegg.ch/cr%C3%AApes-schn%C3%A4gg/kunst-f%C3%BCrs-cr%C3%AApes-mobil/>`_
  67. for the extremely awesome parso logo.
  68. .. _jedi: https://github.com/davidhalter/jedi