Skip to content

Commit

Permalink
feat: comments, boolean and delimiters regex
Browse files Browse the repository at this point in the history
  • Loading branch information
danielogen committed Aug 12, 2024
1 parent 3baab4e commit 536238d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
33 changes: 33 additions & 0 deletions src/PyReprism/languages/csharp.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,41 @@ def operator_regex():
def keywords_regex():
return re.compile(r'\b(' + '|'.join(CSharp.keywords()) + r')\b')

@staticmethod
def boolean_regex() -> re.Pattern:
"""
Compile and return a regular expression pattern to identify C# boolean literals.
This function generates a regular expression that matches the C# boolean literals `true`, `false`, and the special constant `null`.
:return: A compiled regex pattern to match C# boolean literals and `null`.
:rtype: re.Pattern
"""
return re.compile(r'\b(?:true|false|null)\b', re.IGNORECASE)

@staticmethod
def delimiters_regex() -> re.Pattern:
"""
Compile and return a regular expression pattern to identify C# delimiters.
This function generates a regular expression that matches C# language delimiters, which include parentheses `()`, brackets `[]`, braces `{}`, commas `,`, colons `:`, periods `.`, semicolons `;`, at symbols `@`, angle brackets `<`, `>`, and the question mark `?`.
:return: A compiled regex pattern to match C# delimiters.
:rtype: re.Pattern
"""
return re.compile(r'[()\[\]{}.,:;@<>?]')

@staticmethod
def remove_comments(source_code: str, isList: bool = False) -> str:
"""
Remove comments from the provided Python source code string.
:param str source_code: The Python source code from which to remove comments.
:param bool isList: (Optional) A flag indicating if the input is a list of source code lines. This parameter is not used in the function logic.
:return: The source code with all comments removed.
:rtype: str
"""
return CSharp.comment_regex().sub(lambda match: match.group('noncomment') if match.group('noncomment') else '', source_code).strip()
result = []
for match in CSharp.comment_regex().finditer(source_code):
if match.group('noncomment'):
Expand Down
25 changes: 25 additions & 0 deletions src/PyReprism/languages/dart.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,33 @@ def operator_regex():
def keywords_regex():
return re.compile(r'\b(' + '|'.join(Dart.keywords()) + r')\b')

@staticmethod
def delimiters_regex() -> re.Pattern:
"""
Compile and return a regular expression pattern to identify Dart language delimiters.
This function generates a regular expression that matches Dart language delimiters, which include parentheses `()`, brackets `[]`, braces `{}`, commas `,`, colons `:`, periods `.`, semicolons `;`, and angle brackets `<`, `>`.
:return: A compiled regex pattern to match Dart delimiters.
:rtype: re.Pattern
"""
return re.compile(r'[()\[\]{}.,:;<>]')

@staticmethod
def boolean_regex() -> re.Pattern:
"""
Compile and return a regular expression pattern to identify Dart boolean literals.
This function generates a regular expression that matches the Dart boolean literals `true`, `false`, and the special constant `null`.
:return: A compiled regex pattern to match Dart boolean literals and `null`.
:rtype: re.Pattern
"""
return re.compile(r'\b(?:true|false|null)\b')

@staticmethod
def remove_comments(source_code: str, isList: bool = False) -> str:
return Dart.comment_regex().sub(lambda match: match.group('noncomment') if match.group('noncomment') else '', source_code).strip()
result = []
for match in Dart.comment_regex().finditer(source_code):
if match.group('noncomment'):
Expand Down
9 changes: 3 additions & 6 deletions src/PyReprism/languages/go.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@ def operator_regex() -> re.Pattern:
@staticmethod
def keywords_regex() -> re.Pattern:
return re.compile(r'\b(' + '|'.join(Go.keywords()) + r')\b')

@staticmethod
def delimiters_regex() -> re.Pattern:
"""
Compile and return a regular expression pattern to identify Go delimiters.
This function generates a regular expression that matches Go language delimiters,
which include parentheses `()`, brackets `[]`, braces `{}`, commas `,`, colons `:`,
periods `.`, semicolons `;`, and the ellipsis `...`.
This function generates a regular expression that matches Go language delimiters, which include parentheses `()`, brackets `[]`, braces `{}`, commas `,`, colons `:`, periods `.`, semicolons `;`, and the ellipsis `...`.
:return: A compiled regex pattern to match Go delimiters.
:rtype: re.Pattern
Expand All @@ -53,8 +51,7 @@ def boolean_regex() -> re.Pattern:
"""
Compile and return a regular expression pattern to identify Go boolean literals.
This function generates a regular expression that matches the Go boolean literals
`true`, `false`, and the special constant `nil`.
This function generates a regular expression that matches the Go boolean literals `true`, `false`, and the special constant `nil`.
:return: A compiled regex pattern to match Go boolean literals and `nil`.
:rtype: re.Pattern
Expand Down

0 comments on commit 536238d

Please sign in to comment.