Skip to content

Add support for character classes [...] #250

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

Draft
wants to merge 9 commits into
base: develop
Choose a base branch
from

Conversation

StefanosChaliasos
Copy link

@StefanosChaliasos StefanosChaliasos commented Mar 6, 2025

Fixes #249

It also makes the quantifiers more expressive:

I.e., now it supports: {,4}, {4}, {1,3}, {1,} instead of just {1,3} and {1,}

@eliotwrobson
Copy link
Collaborator

@StefanosChaliasos thanks for this contribution! A bit busy today but I'll try to review this at some point in the evening.

@coveralls
Copy link

coveralls commented Mar 6, 2025

Coverage Status

coverage: 98.008% (-1.6%) from 99.613%
when pulling df53edc on StefanosChaliasos:add-support-for-charclass
into 1bdf9b7 on caleb531:develop.

@eliotwrobson eliotwrobson changed the base branch from main to develop March 6, 2025 18:52
Copy link
Collaborator

@eliotwrobson eliotwrobson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like some dead code was included accidentally? Otherwise the logic itself seems fine.

if is_negated:
expanded_content = expanded_content[1:] # Remove ^ from the content

return cls(match.group(), expanded_content, is_negated)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like there are some missing fields that are used in the constructor? Shouldn't this line throw an exception?

EDIT: Based on the coverage report, this line isn't being hit at all.

greek_nfa = NFA.from_regex("[Ͱ-Ͽ]+", input_symbols=input_symbols)
cyrillic_nfa = NFA.from_regex("[Ѐ-ӿ]+", input_symbols=input_symbols)

latin_samples = ["¡", "£", "Ā", "ŕ", "ƿ"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use the \u... notation? This will make these tests easier to maintain (albiet less elegant in the editor).

self.counter = counter

@classmethod
def from_match(cls: Type[Self], match: re.Match) -> Self:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the logic here heavily overlaps with the process_char_class function. Could one of these be made to call the other?

)

lexer.register_token(
character_class_factory,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would personally prefer to use the from_match syntax the way the other token types are registered, but either syntax is fine. But it seems like the from_match in the new token class isn't being called at all.

@@ -577,3 +691,50 @@ def parse_regex(regexstr: str, input_symbols: AbstractSet[str]) -> NFARegexBuild
postfix = tokens_to_postfix(tokens_with_concats)

return parse_postfix_tokens(postfix)


def process_char_class(class_str: str) -> Tuple[bool, Set[str]]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Might be good to have a couple of small test cases for this function independently to aid in debugging later, but won't make any hard requests for this.

@StefanosChaliasos
Copy link
Author

Will go over everything tomorrow. Thanks a lot for the feedback.

@StefanosChaliasos
Copy link
Author

I did some more changes, can you review the new ones. Basically I added support for shorthand (e.g., '\d') and I tokenised whitespace. I need to add more tests and polish the code. I'll change the PR as a draft until done.

@StefanosChaliasos StefanosChaliasos marked this pull request as draft March 7, 2025 08:54
Comment on lines +234 to +245
if "\\s" in regex:
additional_symbols.update(WHITESPACE_CHARS)
if "\\S" in regex:
additional_symbols.update(NON_WHITESPACE_CHARS)
if "\\d" in regex:
additional_symbols.update(DIGIT_CHARS)
if "\\D" in regex:
additional_symbols.update(NON_DIGIT_CHARS)
if "\\w" in regex:
additional_symbols.update(WORD_CHARS)
if "\\W" in regex:
additional_symbols.update(NON_WORD_CHARS)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@StefanosChaliasos Can you please refactor this to use a dict-based lookup table? That would make this much less repetitive.

cc @eliotwrobson

Comment on lines +221 to +228
from automata.regex.parser import (
DIGIT_CHARS,
NON_DIGIT_CHARS,
NON_WHITESPACE_CHARS,
NON_WORD_CHARS,
WHITESPACE_CHARS,
WORD_CHARS,
)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@StefanosChaliasos Can you please keep all imports at the top of the file? There's no particular need for the tighter scoping here, IMO.

cc @eliotwrobson

additional_symbols.update(WORD_CHARS)
pos += 2
continue
elif class_content[pos + 1] in "S":
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@StefanosChaliasos What is the intention of using in here as opposed to ==? If the right-hand side is just a single character, the only difference that seems to make is permitting class_content[pos + 1] to be empty string (in addition to the character itself). In other words:

"S" in "S" True
"" in "S"  # True

continue

# Handle escape sequence in character class
from automata.regex.parser import _handle_escape_sequences
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@StefanosChaliasos Can you also please move this import to the top of the file?

@@ -24,10 +25,21 @@
validate_tokens,
)

# Add these at the top of the file to define our shorthand character sets
ASCII_PRINTABLE_CHARS = frozenset(string.printable)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eliotwrobson The implication here is that only ASCII characters are deemed as printable characters, but how would that work given that #233 just added support for Unicode characters?

@caleb531
Copy link
Owner

Hey, @StefanosChaliasos! I left some additional comments on the PR—apologies if they seem nitpicky, but just wanting to maintain solid code quality and consistency for this project.

@StefanosChaliasos
Copy link
Author

Thanks for the review, I will address the comments once I find some time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regex: Support for character classes [...]
4 participants