1+ // SYNTAX TEST "ChaiScript.sublime-syntax"
2+ #!/usr/bin/env chai
3+ // <- comment.line.shebang.chai
4+
5+ // --- VARIABLES --
6+
7+ var i;
8+ auto j;
9+ var l := k;
10+ // ^^ keyword.operator.assignment.chai
11+ auto &m = k;
12+ // ^ keyword.operator.chai
13+ global g = "This is a global";
14+ // <- storage.type.chai
15+ var x = true;
16+ // <- storage.type.chai
17+ auto z = false;
18+ // <- storage.type.chai
19+
20+ var misc = [1, 2.0f, 3u, 4ll, "16", `+`];
21+ // ^ punctuation.definition.brackets.chai
22+ var map = ["a":1, "b":2];
23+
24+ // --- IF ---
25+
26+ if (5 > 2)
27+ // <- keyword.control.conditional.chai
28+ {
29+ print("Yup, 5 > 2");
30+ // ^ punctuation.definition.string.end.chai
31+ }
32+ else if (true || false)
33+ // <- keyword.control.conditional.chai
34+ {
35+ foo()
36+ }
37+ else
38+ // ^ keyword.control.conditional.chai
39+ {
40+ bar()
41+ }
42+
43+ // --- LOOPS ---
44+
45+ while (x)
46+ // ^ keyword.control.loop.chai
47+ {
48+ print("x was true")
49+ x = false;
50+ // ^ constant.language.boolean.false.chai
51+ }
52+
53+ for (var i = 1; i < 10; ++i)
54+ {
55+ // <- meta.block.chai
56+ z = true;
57+ print(i);
58+ // ^ meta.function-call.chai
59+ }
60+
61+ for (x : [1,2,3]) { print(i); }
62+
63+ // --- FUNCTIONS --
64+
65+ def myFunc(x) { print(x); }
66+ //^ meta.function.declaration.chai
67+
68+ def myFunc(x) : x > 2 && x < 5 {
69+ // ^ meta.function.declaration.chai
70+ print(to_string(x) + " is between 2 and 5")
71+ // ^ keyword.operator.arithmetic.chai
72+ }
73+
74+ def myFunc(x, y) : x >= 5 {
75+ print(to_string(x) + " is greater than or equal to 5")
76+ //^^^^^ support.function.chai
77+ }
78+
79+ myFunc(3)
80+ // <- variable.function.chai
81+
82+ print("${3 + 5} is 8");
83+ // ^ string.quoted.double.chai
84+
85+ var value = eval("4 + 2 + 12");
86+ // ^^^^ support.function.chai
87+
88+
89+ var plus = `+`;
90+ // ^^^ constant.character.chai
91+ var minus = `-`;
92+ var mult = `*`;
93+ var div = `/`;
94+
95+ print(plus("a", "b")) // prints "ab"
96+ print(plus(1 , 3)) // prints "4"
97+
98+ var my_print = print
99+ my_print(10)
100+
101+ var my_fun = fun(x) { return x + 2; }
102+ // ^^^^^^ keyword.control
103+ my_fun(5)
104+
105+ my_print(my_fun.get_arity())
106+ // ^^^^^^^^^ meta.function-call.method.chai
107+
108+ my_print(`+`.get_arity())
109+
110+ var print_arg_types = fun(f)
111+ // ^^^ storage.type.chai
112+ {
113+ var join_str = ", "
114+ print("${f.get_param_types()[0].name();} (${f.get_param_types().drop(1).map(name).join(join_str)})")
115+ }
116+
117+ `+`.get_contained_functions().for_each(print_arg_types)
118+
119+ try {
120+ eval("BLARG")
121+ } catch (e) {
122+ print("Error while processing eval statement")
123+ }
124+
125+ try {
126+ throw(5.2)
127+ } catch(e) : is_type(e, "int") {
128+ // ^^^^^^^ support.function.chai
129+ print("Int: ${e}");
130+
131+ } catch(e) : is_type(e, "double") {
132+ print("Double: ${e}");
133+ }
134+
135+ // --- CLASS ---
136+
137+ class MyClass
138+ // <- storage.type.class.chai
139+ {
140+ var data
141+ //^^^ meta.class.chai meta.block.chai storage.type.chai
142+
143+ def MyClass(x)
144+ {
145+ this.set_explicit(true);
146+ // ^^^^ meta.class.chai keyword.language.chai
147+ this.data = x
148+ }
149+
150+ def getStuff(y)
151+ {
152+ return this.data + y
153+ }
154+
155+ def getName()
156+ {
157+ return __CLASS__;
158+ }
159+ }
160+
161+ var obj = MyClass(10)
162+ obj.getStuff(15)
163+
164+ def string::isStringLong()
165+ {
166+ return this.size() > 10;
167+ }
168+
169+ print("Hello".isStringLong())
170+ print("Hello World".isStringLong())
171+
172+ def MyClass::getMoreStuff(d)
173+ {
174+ return this.data * d
175+ }
176+
177+ print(obj.getMoreStuff(10))
178+
179+ // TODO: attribute definition
180+ attr MyType::value;
181+ // <- storage.type.chai
182+ // ^^^^^^ meta.function.declaration.chai entity.name.function.chai
183+ def MyType::MyType() { this.value = "a"; }
184+ // ^^^^^^ meta.function.declaration.chai entity.name.function.chai
185+ def MyType::get_value() { "Value Is: " + this.value; }
186+ //
187+
188+ // --- BUILT INS ---
189+
190+ print("current line: " + __LINE__);
191+ print("current file: " + __FILE__);
192+ // ^ support.constant.chai
0 commit comments