Skip to content

Commit

Permalink
Fix parsing identifiers with '=' symbol
Browse files Browse the repository at this point in the history
See #568.
  • Loading branch information
pfoerster committed Jul 7, 2023
1 parent d97d4de commit 5e657d8
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fix parsing paths with `|` ([#568](https://github.com/latex-lsp/texlab/issues/568))
- Fix parsing LaTeX identifiers with `=` ([#568](https://github.com/latex-lsp/texlab/issues/568))

## [5.7.0] - 2023-06-07

Expand Down
23 changes: 11 additions & 12 deletions crates/parser/src/latex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,19 +340,18 @@ impl<'a> Parser<'a> {
}

fn key(&mut self) {
self.key_with_eq(true);
}

fn key_with_eq(&mut self, allow_eq: bool) {
self.builder.start_node(KEY.into());
self.eat();
while self
.peek()
.filter(|&kind| {
matches!(
kind,
Token::Whitespace | Token::LineComment | Token::Word | Token::Pipe
)
})
.is_some()
{
self.eat();
while let Some(kind) = self.peek() {
match kind {
Token::Whitespace | Token::LineComment | Token::Word | Token::Pipe => self.eat(),
Token::Eq if allow_eq => self.eat(),
_ => break,
}
}

self.trivia();
Expand All @@ -375,7 +374,7 @@ impl<'a> Parser<'a> {

fn key_value_pair(&mut self) {
self.builder.start_node(KEY_VALUE_PAIR.into());
self.key();
self.key_with_eq(false);
if self.peek() == Some(Token::Eq) {
self.eat();
self.trivia();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: crates/parser/src/latex.rs
expression: root
input_file: crates/parser/src/test_data/latex/issue_568.txt
---
ROOT@0..51
PREAMBLE@0..51
LATEX_INCLUDE@0..36
COMMAND_NAME@0..6 "\\input"
CURLY_GROUP_WORD_LIST@6..36
L_CURLY@6..7 "{"
WORD@7..8 "|"
KEY@8..34
WORD@8..15 "ipython"
WHITESPACE@15..16 " "
WORD@16..34 "scripts/test.ipynb"
R_CURLY@34..35 "}"
LINE_BREAK@35..36 "\n"
LABEL_DEFINITION@36..51
COMMAND_NAME@36..42 "\\label"
CURLY_GROUP_WORD@42..51
L_CURLY@42..43 "{"
KEY@43..50
WORD@43..48 "fig:x"
EQUALITY_SIGN@48..49 "="
WORD@49..50 "2"
R_CURLY@50..51 "}"

1 change: 1 addition & 0 deletions crates/parser/src/test_data/latex/issue_568.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
\input{|ipython scripts/test.ipynb}
\label{fig:x=2}
15 changes: 11 additions & 4 deletions crates/syntax/src/latex/cst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,19 @@ impl Eq for Key {}
impl ToString for Key {
fn to_string(&self) -> String {
let mut buf = String::new();
for word in self.words() {
buf.push_str(word.text());
buf.push(' ');
for token in self
.syntax()
.children_with_tokens()
.filter_map(|node| node.into_token())
{
if matches!(token.kind(), WHITESPACE | LINE_BREAK | COMMENT) {
buf.push(' ');
} else {
buf.push_str(token.text());
}
}

buf.pop().unwrap();
buf = String::from(buf.trim());
buf
}
}
Expand Down

0 comments on commit 5e657d8

Please sign in to comment.