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

Commit 4d87233

Browse files
author
mikesamuel
committed
language extension for LUA
1 parent a25617a commit 4d87233

File tree

5 files changed

+194
-21
lines changed

5 files changed

+194
-21
lines changed

CHANGES.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,17 @@ <h2>29 March 2007</h2>
3737
>patch</a>
3838
<li>Added a <a href="http://google-code-prettify.googlecode.com/files/prettify-small.zip">distribution</a> that has comments and
3939
whitespace removed to reduce download size from 45.5kB to 12.8kB.
40+
</ul>
41+
<h2>4 Jul 2008</h2>
42+
<ul>
4043
<li>Added <a href="http://code.google.com/p/google-code-prettify/issues/detail?id=17">language specific formatters</a> that are triggered by the presence
4144
of a <code>lang-&lt;language-file-extension&gt;</code></li>
4245
<li>Fixed <a href="http://code.google.com/p/google-code-prettify/issues/detail?id=29">bug</a>: python handling of <code>'''string'''</code>
4346
<li>Fixed bug: <code>/</code> in regex <code>[charsets] should not end regex</code>
4447
</ul>
48+
<h2>5 Jul 2008</h2>
49+
<ul>
50+
<li>Defined language extensions for Lisp and LUA</code>
51+
</ul>
4552
</body>
4653
</html>

README.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ <h3>Which languages does it work for?</h3>
7373
<p>LISPy languages are supported via an extension:
7474
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-lisp.js"
7575
><code>lang-lisp.js</code></a>.</p>
76+
<p>And similarly for <a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-lua.js"
77+
><code>LUA</code></a>
7678

7779
<p>If you'd like to add an extension for your favorite language, please
7880
look at lang-lisp.js and file an

