Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion sqlglotrs/src/token.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::settings::TokenType;
use pyo3::prelude::*;
use pyo3::types::{PyList, PyString};
use pyo3::{pyclass, Py, PyObject, Python};
use pyo3::{pyclass, pymethods, Py, PyObject, Python};

#[derive(Debug)]
#[pyclass]
Expand Down Expand Up @@ -57,3 +57,25 @@ impl Token {
});
}
}

#[pymethods]
impl Token {
fn __repr__(&self, py: Python) -> PyResult<String> {
let text = self.text.bind(py).to_str()?;
let comments = self.comments.bind(py);
let token_type_str = self.token_type_py.bind(py).str()?;
let comments_repr = comments.repr()?;
let comments_str = comments_repr.to_str()?;

Ok(format!(
"<Token token_type: {}, text: {}, line: {}, col: {}, start: {}, end: {}, comments: {}>",
token_type_str,
text,
self.line,
self.col,
self.start,
self.end,
comments_str
))
}
}
7 changes: 7 additions & 0 deletions tests/test_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,10 @@ def test_partial_token_list(self):
self.assertEqual(len(partial_tokens), 1)
self.assertEqual(partial_tokens[0].token_type, TokenType.VAR)
self.assertEqual(partial_tokens[0].text, "foo")

def test_token_repr(self):
# Ensures both the Python and the Rust tokenizer produce a human-friendly representation
self.assertEqual(
repr(Tokenizer().tokenize("foo")),
"[<Token token_type: TokenType.VAR, text: foo, line: 1, col: 3, start: 0, end: 2, comments: []>]",
)