Skip to content
This repository was archived by the owner on Apr 22, 2020. It is now read-only.

Commit 62d129a

Browse files
author
mikesamuel@gmail.com
committed
issue 169: erlang support courtesy Andrew Allen
1 parent 4060323 commit 62d129a

4 files changed

Lines changed: 151 additions & 2 deletions

File tree

CHANGES.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ <h2>29 March 2011</h2>
153153
</ul>
154154
<h2>4 February 2013</h2>
155155
<ul>
156-
<li>Language handlers for Dart, TCL, R.</li>
156+
<li>Language handlers for Dart, Erlang, TCL, R.</li>
157157
<li>Bug fix: VB REM style comments</li>
158158
<li>Bug fix: CSS color literals / ID selector confusion.</li>
159159
</ul>

README.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ <h3 id="langs">For which languages does it work?</h3>
8080
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-css.js"
8181
><code>CSS</code></a>,
8282
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-go.js"
83+
><code>Erlang</code></a>,
84+
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-erlang.js"
8385
><code>Go</code></a>,
8486
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-hs.js"
8587
><code>Haskell</code></a>,

src/lang-erlang.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright (C) 2013 Andrew Allen
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
16+
/**
17+
* @fileoverview
18+
* Registers a language handler for Erlang.
19+
*
20+
* Derived from https://raw.github.com/erlang/otp/dev/lib/compiler/src/core_parse.yrl
21+
* Modified from Mike Samuel's Haskell plugin for google-code-prettify
22+
*
23+
* @author achew22@gmail.com
24+
*/
25+
26+
PR['registerLangHandler'](
27+
PR['createSimpleLexer'](
28+
[
29+
// Whitespace
30+
// whitechar -> newline | vertab | space | tab | uniWhite
31+
// newline -> return linefeed | return | linefeed | formfeed
32+
[PR['PR_PLAIN'], /^[\t\n\x0B\x0C\r ]+/, null, '\t\n\x0B\x0C\r '],
33+
// Single line double-quoted strings.
34+
[PR['PR_STRING'], /^\"(?:[^\"\\\n\x0C\r]|\\[\s\S])*(?:\"|$)/,
35+
null, '"'],
36+
37+
// Handle atoms
38+
[PR['PR_LITERAL'], /^[a-z][a-zA-Z0-9_]*/],
39+
// Handle single quoted atoms
40+
[PR['PR_LITERAL'], /^\'(?:[^\'\\\n\x0C\r]|\\[^&])+\'?/,
41+
null, "'"],
42+
43+
// Handle macros. Just to be extra clear on this one, it detects the ?
44+
// then uses the regexp to end it so be very careful about matching
45+
// all the terminal elements
46+
[PR['PR_LITERAL'], /^\?[^ \t\n({]+/, null, "?"],
47+
48+
49+
50+
// decimal -> digit{digit}
51+
// octal -> octit{octit}
52+
// hexadecimal -> hexit{hexit}
53+
// integer -> decimal
54+
// | 0o octal | 0O octal
55+
// | 0x hexadecimal | 0X hexadecimal
56+
// float -> decimal . decimal [exponent]
57+
// | decimal exponent
58+
// exponent -> (e | E) [+ | -] decimal
59+
[PR['PR_LITERAL'],
60+
/^(?:0o[0-7]+|0x[\da-f]+|\d+(?:\.\d+)?(?:e[+\-]?\d+)?)/i,
61+
null, '0123456789']
62+
],
63+
[
64+
// TODO: catch @declarations inside comments
65+
66+
// Comments in erlang are started with % and go till a newline
67+
[PR['PR_COMMENT'], /^%[^\n]*/],
68+
69+
// Catch macros
70+
//[PR['PR_TAG'], /?[^( \n)]+/],
71+
72+
/**
73+
* %% Keywords (atoms are assumed to always be single-quoted).
74+
* 'module' 'attributes' 'do' 'let' 'in' 'letrec'
75+
* 'apply' 'call' 'primop'
76+
* 'case' 'of' 'end' 'when' 'fun' 'try' 'catch' 'receive' 'after'
77+
*/
78+
[PR['PR_KEYWORD'], /^(?:module|attributes|do|let|in|letrec|apply|call|primop|case|of|end|when|fun|try|catch|receive|after|char|integer|float,atom,string,var)\b/],
79+
80+
/**
81+
* Catch definitions (usually defined at the top of the file)
82+
* Anything that starts -something
83+
*/
84+
[PR['PR_KEYWORD'], /^-[a-z_]+/],
85+
86+
// Catch variables
87+
[PR['PR_TYPE'], /^[A-Z_][a-zA-Z0-9_]*/],
88+
89+
// matches the symbol production
90+
[PR['PR_PUNCTUATION'], /^[.,;]/]
91+
]),
92+
['erlang', 'erl']);

tests/prettify_test.html

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
var sources = [
99
"prettify.js",
1010
"lang-css.js",
11+
"lang-erlang.js",
1112
"lang-go.js",
1213
"lang-hs.js",
1314
"lang-lisp.js",
@@ -1322,6 +1323,35 @@ <h1>Go mode</h1>
13221323
/* ` */ `foo /* ` /*/ */
13231324
</pre>
13241325

1326+
<h1>Erlang mode</h1>
1327+
<pre class="prettyprint lang-erlang" id="erlang">
1328+
% Sample comment
1329+
1330+
-module(my_test).
1331+
-include_lib("my_sample_lib.hrl").
1332+
-export([
1333+
test/2
1334+
]).
1335+
1336+
%% @doc Define a macro
1337+
-define(my_macro, Variable).
1338+
1339+
%% @doc My function
1340+
test(Variables, MoreVariables) ->
1341+
% Inline comment
1342+
{ok,Scanned,_} = my_lib:do_stuff(),
1343+
1344+
Variable = fun(V) -> {ok, V} end,
1345+
1346+
try ?my_macro({value, test}) of
1347+
{value, Result, _} ->
1348+
{ok, Result}
1349+
catch
1350+
Type:Error ->
1351+
{'error', Type, Error}
1352+
end.
1353+
</pre>
1354+
13251355
Test IE by copy/pasting content here.
13261356
<textarea cols="80" rows="40"></textarea>
13271357
</body>
@@ -2992,7 +3022,32 @@ <h1>Go mode</h1>
29923022
'}\n' +
29933023
'\n' +
29943024
'`END`COM/* " *\/`END`PLN "foo /* " `END`COM/*\/ *\/`END`PLN\n' +
2995-
'`END`COM/* ` *\/`END`PLN `foo /* ` `END`COM/*\/ *\/`END'
3025+
'`END`COM/* ` *\/`END`PLN `foo /* ` `END`COM/*\/ *\/`END',
3026+
erlang: '`COM% Sample comment`END`PLN\n' +
3027+
'\n' +
3028+
'`END`KWD-module`END`PLN(my_test)`END`PUN.`END`PLN\n' +
3029+
'`END`KWD-include_lib`END`PLN(`END`STR"my_sample_lib.hrl"`END`PLN)`END`PUN.`END`PLN\n' +
3030+
'`END`KWD-export`END`PLN([\n' +
3031+
' test/`END`LIT2`END`PLN\n' +
3032+
'])`END`PUN.`END`PLN\n' +
3033+
'\n' +
3034+
'`END`COM%% @doc Define a macro`END`PLN\n' +
3035+
'`END`KWD-define`END`PLN(my_macro`END`PUN,`END`PLN `END`TYPVariable`END`PLN)`END`PUN.`END`PLN\n' +
3036+
'\n' +
3037+
'`END`COM%% @doc My function`END`PLN\n' +
3038+
'test(`END`TYPVariables`END`PUN,`END`PLN `END`TYPMoreVariables`END`PLN) -&gt;\n' +
3039+
' `END`COM% Inline comment`END`PLN\n' +
3040+
' {ok`END`PUN,`END`TYPScanned`END`PUN,`END`TYP_`END`PLN} = my_lib:do_stuff()`END`PUN,`END`PLN\n' +
3041+
'\n' +
3042+
' `END`TYPVariable`END`PLN = `END`KWDfun`END`PLN(`END`TYPV`END`PLN) -&gt; {ok`END`PUN,`END`PLN `END`TYPV`END`PLN} `END`KWDend`END`PUN,`END`PLN\n' +
3043+
'\n' +
3044+
' `END`KWDtry`END`PLN `END`LIT?my_macro`END`PLN({value`END`PUN,`END`PLN test}) `END`KWDof`END`PLN\n' +
3045+
' {value`END`PUN,`END`PLN `END`TYPResult`END`PUN,`END`PLN `END`TYP_`END`PLN} -&gt;\n' +
3046+
' {ok`END`PUN,`END`PLN `END`TYPResult`END`PLN}\n' +
3047+
' `END`KWDcatch`END`PLN\n' +
3048+
' `END`TYPType`END`PLN:`END`TYPError`END`PLN -&gt;\n' +
3049+
' {`END`LIT\'error\'`END`PUN,`END`PLN `END`TYPType`END`PUN,`END`PLN `END`TYPError`END`PLN}\n' +
3050+
' `END`KWDend`END`PUN.`END'
29963051
};
29973052
</script>
29983053

0 commit comments

Comments
 (0)