src/lang-lua.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (C) 2008 Google Inc.
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+
/**
18+
* @fileoverview
19+
* Registers a language handler for LUA.
20+
*
21+
*
22+
* To use, include prettify.js and this file in your HTML page.
23+
* Then put your code in an HTML tag like
24+
* <pre class="prettyprint lang-lua">(my LUA code)</pre>
25+
*
26+
*
27+
* I used http://www.lua.org/manual/5.1/manual.html#2.1
28+
* Because of the long-bracket concept used in strings and comments, LUA does
29+
* not have a regular lexical grammar, but luckily it fits within the space
30+
* of irregular grammars supported by javascript regular expressions.
31+
*
32+
33+
*/
34+
35+
PR.registerLangHandler(
36+
PR.createSimpleLexer(
37+
[
38+
// Whitespace
39+
[PR.PR_PLAIN, /^[\t\n\r \xA0]+/, null, '\t\n\r \xA0'],
40+
// A double or single quoted, possibly multi-line, string.
41+
[PR.PR_STRING, /^(?:\"(?:[^\"\\]|\\[\s\S])*(?:\"|$)|\'(?:[^\'\\]|\\[\s\S])*(?:\'|$))/, null, '"\'']
42+
],
43+
[
44+
// A comment is either a line comment that starts with two dashes, or
45+
// two dashes preceding a long bracketed block.
46+
[PR.PR_COMMENT, /^--(?:\[(=*)\[[\s\S]*?(?:\]\1\]|$)|[^\r\n]*)/],
47+
// A long bracketed block not preceded by -- is a string.
48+
[PR.PR_STRING, /^\[(=*)\[[\s\S]*?(?:\]\1\]|$)/],
49+
[PR.PR_KEYWORD, /^(?:and|break|do|else|elseif|end|false|for|function|if|in|local|nil|not|or|repeat|return|then|true|until|while)\b/, null],
50+
// A number is a hex integer literal, a decimal real literal, or in
51+
// scientific notation.
52+
[PR.PR_LITERAL,
53+
/^[+-]?(?:0x[\da-f]+|(?:(?:\.\d+|\d+(?:\.\d*)?)(?:[eE][+\-]?\d+)?))/i
54+
],
55+
// An identifier
56+
[PR.PR_PLAIN, /^\w+/],
57+
// A run of punctuation
58+
[PR.PR_PUNCTUATION, /^[^\w\t\n\r \xA0]+/]
59+
]),
60+
['lua']);

src/prettify.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,11 @@ function pr_isIE6() {
104104
}
105105

106106
// Keyword lists for various languages.
107-
var C_KEYWORDS = "auto break case char const continue default do double " +
108-
"else enum extern float for goto if int long register return short " +
109-
"signed sizeof static struct switch typedef union unsigned void " +
110-
"volatile while ";
107+
var FLOW_CONTROL_KEYWORDS =
108+
"break continue do else for if return while ";
109+
var C_KEYWORDS = FLOW_CONTROL_KEYWORDS + "auto case char const default " +
110+
"double enum extern float goto int long register short signed sizeof " +
111+
"static struct switch typedef union unsigned void volatile ";
111112
var COMMON_KEYWORDS = C_KEYWORDS + "catch class delete false import " +
112113
"new operator private protected public this throw true try ";
113114
var CPP_KEYWORDS = COMMON_KEYWORDS + "alignof align_union asm axiom bool " +
@@ -127,17 +128,17 @@ function pr_isIE6() {
127128
"debugger eval export function get null set undefined var with " +
128129
"Infinity NaN ";
129130
var PERL_KEYWORDS = "caller delete die do dump elsif eval exit foreach for " +
130-
"goto if import last local my next no our package redo require sub " +
131-
"undef unless until use wantarray while BEGIN END ";
132-
var PYTHON_KEYWORDS = "and as assert break class continue def del elif " +
133-
"else except exec finally for from global if import in is lambda not " +
134-
"or pass print raise return try while with yield False True None ";
135-
var RUBY_KEYWORDS = "alias and begin break case class def defined do else " +
136-
"elsif end ensure false for if in module next nil not or redo rescue " +
137-
"retry return self super then true undef unless until when while yield " +
138-
"BEGIN END ";
139-
var SH_KEYWORDS = "break case continue do done elif else esac eval fi for " +
140-
"function if in local set then until while ";
131+
"goto if import last local my next no our print package redo require " +
132+
"sub undef unless until use wantarray while BEGIN END ";
133+
var PYTHON_KEYWORDS = FLOW_CONTROL_KEYWORDS + "and as assert class def del " +
134+
"elif except exec finally from global import in is lambda " +
135+
"nonlocal not or pass print raise try with yield " +
136+
"False True None ";
137+
var RUBY_KEYWORDS = FLOW_CONTROL_KEYWORDS + "alias and begin case class def" +
138+
" defined elsif end ensure false in module next nil not or redo rescue " +
139+
"retry self super then true undef unless until when yield BEGIN END ";
140+
var SH_KEYWORDS = FLOW_CONTROL_KEYWORDS + "case done elif esac eval fi " +
141+
"function in local set then until ";
141142
var ALL_KEYWORDS = (
142143
CPP_KEYWORDS + CSHARP_KEYWORDS + JSCRIPT_KEYWORDS + PERL_KEYWORDS +
143144
PYTHON_KEYWORDS + RUBY_KEYWORDS + SH_KEYWORDS);

tests/prettify_test.html

Lines changed: 109 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
onerror="alert('Error: failed to load ' + this.src)"></script>
77
<script src="../src/lang-lisp.js" type="text/javascript"
88
onerror="alert('Error: failed to load ' + this.src)"></script>
9+
<script src="../src/lang-lua.js" type="text/javascript"
10+
onerror="alert('Error: failed to load ' + this.src)"></script>
911
<script type="text/javascript">
1012
// get accurate timing
1113
PR_SHOULD_USE_CONTINUATION = false;
@@ -708,6 +710,58 @@ <h1>Bug 21 - code doesn't copy and paste well in IE</h1>
708710
<p>To test this bug, disable overriding of PR_isIE6 above, and copy and paste
709711
the above into Notepad.</p>
710712

713+
<h1>Bug 24 - LUA Syntax Highlighting</h1>
714+
<pre class="prettyprint lang-lua" id="issue24"
715+
>
716+
-- Examples from the language reference
717+
a = 'alo\n123"'
718+
a = "alo\n123\&quot;"
719+
a = '\97lo\10\04923"'
720+
a = [[alo
721+
123&quot;]]
722+
a = [==[
723+
alo
724+
123&quot;]==]
725+
726+
3 3.0 3.1416 314.16e-2 0.31416E1 0xff 0x56
727+
728+
-- Some comments that demonstrate long brackets
729+
double_quoted = "Not a long bracket [=["
730+
--[=[ quoting out
731+
[[ foo ]]
732+
[==[does not end comment either]==]
733+
]=]
734+
past_end_of_comment
735+
--]=]
736+
737+
-- Example code courtesy Joseph Harmbruster
738+
#
739+
do
740+
local function ssgeneral(t, n, before)
741+
for _, h in ipairs(incs) do
742+
for i = h + 1, n do
743+
local v = t[i]
744+
for j = i - h, 1, -h do
745+
local testval = t[j]
746+
if not before(v, testval) then break end
747+
t[i] = testval; i = j
748+
end
749+
t[i] = v
750+
end
751+
end
752+
return t
753+
end
754+
755+
function shellsort(t, before, n)
756+
n = n or #t
757+
if not before or before == "<" then return ssup(t, n)
758+
elseif before == ">" then return ssdown(t, n)
759+
else return ssgeneral(t, n, before)
760+
end
761+
end
762+
return shellsort
763+
end</pre>
764+
711765
<h1>Bug 42 - Lisp Syntax Highlighting</h1>
712766
<pre class="prettyprint lang-el" id="issue42"
713767
>; -*- mode: lisp -*-
@@ -750,9 +804,9 @@ <h1>Bug 42 - Lisp Syntax Highlighting</h1>
750804

