Skip to content

Commit ccc97d7

Browse files
author
Kim
committed
added test cases, added snippets, improved syntax detection
1 parent 8a2c032 commit ccc97d7

8 files changed

+242
-3
lines changed

ChaiScript.sublime-syntax

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
%YAML 1.2
22
---
3-
# Derived from the C++/JavaScript package of Sublime Text 3
3+
# Derived from the C++/JavaScript packages of Sublime Text 3
44
name: ChaiScript
55
file_extensions:
66
- chai
77
first_line_match: ^#!/.*\b(chai)$\n?
88
scope: source.chai
99
variables:
10+
builtin_func: 'back|bind|bob_back|call_exists|collate|concat|drop_while|drop|dump_system|Dynamic_Object|empty|eval|even|filter|foldl|for_each|front|generate_range|get_arity|get_contained_functions|is_type|join|max|method_missing|min|pop_front|print|product|puts|reduce|retro|reverse|set_explicit|set_explicit|size|sum|take_while|take|to_string|zip_with|zip'
1011
identifier: '\b[[:alpha:]_][[:alnum:]_]*\b'
1112
func_lookahead: '\s*\b(fun\s+)?def\b'
1213

@@ -411,7 +412,7 @@ contexts:
411412
push:
412413
- meta_scope: meta.function-call.chai
413414
- include: function-call-params
414-
- match: \b(print|is_type|puts|eval|to_string)\b(?=\()
415+
- match: \b({{builtin_func}})\b(?=\()
415416
scope: support.function.chai
416417
push:
417418
- meta_scope: meta.function-call.chai
@@ -438,7 +439,7 @@ contexts:
438439
pop: true
439440

440441
method-call:
441-
- match: 'get_arity|for_each|get_contained_functions|set_explicit(?=\()'
442+
- match: '{{builtin_func}}(?=\()'
442443
scope: support.function.chai
443444
push:
444445
- meta_scope: meta.function-call.method.chai

Completion Rules.tmPreferences

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<plist version="1.0">
3+
<dict>
4+
<key>scope</key>
5+
<string>source.chai</string>
6+
<key>settings</key>
7+
<dict>
8+
<key>cancelCompletion</key>
9+
<string>^\s*(\{?\s*(else|return)|(def|fun)\s*[a-zA-Z_0-9]+)$</string>
10+
</dict>
11+
</dict>
12+
</plist>

Snippets/for-i.sublime-snippet

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<snippet>
2+
<content><![CDATA[for (var ${20:i} = 0; $2 < ${1:count}; ${3:i}++) {
3+
${0:// code}
4+
}]]></content>
5+
<tabTrigger>fori</tabTrigger>
6+
<scope>source.chai</scope>
7+
<description>for i Loop</description>
8+
</snippet>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<snippet>
2+
<content><![CDATA[def ${1:function_name}(${2:argument}) {
3+
${0:// body...}
4+
}]]></content>
5+
<tabTrigger>def</tabTrigger>
6+
<scope>source.chai</scope>
7+
<description>Function def</description>
8+
</snippet>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<snippet>
2+
<content><![CDATA[fun($1) {${0:$TM_SELECTED_TEXT}}]]></content>
3+
<tabTrigger>fun</tabTrigger>
4+
<scope>source.chai</scope>
5+
<description>Anonymous Function fun</description>
6+
</snippet>

Snippets/if-else.sublime-snippet

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<snippet>
2+
<content><![CDATA[if (${1:true}) {${0:$TM_SELECTED_TEXT}} else {}]]></content>
3+
<tabTrigger>ife</tabTrigger>
4+
<scope>source.chai</scope>
5+
<description>if … else</description>
6+
</snippet>

Snippets/if.sublime-snippet

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<snippet>
2+
<content><![CDATA[if (${1:true}) {${0:$TM_SELECTED_TEXT}}]]></content>
3+
<tabTrigger>if</tabTrigger>
4+
<scope>source.chai</scope>
5+
<description>if</description>
6+
</snippet>

syntax_test_chai.chai

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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

Comments
 (0)