Skip to content

Commit

Permalink
Added support for GDScript (#2006)
Browse files Browse the repository at this point in the history
This adds support for the GDScript language.
  • Loading branch information
RunDevelopment authored Aug 14, 2019
1 parent af5a36a commit e2b99f4
Show file tree
Hide file tree
Showing 13 changed files with 400 additions and 2 deletions.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@
"title": "G-code",
"owner": "RunDevelopment"
},
"gdscript": {
"title": "GDScript",
"owner": "RunDevelopment"
},
"gedcom": {
"title": "GEDCOM",
"owner": "Golmote"
Expand Down
27 changes: 27 additions & 0 deletions components/prism-gdscript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Prism.languages.gdscript = {
'comment': /#.*/,
'string': {
pattern: /@?(?:("|')(?:(?!\1)[^\n\\]|\\[\s\S])*\1(?!"|')|"""(?:[^\\]|\\[\s\S])*?""")/,
greedy: true
},
'class-name': {
// class_name Foo, extends Bar, class InnerClass
// export(int) var baz, export(int, 0) var i
// as Node
// const FOO: int = 9, var bar: bool = true
// func add(reference: Item, amount: int) -> Item:
pattern: /(^(?:class_name|class|extends)[ \t]+|^export\([ \t]*|\bas[ \t]+|(?:\b(?:const|var)[ \t]|[,(])[ \t]*\w+[ \t]*:[ \t]*|->[ \t]*)[a-zA-Z_]\w*/m,
lookbehind: true
},
'keyword': /\b(?:and|as|assert|break|breakpoint|class|class_name|const|continue|elif|else|enum|export|extends|for|func|if|in|is|master|mastersync|match|not|null|onready|or|pass|preload|puppet|puppetsync|remote|remotesync|return|self|setget|signal|static|tool|var|while|yield)\b/,
'function': /[a-z_]\w*(?=[ \t]*\()/i,
'variable': /\$\w+/,
'number': [
/\b0b[01_]+\b|\b0x[\da-fA-F_]+\b|(?:\b\d[\d_]*(?:\.[\d_]*)?|\B\.[\d_]+)(?:e[+-]?[\d_]+)?\b/,
/\b(?:INF|NAN|PI|TAU)\b/
],
'constant': /\b[A-Z][A-Z_\d]*\b/,
'boolean': /\b(?:false|true)\b/,
'operator': /->|:=|&&|\|\||<<|>>|[-+*/%&|!<>=]=?|[~^]/,
'punctuation': /[.:,;()[\]{}]/
};
1 change: 1 addition & 0 deletions components/prism-gdscript.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions examples/prism-gdscript.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<h2>Full example</h2>
<pre><code>extends BaseClass
class_name MyClass, "res://path/to/optional/icon.svg"

# Member Variables

var a = 5
var s = "Hello"
var arr = [1, 2, 3]
var dict = {"key": "value", 2:3}
var typed_var: int
var inferred_type := "String"

# Constants

const ANSWER = 42
const THE_NAME = "Charly"

# Enums

enum {UNIT_NEUTRAL, UNIT_ENEMY, UNIT_ALLY}
enum Named {THING_1, THING_2, ANOTHER_THING = -1}

# Built-in Vector Types

var v2 = Vector2(1, 2)
var v3 = Vector3(1, 2, 3)

# Function

func some_function(param1, param2):
var local_var = 5

if param1 &lt; local_var:
print(param1)
elif param2 > 5:
print(param2)
else:
print("Fail!")

for i in range(20):
print(i)

while param2 != 0:
param2 -= 1

var local_var2 = param1 + 3
return local_var2

# Functions override functions with the same name on the base/parent class.
# If you still want to call them, use '.' (like 'super' in other languages).

func something(p1, p2):
.something(p1, p2)

# Inner Class

class Something:
var a = 10

# Constructor

func _init():
print("Constructed!")
var lv = Something.new()
print(lv.a)</code></pre>
1 change: 1 addition & 0 deletions plugins/show-language/prism-show-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"erb": "ERB",
"fsharp": "F#",
"gcode": "G-code",
"gdscript": "GDScript",
"gedcom": "GEDCOM",
"glsl": "GLSL",
"gml": "GameMaker Language",
Expand Down
2 changes: 1 addition & 1 deletion plugins/show-language/prism-show-language.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 79 additions & 0 deletions tests/languages/gdscript/class-name_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
class_name Foo
extends Bar

class InnerClass:

export(int) var baz
export(int, 0) var i

return foo as Node

const FOO: int = 9
var bar: bool = true

func add(reference: Item, amount: int) -> Item:

----------------------------------------------------

[
["keyword", "class_name"],
["class-name", "Foo"],
["keyword", "extends"],
["class-name", "Bar"],

["keyword", "class"],
["class-name", "InnerClass"],
["punctuation", ":"],

["keyword", "export"],
["punctuation", "("],
["class-name", "int"],
["punctuation", ")"],
["keyword", "var"],
" baz\n",
["keyword", "export"],
["punctuation", "("],
["class-name", "int"],
["punctuation", ","],
["number", "0"],
["punctuation", ")"],
["keyword", "var"],
" i\n\n",

["keyword", "return"],
" foo ",
["keyword", "as"],
["class-name", "Node"],

["keyword", "const"],
["constant", "FOO"],
["punctuation", ":"],
["class-name", "int"],
["operator", "="],
["number", "9"],
["keyword", "var"],
" bar",
["punctuation", ":"],
["class-name", "bool"],
["operator", "="],
["boolean", "true"],

["keyword", "func"],
["function", "add"],
["punctuation", "("],
"reference",
["punctuation", ":"],
["class-name", "Item"],
["punctuation", ","],
" amount",
["punctuation", ":"],
["class-name", "int"],
["punctuation", ")"],
["operator", "->"],
["class-name", "Item"],
["punctuation", ":"]
]

----------------------------------------------------

Checks for class names.
91 changes: 91 additions & 0 deletions tests/languages/gdscript/keyword_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
and
as
assert
break
breakpoint
class
class_name
const
continue
elif
else
enum
export
extends
for
func
if
in
is
master
mastersync
match
not
null
onready
or
pass
preload
puppet
puppetsync
remote
remotesync
return
self
setget
signal
static
tool
var
while
yield

----------------------------------------------------

[
["keyword", "and"],
["keyword", "as"],
["keyword", "assert"],
["keyword", "break"],
["keyword", "breakpoint"],
["keyword", "class"],
["keyword", "class_name"],
["keyword", "const"],
["keyword", "continue"],
["keyword", "elif"],
["keyword", "else"],
["keyword", "enum"],
["keyword", "export"],
["keyword", "extends"],
["keyword", "for"],
["keyword", "func"],
["keyword", "if"],
["keyword", "in"],
["keyword", "is"],
["keyword", "master"],
["keyword", "mastersync"],
["keyword", "match"],
["keyword", "not"],
["keyword", "null"],
["keyword", "onready"],
["keyword", "or"],
["keyword", "pass"],
["keyword", "preload"],
["keyword", "puppet"],
["keyword", "puppetsync"],
["keyword", "remote"],
["keyword", "remotesync"],
["keyword", "return"],
["keyword", "self"],
["keyword", "setget"],
["keyword", "signal"],
["keyword", "static"],
["keyword", "tool"],
["keyword", "var"],
["keyword", "while"],
["keyword", "yield"]
]

----------------------------------------------------

Checks for keywords.
34 changes: 34 additions & 0 deletions tests/languages/gdscript/number_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
123
-123
123.456
.5
1.2e-34

0xBadFace
0b01010101

1_000_000_000_000
0xBAD_FACE
0b_0010_0010_0100_0100

----------------------------------------------------

[
["number", "123"],
["operator", "-"],
["number", "123"],
["number", "123.456"],
["number", ".5"],
["number", "1.2e-34"],

["number", "0xBadFace"],
["number", "0b01010101"],

["number", "1_000_000_000_000"],
["number", "0xBAD_FACE"],
["number", "0b_0010_0010_0100_0100"]
]

----------------------------------------------------

Checks for numbers.
46 changes: 46 additions & 0 deletions tests/languages/gdscript/operator_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
+ - * / %
& | ~ ^ >> <<
&& || !
> < = == != >= <=
:=
->
+= -= *= /= %= &= |=

----------------------------------------------------

[
["operator", "+"],
["operator", "-"],
["operator", "*"],
["operator", "/"],
["operator", "%"],
["operator", "&"],
["operator", "|"],
["operator", "~"],
["operator", "^"],
["operator", ">>"],
["operator", "<<"],
["operator", "&&"],
["operator", "||"],
["operator", "!"],
["operator", ">"],
["operator", "<"],
["operator", "="],
["operator", "=="],
["operator", "!="],
["operator", ">="],
["operator", "<="],
["operator", ":="],
["operator", "->"],
["operator", "+="],
["operator", "-="],
["operator", "*="],
["operator", "/="],
["operator", "%="],
["operator", "&="],
["operator", "|="]
]

----------------------------------------------------

Checks for operators.
20 changes: 20 additions & 0 deletions tests/languages/gdscript/punctuation_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
. : , ; [] () {}

----------------------------------------------------

[
["punctuation", "."],
["punctuation", ":"],
["punctuation", ","],
["punctuation", ";"],
["punctuation", "["],
["punctuation", "]"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"]
]

----------------------------------------------------

Checks for punctuation.
Loading

0 comments on commit e2b99f4

Please sign in to comment.