|
2 | 2 | from PyReprism.utils import extension
|
3 | 3 |
|
4 | 4 |
|
5 |
| -class Dart: |
| 5 | +class Rust: |
6 | 6 | def __init__():
|
7 | 7 | pass
|
8 | 8 |
|
9 | 9 | @staticmethod
|
10 | 10 | def file_extension() -> str:
|
11 |
| - return extension.dart |
| 11 | + return extension.rust |
12 | 12 |
|
13 | 13 | @staticmethod
|
14 | 14 | def keywords() -> list:
|
15 | 15 | keyword = 'abstract|alignof|as|be|box|break|const|continue|crate|do|else|enum|extern|false|final|fn|for|if|impl|in|let|loop|match|mod|move|mut|offsetof|once|override|priv|pub|pure|ref|return|sizeof|static|self|struct|super|true|trait|type|typeof|unsafe|unsized|use|virtual|where|while|yield'.split('|')
|
16 | 16 | return keyword
|
17 | 17 |
|
18 | 18 | @staticmethod
|
19 |
| - def comment_regex(): |
| 19 | + def comment_regex() -> re.Pattern: |
20 | 20 | pattern = re.compile(r'(?P<comment>//.*?$|///.*?$|/\*[\s\S]*?\*/|/\*.*?$|^.*?\*/)|(?P<noncomment>\'(\\.|[^\\\'])*\'|"(\\.|[^\\"])*"|.[^/\'"]*)', re.DOTALL | re.MULTILINE)
|
21 | 21 | return pattern
|
22 | 22 |
|
23 | 23 | @staticmethod
|
24 |
| - def number_regex(): |
| 24 | + def number_regex() -> re.Pattern: |
25 | 25 | pattern = re.compile(r'\b(?:0x[\dA-Fa-f](?:_?[\dA-Fa-f])*|0o[0-7](?:_?[0-7])*|0b[01](?:_?[01])*|(\d(?:_?\d)*)?\.?\d(?:_?\d)*(?:[Ee][+-]?\d+)?)(?:_?(?:[iu](?:8|16|32|64)?|f32|f64))?\b')
|
26 | 26 | return pattern
|
27 | 27 |
|
28 | 28 | @staticmethod
|
29 |
| - def operator_regex(): |
| 29 | + def operator_regex() -> re.Pattern: |
30 | 30 | pattern = re.compile(r'[-+*\/%!^]=?|=[=>]?|@|&[&=]?|\|[|=]?|<<?=?|>>?=?')
|
31 | 31 | return pattern
|
32 | 32 |
|
33 | 33 | @staticmethod
|
34 |
| - def keywords_regex(): |
35 |
| - return re.compile(r'\b(' + '|'.join(Dart.keywords()) + r')\b') |
| 34 | + def keywords_regex() -> re.Pattern: |
| 35 | + return re.compile(r'\b(' + '|'.join(Rust.keywords()) + r')\b') |
| 36 | + |
| 37 | + @staticmethod |
| 38 | + def boolean_regex() -> re.Pattern: |
| 39 | + """ |
| 40 | + Compile and return a regular expression pattern to identify Rust boolean literals. |
| 41 | +
|
| 42 | + This function generates a regular expression that matches the Rust boolean literals `true`, `false`, and the special constant `None`. |
| 43 | +
|
| 44 | + :return: A compiled regex pattern to match Rust boolean literals and `None`. |
| 45 | + :rtype: re.Pattern |
| 46 | + """ |
| 47 | + return re.compile(r'\b(?:true|false|None)\b') |
| 48 | + |
| 49 | + @staticmethod |
| 50 | + def rust_delimiters_regex() -> re.Pattern: |
| 51 | + """ |
| 52 | + Compile and return a regular expression pattern to identify Rust language delimiters. |
| 53 | +
|
| 54 | + This function generates a regular expression that matches Rust language delimiters, which include parentheses `()`, brackets `[]`, braces `{}`, commas `,`, colons `:`, periods `.`, semicolons `;`, angle brackets `<`, `>`, and the question mark `?`. |
| 55 | +
|
| 56 | + :return: A compiled regex pattern to match Rust delimiters. |
| 57 | + :rtype: re.Pattern |
| 58 | + """ |
| 59 | + return re.compile(r'[()\[\]{}.,:;<>?]') |
36 | 60 |
|
37 | 61 | @staticmethod
|
38 | 62 | def remove_comments(source_code: str, isList: bool = False) -> str:
|
| 63 | + """ |
| 64 | + Remove comments from the provided Rust source code string. |
| 65 | +
|
| 66 | + :param str source_code: The Rust source code from which to remove comments. |
| 67 | + :return: The source code with all comments removed. |
| 68 | + :rtype: str |
| 69 | + """ |
| 70 | + return Rust.comment_regex().sub(lambda match: match.group('noncomment') if match.group('noncomment') else '', source_code).strip() |
39 | 71 | result = []
|
40 |
| - for match in Dart.comment_regex().finditer(source_code): |
| 72 | + for match in Rust.comment_regex().finditer(source_code): |
41 | 73 | if match.group('noncomment'):
|
42 | 74 | result.append(match.group('noncomment'))
|
43 | 75 | if isList:
|
44 | 76 | return result
|
45 | 77 | return ''.join(result)
|
46 | 78 |
|
47 | 79 | @staticmethod
|
48 |
| - def remove_keywords(source: str): |
49 |
| - return re.sub(re.compile(Dart.keywords_regex()), '', source) |
| 80 | + def remove_keywords(source: str) -> str: |
| 81 | + return re.sub(re.compile(Rust.keywords_regex()), '', source) |
0 commit comments