CORE/preprocessor.gd

218 lines
8.6 KiB
GDScript3
Raw Normal View History

######################################
# THE CORE FRAMEWORK #
# MADE BY THE STAROPENSOURCE PROJECT #
# AND CONTRIBUTERS (THANK YOU!) #
# #
# COPYRIGHT 2023 THE STAROPENSOURCE #
# PROJECT AND CONTRIBUTERS #
# #
# LICENSED UNDER THE GNU GENERAL #
# PUBLIC LICENSE VERSION 3 (ONLY) #
######################################
extends Node
2023-03-18 16:34:12 +01:00
## ## # ##### ##### ####
## # # # # # # #
## # # # # # # ####
## # ## # # # #
## # # ##### # ####
## The preprocessor is *very* undocumented at this point
## in time as it is a little bit complicated. We may document
## it in the future, but for now you have to read the source.
##
## - JeremyStarTM
2023-03-18 16:34:12 +01:00
# CORE modules
var core: Node = null
var logger: Node = null
2023-03-18 16:34:12 +01:00
# Enables/disables preprocessor functionality
@export var enabled = true
# Do not enable unless you exactly know what you are doing!
# It will spam your log chock full of useless garbage!
@export var diagnostic = false
2023-03-18 16:34:12 +01:00
# Checks if logger is null
func initialize() -> void:
if core.protection_mode: return
if logger == null:
2023-03-18 16:34:12 +01:00
return
logger.diag("CORE/preprocessor.gd","Preprocessor is ready.",false)
2023-03-18 16:34:12 +01:00
# Parses <nl> tags
func _newline(msg:String,prefix:String,pre_msg:String,post_msg:String,exclusion_filter:Array) -> String:
if core.protection_mode: return ""
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsing newline tags",false)
var prefix_space = prefix
for exclusion in exclusion_filter:
prefix_space = prefix_space.replace(exclusion,"")
prefix_space = " ".repeat(prefix_space.length())
var msg_proc = prefix + pre_msg + msg.replace("<nl>",post_msg + "\n" + prefix_space + pre_msg) + post_msg
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsed newline tags. Transformed \"" + msg + "\" into \"" + msg_proc + "\"",false)
2023-03-18 16:34:12 +01:00
return msg_proc
# Parses <lo> and </lo> tags
2023-03-18 16:34:12 +01:00
func _case_lower(msg:String) -> String:
if core.protection_mode: return ""
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsing lowercase tags",false)
2023-03-18 16:34:12 +01:00
var msg_proc = msg
while true:
var index_one = msg_proc.find("<lo>")
2023-07-07 21:25:04 +02:00
var index_two = msg_proc.find("</lo>")
2023-03-18 16:34:12 +01:00
if index_one != -1:
if index_two != -1:
2023-07-07 21:25:04 +02:00
index_two = index_two + 5 - index_one
2023-03-18 16:34:12 +01:00
var index_proc = msg_proc.substr(index_one,index_two)
msg_proc = msg_proc.replace(index_proc,index_proc.replace("<lo>","").replace("</lo>","").to_lower())
else:
core.exception("Preprocessor","Failed parsing lowercase tags in message \"" + msg + "\" (missing end tag?)")
2023-03-18 16:34:12 +01:00
msg_proc = msg
break
else:
break
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsed lowercase tags. Transformed \"" + msg + "\" into \"" + msg_proc + "\"",false)
2023-03-18 16:34:12 +01:00
return msg_proc
# Parses <up> and </up> tags
2023-03-18 16:34:12 +01:00
func _case_upper(msg:String) -> String:
if core.protection_mode: return ""
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsing uppercase tags",false)
2023-03-18 16:34:12 +01:00
var msg_proc = msg
while true:
var index_one = msg_proc.find("<up>")
2023-07-07 21:25:04 +02:00
var index_two = msg_proc.find("</up>")
2023-03-18 16:34:12 +01:00
if index_one != -1:
if index_two != -1:
2023-07-07 21:25:04 +02:00
index_two = index_two + 5 - index_one
2023-03-18 16:34:12 +01:00
var index_proc = msg_proc.substr(index_one,index_two)
msg_proc = msg_proc.replace(index_proc,index_proc.replace("<up>","").replace("</up>","").to_upper())
else:
core.exception("Preprocessor","Failed parsing uppercase tags in message \"" + msg + "\" (missing end tag?)")
2023-03-18 16:34:12 +01:00
msg_proc = msg
break
else:
break
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsed uppercase tags. Transformed \"" + msg + "\" into \"" + msg_proc + "\"",false)
2023-03-18 16:34:12 +01:00
return msg_proc
# Parses <ca> and </ca> tags
2023-03-18 16:34:12 +01:00
func _case_camelcase(msg:String) -> String:
if core.protection_mode: return ""
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsing camelcase tags",false)
2023-03-18 16:34:12 +01:00
var msg_proc = msg
while true:
var index_one = msg_proc.find("<ca>")
2023-07-07 21:25:04 +02:00
var index_two = msg_proc.find("</ca>")
2023-03-18 16:34:12 +01:00
if index_one != -1:
if index_two != -1:
2023-07-07 21:25:04 +02:00
index_two = index_two + 5 - index_one
2023-03-18 16:34:12 +01:00
var index_proc = msg_proc.substr(index_one,index_two)
msg_proc = msg_proc.replace(index_proc,index_proc.replace("<ca>","").replace("</ca>","").to_camel_case())
else:
core.exception("Preprocessor","Failed parsing camelcase tags in message \"" + msg + "\" (missing end tag?)")
2023-03-18 16:34:12 +01:00
msg_proc = msg
break
else:
break
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsed camelcase tags. Transformed \"" + msg + "\" into \"" + msg_proc + "\"",false)
2023-03-18 16:34:12 +01:00
return msg_proc
# Parses <pa> and </pa> tags
2023-03-18 16:34:12 +01:00
func _case_pascalcase(msg:String) -> String:
if core.protection_mode: return ""
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsing pascalcase tags",false)
2023-03-18 16:34:12 +01:00
var msg_proc = msg
while true:
var index_one = msg_proc.find("<pa>")
2023-07-07 21:25:04 +02:00
var index_two = msg_proc.find("</pa>")
2023-03-18 16:34:12 +01:00
if index_one != -1:
if index_two != -1:
2023-07-07 21:25:04 +02:00
index_two = index_two + 5 - index_one
2023-03-18 16:34:12 +01:00
var index_proc = msg_proc.substr(index_one,index_two)
msg_proc = msg_proc.replace(index_proc,index_proc.replace("<pa>","").replace("</pa>","").to_pascal_case())
else:
core.exception("Preprocessor","Failed parsing pascalcase tags in message \"" + msg + "\" (missing end tag?)")
2023-03-18 16:34:12 +01:00
msg_proc = msg
break
else:
break
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsed pascalcase tags. Transformed \"" + msg + "\" into \"" + msg_proc + "\"",false)
2023-03-18 16:34:12 +01:00
return msg_proc
# Parses <sn> and </sn> tags
2023-03-18 16:34:12 +01:00
func _case_snakecase(msg:String) -> String:
if core.protection_mode: return ""
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsing snakecase tags",false)
2023-03-18 16:34:12 +01:00
var msg_proc = msg
while true:
var index_one = msg_proc.find("<sn>")
2023-07-07 21:25:04 +02:00
var index_two = msg_proc.find("</sn>")
2023-03-18 16:34:12 +01:00
if index_one != -1:
if index_two != -1:
2023-07-07 21:25:04 +02:00
index_two = index_two + 5 - index_one
2023-03-18 16:34:12 +01:00
var index_proc = msg_proc.substr(index_one,index_two)
msg_proc = msg_proc.replace(index_proc,index_proc.replace("<sn>","").replace("</sn>","").to_snake_case())
else:
core.exception("Preprocessor","Failed parsing snakecase tags in message \"" + msg + "\" (missing end tag?)")
2023-03-18 16:34:12 +01:00
msg_proc = msg
break
else:
break
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsed snakecase tags. Transformed \"" + msg + "\" into \"" + msg_proc + "\"",false)
2023-03-18 16:34:12 +01:00
return msg_proc
# Function calls to everything *case-related
2023-03-18 16:34:12 +01:00
func _case(msg:String) -> String:
if core.protection_mode: return ""
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsing casing tags",false)
2023-03-18 16:34:12 +01:00
var msg_proc = _case_lower(msg)
msg_proc = _case_upper(msg_proc)
msg_proc = _case_camelcase(msg_proc)
msg_proc = _case_pascalcase(msg_proc)
msg_proc = _case_snakecase(msg_proc)
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsed casing tags. Transformed \"" + msg + "\" into \"" + msg_proc + "\"",false)
2023-03-18 16:34:12 +01:00
return msg_proc
# Replaces all escapes
func _escapes(msg:String) -> String:
if core.protection_mode: return ""
if diagnostic:logger.diag("CORE/preprocessor.gd","Parsing escape tags",false)
2023-03-18 16:34:12 +01:00
var msg_proc = msg.replace("<esc_nl>","<nl>")
msg_proc = msg_proc.replace("<esc_up>","<up>")
msg_proc = msg_proc.replace("<esc_lo>","<lo>")
msg_proc = msg_proc.replace("<esc_ca>","<ca>")
msg_proc = msg_proc.replace("<esc_pa>","<pa>")
msg_proc = msg_proc.replace("<esc_sn>","<sn>")
msg_proc = msg_proc.replace("</esc_up>","</up>")
msg_proc = msg_proc.replace("</esc_lo>","</lo>")
msg_proc = msg_proc.replace("</esc_ca>","</ca>")
msg_proc = msg_proc.replace("</esc_pa>","</pa>")
msg_proc = msg_proc.replace("</esc_sn>","</sn>")
msg_proc = msg_proc.replace("<esc_np>","<np>")
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsed escape tags. Transformed \"" + msg + "\" into \"" + msg_proc + "\"",false)
2023-03-18 16:34:12 +01:00
return msg_proc
# Checks for <np> tag and if found stops processing
2023-07-07 22:17:28 +02:00
func _noprocess(msg:String) -> bool:
if core.protection_mode: return true
if diagnostic: logger.diag("CORE/preprocessor.gd","Checking for noprocess tag", false)
2023-07-07 22:17:28 +02:00
if msg.contains("<np>"):
return true
return false
2023-03-18 16:34:12 +01:00
# Executes all preprocessing functions and returns their combined work
func process(msg:String,prefix:String = "",pre_msg:String = "",post_msg:String = "",exclusion_filter:Array = []) -> String:
if core.protection_mode: return ""
if diagnostic: logger.diag("CORE/preprocessor.gd","Call to process() recieved",false)
2023-03-18 16:34:12 +01:00
if !enabled:
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsing failed: Preprocessor is disabled",false)
2023-03-18 16:34:12 +01:00
return msg
2023-07-07 22:17:28 +02:00
if _noprocess(msg):
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsing stopped: noprocess tag detected", false)
2023-07-07 22:17:28 +02:00
return msg
var msg_proc = _case(msg)
msg_proc = _newline(msg_proc,prefix,pre_msg,post_msg,exclusion_filter)
2023-03-18 16:34:12 +01:00
msg_proc = _escapes(msg_proc)
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsing complete", false)
2023-03-18 16:34:12 +01:00
return msg_proc