Skip to content

Commit 9c276ab

Browse files
committed
Merge pull request #626 from Golmote/prism-pure
Add support for Pure
2 parents a6a2711 + dbc5e9c commit 9c276ab

14 files changed

+1122
-0
lines changed

components.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,10 @@ var components = {
305305
"title": "PowerShell",
306306
"owner": "nauzilus"
307307
},
308+
"pure": {
309+
"title": "Pure",
310+
"owner": "Golmote"
311+
},
308312
"python": {
309313
"title": "Python",
310314
"owner": "multipetros"

components/prism-pure.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
(function (Prism) {
2+
Prism.languages.pure = {
3+
'inline-lang': {
4+
pattern: /%<[\s\S]+?%>/,
5+
inside: {
6+
'lang': {
7+
pattern: /(^%< *)-\*-.+?-\*-/,
8+
lookbehind: true,
9+
alias: 'comment'
10+
},
11+
'delimiter': {
12+
pattern: /^%<.*|%>$/,
13+
alias: 'punctuation'
14+
}
15+
}
16+
},
17+
'comment': [
18+
{
19+
pattern: /(^|[^\\])\/\*[\w\W]*?\*\//,
20+
lookbehind: true
21+
},
22+
{
23+
pattern: /(^|[^\\:])\/\/.*/,
24+
lookbehind: true
25+
},
26+
/#!.+/
27+
],
28+
'string': /"(?:\\.|[^"\\\r\n])*"/,
29+
'number': {
30+
// The look-behind prevents wrong highlighting of the .. operator
31+
pattern: /((?:\.\.)?)(?:\b(?:inf|nan)\b|\b0x[\da-f]+|(?:\b(?:0b)?\d+(?:\.\d)?|\B\.\d)\d*(?:e[+-]?\d+)?L?)/i,
32+
lookbehind: true
33+
},
34+
'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/,
35+
'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/,
36+
'special': {
37+
pattern: /\b__[a-z]+__\b/i,
38+
alias: 'builtin'
39+
},
40+
// Any combination of operator chars can be an operator
41+
'operator': /(?=\b_|[^_])[!"#$%&'*+,\-.\/:<=>?@\\^_`|~\u00a1-\u00bf\u00d7-\u00f7\u20d0-\u2bff]+|\b(?:and|div|mod|not|or)\b/,
42+
// FIXME: How can we prevent | and , to be highlighted as operator when they are used alone?
43+
'punctuation': /[(){}\[\];,|]/
44+
};
45+
46+
var inlineLanguages = [
47+
'c',
48+
{ lang: 'c++', alias: 'cpp' },
49+
'fortran',
50+
'ats',
51+
'dsp'
52+
];
53+
var inlineLanguageRe = '%< *-\\*- *{lang}\\d* *-\\*-[\\s\\S]+?%>';
54+
55+
inlineLanguages.forEach(function (lang) {
56+
var alias = lang;
57+
if (typeof lang !== 'string') {
58+
alias = lang.alias;
59+
lang = lang.lang;
60+
}
61+
if (Prism.languages[alias]) {
62+
var o = {};
63+
o['inline-lang-' + alias] = {
64+
pattern: RegExp(inlineLanguageRe.replace('{lang}', lang.replace(/([.+*?\/\\(){}\[\]])/g,'\\$1')), 'i'),
65+
inside: Prism.util.clone(Prism.languages.pure['inline-lang'].inside)
66+
};
67+
o['inline-lang-' + alias].inside.rest = Prism.util.clone(Prism.languages[alias]);
68+
Prism.languages.insertBefore('pure', 'inline-lang', o);
69+
}
70+
});
71+
72+
// C is the default inline language
73+
if (Prism.languages.c) {
74+
Prism.languages.pure['inline-lang'].inside.rest = Prism.util.clone(Prism.languages.c);
75+
}
76+
77+
}(Prism));

