Skip to content

Commit

Permalink
chore: moved theme setting to another folder
Browse files Browse the repository at this point in the history
  • Loading branch information
chaaals committed May 22, 2024
1 parent b9ff82c commit 6dc0bc2
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 145 deletions.
130 changes: 0 additions & 130 deletions constants/theme.py

This file was deleted.

36 changes: 21 additions & 15 deletions src/components/code_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from customtkinter import *
from tkinter import *
from constants.path import *
from constants.theme import UWU_COMPILER_THEME
from theme import themes

from src.lexer import Lexer, Token, Error
from src.lexer.token import UniqueTokenType
Expand All @@ -17,7 +17,8 @@
from enum import Enum
from PIL import Image


# edit theme to change the color scheme
EDITOR_THEME = themes['tokyo']
class Linenums(CTkCanvas):
def __init__(self, master, text_widget: CTkTextbox, **kwargs):
super().__init__(master, **kwargs)
Expand Down Expand Up @@ -153,13 +154,17 @@ def on_idle_gui(self, event = None):
typing_timer = self.text.after(1500, self.syntax_highlight)

def syntax_highlight(self, event = None):
src = [v if v else v + '\n' for v in self.text.get('1.0', 'end-1c').split('\n')]
# print(src)
# print(self.text.get('1.0', END).strip().split('\n'))
lx_tokens = self.lexer(src)
self.text.tag_delete(*self.text.tag_names())
self.source_code = [v if v else v + '\n' for v in self.text.get('1.0', 'end-1c').split('\n')]

lx = self.lexer(self.source_code)
self.tokens = lx.tokens
self.lx_errors = lx.errors

for i,token in enumerate(lx_tokens.tokens):
for tag in self.text.tag_names():
if tag not in ['token_highlight', 'error_highlight']:
self.text.tag_delete(tag)

for i,token in enumerate(self.tokens):
if(token.token.token == 'WHITESPACE'):
continue

Expand All @@ -177,22 +182,22 @@ def syntax_highlight(self, event = None):
if(isinstance(token.token, UniqueTokenType)):
tok = token.token.unique_type.split('_')[0]

if(tok == 'IDENTIFIER' and lx_tokens.tokens[i - 1].token.token == '.'):
if(tok == 'IDENTIFIER' and self.tokens[i - 1].token.token == '.'):
tok = 'METHOD'
else:
tok = token.token.token

fg = UWU_COMPILER_THEME[tok]
fg = EDITOR_THEME[tok]
self.text.tag_config(token_tag_name, foreground=fg)
except:
self.text.tag_config(token_tag_name, foreground=UWU_COMPILER_THEME['default'])
self.text.tag_config(token_tag_name, foreground=EDITOR_THEME['default'])

def run_lexer(self) -> bool:
self.source_code = [v if v else v + '\n' for v in self.text.get('1.0', 'end-1c').split('\n')]
lx: Lexer = self.lexer(self.source_code)
# self.source_code = [v if v else v + '\n' for v in self.text.get('1.0', 'end-1c').split('\n')]
# lx: Lexer = self.lexer(self.source_code)

self.tokens = lx.tokens
self.lx_errors = lx.errors
# self.tokens = lx.tokens
# self.lx_errors = lx.errors

if(len(self.lx_errors) > 0 and self.program):
self.program = None
Expand Down Expand Up @@ -418,6 +423,7 @@ def auto_format_code(self):
# Replace source code with formatted string
self.editor.text.delete("1.0", END)
self.editor.text.insert("1.0", self.editor.program.formatted_string())
self.editor.syntax_highlight()

# Reset states
self.editor.lx_errors = []
Expand Down
12 changes: 12 additions & 0 deletions theme/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'''
This file contains compiler theme configurations and color schemes.
Use the sample file to make more templates
'''

from .tokyo import tokyo

themes = {
'tokyo' : tokyo.config
# Add more themes
}
133 changes: 133 additions & 0 deletions theme/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
from enum import Enum

class ThemeColors():
def __init__(self, colors: dict[str, str]):
self.colors = colors
self._assign_colors()

def _assign_colors(self):
for key, value in self.colors.items():
setattr(self, key, value)

def __str__(self):
return str(self.colors)

MAIN = "MAIN"
GLOBAL = "GLOBAL"
KEYWORD = "KEYWORD"
IDENTIFIER = "IDENTIFIER"
CWASS = "CWASS"
METHOD = "METHOD"
CONTROL_STRUCTURE = "CONTROL_STRUCTURE"
CONSTANT_TYPE = "CONSTANT_TYPE"
TYPE = "TYPE"
COMMENT = "COMMENT"
GROUPING = "GROUPING"
NUMBER = "NUMBER"
BOOL = "BOOL"
STRING = "STRING"
NUWW = "NUWW"
ARITHMETIC_OPERATOR = "ARITHMETIC_OPERATOR"
LOGICAL_OPERATOR = "LOGICAL_OPERATOR"
UNARY_OPERATOR = "UNARY_OPERATOR"
OTHER_SYMBOL = "OTHER_SYMBOL"
TERMINATOR = "TERMINATOR"
DEFAULT = "DEFAULT"

