about summary refs log tree commit diff
path: root/compiler/rustc_parse/src/lexer
AgeCommit message (Collapse)AuthorLines
2023-02-28remove duplicated diagnostic for unclosed delimiteryukang-8/+9
2023-02-14Don't recover lifetimes/labels containing emojis as character literals许杰友 Jieyou Xu (Joe)-2/+7
Note that at the time of this commit, `unic-emoji-char` seems to have data tables only up to Unicode 5.0, but Unicode is already newer than this. A newer emoji such as `🥺` will not be recognized as an emoji but older emojis such as `🐱` will.
2023-02-06Migrate `rustc_parse` to derive diagnosticsclubby789-317/+150
2023-01-28Rollup merge of #104012 - chenyukang:yukang/fix-103882-deli-indentation, ↵Matthias Krüger-87/+162
r=petrochenkov Improve unexpected close and mismatch delimiter hint in TokenTreesReader Fixes #103882 Fixes #68987 Fixes #69259 The inner indentation mismatching will be covered by outer block, the new added function `report_error_prone_delim_block` will find out the error prone candidates for reporting.
2023-01-27Improve unexpected close and mismatch delimiter hint in TokenTreesReaderyukang-87/+162
2023-01-19Add double-equals homoglyphclubby789-0/+2
2023-01-19Use strings for homoglyph replacementsclubby789-320/+320
2023-01-14Emit only one nbsp error per fileDavid Tolnay-5/+26
2023-01-12Emit a single error for contiguous sequences of Unicode homoglyphsclubby789-6/+32
2022-11-27Prefer doc comments over `//`-comments in compilerMaybe Waffle-2/+2
2022-11-16Use `token::Lit` in `ast::ExprKind::Lit`.Nicholas Nethercote-1/+12
Instead of `ast::Lit`. Literal lowering now happens at two different times. Expression literals are lowered when HIR is crated. Attribute literals are lowered during parsing. This commit changes the language very slightly. Some programs that used to not compile now will compile. This is because some invalid literals that are removed by `cfg` or attribute macros will no longer trigger errors. See this comment for more details: https://github.com/rust-lang/rust/pull/102944#issuecomment-1277476773
2022-11-09Rollup merge of #103919 - nnethercote:unescaping-cleanups, r=matkladDylan DPC-83/+66
Unescaping cleanups Some code improvements, and some error message improvements. Best reviewed one commit at a time. r? ````@matklad````
2022-11-07Make underscore_literal_suffix a hard error.Nicholas Nethercote-11/+1
It's been a warning for 5.5 years. Time to make it a hard error. Closes #42326.
2022-11-04Inline and remove `validate_int_literal`.Nicholas Nethercote-18/+13
It has a single callsite, and is fairly small. The `Float` match arm already has base-specific checking inline, so this makes things more consistent.
2022-11-04Refactor `cook_lexer_literal`.Nicholas Nethercote-42/+36
It deals with eight cases: ints, floats, and the six quoted types (char/byte/strings). For ints and floats we have an early return, and the other six types fall through to the code at the end, which makes the function hard to read. This commit rearranges things to avoid the early returns.
2022-11-04Make non-ASCII errors more consistent.Nicholas Nethercote-19/+13
There are three kinds of "byte" literals: byte literals, byte string literals, and raw byte string literals. None are allowed to have non-ASCII chars in them. Two `EscapeError` variants exist for when that constraint is violated. - `NonAsciiCharInByte`: used for byte literals and byte string literals. - `NonAsciiCharInByteString`: used for raw byte string literals. As a result, the messages for raw byte string literals use different wording, without good reason. Also, byte string literals are incorrectly described as "byte constants" in some error messages. This commit eliminates `NonAsciiCharInByteString` so the three cases are handled similarly, and described correctly. The `mode` is enough to distinguish them. Note: Some existing error messages mention "byte constants" and some mention "byte literals". I went with the latter here, because it's a more correct name, as used by the Reference.
2022-11-03Use `Mode` less.Nicholas Nethercote-7/+7
It's passed to numerous places where we just need an `is_byte` bool. Passing the bool avoids the need for some assertions. Also rename `is_bytes()` as `is_byte()`, to better match `Mode::Byte`, `Mode::ByteStr`, and `Mode::RawByteStr`.
2022-10-23Rollup merge of #101293 - compiler-errors:lt-is-actually-char, r=estebankDylan DPC-2/+7
Recover when unclosed char literal is parsed as a lifetime in some positions Fixes #101278
2022-10-22Recover unclosed char literal being parsed as lifetimeMichael Goulet-2/+7
2022-10-22Properly escape quotes when suggesting switching between char/string literalsclubby789-2/+17
2022-10-03Invert `is_top_level` to avoid negation.Nicholas Nethercote-5/+5
2022-10-03Remove `TokenStreamBuilder`.Nicholas Nethercote-37/+20
It's now only used in one function. Also, the "should we glue the tokens?" check is only necessary when pushing a `TokenTree::Token`, not when pushing a `TokenTree::Delimited`. As part of this, we now do the "should we glue the tokens?" check immediately, which avoids having look back at the previous token. It also puts all the logic dealing with token gluing in a single place.
2022-10-03Inline and remove `parse_token_tree_non_delim_non_eof`.Nicholas Nethercote-16/+14
It has a single call site.
2022-10-03Merge `parse_token_trees_until_close_delim` and `parse_all_token_trees`.Nicholas Nethercote-24/+17
Because they're very similar, and this will allow some follow-up changes.
2022-09-28Address review comments.Nicholas Nethercote-8/+8
2022-09-27Rename some variables.Nicholas Nethercote-10/+10
These make the delimiter processing clearer.
2022-09-27Minor improvements.Nicholas Nethercote-3/+5
Add some comments, and mark one path as unreachable.
2022-09-26Inline and remove `cook_lexer_token`.Nicholas Nethercote-171/+174
This is a small performance win, alas.
2022-09-26Add `rustc_lexer::TokenKind::Eof`.Nicholas Nethercote-8/+2
For alignment with `rust_ast::TokenKind::Eof`. Plus it's a bit faster, due to less `Option` manipulation in `StringReader::next_token`.
2022-09-26Use less DRY in `cook_lexer_token`.Nicholas Nethercote-19/+19
This is a case where a small amount of repetition results in code that is faster and easier to read.
2022-09-26Make `rustc_lexer::cursor::Cursor` public.Nicholas Nethercote-10/+13
`Cursor` is currently hidden, and the main tokenization path uses `rustc_lexer::first_token` which involves constructing a new `Cursor` for every single token, which is weird. Also, `first_token` also can't handle empty input, so callers have to check for that first. This commit makes `Cursor` public, so `StringReader` can contain a `Cursor`, which results in a simpler structure. The commit also changes `StringReader::advance_token` so it returns an `Option<Token>`, simplifying the the empty input case.
2022-09-26[ui] Rearrange `StringReader`/`TokenTreesReader` creation.Nicholas Nethercote-20/+20
`TokenTreesReader` wraps a `StringReader`, but the `into_token_trees` function obscures this. This commit moves to a more straightforward control flow.
2022-09-26Remove `ast::Token::take`.Nicholas Nethercote-2/+1
Instead of replacing `TokenTreesReader::token` in two steps, we can just do it in one, which is both simpler and faster.
2022-09-26Remove `TokenTreesReader::bump`.Nicholas Nethercote-17/+9
It's an unnecessary layer that obfuscates when I am looking for optimizations.
2022-09-26Clarify spacing computation.Nicholas Nethercote-13/+20
The spacing computation is done in two parts. In the first part `next_token` and `bump` use `Spacing::Alone` to mean "preceded by whitespace" and `Spacing::Joint` to mean the opposite. In the second part `parse_token_tree_other` then adjusts the `spacing` value to mean the usual thing (i.e. "is the following token joinable punctuation?"). This shift in meaning is very confusing and it took me some time to understand what was going on. This commit changes the first part to use a bool, and adds some comments, which makes things much clearer.
2022-09-26Move `#!` checking.Nicholas Nethercote-9/+8
Currently does the "is this a `#!` at the start of the file?" check for every single token(!) This commit moves it so it only happens once.
2022-09-26Remove unnecessary `spacing` assignment.Nicholas Nethercote-1/+0
It has no useful effect.
2022-09-26Rearrange `TokenTreesReader::parse_token_tree`.Nicholas Nethercote-178/+168
`parse_token_tree` is basically a match with four arms: `Eof`, `OpenDelim`, `CloseDelim`, and "other". It has two call sites, and at each call site one of the arms is unreachable. It's also not inlined. This commit removes `parse_token_tree` by splitting it into four functions and inlining them. This avoids some repeated conditional tests and also some non-inlined function calls on the hot path.
2022-09-01Always import all tracing macros for the entire crate instead of piecemeal ↵Oli Scherer-8/+2
by module
2022-08-01Shrink `Token`.Nicholas Nethercote-23/+29
From 72 bytes to 12 bytes (on x86-64). There are two parts to this: - Changing various source code offsets from 64-bit to 32-bit. This is not a problem because the rest of rustc also uses 32-bit source code offsets. This means `Token` is no longer `Copy` but this causes no problems. - Removing the `RawStrError` from `LiteralKind`. Raw string literal invalidity is now indicated by a `None` value within `RawStr`/`RawByteStr`, and the new `validate_raw_str` function can be used to re-lex an invalid raw string literal to get the `RawStrError`. There is one very small change in behaviour. Previously, if a raw string literal matched both the `InvalidStarter` and `TooManyHashes` cases, the latter would override the former. This has now changed, because `raw_double_quoted_string` now uses `?` and so returns immediately upon detecting the `InvalidStarter` case. I think this is a slight improvement to report the earlier-detected error, and it explains the change in the `test_too_many_hashes` test. The commit also removes a couple of comments that refer to #77629 and say that the size of these types don't affect performance. These comments are wrong, though the performance effect is small.
2022-08-01Add a size assertion for `Token`.Nicholas Nethercote-0/+7
2022-08-01Inline `TokenStreamBuilder::push`.Nicholas Nethercote-0/+1
Because it's small and hot.
2022-08-01Avoid an unnecessary `return`.Nicholas Nethercote-2/+2
2022-08-01Remove `StringReader::end_src_index`.Nicholas Nethercote-5/+2
It not needed, always being set to the end of the text.
2022-08-01Improve shebang handling.Nicholas Nethercote-8/+5
Avoid doing stuff until it's necessary.
2022-07-29Remove `TreeAndSpacing`.Nicholas Nethercote-16/+12
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is not quite right. `Spacing` makes sense for `TokenTree::Token`, but does not make sense for `TokenTree::Delimited`, because a `TokenTree::Delimited` cannot be joined with another `TokenTree`. This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`, changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the `TreeAndSpacing` typedef. The commit removes these two impls: - `impl From<TokenTree> for TokenStream` - `impl From<TokenTree> for TreeAndSpacing` These were useful, but also resulted in code with many `.into()` calls that was hard to read, particularly for anyone not highly familiar with the relevant types. This commit makes some other changes to compensate: - `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`. - `TokenStream::token_{alone,joint}()` are added. - `TokenStream::delimited` is added. This results in things like this: ```rust TokenTree::token(token::Semi, stmt.span).into() ``` changing to this: ```rust TokenStream::token_alone(token::Semi, stmt.span) ``` This makes the type of the result, and its spacing, clearer. These changes also simplifies `Cursor` and `CursorRef`, because they no longer need to distinguish between `next` and `next_with_spacing`.
2022-06-13remove unnecessary `to_string` and `String::new`Takayuki Maeda-5/+5
2022-05-20Remove `crate` visibility usage in compilerJacob Pratt-1/+1
2022-04-29errors: `span_suggestion` takes `impl ToString`David Wood-2/+2
Change `span_suggestion` (and variants) to take `impl ToString` rather than `String` for the suggested code, as this simplifies the requirements on the diagnostic derive. Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-28rustc_ast: Harmonize delimiter naming with `proc_macro::Delimiter`Vadim Petrochenkov-22/+22