Skip to content

Commit

Permalink
Added support for ChaiScript (#2706)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Jan 24, 2021
1 parent 04ef309 commit 3f7d745
Show file tree
Hide file tree
Showing 15 changed files with 571 additions and 2 deletions.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@
"require": "c",
"owner": "zeitgeist87"
},
"chaiscript": {
"title": "ChaiScript",
"require": ["clike", "cpp"],
"owner": "RunDevelopment"
},
"cil": {
"title": "CIL",
"owner": "sbrl"
Expand Down
60 changes: 60 additions & 0 deletions components/prism-chaiscript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Prism.languages.chaiscript = Prism.languages.extend('clike', {
'string': {
pattern: /(^|[^\\])'(?:[^'\\]|\\[\s\S])*'/,
lookbehind: true,
greedy: true
},
'class-name': [
{
// e.g. class Rectangle { ... }
pattern: /(\bclass\s+)\w+/,
lookbehind: true
},
{
// e.g. attr Rectangle::height, def Rectangle::area() { ... }
pattern: /(\b(?:attr|def)\s+)\w+(?=\s*::)/,
lookbehind: true
}
],
'keyword': /\b(?:attr|auto|break|case|catch|class|continue|def|default|else|finally|for|fun|global|if|return|switch|this|try|var|while)\b/,
'number': [
Prism.languages.cpp.number,
/\b(?:Infinity|NaN)\b/
],
'operator': />>=?|<<=?|\|\||&&|:[:=]?|--|\+\+|[=!<>+\-*/%|&^]=?|[?~]|`[^`\r\n]{1,4}`/,
});

Prism.languages.insertBefore('chaiscript', 'operator', {
'parameter-type': {
// e.g. def foo(int x, Vector y) {...}
pattern: /([,(]\s*)\w+(?=\s+\w)/,
lookbehind: true,
alias: 'class-name'
},
});

Prism.languages.insertBefore('chaiscript', 'string', {
'string-interpolation': {
pattern: /(^|[^\\])"(?:[^"$\\]|\\[\s\S]|\$(?!\{)|\$\{(?:[^{}]|\{(?:[^{}]|\{[^{}]*\})*\})*\})*"/,
lookbehind: true,
greedy: true,
inside: {
'interpolation': {
pattern: /((?:^|[^\\])(?:\\{2})*)\$\{(?:[^{}]|\{(?:[^{}]|\{[^{}]*\})*\})*\}/,
lookbehind: true,
inside: {
'interpolation-expression': {
pattern: /(^\$\{)[\s\S]+(?=\}$)/,
lookbehind: true,
inside: Prism.languages.chaiscript
},
'interpolation-punctuation': {
pattern: /^\$\{|\}$/,
alias: 'punctuation'
}
}
},
'string': /[\s\S]+/
}
},
});
1 change: 1 addition & 0 deletions components/prism-chaiscript.min.js

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

56 changes: 56 additions & 0 deletions examples/prism-chaiscript.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<h2>Full example</h2>
<pre><code>// http://chaiscript.com/examples.html#ChaiScript_Language_Examples
// Source: https://gist.github.com/lefticus/cf058f2927fef68d58e0#file-chaiscript_overview-chai

// ChaiScript supports the normal kind of control blocks you've come to expect from
// C++ and JavaScript


if (5 > 2) {
print("Yup, 5 > 2");
} else if (2 > 5) {
// never gonna happen
} else {
// really not going to happen
}

var x = true;

while (x)
{
print("x was true")
x = false;
}

for (var i = 1; i &lt; 10; ++i)
{
print(i); // prints 1 through 9
}


// function definition

def myFunc(x) {
print(x);
}

// function definition with function guards
def myFunc(x) : x > 2 && x &lt; 5 {
print(to_string(x) + " is between 2 and 5")
}

def myFunc(x) : x >= 5 {
print(t_string(x) + " is greater than or equal to 5")
}

myFunc(3)

// ChaiScript also supports in string evaluation, which C++ does not

print("${3 + 5} is 8");

// And dynamic code evaluation

var value = eval("4 + 2 + 12");

// value is equal to 18</code></pre>
4 changes: 4 additions & 0 deletions plugins/autoloader/prism-autoloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
"c": "clike",
"csharp": "clike",
"cpp": "c",
"chaiscript": [
"clike",
"cpp"
],
"coffeescript": "javascript",
"crystal": "ruby",
"css-extras": "css",
Expand Down
2 changes: 1 addition & 1 deletion plugins/autoloader/prism-autoloader.min.js

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

9 changes: 9 additions & 0 deletions tests/languages/chaiscript/boolean_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
true
false

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

[
["boolean", "true"],
["boolean", "false"]
]
16 changes: 16 additions & 0 deletions tests/languages/chaiscript/comment_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// comment
/*
comment
*/

/*
comment

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

[
["comment", "// comment"],
["comment", "/*\r\n comment\r\n*/"],

["comment", "/*\r\n comment"]
]
190 changes: 190 additions & 0 deletions tests/languages/chaiscript/function_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
attr Rectangle::height
attr Rectangle::width
def Rectangle::Rectangle() { this.height = 10; this.width = 20 }
def Rectangle::area() { this.height * this.width }
var rect = Rectangle()
rect.height = 30
print(rect.area())

class Rectangle {
attr height
attr width
def Rectangle() { this.height = 10; this.width = 20 }
def area() { this.height * this.width }
}
var rect = Rectangle()
rect.height = 30
print(rect.area())

def update(dt) {
x = x + 20.0f * dt
}

def foo(int x, Vector y) {}

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

[
["keyword", "attr"],
["class-name", "Rectangle"],
["operator", "::"],
"height\r\n",

["keyword", "attr"],
["class-name", "Rectangle"],
["operator", "::"],
"width\r\n",

["keyword", "def"],
["class-name", "Rectangle"],
["operator", "::"],
["function", "Rectangle"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["keyword", "this"],
["punctuation", "."],
"height ",
["operator", "="],
["number", "10"],
["punctuation", ";"],
["keyword", "this"],
["punctuation", "."],
"width ",
["operator", "="],
["number", "20"],
["punctuation", "}"],

["keyword", "def"],
["class-name", "Rectangle"],
["operator", "::"],
["function", "area"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["keyword", "this"],
["punctuation", "."],
"height ",
["operator", "*"],
["keyword", "this"],
["punctuation", "."],
"width ",
["punctuation", "}"],

["keyword", "var"],
" rect ",
["operator", "="],
["function", "Rectangle"],
["punctuation", "("],
["punctuation", ")"],

"\r\nrect",
["punctuation", "."],
"height ",
["operator", "="],
["number", "30"],

["function", "print"],
["punctuation", "("],
"rect",
["punctuation", "."],
["function", "area"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ")"],

["keyword", "class"],
["class-name", "Rectangle"],
["punctuation", "{"],

["keyword", "attr"],
" height\r\n ",

["keyword", "attr"],
" width\r\n ",

["keyword", "def"],
["function", "Rectangle"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["keyword", "this"],
["punctuation", "."],
"height ",
["operator", "="],
["number", "10"],
["punctuation", ";"],
["keyword", "this"],
["punctuation", "."],
"width ",
["operator", "="],
["number", "20"],
["punctuation", "}"],

["keyword", "def"],
["function", "area"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["keyword", "this"],
["punctuation", "."],
"height ",
["operator", "*"],
["keyword", "this"],
["punctuation", "."],
"width ",
["punctuation", "}"],

["punctuation", "}"],

["keyword", "var"],
" rect ",
["operator", "="],
["function", "Rectangle"],
["punctuation", "("],
["punctuation", ")"],

"\r\nrect",
["punctuation", "."],
"height ",
["operator", "="],
["number", "30"],

["function", "print"],
["punctuation", "("],
"rect",
["punctuation", "."],
["function", "area"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ")"],

["keyword", "def"],
["function", "update"],
["punctuation", "("],
"dt",
["punctuation", ")"],
["punctuation", "{"],

"\r\n x ",
["operator", "="],
" x ",
["operator", "+"],
["number", "20.0f"],
["operator", "*"],
" dt\r\n",

["punctuation", "}"],

["keyword", "def"],
["function", "foo"],
["punctuation", "("],
["parameter-type", "int"],
" x",
["punctuation", ","],
["parameter-type", "Vector"],
" y",
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"]
]
Loading

0 comments on commit 3f7d745

Please sign in to comment.