Commit graph

25 commits

Author SHA1 Message Date
martin
9542fc3882
remove entry points to pure parser (#1375)
* rm: ci

* rm: entry point

* fix: tests

* fix: remove combine step from ci

* linter fixes

* omit the _parser

* fix newlines

* fix: remove optional

* fix: linter

---------

Co-authored-by: thereversiblewheel <martin.li@uwaterloo.ca>
2025-07-30 16:27:20 +00:00
MapleCCC
973895a6c0
Several trivial refactors (#770)
* Enumeration members are singletons. Copying on them would be no-op

* Avoid generating unnecessary `pass` statement

* Several trivial refactor

* Avoid building unnecessary intermediate lists, which are mere slight waste of time and space

* Remove unused import, an overlook from commit 8e6bf9e9

* `collections.abc.Mapping.get()` defaults to return `None` when key doesn't exist

* Just use unittest's `assertRaises` to specify expected exception types, instead of catching every possible `Exception`s, which could suppress legitimate errors and hide bugs

* We know for sure that the body of `CSTTypedTransformerFunctions` won't be empty, so don't bother with complex formal completeness
2022-09-14 14:33:45 +01:00
Zsolt Dollenstein
c30bbcfa48
make sure ParserError's raw_line is zero-indexed (#681) 2022-05-04 05:51:49 -06:00
Zsolt Dollenstein
fb56fa6b8f
[native] Make IntoPy conversion fallible (#639)
* Make IntoPy fallible
* Simplify test case so it works on 3.6
2022-02-07 11:52:29 +00:00
Zsolt Dollenstein
c44ff0500b
Fix license headers (#560)
* Facebook -> Meta

* remove year from doc copyright
2021-12-28 11:55:18 +00:00
John Reese
10c3aa09a7
Upgrade to µsort 1.0.0rc1, and apply formatting changes (#565)
* Upgrade to usort==1.0.0rc1

* Apply sorting changes from usort 1.0.0rc1

* reapply codegen

Co-authored-by: Zsolt Dollenstein <zsol.zsol@gmail.com>
2021-12-21 14:55:04 -08:00
Zsolt Dollenstein
c02de9b718
Implement a Python PEG parser in Rust (#566)
This massive PR implements an alternative Python parser that will allow LibCST to parse Python 3.10's new grammar features. The parser is implemented in Rust, but it's turned off by default through the `LIBCST_PARSER_TYPE` environment variable. Set it to `native` to enable. The PR also enables new CI steps that test just the Rust parser, as well as steps that produce binary wheels for a variety of CPython versions and platforms.

Note: this PR aims to be roughly feature-equivalent to the main branch, so it doesn't include new 3.10 syntax features. That will be addressed as a follow-up PR.

The new parser is implemented in the `native/` directory, and is organized into two rust crates: `libcst_derive` contains some macros to facilitate various features of CST nodes, and `libcst` contains the `parser` itself (including the Python grammar), a `tokenizer` implementation by @bgw, and a very basic representation of CST `nodes`. Parsing is done by
1. **tokenizing** the input utf-8 string (bytes are not supported at the Rust layer, they are converted to utf-8 strings by the python wrapper)
2. running the **PEG parser** on the tokenized input, which also captures certain anchor tokens in the resulting syntax tree
3. using the anchor tokens to **inflate** the syntax tree into a proper CST

Co-authored-by: Benjamin Woodruff <github@benjam.info>
2021-12-21 18:14:39 +00:00
Steven Troxler
5e1e3fe970
The ufmt tool combines usort and black with a consistent wrapper, (#515)
which ensures we won't have inconsistent black-vs-isort errors
going forward. We can always format by running `ufmt format .`
at the root, and check with `ufmt check .` in our CI actions.
2021-08-25 20:39:29 -04:00
Jimmy Lai
6a02e2e995
[CI] add Fixit to tox -e lint (#386) 2020-09-09 17:33:49 -07:00
jimmylai
c023fa7c4c
[typing] enable Pyre strict mode by default (#313)
Co-authored-by: Jimmy Lai <jimmylai@fb.com>
2020-06-12 18:24:18 -07:00
Tim Hatch
dbcb5bed99
Improve default-version selection logic (#306)
* Add Python 3.9 to tox envlist

* Require newer typing_extensions for 3.9

For simplicity, use the new version in all cases.

* Improve default-version selection to work on 3.9

While were at it, improve the code to work with a likely 3.10 by
allowing multiple digits for minor version.
2020-06-10 10:29:58 -07:00
Tim Hatch
0c7d8b42ba Plumb through future_imports 2020-03-12 08:45:18 -07:00
Tim Hatch
522eb5ee0c Add detecting future imports to config.
Several of the python 2 features are gated on these in addition to
version (like `with_statement`), and a refactoring tool like Bowler
commonly needs this information anyway.
2020-03-12 08:45:18 -07:00
Jennifer Taylor
57860f3d76 Fix trailing newline detection around continuation.
If you have such a program like "pass\\\n", this is technically a program without a trailing newline, since line continuations are defined as being a `\` followed by a newline. We were misdetecting this as having a trailing newline, thus making it impossible to parse the continuation. Add some tests to verify this behavior and then fix the problem.

Note that this was found via hypothesis.
2019-10-21 13:24:25 -07:00
Jennifer Taylor
a6bdc9d03d Add test case for uniqueness of resulting tree when parsing. 2019-09-25 12:36:33 -07:00
Jennifer Taylor
fc430343b5 Fix internal underscore convention.
Standardize on the convention that private modules (those we don't expect people to directly import) are prefixed with an underscore. Everything under a directory/module that has an underscore is considered private, unless it is re-exported from a non-underscored module. Most things are exported from libcst directly, but there are a few things in libcst.tool, libcst.codegen and libcst.metadata that are namedspaced as such.
2019-09-17 13:52:42 -07:00
Jennifer Taylor
dcf27644b5 Add support for Python 3.5
There were only grammar changes between 3.5 and 3.6, which are pretty trivial to put in. So, do that trivial bit and get us completely covered for production Python 3 versions. This could use better testing, but it is good enough as-is and we can address issues with GitHub's issue tracker.
2019-09-16 15:57:59 -07:00
Jennifer Taylor
e5b28ab45b Teach LibCST to be multi-version aware.
This adds the ability to subdivide the grammar into different python versions,
as well as the ability to change the tokenizer per python version. I use this
to add support for Python 3.6 since this is a supported version we run on. This
can be used for others to add support for older or newer Python versions, regardless
of tokenizer/grammar changes.
2019-09-16 15:57:59 -07:00
Jennifer Taylor
34767ca6c1 Fork parso's token and tokenize modules.
Fork both of these and convert internal LibCST stuff to point at them.
Removes the TODO from previous commit now that the tokenizer is in
complete agreement.
2019-09-12 14:09:00 -07:00
Jennifer Taylor
f28b0e2537 Fork Parso's utils.py
Brings a copy of utils.py internal to LibCST, rewriting our uses of it
to use our internal fork. A compatibility shim (__iter__) is currently
included to make PythonVersionInfo compatible with Parso directly.
2019-09-12 14:09:00 -07:00
Jennifer Taylor
95f649af79 Fix incorrect round-tripping of newlines when parsing statements.
Hypothesis found that when we have a statement like `pass\r`, we detect that
`\r` is the default and parse the trailing newline as `Newline(None)`. However, when
we render the statement back out again, since we don't have a module, we construct
a default module which treats `Newline(None)` as a `\n` not a '\r'. So, when we are
parsing statements or expressions, disable auto-inferring the default newline and always
infer the default rendered newline (`\n`) so that rendering a statement/expression back
out behaves as expected.
2019-08-29 11:33:57 -07:00
Benjamin Woodruff
283a736173 Make conversion functions use ParserSyntaxError
For anything that's not an internal logic error, conversion functions
should raise a ParserSyntaxError.

Internal logic errors should probably use an AssertionError or an assert
statement, but that's not as important and is out of scope for this PR.
2019-08-22 13:35:11 -07:00
Benjamin Woodruff
e9567a08c0 Add tests to the parser for syntax errors
This tries to test all the ways a ParserSyntaxError can be thrown.
2019-08-22 13:35:11 -07:00
Benjamin Woodruff
2459ba0aa0 Make ParserSyntaxError more generic (#24)
This removes the hard-coded logic about encountered/expected, and moves
it into a separate helper method.

Line and column can now be initialized lazily. We'll use this later to
raise `ParserSyntaxError`s inside of conversion functions, backfilling
the positions inside `_base_parser`, since conversion functions don't
always have access to position information.
2019-08-22 13:35:11 -07:00
Jennifer Taylor
3fb60b9706 Make parser package private
Now that this is imported from the top level, make it private.
2019-07-26 12:48:22 -07:00