class ThemeConfig():
def __init__(self, theme_colors: ThemeColors):
self.theme_colors = theme_colors

@property
def config(self) -> dict[str, ThemeColors]:
return {
# main
'mainuwu' : self.theme_colors.MAIN,

# globals and keywords
'fwunc' : self.theme_colors.GLOBAL,
'gwobaw' : self.theme_colors.GLOBAL,
'cwass' : self.theme_colors.GLOBAL,
'inpwt' : self.theme_colors.KEYWORD,
'pwint' : self.theme_colors.KEYWORD,
'wetuwn' : self.theme_colors.KEYWORD,

# identifiers
'IDENTIFIER' : self.theme_colors.IDENTIFIER,
'CWASS' : self.theme_colors.CWASS,

# types
'chan' : self.theme_colors.TYPE,
'kun' : self.theme_colors.TYPE,
'sama' : self.theme_colors.TYPE,
'senpai' : self.theme_colors.TYPE,
'san' : self.theme_colors.TYPE,
'dono' : self.theme_colors.CONSTANT_TYPE,

# arith operators
"=" : self.theme_colors.ARITHMETIC_OPERATOR,
"+" : self.theme_colors.ARITHMETIC_OPERATOR,
"-" : self.theme_colors.ARITHMETIC_OPERATOR,
"*" : self.theme_colors.ARITHMETIC_OPERATOR,
"/" : self.theme_colors.ARITHMETIC_OPERATOR,
"%" : self.theme_colors.ARITHMETIC_OPERATOR,

# logical, unary opt
">" : self.theme_colors.LOGICAL_OPERATOR,
"<" : self.theme_colors.LOGICAL_OPERATOR,
">=" : self.theme_colors.LOGICAL_OPERATOR,
"<=" : self.theme_colors.LOGICAL_OPERATOR,
"==" : self.theme_colors.LOGICAL_OPERATOR,
"!=" : self.theme_colors.LOGICAL_OPERATOR,
"&&" : self.theme_colors.LOGICAL_OPERATOR,
"||" : self.theme_colors.LOGICAL_OPERATOR,
"++" : self.theme_colors.UNARY_OPERATOR,
"--" : self.theme_colors.UNARY_OPERATOR,

# control structure
'fow' : self.theme_colors.CONTROL_STRUCTURE,
'whiwe' : self.theme_colors.CONTROL_STRUCTURE,
'do whiwe' : self.theme_colors.CONTROL_STRUCTURE,
'iwf' : self.theme_colors.CONTROL_STRUCTURE,
'ewse' : self.theme_colors.CONTROL_STRUCTURE,
'ewse iwf' : self.theme_colors.CONTROL_STRUCTURE,
'bweak' : self.theme_colors.CONTROL_STRUCTURE,

# grouping symbols
"[[" : self.theme_colors.GROUPING,
"]]" : self.theme_colors.GROUPING,
"[" : self.theme_colors.GROUPING,
"]" : self.theme_colors.GROUPING,
"{" : self.theme_colors.GROUPING,
"}" : self.theme_colors.GROUPING,
"(" : self.theme_colors.GROUPING,
")" : self.theme_colors.GROUPING,

# literals
'INT_LITERAL' : self.theme_colors.NUMBER,
'FLOAT_LITERAL' : self.theme_colors.NUMBER,
'STRING_LITERAL' : self.theme_colors.STRING,
'STRING_PART_START' : self.theme_colors.STRING,
'STRING_PART_END' : self.theme_colors.STRING,
'fax' : self.theme_colors.BOOL,
'cap' : self.theme_colors.BOOL,
'nuww' : self.theme_colors.NUWW,

# others
"&" : self.theme_colors.OTHER_SYMBOL,
"," : self.theme_colors.OTHER_SYMBOL,
"." : self.theme_colors.OTHER_SYMBOL,

# comments
'COMMENT' : self.theme_colors.COMMENT,
'MULTI LINE COMMENT' : self.theme_colors.COMMENT,

# methods
'METHOD' : self.theme_colors.METHOD,

# terminator
"~" : self.theme_colors.TERMINATOR,

# default
'default' : self.theme_colors.DEFAULT
}
28 changes: 28 additions & 0 deletions theme/sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from .config import *

colors = {
"MAIN": '',
"GLOBAL": '',
"KEYWORD": '',
"IDENTIFIER": '',
"CWASS": '',
"METHOD": '',
"CONTROL_STRUCTURE": '',
"CONSTANT_TYPE": '',
"TYPE": '',
"COMMENT": '',
"GROUPING": '',
"NUMBER": '',
"BOOL": '',
"STRING": '',
"NUWW": '',
"ARITHMETIC_OPERATOR": '',
"LOGICAL_OPERATOR": '',
"UNARY_OPERATOR": '',
"OTHER_SYMBOL": '',
"TERMINATOR": '',
"DEFAULT": '',
}

tc = ThemeColors(colors=colors)
sample = ThemeConfig(theme_colors=tc)
Loading

0 comments on commit 6dc0bc2

Please sign in to comment.