Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Pure #626

Merged
merged 4 commits into from
Sep 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions components.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ var components = {
"title": "PowerShell",
"owner": "nauzilus"
},
"pure": {
"title": "Pure",
"owner": "Golmote"
},
"python": {
"title": "Python",
"owner": "multipetros"
Expand Down
77 changes: 77 additions & 0 deletions components/prism-pure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
(function (Prism) {
Prism.languages.pure = {
'inline-lang': {
pattern: /%<[\s\S]+?%>/,
inside: {
'lang': {
pattern: /(^%< *)-\*-.+?-\*-/,
lookbehind: true,
alias: 'comment'
},
'delimiter': {
pattern: /^%<.*|%>$/,
alias: 'punctuation'
}
}
},
'comment': [
{
pattern: /(^|[^\\])\/\*[\w\W]*?\*\//,
lookbehind: true
},
{
pattern: /(^|[^\\:])\/\/.*/,
lookbehind: true
},
/#!.+/
],
'string': /"(?:\\.|[^"\\\r\n])*"/,
'number': {
// The look-behind prevents wrong highlighting of the .. operator
pattern: /((?:\.\.)?)(?:\b(?:inf|nan)\b|\b0x[\da-f]+|(?:\b(?:0b)?\d+(?:\.\d)?|\B\.\d)\d*(?:e[+-]?\d+)?L?)/i,
lookbehind: true
},
'keyword': /\b(?:ans|break|bt|case|catch|cd|clear|const|def|del|dump|else|end|exit|extern|false|force|help|if|infix[lr]?|interface|let|ls|mem|namespace|nonfix|NULL|of|otherwise|outfix|override|postfix|prefix|private|public|pwd|quit|run|save|show|stats|then|throw|trace|true|type|underride|using|when|with)\b/,
'function': /\b(?:abs|add_(?:(?:fundef|interface|macdef|typedef)(?:_at)?|addr|constdef|vardef)|all|any|applp?|arity|bigintp?|blob(?:_crc|_size|p)?|boolp?|byte_(?:matrix|pointer)|byte_c?string(?:_pointer)?|calloc|cat|catmap|ceil|char[ps]?|check_ptrtag|chr|clear_sentry|clearsym|closurep?|cmatrixp?|cols?|colcat(?:map)?|colmap|colrev|colvector(?:p|seq)?|complex(?:_float_(?:matrix|pointer)|_matrix(?:_view)?|_pointer|p)?|conj|cookedp?|cst|cstring(?:_(?:dup|list|vector))?|curry3?|cyclen?|del_(?:constdef|fundef|interface|macdef|typedef|vardef)|delete|diag(?:mat)?|dim|dmatrixp?|do|double(?:_matrix(?:_view)?|_pointer|p)?|dowith3?|drop|dropwhile|eval(?:cmd)?|exactp|filter|fix|fixity|flip|float(?:_matrix|_pointer)|floor|fold[lr]1?|frac|free|funp?|functionp?|gcd|get(?:_(?:byte|constdef|double|float|fundef|int(?:64)?|interface(?:_typedef)?|long|macdef|pointer|ptrtag|short|sentry|string|typedef|vardef))?|globsym|hash|head|id|im|imatrixp?|index|inexactp|infp|init|insert|int(?:_matrix(?:_view)?|_pointer|p)?|int64_(?:matrix|pointer)|integerp?|iteraten?|iterwhile|join|keys?|lambdap?|last(?:err(?:pos)?)?|lcd|list[2p]?|listmap|make_ptrtag|malloc|map|matcat|matrixp?|max|member|min|nanp|nargs|nmatrixp?|null|numberp?|ord|pack(?:ed)?|pointer(?:_cast|_tag|_type|p)?|pow|pred|ptrtag|put(?:_(?:byte|double|float|int(?:64)?|long|pointer|short|string))?|rationalp?|re|realp?|realloc|recordp?|redim|reduce(?:_with)?|refp?|repeatn?|reverse|rlistp?|round|rows?|rowcat(?:map)?|rowmap|rowrev|rowvector(?:p|seq)?|same|scan[lr]1?|sentry|sgn|short_(?:matrix|pointer)|slice|smatrixp?|sort|split|str|strcat|stream|stride|string(?:_(?:dup|list|vector)|p)?|subdiag(?:mat)?|submat|subseq2?|substr|succ|supdiag(?:mat)?|symbolp?|tail|take|takewhile|thunkp?|transpose|trunc|tuplep?|typep|ubyte|uint(?:64)?|ulong|uncurry3?|unref|unzip3?|update|ushort|vals?|varp?|vector(?:p|seq)?|void|zip3?|zipwith3?)\b/,
'special': {
pattern: /\b__[a-z]+__\b/i,
alias: 'builtin'
},
// Any combination of operator chars can be an operator
'operator': /(?=\b_|[^_])[!"#$%&'*+,\-.\/:<=>?@\\^_`|~\u00a1-\u00bf\u00d7-\u00f7\u20d0-\u2bff]+|\b(?:and|div|mod|not|or)\b/,
// FIXME: How can we prevent | and , to be highlighted as operator when they are used alone?
'punctuation': /[(){}\[\];,|]/
};

var inlineLanguages = [
'c',
{ lang: 'c++', alias: 'cpp' },
'fortran',
'ats',
'dsp'
];
var inlineLanguageRe = '%< *-\\*- *{lang}\\d* *-\\*-[\\s\\S]+?%>';

inlineLanguages.forEach(function (lang) {
var alias = lang;
if (typeof lang !== 'string') {
alias = lang.alias;
lang = lang.lang;
}
if (Prism.languages[alias]) {
var o = {};
o['inline-lang-' + alias] = {
pattern: RegExp(inlineLanguageRe.replace('{lang}', lang.replace(/([.+*?\/\\(){}\[\]])/g,'\\$1')), 'i'),
inside: Prism.util.clone(Prism.languages.pure['inline-lang'].inside)
};
o['inline-lang-' + alias].inside.rest = Prism.util.clone(Prism.languages[alias]);
Prism.languages.insertBefore('pure', 'inline-lang', o);
}
});

// C is the default inline language
if (Prism.languages.c) {
Prism.languages.pure['inline-lang'].inside.rest = Prism.util.clone(Prism.languages.c);
}

}(Prism));
1 change: 1 addition & 0 deletions components/prism-pure.min.js

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

