Welcome to Baron’s documentation!


Baron is a Full Syntax Tree (FST) for Python. It represents source code as a structured tree, easily parsable by a computer. By opposition to an Abstract Syntax Tree (AST) which drops syntax information in the process of its creation (like empty lines, comments, formatting), a FST keeps everything and guarantees the operation fst_to_code(code_to_fst(source_code)) == source_code.

If you want to understand why this is important, read this: https://github.com/PyCQA/baron#why-is-this-important

Github (code, bug tracker, etc.)



pip install baron


There is a good chance that you’ll want to use RedBaron instead of using Baron directly. Think of Baron as the “bytecode of python source code” and RedBaron as some sort of usable layer on top of it, a bit like dom/jQuery or html/Beautifulsoup.

Basic usage

In [1]: from baron import parse, dumps

In [2]: source_code = "a = 1"

In [3]: fst = parse(source_code)

In [4]: fst
[{'annotation': {},
  'annotation_first_formatting': [],
  'annotation_second_formatting': [],
  'first_formatting': [{'type': 'space', 'value': ' '}],
  'operator': '',
  'second_formatting': [{'type': 'space', 'value': ' '}],
  'target': {'type': 'name', 'value': 'a'},
  'type': 'assignment',
  'value': {'section': 'number', 'type': 'int', 'value': '1'}}]

In [5]: generated_source_code = dumps(fst)

In [6]: generated_source_code
Out[6]: 'a = 1'

In [7]: source_code == generated_source_code
Out[7]: True

Financial support

Baron and RedBaron are a very advanced piece of engineering that requires a lot of time of concentration to work on. Until the end of 2018, the development has been a full volunteer work mostly done by [Bram](https://github.com/psycojoker), but now, to reach the next level and bring those projects to the stability and quality you expect, we need your support.

You can join our contributors and sponsors on our transparent [OpenCollective](https://opencollective.com/redbaron), every contribution will count and will be mainly used to work on the projects stability and quality but also on continuing, on the side, the R&D side of those projects.

Our supporters

https://opencollective.com/redbaron/tiers/i-like-this,-keep-going!/badge.svg?label=Ilikethis,keepgoing!&color=brightgreen https://opencollective.com/redbaron/tiers/it-looks-cool!/badge.svg?label=Itlookscool!&color=brightgreen https://opencollective.com/redbaron/tiers/oh-god,-that-saved-me-so-much-time!/badge.svg?label=Ohgod,thatsavedmesomuchtime!&color=brightgreen


Become our first sponsor!


Indices and tables