mirror of
https://github.com/tcsenpai/scripting-language-factory.git
synced 2025-06-05 18:55:28 +00:00
vscode generator now is standalone too ** also fixed a string problem
This commit is contained in:
parent
7110ae4eec
commit
06a8b3db6a
4
.gitignore
vendored
4
.gitignore
vendored
@ -4,4 +4,6 @@ __pycache__/
|
|||||||
*.pyd
|
*.pyd
|
||||||
*.pyw
|
*.pyw
|
||||||
*.pyz
|
*.pyz
|
||||||
*.pywz
|
*.pywz+
|
||||||
|
|
||||||
|
custom_langs/
|
||||||
|
@ -142,9 +142,36 @@ class Transpiler:
|
|||||||
Returns:
|
Returns:
|
||||||
str: Code with keywords replaced
|
str: Code with keywords replaced
|
||||||
"""
|
"""
|
||||||
for keyword in self.sorted_keywords:
|
# Split the code into string literals and non-string parts
|
||||||
code = self.patterns[keyword].sub(self.mapping[keyword], code)
|
parts = self._split_by_strings(code)
|
||||||
return code
|
|
||||||
|
# Only apply replacements to non-string parts
|
||||||
|
for i in range(0, len(parts), 2): # Even indices are non-string parts
|
||||||
|
for keyword in self.sorted_keywords:
|
||||||
|
parts[i] = self.patterns[keyword].sub(self.mapping[keyword], parts[i])
|
||||||
|
|
||||||
|
# Rejoin the parts
|
||||||
|
return ''.join(parts)
|
||||||
|
|
||||||
|
def _split_by_strings(self, code):
|
||||||
|
"""
|
||||||
|
Split code into string literals and non-string parts.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
code (str): Source code
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
list: Alternating non-string and string parts
|
||||||
|
"""
|
||||||
|
# Regex to match string literals (handles both single and double quotes)
|
||||||
|
# Accounts for escaped quotes within strings
|
||||||
|
string_pattern = r'(\'(?:\\\'|[^\'])*\'|"(?:\\"|[^"])*")'
|
||||||
|
|
||||||
|
# Split the code by string literals
|
||||||
|
parts = re.split(string_pattern, code)
|
||||||
|
|
||||||
|
# parts will have non-string parts at even indices and string literals at odd indices
|
||||||
|
return parts
|
||||||
|
|
||||||
def _fix_syntax_issues(self, code):
|
def _fix_syntax_issues(self, code):
|
||||||
"""
|
"""
|
||||||
@ -207,9 +234,17 @@ class Transpiler:
|
|||||||
"""
|
"""
|
||||||
result = python_code
|
result = python_code
|
||||||
|
|
||||||
# Apply regular word replacements first
|
# Split the code into string literals and non-string parts
|
||||||
for keyword in self.sorted_reverse_keywords:
|
parts = self._split_by_strings(result)
|
||||||
result = self.reverse_patterns[keyword].sub(self.reverse_mapping[keyword], result)
|
|
||||||
|
# Only apply replacements to non-string parts
|
||||||
|
for i in range(0, len(parts), 2): # Even indices are non-string parts
|
||||||
|
# Apply regular word replacements
|
||||||
|
for keyword in self.sorted_reverse_keywords:
|
||||||
|
parts[i] = self.reverse_patterns[keyword].sub(self.reverse_mapping[keyword], parts[i])
|
||||||
|
|
||||||
|
# Rejoin the parts
|
||||||
|
result = ''.join(parts)
|
||||||
|
|
||||||
# Then apply special patterns in reverse
|
# Then apply special patterns in reverse
|
||||||
result = self._apply_reverse_special_patterns(result)
|
result = self._apply_reverse_special_patterns(result)
|
||||||
@ -226,26 +261,33 @@ class Transpiler:
|
|||||||
Returns:
|
Returns:
|
||||||
str: Code with special patterns reversed
|
str: Code with special patterns reversed
|
||||||
"""
|
"""
|
||||||
for pattern, replacement in self.compiled_special_patterns.items():
|
# Split the code into string literals and non-string parts
|
||||||
# Create reverse pattern
|
parts = self._split_by_strings(code)
|
||||||
reverse_pattern = re.compile(r'\b' + re.escape(replacement) + r'\b')
|
|
||||||
# Extract capture groups if any
|
# Only apply replacements to non-string parts
|
||||||
match = re.search(r'\\(\d+)', pattern.pattern)
|
for i in range(0, len(parts), 2): # Even indices are non-string parts
|
||||||
if match:
|
for pattern, replacement in self.compiled_special_patterns.items():
|
||||||
# If there are capture groups, we need to handle them specially
|
# Create reverse pattern
|
||||||
capture_group = int(match.group(1))
|
reverse_pattern = re.compile(r'\b' + re.escape(replacement) + r'\b')
|
||||||
# Find all matches of the reverse pattern
|
# Extract capture groups if any
|
||||||
matches = reverse_pattern.finditer(code)
|
match = re.search(r'\\(\d+)', pattern.pattern)
|
||||||
for m in matches:
|
if match:
|
||||||
# Extract the captured value
|
# If there are capture groups, we need to handle them specially
|
||||||
captured = m.group(capture_group) if capture_group <= len(m.groups()) else ""
|
capture_group = int(match.group(1))
|
||||||
# Replace with the original pattern format
|
# Find all matches of the reverse pattern
|
||||||
original_format = pattern.pattern.replace(f'\\{capture_group}', captured)
|
matches = reverse_pattern.finditer(parts[i])
|
||||||
code = code.replace(m.group(0), original_format)
|
for m in matches:
|
||||||
else:
|
# Extract the captured value
|
||||||
# Simple replacement
|
captured = m.group(capture_group) if capture_group <= len(m.groups()) else ""
|
||||||
code = reverse_pattern.sub(pattern.pattern, code)
|
# Replace with the original pattern format
|
||||||
return code
|
original_format = pattern.pattern.replace(f'\\{capture_group}', captured)
|
||||||
|
parts[i] = parts[i].replace(m.group(0), original_format)
|
||||||
|
else:
|
||||||
|
# Simple replacement
|
||||||
|
parts[i] = reverse_pattern.sub(pattern.pattern, parts[i])
|
||||||
|
|
||||||
|
# Rejoin the parts
|
||||||
|
return ''.join(parts)
|
||||||
|
|
||||||
def transpile_file(self, input_file, output_file=None, reverse=False):
|
def transpile_file(self, input_file, output_file=None, reverse=False):
|
||||||
"""
|
"""
|
||||||
|
@ -272,4 +272,12 @@ def generate_vscode_extension(mapping_file, output_dir=None):
|
|||||||
# Create README.md
|
# Create README.md
|
||||||
with open(os.path.join(output_dir, "README.md"), 'w') as f:
|
with open(os.path.join(output_dir, "README.md"), 'w') as f:
|
||||||
f.write(f"# {language_name} VS Code Extension\n\n")
|
f.write(f"# {language_name} VS Code Extension\n\n")
|
||||||
f.write(f"This extension provides syntax highlighting for {language_name} files.\n")
|
f.write(f"This extension provides syntax highlighting for {language_name} files.\n")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parser = argparse.ArgumentParser(description="Generate a VS Code extension for syntax highlighting based on a mapping file.")
|
||||||
|
parser.add_argument("mapping_file", help="Path to the mapping file")
|
||||||
|
parser.add_argument("-o", "--output-dir", help="Output directory for the extension", default=None)
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
generate_vscode_extension(args.mapping_file, args.output_dir)
|
Loading…
x
Reference in New Issue
Block a user