136 changes: 136 additions & 0 deletions examples/prism-pure.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<h1>Pure</h1>
<p>To use this language, use the class "language-pure".</p>

<h2>Comments</h2>
<pre><code>#! shebang
// Single line comment
/* Multi-line
comment */</code></pre>

<h2>Strings</h2>
<pre><code>"This is a string."
"This is a string with \"quotes\" in it."</code></pre>

<h2>Numbers</h2>
<pre><code>4711
4711L
1.2e-3
.14
1000
0x3e8
01750
0b1111101000
inf
nan</code></pre>

<h2>Inline code</h2>
<p>Inline code requires the desired language to be loaded.
On this page, check C, C++ and Fortran <strong>before</strong> checking Pure should make
the examples below work properly.</p>
<pre><code>%<
int mygcd(int x, int y)
{
if (y == 0)
return x;
else
return mygcd(y, x%y);
}
%>

%< -*- Fortran90 -*-
function fact(n) result(p)
integer n, p
p = 1
do i = 1, n
p = p*i
end do
end function fact
%>

%< -*- C++ -*-

#include &lt;pure/runtime.h>
#include &lt;string>
#include &lt;map>

// An STL map mapping strings to Pure expressions.

using namespace std;
typedef map&lt;string,pure_expr*> exprmap;

// Since we can't directly deal with C++ classes in Pure, provide some C
// functions to create, destroy and manipulate these objects.

extern "C" exprmap *map_create()
{
return new exprmap;
}

