Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pygments/lexers/gdscript.py: 64%

22 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-01 06:54 +0000

1""" 

2 pygments.lexers.gdscript 

3 ~~~~~~~~~~~~~~~~~~~~~~~~ 

4 

5 Lexer for GDScript. 

6 

7 Modified by Daniel J. Ramirez <djrmuv@gmail.com> based on the original 

8 python.py. 

9 

10 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS. 

11 :license: BSD, see LICENSE for details. 

12""" 

13 

14import re 

15 

16from pygments.lexer import RegexLexer, include, bygroups, default, words, \ 

17 combined 

18from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ 

19 Number, Punctuation, Whitespace 

20 

21__all__ = ["GDScriptLexer"] 

22 

23 

24class GDScriptLexer(RegexLexer): 

25 """ 

26 For GDScript source code. 

27 """ 

28 

29 name = "GDScript" 

30 url = 'https://www.godotengine.org' 

31 aliases = ["gdscript", "gd"] 

32 filenames = ["*.gd"] 

33 mimetypes = ["text/x-gdscript", "application/x-gdscript"] 

34 

35 def innerstring_rules(ttype): 

36 return [ 

37 # the old style '%s' % (...) string formatting 

38 (r"%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?" 

39 "[hlL]?[E-GXc-giorsux%]", 

40 String.Interpol), 

41 # backslashes, quotes and formatting signs must be parsed one at a time 

42 (r'[^\\\'"%\n]+', ttype), 

43 (r'[\'"\\]', ttype), 

44 # unhandled string formatting sign 

45 (r"%", ttype), 

46 # newlines are an error (use "nl" state) 

47 ] 

48 