751805
<h1>Bug 45 - Square brackets in strings</h1>
752806
<pre class="prettyprint" id="issue45">
753-
throw new RuntimeException("Element [" + element.getName() +
754-
"] missing attribute.");
755-
variable++;
807+
throw new RuntimeException("Element [" + element.getName() +
808+
"] missing attribute.");
809+
variable++;
756810
</pre>
757811

758812
</body>
@@ -1681,6 +1735,55 @@ <h1>Bug 45 - Square brackets in strings</h1>
16811735
'`PUN&lt;/`END`TAGtitle`END`PUN&gt;`END`PLN<br>' +
16821736
'&nbsp; `END`PUN&lt;/`END`TAGhead`END`PUN&gt;`END`PLN<br>' +
16831737
'`END`PUN&lt;/`END`TAGhtml`END`PUN&gt;`END'),
1738+
issue24: (
1739+
'`COM-- Examples from the language reference`END`PLN<br>' +
1740+
'&nbsp; &nbsp; &nbsp;a `END`PUN=`END`PLN `END`STR\'alo\\n123"\'`END`PLN<br>' +
1741+
'&nbsp; &nbsp; &nbsp;a `END`PUN=`END`PLN `END`STR"alo\\n123\\""`END`PLN<br>' +
1742+
'&nbsp; &nbsp; &nbsp;a `END`PUN=`END`PLN `END`STR\'\\97lo\\10\\04923"\'`END`PLN<br>' +
1743+
'&nbsp; &nbsp; &nbsp;a `END`PUN=`END`PLN `END`STR[[alo<br>' +
1744+
'&nbsp; &nbsp; &nbsp;123"]]`END`PLN<br>' +
1745+
'&nbsp; &nbsp; &nbsp;a `END`PUN=`END`PLN `END`STR[==[<br>' +
1746+
'&nbsp; &nbsp; &nbsp;alo<br>' +
1747+
'&nbsp; &nbsp; &nbsp;123"]==]`END`PLN<br>' +
1748+
'<br>' +
1749+
'`END`LIT3`END`PLN &nbsp; `END`LIT3.0`END`PLN &nbsp; `END`LIT3.1416`END`PLN &nbsp; `END`LIT314.16e-2`END`PLN &nbsp; `END`LIT0.31416E1`END`PLN &nbsp; `END`LIT0xff`END`PLN &nbsp; `END`LIT0x56`END`PLN<br>' +
1750+
'<br>' +
1751+
'`END`COM-- Some comments that demonstrate long brackets`END`PLN<br>' +
1752+
'double_quoted `END`PUN=`END`PLN `END`STR"Not a long bracket [=["`END`PLN<br>' +
1753+
'`END`COM--[=[ quoting out<br>' +
1754+
'&nbsp;[[ foo ]]<br>' +
1755+
'&nbsp;[==[does not end comment either]==]<br>' +
1756+
']=]`END`PLN<br>' +
1757+
'past_end_of_comment<br>' +
1758+
'`END`COM--]=]`END`PLN<br>' +
1759+
'<br>' +
1760+
'`END`COM-- Example code courtesy Joseph Harmbruster`END`PLN<br>' +
1761+
'`END`PUN#`END`PLN<br>' +
1762+
'`END`KWDdo`END`PLN<br>' +
1763+
'&nbsp; `END`KWDlocal`END`PLN `END`KWDfunction`END`PLN ssgeneral`END`PUN(`END`PLNt`END`PUN,`END`PLN n`END`PUN,`END`PLN before`END`PUN)`END`PLN<br>' +
1764+
'&nbsp; &nbsp; `END`KWDfor`END`PLN _`END`PUN,`END`PLN h `END`KWDin`END`PLN ipairs`END`PUN(`END`PLNincs`END`PUN)`END`PLN `END`KWDdo`END`PLN<br>' +
1765+
'&nbsp; &nbsp; &nbsp; `END`KWDfor`END`PLN i `END`PUN=`END`PLN h `END`PUN+`END`PLN `END`LIT1`END`PUN,`END`PLN n `END`KWDdo`END`PLN<br>' +
1766+
'&nbsp; &nbsp; &nbsp; &nbsp; `END`KWDlocal`END`PLN v `END`PUN=`END`PLN t`END`PUN[`END`PLNi`END`PUN]`END`PLN<br>' +
1767+
'&nbsp; &nbsp; &nbsp; &nbsp; `END`KWDfor`END`PLN j `END`PUN=`END`PLN i `END`PUN-`END`PLN h`END`PUN,`END`PLN `END`LIT1`END`PUN,`END`PLN `END`PUN-`END`PLNh `END`KWDdo`END`PLN<br>' +
1768+
'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `END`KWDlocal`END`PLN testval `END`PUN=`END`PLN t`END`PUN[`END`PLNj`END`PUN]`END`PLN<br>' +
1769+
'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `END`KWDif`END`PLN `END`KWDnot`END`PLN before`END`PUN(`END`PLNv`END`PUN,`END`PLN testval`END`PUN)`END`PLN `END`KWDthen`END`PLN `END`KWDbreak`END`PLN `END`KWDend`END`PLN<br>' +
1770+
'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t`END`PUN[`END`PLNi`END`PUN]`END`PLN `END`PUN=`END`PLN testval`END`PUN;`END`PLN i `END`PUN=`END`PLN j<br>' +
1771+
'&nbsp; &nbsp; &nbsp; &nbsp; `END`KWDend`END`PLN<br>' +
1772+
'&nbsp; &nbsp; &nbsp; &nbsp; t`END`PUN[`END`PLNi`END`PUN]`END`PLN `END`PUN=`END`PLN v<br>' +
1773+
'&nbsp; &nbsp; &nbsp; `END`KWDend`END`PLN <br>' +
1774+
'&nbsp; &nbsp; `END`KWDend`END`PLN<br>' +
1775+
'&nbsp; &nbsp; `END`KWDreturn`END`PLN t<br>' +
1776+
'&nbsp; `END`KWDend`END`PLN<br>' +
1777+
'<br>' +
1778+
'&nbsp; `END`KWDfunction`END`PLN shellsort`END`PUN(`END`PLNt`END`PUN,`END`PLN before`END`PUN,`END`PLN n`END`PUN)`END`PLN<br>' +
1779+
'&nbsp; &nbsp; n `END`PUN=`END`PLN n `END`KWDor`END`PLN `END`PUN#`END`PLNt<br>' +
1780+
'&nbsp; &nbsp; `END`KWDif`END`PLN `END`KWDnot`END`PLN before `END`KWDor`END`PLN before `END`PUN==`END`PLN `END`STR"&lt;"`END`PLN `END`KWDthen`END`PLN `END`KWDreturn`END`PLN ssup`END`PUN(`END`PLNt`END`PUN,`END`PLN n`END`PUN)`END`PLN<br>' +
1781+
'&nbsp; &nbsp; `END`KWDelseif`END`PLN before `END`PUN==`END`PLN `END`STR"&gt;"`END`PLN `END`KWDthen`END`PLN `END`KWDreturn`END`PLN ssdown`END`PUN(`END`PLNt`END`PUN,`END`PLN n`END`PUN)`END`PLN<br>' +
1782+
'&nbsp; &nbsp; `END`KWDelse`END`PLN `END`KWDreturn`END`PLN ssgeneral`END`PUN(`END`PLNt`END`PUN,`END`PLN n`END`PUN,`END`PLN before`END`PUN)`END`PLN<br>' +
1783+
'&nbsp; &nbsp; `END`KWDend`END`PLN<br>' +
1784+
'&nbsp; `END`KWDend`END`PLN<br>' +
1785+
'&nbsp; `END`KWDreturn`END`PLN shellsort<br>' +
1786+
'`END`KWDend`END'),
16841787
issue42: (
16851788
'`COM; -*- mode: lisp -*-`END`PLN<br>' +
16861789
'<br>' +
@@ -1719,9 +1822,9 @@ <h1>Bug 45 - Square brackets in strings</h1>
17191822
'&nbsp; &nbsp;`END`CLO)`END`PLN<br>' +
17201823
'`END`CLO)`END'),
17211824
issue45: (
1722-
'`PLN &nbsp;`END`KWDthrow`END`PLN `END`KWDnew`END`PLN `END`TYPRuntimeException`END`PUN(`END`STR"Element ["`END`PLN `END`PUN+`END`PLN element`END`PUN.`END`PLNgetName`END`PUN()`END`PLN `END`PUN+`END`PLN <br>' +
1723-
'&nbsp; &nbsp; `END`STR"] missing attribute."`END`PUN);`END`PLN<br>' +
1724-
'&nbsp; variable`END`PUN++;`END')
1825+
'`KWDthrow`END`PLN `END`KWDnew`END`PLN `END`TYPRuntimeException`END`PUN(`END`STR"Element ["`END`PLN `END`PUN+`END`PLN element`END`PUN.`END`PLNgetName`END`PUN()`END`PLN `END`PUN+`END`PLN <br>' +
1826+
'&nbsp; `END`STR"] missing attribute."`END`PUN);`END`PLN<br>' +
1827+
'variable`END`PUN++;`END')
17251828
};
17261829

17271830

0 commit comments

Comments
 (0)