Skip to content

Commit

Permalink
Backport to Godot v4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
dalexeev committed Dec 20, 2023
1 parent 748c810 commit 9fb3a08
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 80 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/test_preprocessor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ name: Test Preprocessor
on: [push, pull_request]

env:
GODOT_EXECUTABLE: Godot_v${{ vars.GODOT_VERSION }}_linux.x86_64
GODOT_VERSION: 4.1.3-stable
GODOT_EXECUTABLE: Godot_v${{ env.GODOT_VERSION }}_linux.x86_64

jobs:
test-preprocessor:
Expand All @@ -19,12 +20,12 @@ jobs:
cache-name: cache-godot
with:
path: ./${{ env.GODOT_EXECUTABLE }}
key: ${{ runner.os }}-${{ env.cache-name }}-${{ vars.GODOT_VERSION }}
key: ${{ runner.os }}-${{ env.cache-name }}-${{ env.GODOT_VERSION }}

- if: ${{ steps.cache-godot.outputs.cache-hit != 'true' }}
name: Install Godot
run: |
wget -q https://github.com/godotengine/godot-builds/releases/download/${{ vars.GODOT_VERSION }}/${{ env.GODOT_EXECUTABLE }}.zip
wget -q https://github.com/godotengine/godot-builds/releases/download/${{ env.GODOT_VERSION }}/${{ env.GODOT_EXECUTABLE }}.zip
unzip ${{ env.GODOT_EXECUTABLE }}.zip
- name: Run tests
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

An export plugin for stripping comments and "conditional compilation" of GDScript.

Compatible with Godot 4.2.
Compatible with Godot 4.1.

## How to use

Expand Down
3 changes: 1 addition & 2 deletions addons/gdscript_preprocessor/export_plugin.gd
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
extends EditorExportPlugin


@warning_ignore("inferred_declaration")
const _Preprocessor = preload("./preprocessor.gd")

var _config_path: String
Expand Down Expand Up @@ -37,7 +36,7 @@ func _export_begin(
regex = _get_option(config, "statements", "removing_regex_debug", "")
else:
regex = _get_option(config, "statements", "removing_regex_release",
r"^(?:breakpoint|assert\(|print_debug\(|print_stack\()")
"^(?:breakpoint|assert\\(|print_debug\\(|print_stack\\()")
if not regex.is_empty():
_preprocessor.statement_removing_regex = RegEx.create_from_string(regex)
if not _preprocessor.statement_removing_regex.is_valid():
Expand Down
1 change: 0 additions & 1 deletion addons/gdscript_preprocessor/plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
extends EditorPlugin


@warning_ignore("inferred_declaration")
const _ExportPlugin = preload("./export_plugin.gd")

var _export_plugin: _ExportPlugin = _ExportPlugin.new()
Expand Down
32 changes: 9 additions & 23 deletions addons/gdscript_preprocessor/preprocessor.gd
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const _PAREN_CLOSE: int = 0x0029 # ")"
const _BRACKET_OPEN: int = 0x005B # "["
const _BACKSLASH: int = 0x005C # "\\"
const _BRACKET_CLOSE: int = 0x005D # "]"
const _SMALL_R: int = 0x0072 # "r"
const _BRACE_OPEN: int = 0x007B # "{"
const _BRACE_CLOSE: int = 0x007D # "}"

Expand Down Expand Up @@ -63,9 +62,9 @@ var _output_enabled: bool = true
var _paren_stack: Array[int]

var _os_has_feature_regex: RegEx = RegEx.create_from_string(
r"""OS\.has_feature\((["'])(\w+)\1\)""")
"""OS\\.has_feature\\((["'])(\\w+)\\1\\)""")
var _cond_regex: RegEx = RegEx.create_from_string(
r"^(false|true|and|or|not|&&|\|\||!|\(|\)| |\t|\r|\n)+$")
"^(false|true|and|or|not|&&|\\|\\||!|\\(|\\)| |\\t|\\r|\\n)+$")
var _expression: Expression = Expression.new()


Expand Down Expand Up @@ -187,17 +186,9 @@ func _parse_statement() -> void:
return
_position += 1
elif c == _QUOT or c == _APOS:
_parse_string(false)
_parse_string()
if not error_message.is_empty():
return
elif c == _SMALL_R:
_position += 1
if _position < _length:
var q: int = _source.unicode_at(_position)
if q == _QUOT or q == _APOS:
_parse_string(true)
if not error_message.is_empty():
return
elif c == _HASH:
# Skip comment.
string += _source.substr(from, _position - from)
Expand Down Expand Up @@ -324,7 +315,7 @@ func _parse_statement() -> void:
current_block.status = _Status.NORMAL


