Skip to content

Commit

Permalink
Merge pull request #804 from Golmote/prism-lua
Browse files Browse the repository at this point in the history
Add support for Lua
  • Loading branch information
Golmote committed Oct 17, 2015
2 parents b43c5f3 + 1a4ff7a commit a36bc4a
Show file tree
Hide file tree
Showing 10 changed files with 301 additions and 0 deletions.
4 changes: 4 additions & 0 deletions components.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ var components = {
"title": "LOLCODE",
"owner": "Golmote"
},
"lua": {
"title": "Lua",
"owner": "Golmote"
},
"makefile": {
"title": "Makefile",
"owner": "Golmote"
Expand Down
17 changes: 17 additions & 0 deletions components/prism-lua.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Prism.languages.lua = {
'comment': /^#!.+|--(?:\[(=*)\[[\s\S]*?\]\1\]|.*)/m,
// \z may be used to skip the following space
'string': /(["'])(?:(?!\1)[^\\\r\n]|\\z(?:\r\n|\s)|\\(?:\r\n|[\s\S]))*\1|\[(=*)\[[\s\S]*?\]\2\]/,
'number': /\b0x[a-f\d]+\.?[a-f\d]*(?:p[+-]?\d+)?\b|\b\d+(?:\.\B|\.?\d*(?:e[+-]?\d+)?\b)|\B\.\d+(?:e[+-]?\d+)?\b/i,
'keyword': /\b(?:and|break|do|else|elseif|end|false|for|function|goto|if|in|local|nil|not|or|repeat|return|then|true|until|while)\b/,
'function': /(?!\d)\w+(?=\s*(?:[({]))/,
'operator': [
/[-+*%^&|#]|\/\/?|<[<=]?|>[>=]?|[=~]=?/,
{
// Match ".." but don't break "..."
pattern: /(^|[^.])\.\.(?!\.)/,
lookbehind: true
}
],
'punctuation': /[\[\](){},;]|\.+|:+/
};
1 change: 1 addition & 0 deletions components/prism-lua.min.js

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

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

<h2>Comments</h2>
<pre><code>#!/usr/local/bin/lua
--
-- Single line comment
--[[ Multi line
comment ]]
--[====[ Multi line
comment ]====]</code></pre>

<h2>Strings</h2>
<pre><code>""
"Foo\"bar"
"Foo\
bar \z
baz"
''
'Foo\'bar'
'Foo\
bar \z
baz'
[[Multi "line"
string]]
[==[Multi [["line"]]
string]==]</code></pre>

<h2>Numbers</h2>
<pre><code>3
345
0xff
0xBEBADA
3, 3., 3.1, .3,
3e12, 3.e-41, 3.1E+1, .3e1
0x0.1E
0xA23p-4
0X1.921FB54442D18P+1</code></pre>

<h2>Full example</h2>
<pre><code>function To_Functable(t, fn)
return setmetatable(t,
{
__index = function(t, k) return fn(k) end,
__call = function(t, k) return t[k] end
})
end

-- Functable bottles of beer implementation

spell_out = {
"One", "Two", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
[0] = "No more",
[-1] = "Lots more"
}

spell_out = To_Functable(spell_out, function(i) return i end)

bottles = To_Functable({"Just one bottle of beer"},
function(i)
return spell_out(i) .. " bottles of beer"
end)

function line1(i)
return bottles(i) .. " on the wall, " .. bottles(i) .. "\n"
end

line2 = To_Functable({[0] = "Go to the store, Buy some more,\n"},
function(i)
return "Take one down and pass it around,\n"
end)

function line3(i)
return bottles(i) .. " on the wall.\n"
end

function song(n)
for i = n, 0, -1 do
io.write(line1(i), line2(i), line3(i - 1), "\n")
end
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>Comment-like substrings</h3>
<pre><code>"foo -- bar";</code></pre>

<h3>Functions with a single string parameter not using parentheses are not highlighted</h3>
<pre><code>foobar"param";</code></pre>
22 changes: 22 additions & 0 deletions tests/languages/lua/comment_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/local/bin/lua
--
-- Foobar
--[[Foo
bar]]
--[====[Foo
bar]=====] ]===]
baz]====]

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

[
["comment", "#!/usr/local/bin/lua"],
["comment", "--"],
["comment", "-- Foobar"],
["comment", "--[[Foo\r\nbar]]"],
["comment", "--[====[Foo\r\nbar]=====] ]===]\r\nbaz]====]"]
]

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

Checks for comments.
17 changes: 17 additions & 0 deletions tests/languages/lua/function_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
foo ()
Foo_bar_42()
foo {}
Foo_bar_42{}

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

[
["function", "foo"], ["punctuation", "("], ["punctuation", ")"],
["function", "Foo_bar_42"], ["punctuation", "("], ["punctuation", ")"],
["function", "foo"], ["punctuation", "{"], ["punctuation", "}"],
["function", "Foo_bar_42"], ["punctuation", "{"], ["punctuation", "}"]
]

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

Checks for functions.
53 changes: 53 additions & 0 deletions tests/languages/lua/keyword_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
and
break
do
else
elseif
end
false
for
function
goto
if
in
local
nil
not
or
repeat
return
then
true
until
while

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

[
["keyword", "and"],
["keyword", "break"],
["keyword", "do"],
["keyword", "else"],
["keyword", "elseif"],
["keyword", "end"],
["keyword", "false"],
["keyword", "for"],
["keyword", "function"],
["keyword", "goto"],
["keyword", "if"],
["keyword", "in"],
["keyword", "local"],
["keyword", "nil"],
["keyword", "not"],
["keyword", "or"],
["keyword", "repeat"],
["keyword", "return"],
["keyword", "then"],
["keyword", "true"],
["keyword", "until"],
["keyword", "while"]
]

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

Checks for keywords.
35 changes: 35 additions & 0 deletions tests/languages/lua/number_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
0
42
3.14159
3.
.42
4e14
3.14e+8
.7E-1
4.e12
0xBadFace
0x0.1E
0xA23p-4
0X1.921FB54442D18P+1

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

[
["number", "0"],
["number", "42"],
["number", "3.14159"],
["number", "3."],
["number", ".42"],
["number", "4e14"],
["number", "3.14e+8"],
["number", ".7E-1"],
["number", "4.e12"],
["number", "0xBadFace"],
["number", "0x0.1E"],
["number", "0xA23p-4"],
["number", "0X1.921FB54442D18P+1"]
]

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

Checks for numbers.
25 changes: 25 additions & 0 deletions tests/languages/lua/operator_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
+ - * %
^ & | #
/ //
< << <=
> >> >=
= ==
~ ~=
..

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

[
["operator", "+"], ["operator", "-"], ["operator", "*"], ["operator", "%"],
["operator", "^"], ["operator", "&"], ["operator", "|"], ["operator", "#"],
["operator", "/"], ["operator", "//"],
["operator", "<"], ["operator", "<<"], ["operator", "<="],
["operator", ">"], ["operator", ">>"], ["operator", ">="],
["operator", "="], ["operator", "=="],
["operator", "~"], ["operator", "~="],
["operator", ".."]
]

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

Checks for operators.
32 changes: 32 additions & 0 deletions tests/languages/lua/string_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
""
"Fo\"obar"
"Foo\
bar\z
baz"
''
'Fo\'obar'
'Foo\
bar\z
baz'
[[Foo
bar]]
[====[Foo
bar]=====] ]===]
baz]====]

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

[
["string", "\"\""],
["string", "\"Fo\\\"obar\""],
["string", "\"Foo\\\r\nbar\\z\r\nbaz\""],
["string", "''"],
["string", "'Fo\\'obar'"],
["string", "'Foo\\\r\nbar\\z\r\nbaz'"],
["string", "[[Foo\r\nbar]]"],
["string", "[====[Foo\r\nbar]=====] ]===]\r\nbaz]====]"]
]

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

Checks for strings.

0 comments on commit a36bc4a

Please sign in to comment.