49 tokens = { 

50 "root": [ 

51 (r"\n", Whitespace), 

52 (r'^(\s*)([rRuUbB]{,2})("""(?:.|\n)*?""")', 

53 bygroups(Whitespace, String.Affix, String.Doc)), 

54 (r"^(\s*)([rRuUbB]{,2})('''(?:.|\n)*?''')", 

55 bygroups(Whitespace, String.Affix, String.Doc)), 

56 (r"[^\S\n]+", Whitespace), 

57 (r"#.*$", Comment.Single), 

58 (r"[]{}:(),;[]", Punctuation), 

59 (r"(\\)(\n)", bygroups(Text, Whitespace)), 

60 (r"\\", Text), 

61 (r"(in|and|or|not)\b", Operator.Word), 

62 (r"!=|==|<<|>>|&&|\+=|-=|\*=|/=|%=|&=|\|=|\|\||[-~+/*%=<>&^.!|$]", 

63 Operator), 

64 include("keywords"), 

65 (r"(func)(\s+)", bygroups(Keyword, Whitespace), "funcname"), 

66 (r"(class)(\s+)", bygroups(Keyword, Whitespace), "classname"), 

67 include("builtins"), 

68 ('([rR]|[uUbB][rR]|[rR][uUbB])(""")', 

69 bygroups(String.Affix, String.Double), 

70 "tdqs"), 

71 ("([rR]|[uUbB][rR]|[rR][uUbB])(''')", 

72 bygroups(String.Affix, String.Single), 

73 "tsqs"), 

74 ('([rR]|[uUbB][rR]|[rR][uUbB])(")', 

75 bygroups(String.Affix, String.Double), 

76 "dqs"), 

77 ("([rR]|[uUbB][rR]|[rR][uUbB])(')", 

78 bygroups(String.Affix, String.Single), 

79 "sqs"), 

80 ('([uUbB]?)(""")', 

81 bygroups(String.Affix, String.Double), 

82 combined("stringescape", "tdqs")), 

83 ("([uUbB]?)(''')", 

84 bygroups(String.Affix, String.Single), 

85 combined("stringescape", "tsqs")), 

86 ('([uUbB]?)(")', 

87 bygroups(String.Affix, String.Double), 

88 combined("stringescape", "dqs")), 

89 ("([uUbB]?)(')", 

90 bygroups(String.Affix, String.Single), 

91 combined("stringescape", "sqs")), 

92 include("name"), 

93 include("numbers"), 

94 ], 

95 "keywords": [ 

96 (words(("and", "in", "not", "or", "as", "breakpoint", "class", 

97 "class_name", "extends", "is", "func", "setget", "signal", 

98 "tool", "const", "enum", "export", "onready", "static", 

99 "var", "break", "continue", "if", "elif", "else", "for", 

100 "pass", "return", "match", "while", "remote", "master", 

101 "puppet", "remotesync", "mastersync", "puppetsync"), 

102 suffix=r"\b"), Keyword), 

103 ], 

104 "builtins": [ 

105 (words(("Color8", "ColorN", "abs", "acos", "asin", "assert", "atan", 

106 "atan2", "bytes2var", "ceil", "char", "clamp", "convert", 

107 "cos", "cosh", "db2linear", "decimals", "dectime", "deg2rad", 

108 "dict2inst", "ease", "exp", "floor", "fmod", "fposmod", 

109 "funcref", "hash", "inst2dict", "instance_from_id", "is_inf", 

110 "is_nan", "lerp", "linear2db", "load", "log", "max", "min", 

111 "nearest_po2", "pow", "preload", "print", "print_stack", 

112 "printerr", "printraw", "prints", "printt", "rad2deg", 

113 "rand_range", "rand_seed", "randf", "randi", "randomize", 

114 "range", "round", "seed", "sign", "sin", "sinh", "sqrt", 

115 "stepify", "str", "str2var", "tan", "tan", "tanh", 

116 "type_exist", "typeof", "var2bytes", "var2str", "weakref", 

117 "yield"), prefix=r"(?<!\.)", suffix=r"\b"), 

118 Name.Builtin), 

119 (r"((?<!\.)(self|false|true)|(PI|TAU|NAN|INF)" r")\b", 

120 Name.Builtin.Pseudo), 

121 (words(("bool", "int", "float", "String", "NodePath", "Vector2", 

122 "Rect2", "Transform2D", "Vector3", "Rect3", "Plane", "Quat", 

123 "Basis", "Transform", "Color", "RID", "Object", "NodePath", 

124 "Dictionary", "Array", "PackedByteArray", "PackedInt32Array", 

125 "PackedInt64Array", "PackedFloat32Array", "PackedFloat64Array", 

126 "PackedStringArray", "PackedVector2Array", "PackedVector3Array", 

127 "PackedColorArray", "null", "void"), 

128 prefix=r"(?<!\.)", suffix=r"\b"), 

129 Name.Builtin.Type), 

130 ], 

131 "numbers": [ 

132 (r"(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?j?", Number.Float), 

133 (r"\d+[eE][+-]?[0-9]+j?", Number.Float), 

134 (r"0[xX][a-fA-F0-9]+", Number.Hex), 

135 (r"\d+j?", Number.Integer), 

136 ], 

137 "name": [(r"[a-zA-Z_]\w*", Name)], 

138 "funcname": [(r"[a-zA-Z_]\w*", Name.Function, "#pop"), default("#pop")], 

139 "classname": [(r"[a-zA-Z_]\w*", Name.Class, "#pop")], 

140 "stringescape": [ 

141 ( 

142 r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|' 

143 r"U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})", 

144 String.Escape, 

145 ) 

146 ], 

147 "strings-single": innerstring_rules(String.Single), 

148 "strings-double": innerstring_rules(String.Double), 

149 "dqs": [ 

150 (r'"', String.Double, "#pop"), 

151 (r'\\\\|\\"|\\\n', String.Escape), # included here for raw strings 

152 include("strings-double"), 

153 ], 

154 "sqs": [ 

155 (r"'", String.Single, "#pop"), 

156 (r"\\\\|\\'|\\\n", String.Escape), # included here for raw strings 

157 include("strings-single"), 

158 ], 

159 "tdqs": [ 

160 (r'"""', String.Double, "#pop"), 

161 include("strings-double"), 

162 (r"\n", Whitespace), 

163 ], 

164 "tsqs": [ 

165 (r"'''", String.Single, "#pop"), 

166 include("strings-single"), 

167 (r"\n", Whitespace), 

168 ], 

169 } 

170 

171 def analyse_text(text): 

172 score = 0.0 

173 

174 if re.search( 

175 r"func (_ready|_init|_input|_process|_unhandled_input)", text 

176 ): 

177 score += 0.8 

178 

179 if re.search( 

180 r"(extends |class_name |onready |preload|load|setget|func [^_])", 

181 text 

182 ): 

183 score += 0.4 

184 

185 if re.search(r"(var|const|enum|export|signal|tool)", text): 

186 score += 0.2 

187 

188 return min(score, 1.0)