func _parse_string(is_raw: bool) -> void:
func _parse_string() -> void:
var quote_char: int = _source.unicode_at(_position)
_position += 1

Expand All @@ -346,15 +337,10 @@ func _parse_string(is_raw: bool) -> void:
error_message = "Unterminated string."
return
var esc: int = _source.unicode_at(_position)
if is_raw:
if esc == quote_char or esc == _BACKSLASH:
_position += 1
# else: **not** advance.
else:
# Let's assume the escape is valid.
_position += 1
if esc == _NEWLINE:
_line += 1
# Let's assume the escape is valid.
_position += 1
if esc == _NEWLINE:
_line += 1
elif c == quote_char:
_position += 1
if is_multiline:
Expand All @@ -380,7 +366,7 @@ func _eval_cond(cond: String) -> _Trilean:
.replace("OS.is_debug_build()", "true" if is_debug else "false")

var matches: Array[RegExMatch] = _os_has_feature_regex.search_all(cond)
for i: int in range(matches.size() - 1, -1, -1):
for i in range(matches.size() - 1, -1, -1):
var m: RegExMatch = matches[i]
cond = cond.left(m.get_start()) + ("true" if features.has(m.get_string(2)) else "false") \
+ cond.substr(m.get_end())
Expand Down
4 changes: 1 addition & 3 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ config_version=5
[application]

config/name="gdscript-preprocessor"
config/features=PackedStringArray("4.2", "Forward Plus")
config/features=PackedStringArray("4.1", "Forward Plus")
config/icon="res://icon.png"

[debug]

gdscript/warnings/exclude_addons=false
gdscript/warnings/untyped_declaration=1
gdscript/warnings/inferred_declaration=1
gdscript/warnings/unsafe_property_access=1
gdscript/warnings/unsafe_method_access=1
gdscript/warnings/unsafe_cast=1
Expand Down
3 changes: 1 addition & 2 deletions test_runner.gd
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
extends SceneTree


@warning_ignore("inferred_declaration")
const _Preprocessor = preload("./addons/gdscript_preprocessor/preprocessor.gd")

var _preprocessor: _Preprocessor = _Preprocessor.new()


func _init() -> void:
for file_name: String in DirAccess.get_files_at("tests/"):
for file_name in DirAccess.get_files_at("tests/"):
if not file_name.ends_with(".gd"):
continue

Expand Down
24 changes: 0 additions & 24 deletions tests/strings.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,6 @@ func test() -> void:
print("""test
test \n \" ' '' " "" \\""")

# https://github.com/godotengine/godot/blob/master/
# modules/gdscript/tests/scripts/parser/features/r_strings.gd
print(r"test ' \' \" \\ \n \t \u2023 test")
print(r"\n\\[\t ]*(\w+)")
print(r"")
print(r"\"")
print(r"\\\"")
print(r"\\")
print(r"\" \\\" \\\\\"")
print(r"\ \\ \\\ \\\\ \\\\\ \\")
print(r'"')
print(r'"(?:\\.|[^"])*"')
print(r"""""")
print(r"""test \t "test"="" " \" \\\" \ \\ \\\ test""")
print(r'''r"""test \t "test"="" " \" \\\" \ \\ \\\ test"""''')
print(r"\t
\t")
print(r"\t \
\t")
print(r"""\t
\t""")
print(r"""\t \
\t""")

if true:
if false:
print("test")
21 changes: 0 additions & 21 deletions tests/strings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,5 @@ func test() -> void:
test \n \" ' '' \\")
print("""test
test \n \" ' '' " "" \\""")
print(r"test ' \' \" \\ \n \t \u2023 test")
print(r"\n\\[\t ]*(\w+)")
print(r"")
print(r"\"")
print(r"\\\"")
print(r"\\")
print(r"\" \\\" \\\\\"")
print(r"\ \\ \\\ \\\\ \\\\\ \\")
print(r'"')
print(r'"(?:\\.|[^"])*"')
print(r"""""")
print(r"""test \t "test"="" " \" \\\" \ \\ \\\ test""")
print(r'''r"""test \t "test"="" " \" \\\" \ \\ \\\ test"""''')
print(r"\t
\t")
print(r"\t \
\t")
print(r"""\t
\t""")
print(r"""\t \
\t""")
if true:
pass

0 comments on commit 9fb3a08

Please sign in to comment.