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 Lua #804

Merged
merged 1 commit into from
Oct 17, 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 @@ -266,6 +266,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.