components/prism-pure.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/prism-pure.html

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<h1>Pure</h1>
2+
<p>To use this language, use the class "language-pure".</p>
3+
4+
<h2>Comments</h2>
5+
<pre><code>#! shebang
6+
// Single line comment
7+
/* Multi-line
8+
comment */</code></pre>
9+
10+
<h2>Strings</h2>
11+
<pre><code>"This is a string."
12+
"This is a string with \"quotes\" in it."</code></pre>
13+
14+
<h2>Numbers</h2>
15+
<pre><code>4711
16+
4711L
17+
1.2e-3
18+
.14
19+
1000
20+
0x3e8
21+
01750
22+
0b1111101000
23+
inf
24+
nan</code></pre>
25+
26+
<h2>Inline code</h2>
27+
<p>Inline code requires the desired language to be loaded.
28+
On this page, check C, C++ and Fortran <strong>before</strong> checking Pure should make
29+
the examples below work properly.</p>
30+
<pre><code>%<
31+
int mygcd(int x, int y)
32+
{
33+
if (y == 0)
34+
return x;
35+
else
36+
return mygcd(y, x%y);
37+
}
38+
%>
39+
40+
%< -*- Fortran90 -*-
41+
function fact(n) result(p)
42+
integer n, p
43+
p = 1
44+
do i = 1, n
45+
p = p*i
46+
end do
47+
end function fact
48+
%>
49+
50+
%< -*- C++ -*-
51+
52+
#include &lt;pure/runtime.h>
53+
#include &lt;string>
54+
#include &lt;map>
55+
56+
// An STL map mapping strings to Pure expressions.
57+
58+
using namespace std;
59+
typedef map&lt;string,pure_expr*> exprmap;
60+
61+
// Since we can't directly deal with C++ classes in Pure, provide some C
62+
// functions to create, destroy and manipulate these objects.
63+
64+
extern "C" exprmap *map_create()
65+
{
66+
return new exprmap;
67+
}
68+
69+
extern "C" void map_add(exprmap *m, const char *key, pure_expr *x)
70+
{
71+
exprmap::iterator it = m->find(string(key));
72+
if (it != m->end()) pure_free(it->second);
73+
(*m)[key] = pure_new(x);
74+
}
75+
76+
extern "C" void map_del(exprmap *m, const char *key)
77+
{
78+
exprmap::iterator it = m->find(key);
79+
if (it != m->end()) {
80+
pure_free(it->second);
81+
m->erase(it);
82+
}
83+
}
84+
85+
extern "C" pure_expr *map_get(exprmap *m, const char *key)
86+
{
87+
exprmap::iterator it = m->find(key);
88+
return (it != m->end())?it->second:0;
89+
}
90+
91+
extern "C" pure_expr *map_keys(exprmap *m)
92+
{
93+
size_t i = 0, n = m->size();
94+
pure_expr **xs = new pure_expr*[n];
95+
for (exprmap::iterator it = m->begin(); it != m->end(); ++it)
96+
xs[i++] = pure_string_dup(it->first.c_str());
97+
pure_expr *x = pure_listv(n, xs);
98+
delete[] xs;
99+
return x;
100+
}
101+
102+
extern "C" void map_destroy(exprmap *m)
103+
{
104+
for (exprmap::iterator it = m->begin(); it != m->end(); ++it)
105+
pure_free(it->second);
106+
delete m;
107+
}
108+
109+
%></code></pre>
110+
111+
<h2>Example</h2>
112+
<pre><code>queens n = catch reverse (search n 1 []) with
113+
search n i p = throw p if i>n;
114+
= void [search n (i+1) ((i,j):p) | j = 1..n; safe (i,j) p];
115+
safe (i,j) p = ~any (check (i,j)) p;
116+
check (i1,j1) (i2,j2)
117+
= i1==i2 || j1==j2 || i1+j1==i2+j2 || i1-j1==i2-j2;
118+
end;</code></pre>
119+
120+
<h2>Known failures</h2>
121+
<p>There are certain edge cases where Prism will fail.
122+
There are always such cases in every regex-based syntax highlighter.
123+
However, Prism dares to be open and honest about them.
124+
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.
125+
</p>
126+
127+
<h3>Commented inline code</h3>
128+
<pre><code>/* %<
129+
f()
130+
%> */</code></pre>
131+
132+
<h3>Comment-like substrings</h3>
133+
<pre><code>"foo /* bar */ baz"; "foo // bar";</code></pre>
134+
135+
<h3>Inline code-like substrings</h3>
136+
<pre><code>"foo %< f() %> bar"</code></pre>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
%<
2+
asm ();
3+
%>
4+
5+
%< -*- C -*-
6+
asm ();
7+
%>
8+
9+
----------------------------------------------------
10+
11+
[
12+
["inline-lang", [
13+
["delimiter", "%<"],
14+
["keyword", "asm"], ["punctuation", "("], ["punctuation", ")"], ["punctuation", ";"],
15+
["delimiter", "%>"]
16+
]],
17+
18+
["inline-lang-c", [
19+
["delimiter", "%< "],
20+
["lang", "-*- C -*-"],
21+
["keyword", "asm"], ["punctuation", "("], ["punctuation", ")"], ["punctuation", ";"],
22+
["delimiter", "%>"]
23+
]]
24+
]
25+
26+
----------------------------------------------------
27+
28+
Checks for C in Pure.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
%< -*- C++ -*-
2+
alignas
3+
%>
4+
5+
----------------------------------------------------
6+
7+
[
8+
["inline-lang-cpp", [
9+
["delimiter", "%< "],
10+
["lang", "-*- C++ -*-"],
11+
["keyword", "alignas"],
12+
["delimiter", "%>"]
13+
]]
14+
]
15+
16+
----------------------------------------------------
17+
18+
Checks for C++ in Pure.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
%< -*- Fortran90 -*-
2+
21_SHORT
3+
%>
4+
5+
----------------------------------------------------
6+
7+
[
8+
["inline-lang-fortran", [
9+
["delimiter", "%< "],
10+
["lang", "-*- Fortran90 -*-"],
11+
["number", "21_SHORT"],
12+
["delimiter", "%>"]
13+
]]
14+
]
15+
16+
----------------------------------------------------
17+
18+
Checks for Fortran in Pure.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// Foobar
3+
/**/
4+
/* Foo
5+
bar */
6+
#! --nochecks
7+
8+
----------------------------------------------------
9+
10+
[
11+
["comment", "//"],
12+
["comment", "// Foobar"],
13+
["comment", "/**/"],
14+
["comment", "/* Foo\r\nbar */"],
15+
["comment", "#! --nochecks"]
16+
]
17+
18+
----------------------------------------------------
19+
20+
Checks for comments.

0 commit comments

Comments
 (0)