Skip to content

fix: proper error on unterminated quoted string #936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: Alpha-v9.5.3
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/tagstudio/core/library/alchemy/visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def visit_constraint(self, node: Constraint) -> ColumnElement[bool]:
# raise exception if Constraint stays unhandled
raise NotImplementedError("This type of constraint is not implemented yet")

def visit_property(self, node: Property) -> None:
def visit_property(self, node: Property) -> ColumnElement[bool]:
raise NotImplementedError("This should never be reached!")

def visit_not(self, node: Not) -> ColumnElement[bool]:
Expand Down
6 changes: 3 additions & 3 deletions src/tagstudio/core/query_lang/ast.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import ABC, abstractmethod
from enum import Enum
from typing import Generic, TypeVar
from typing import Generic, TypeVar, Union


class ConstraintType(Enum):
Expand All @@ -12,7 +12,7 @@ class ConstraintType(Enum):
Special = 5

@staticmethod
def from_string(text: str) -> "ConstraintType":
def from_string(text: str) -> Union["ConstraintType", None]:
return {
"tag": ConstraintType.Tag,
"tag_id": ConstraintType.TagID,
Expand All @@ -24,7 +24,7 @@ def from_string(text: str) -> "ConstraintType":


class AST:
parent: "AST" = None
parent: Union["AST", None] = None

def __str__(self):
class_name = self.__class__.__name__
Expand Down
14 changes: 8 additions & 6 deletions src/tagstudio/core/query_lang/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ class Token:
start: int
end: int

def __init__(self, type: TokenType, value: Any, start: int = None, end: int = None) -> None:
def __init__(self, type: TokenType, value: Any, start: int, end: int) -> None:
self.type = type
self.value = value
self.start = start
self.end = end

@staticmethod
def from_type(type: TokenType, pos: int = None) -> "Token":
def from_type(type: TokenType, pos: int) -> "Token":
return Token(type, None, pos, pos)

@staticmethod
def EOF() -> "Token": # noqa: N802
return Token.from_type(TokenType.EOF)
def EOF(pos: int) -> "Token": # noqa: N802
return Token.from_type(TokenType.EOF, pos)

def __str__(self) -> str:
return f"Token({self.type}, {self.value}, {self.start}, {self.end})" # pragma: nocover
Expand All @@ -50,7 +50,7 @@ def __repr__(self) -> str:
class Tokenizer:
text: str
pos: int
current_char: str
current_char: str | None

ESCAPABLE_CHARS = ["\\", '"', '"']
NOT_IN_ULITERAL = [":", " ", "[", "]", "(", ")", "=", ","]
Expand All @@ -63,7 +63,7 @@ def __init__(self, text: str) -> None:
def get_next_token(self) -> Token:
self.__skip_whitespace()
if self.current_char is None:
return Token.EOF()
return Token.EOF(self.pos)

if self.current_char in ("'", '"'):
return self.__quoted_string()
Expand Down Expand Up @@ -119,6 +119,8 @@ def __quoted_string(self) -> Token:
out = ""

while escape or self.current_char != quote:
if self.current_char is None:
raise ParsingError(start, self.pos, "Unterminated quoted string")
if escape:
escape = False
if self.current_char not in Tokenizer.ESCAPABLE_CHARS:
Expand Down