extern "C" void map_add(exprmap *m, const char *key, pure_expr *x)
{
exprmap::iterator it = m->find(string(key));
if (it != m->end()) pure_free(it->second);
(*m)[key] = pure_new(x);
}

extern "C" void map_del(exprmap *m, const char *key)
{
exprmap::iterator it = m->find(key);
if (it != m->end()) {
pure_free(it->second);
m->erase(it);
}
}

extern "C" pure_expr *map_get(exprmap *m, const char *key)
{
exprmap::iterator it = m->find(key);
return (it != m->end())?it->second:0;
}

extern "C" pure_expr *map_keys(exprmap *m)
{
size_t i = 0, n = m->size();
pure_expr **xs = new pure_expr*[n];
for (exprmap::iterator it = m->begin(); it != m->end(); ++it)
xs[i++] = pure_string_dup(it->first.c_str());
pure_expr *x = pure_listv(n, xs);
delete[] xs;
return x;
}

extern "C" void map_destroy(exprmap *m)
{
for (exprmap::iterator it = m->begin(); it != m->end(); ++it)
pure_free(it->second);
delete m;
}

%></code></pre>

<h2>Example</h2>
<pre><code>queens n = catch reverse (search n 1 []) with
search n i p = throw p if i>n;
= void [search n (i+1) ((i,j):p) | j = 1..n; safe (i,j) p];
safe (i,j) p = ~any (check (i,j)) p;
check (i1,j1) (i2,j2)
= i1==i2 || j1==j2 || i1+j1==i2+j2 || i1-j1==i2-j2;
end;</code></pre>

<h2>Known failures</h2>
<p>There are certain edge cases where Prism will fail.
There are always such cases in every regex-based syntax highlighter.
However, Prism dares to be open and honest about them.
If a failure is listed here, it doesn’t mean it will never be fixed. This is more of a “known bugs” list, just with a certain type of bug.
</p>

<h3>Commented inline code</h3>
<pre><code>/* %<
f()
%> */</code></pre>

<h3>Comment-like substrings</h3>
<pre><code>"foo /* bar */ baz"; "foo // bar";</code></pre>

<h3>Inline code-like substrings</h3>
<pre><code>"foo %< f() %> bar"</code></pre>
28 changes: 28 additions & 0 deletions tests/languages/c+pure/c_inclusion.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
%<
asm ();
%>

%< -*- C -*-
asm ();
%>

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

[
["inline-lang", [
["delimiter", "%<"],
["keyword", "asm"], ["punctuation", "("], ["punctuation", ")"], ["punctuation", ";"],
["delimiter", "%>"]
]],

["inline-lang-c", [
["delimiter", "%< "],
["lang", "-*- C -*-"],
["keyword", "asm"], ["punctuation", "("], ["punctuation", ")"], ["punctuation", ";"],
["delimiter", "%>"]
]]
]

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

Checks for C in Pure.
18 changes: 18 additions & 0 deletions tests/languages/cpp+pure/cpp_inclusion.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
%< -*- C++ -*-
alignas
%>

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

[
["inline-lang-cpp", [
["delimiter", "%< "],
["lang", "-*- C++ -*-"],
["keyword", "alignas"],
["delimiter", "%>"]
]]
]

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

Checks for C++ in Pure.
18 changes: 18 additions & 0 deletions tests/languages/fortran+pure/fortran_inclusion.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
%< -*- Fortran90 -*-
21_SHORT
%>

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

[
["inline-lang-fortran", [
["delimiter", "%< "],
["lang", "-*- Fortran90 -*-"],
["number", "21_SHORT"],
["delimiter", "%>"]
]]
]

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

Checks for Fortran in Pure.
20 changes: 20 additions & 0 deletions tests/languages/pure/comment_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Foobar
/**/
/* Foo
bar */
#! --nochecks

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

[
["comment", "//"],
["comment", "// Foobar"],
["comment", "/**/"],
["comment", "/* Foo\r\nbar */"],
["comment", "#! --nochecks"]
]

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

Checks for comments.
Loading