CORE/preprocessor.gd
JeremyStar™ 99703cf03e CORE rewrite (#1)
Reviewed-on: StarOpenSource/core#1

Rewrote CORE and improved the startup process and startup time significantly. The documentation has been beefed up too and is now much better. Existing projects may need major refactoring however.
Co-authored-by: JeremyStarTM <jeremystartm@staropensource.de>
Co-committed-by: JeremyStarTM <jeremystartm@staropensource.de>
2023-08-25 14:34:57 +02:00

218 lines
8.6 KiB
GDScript

######################################
# 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
## ## # ##### ##### ####
## # # # # # # #
## # # # # # # ####
## # ## # # # #
## # # ##### # ####
## 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
# CORE modules
var core: Node = null
var logger: Node = null
# 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
# Checks if logger is null
func initialize() -> void:
if core.protection_mode: return
if logger == null:
return
logger.diag("CORE/preprocessor.gd","Preprocessor is ready.",false)
# 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)
return msg_proc
# Parses <lo> and </lo> tags
func _case_lower(msg:String) -> String:
if core.protection_mode: return ""
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsing lowercase tags",false)
var msg_proc = msg
while true:
var index_one = msg_proc.find("<lo>")
var index_two = msg_proc.find("</lo>")
if index_one != -1:
if index_two != -1:
index_two = index_two + 5 - index_one
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?)")
msg_proc = msg
break
else:
break
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsed lowercase tags. Transformed \"" + msg + "\" into \"" + msg_proc + "\"",false)
return msg_proc
# Parses <up> and </up> tags
func _case_upper(msg:String) -> String:
if core.protection_mode: return ""
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsing uppercase tags",false)
var msg_proc = msg
while true:
var index_one = msg_proc.find("<up>")
var index_two = msg_proc.find("</up>")
if index_one != -1:
if index_two != -1:
index_two = index_two + 5 - index_one
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?)")
msg_proc = msg
break
else:
break
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsed uppercase tags. Transformed \"" + msg + "\" into \"" + msg_proc + "\"",false)
return msg_proc
# Parses <ca> and </ca> tags
func _case_camelcase(msg:String) -> String:
if core.protection_mode: return ""
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsing camelcase tags",false)
var msg_proc = msg
while true:
var index_one = msg_proc.find("<ca>")
var index_two = msg_proc.find("</ca>")
if index_one != -1:
if index_two != -1:
index_two = index_two + 5 - index_one
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?)")
msg_proc = msg
break
else:
break
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsed camelcase tags. Transformed \"" + msg + "\" into \"" + msg_proc + "\"",false)
return msg_proc
# Parses <pa> and </pa> tags
func _case_pascalcase(msg:String) -> String:
if core.protection_mode: return ""
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsing pascalcase tags",false)
var msg_proc = msg
while true:
var index_one = msg_proc.find("<pa>")
var index_two = msg_proc.find("</pa>")
if index_one != -1:
if index_two != -1:
index_two = index_two + 5 - index_one
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?)")
msg_proc = msg
break
else:
break
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsed pascalcase tags. Transformed \"" + msg + "\" into \"" + msg_proc + "\"",false)
return msg_proc
# Parses <sn> and </sn> tags
func _case_snakecase(msg:String) -> String:
if core.protection_mode: return ""
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsing snakecase tags",false)
var msg_proc = msg
while true:
var index_one = msg_proc.find("<sn>")
var index_two = msg_proc.find("</sn>")
if index_one != -1:
if index_two != -1:
index_two = index_two + 5 - index_one
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?)")
msg_proc = msg
break
else:
break
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsed snakecase tags. Transformed \"" + msg + "\" into \"" + msg_proc + "\"",false)
return msg_proc
# Function calls to everything *case-related
func _case(msg:String) -> String:
if core.protection_mode: return ""
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsing casing tags",false)
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)
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)
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)
return msg_proc
# Checks for <np> tag and if found stops processing
func _noprocess(msg:String) -> bool:
if core.protection_mode: return true
if diagnostic: logger.diag("CORE/preprocessor.gd","Checking for noprocess tag", false)
if msg.contains("<np>"):
return true
return false
# 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)
if !enabled:
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsing failed: Preprocessor is disabled",false)
return msg
if _noprocess(msg):
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsing stopped: noprocess tag detected", false)
return msg
var msg_proc = _case(msg)
msg_proc = _newline(msg_proc,prefix,pre_msg,post_msg,exclusion_filter)
msg_proc = _escapes(msg_proc)
if diagnostic: logger.diag("CORE/preprocessor.gd","Parsing complete", false)
return msg_proc