Commit graph

18 commits

Author SHA1 Message Date
jimmylai
b232547826
[typing] refactor TypeVar CSTNodeT for consistency and reusability (#54) 2019-08-29 21:30:34 -07:00
Jennifer Taylor
9a90fda8c3 Document additional caveats with parse_statement and parse_expression. 2019-08-29 11:33:57 -07:00
Jennifer Taylor
6dca3c914e Fix inability to parse certain expressions that ast.parse parses. 2019-08-29 11:33:57 -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
aac5103592 Add PartialParserSyntaxError, simplify ParserSyntaxError
Because the parser conversion functions may not understand their current
position in the code, we instead need to construct a `ParserSyntaxError`
without the current line and column information, filling it in later.

Originally I did this by allowing ParserSyntaxError to be partially
initialized, but this wasn't very type-safe, and required some ugly
assertions.

Instead, I now construct and raise a PartialParserSyntaxError. The
BaseParser class is responsible for catching that partial error object
and constructing a full ParserSyntaxError.
2019-08-22 13:35:11 -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
636cad6e6c Raise a ParserSyntaxError on mismatched braces
If we had more close braces than open braces, we used to raise an
IndexError due to an empty stack. This fixes that bug.
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
Benjamin Woodruff
53a7c782d4 Improve PartialParserConfig's documentation
Adds details about how various configuration options can be used, and
updates some details to reflect reality (e.g. we don't currently infer
the python version from your execution environment).
2019-08-19 17:09:55 -07:00
Benjamin Woodruff
821a613c46 Raise an exception on unsupported python_version
We only support parsing code as 3.7 right now.

This raises a descriptive error message if we receive an unsupported
version number, instead of silently trying to use it with the tokenizer
and failing in potentially strange ways.
2019-08-19 17:09:55 -07:00
Jennifer Taylor
d3ce7da7ce Drop annotation indicator attribute.
We pass down the correct default annotation indicator to use in 100% of code rendering places, so this becomes a useless bit of initialization. We already set this to a sentinel by default, and the only thing that having an explicit str gives us is the inability to copy an annotation from a param to a return or vice versa. So, out it goes. This means we can't render Annotation by itself, so the test that was using this behavior is out too.
2019-08-05 17:13:17 -07:00
Jennifer Taylor
9ca3d39fb6 Rename Ellipses to Ellipsis to match AST and Python stuff. 2019-08-02 17:17:38 -07:00
Jennifer Taylor
64ff2b1996 Better repr for partial configuration.
This adds a __repr__ to various types in the parser config, so that the
generated documentation for functions using these types renders easier
to read.
2019-08-01 16:14:12 -07:00
Jennifer Taylor
47a3c75670 Add documentation for parse_module and parse_expression. 2019-08-01 14:55:19 -07:00
Jennifer Taylor
47be0b1681 Hardcode parser version to Python 3.7 for now. 2019-08-01 14:04:38 -07:00
Benjamin Woodruff
c7fa739087 Add parser for Dict and DictComp
This finishes the parser! 🎉

A few related changes were made:
- `DummyNode` was removed, as it's no longer needed.
- `lpar`/`rpar` was removed from `StarredDictElement`, because I found
  out that it's not valid syntax while developing the parsing logic.
2019-07-31 13:07:01 -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