From 1a4ff7aca1af3ed4577f517d8deda8c225355f1a Mon Sep 17 00:00:00 2001 From: Golmote Date: Sun, 11 Oct 2015 10:38:10 +0200 Subject: [PATCH] Add support for Lua --- components.js | 4 + components/prism-lua.js | 17 ++++ components/prism-lua.min.js | 1 + examples/prism-lua.html | 95 +++++++++++++++++++++++ tests/languages/lua/comment_feature.test | 22 ++++++ tests/languages/lua/function_feature.test | 17 ++++ tests/languages/lua/keyword_feature.test | 53 +++++++++++++ tests/languages/lua/number_feature.test | 35 +++++++++ tests/languages/lua/operator_feature.test | 25 ++++++ tests/languages/lua/string_feature.test | 32 ++++++++ 10 files changed, 301 insertions(+) create mode 100644 components/prism-lua.js create mode 100644 components/prism-lua.min.js create mode 100644 examples/prism-lua.html create mode 100644 tests/languages/lua/comment_feature.test create mode 100644 tests/languages/lua/function_feature.test create mode 100644 tests/languages/lua/keyword_feature.test create mode 100644 tests/languages/lua/number_feature.test create mode 100644 tests/languages/lua/operator_feature.test create mode 100644 tests/languages/lua/string_feature.test diff --git a/components.js b/components.js index c33e015fe5..001b411e17 100644 --- a/components.js +++ b/components.js @@ -266,6 +266,10 @@ var components = { "title": "LOLCODE", "owner": "Golmote" }, + "lua": { + "title": "Lua", + "owner": "Golmote" + }, "makefile": { "title": "Makefile", "owner": "Golmote" diff --git a/components/prism-lua.js b/components/prism-lua.js new file mode 100644 index 0000000000..302842f5ac --- /dev/null +++ b/components/prism-lua.js @@ -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': /[\[\](){},;]|\.+|:+/ +}; \ No newline at end of file diff --git a/components/prism-lua.min.js b/components/prism-lua.min.js new file mode 100644 index 0000000000..1b80f544ad --- /dev/null +++ b/components/prism-lua.min.js @@ -0,0 +1 @@ +Prism.languages.lua={comment:/^#!.+|--(?:\[(=*)\[[\s\S]*?\]\1\]|.*)/m,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:[/[-+*%^&|#]|\/\/?|<[<=]?|>[>=]?|[=~]=?/,{pattern:/(^|[^.])\.\.(?!\.)/,lookbehind:!0}],punctuation:/[\[\](){},;]|\.+|:+/}; \ No newline at end of file diff --git a/examples/prism-lua.html b/examples/prism-lua.html new file mode 100644 index 0000000000..4b418af20b --- /dev/null +++ b/examples/prism-lua.html @@ -0,0 +1,95 @@ +

Lua

+

To use this language, use the class "language-lua".

+ +

Comments

+
#!/usr/local/bin/lua
+--
+-- Single line comment
+--[[ Multi line
+comment ]]
+--[====[ Multi line
+comment ]====]
+ +

Strings

+
""
+"Foo\"bar"
+"Foo\
+bar \z
+baz"
+''
+'Foo\'bar'
+'Foo\
+bar \z
+baz'
+[[Multi "line"
+string]]
+[==[Multi [["line"]]
+string]==]
+ +

Numbers

+
3
+345
+0xff
+0xBEBADA
+3, 3., 3.1, .3,
+3e12, 3.e-41, 3.1E+1, .3e1
+0x0.1E
+0xA23p-4
+0X1.921FB54442D18P+1
+ +

Full example

+
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
+ +

Known failures

+

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. +

+ +

Comment-like substrings

+
"foo -- bar";
+ +

Functions with a single string parameter not using parentheses are not highlighted

+
foobar"param";
diff --git a/tests/languages/lua/comment_feature.test b/tests/languages/lua/comment_feature.test new file mode 100644 index 0000000000..3c639f7434 --- /dev/null +++ b/tests/languages/lua/comment_feature.test @@ -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. \ No newline at end of file diff --git a/tests/languages/lua/function_feature.test b/tests/languages/lua/function_feature.test new file mode 100644 index 0000000000..90e23830e8 --- /dev/null +++ b/tests/languages/lua/function_feature.test @@ -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. \ No newline at end of file diff --git a/tests/languages/lua/keyword_feature.test b/tests/languages/lua/keyword_feature.test new file mode 100644 index 0000000000..0020acae17 --- /dev/null +++ b/tests/languages/lua/keyword_feature.test @@ -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. \ No newline at end of file diff --git a/tests/languages/lua/number_feature.test b/tests/languages/lua/number_feature.test new file mode 100644 index 0000000000..913b5b8e0f --- /dev/null +++ b/tests/languages/lua/number_feature.test @@ -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. \ No newline at end of file diff --git a/tests/languages/lua/operator_feature.test b/tests/languages/lua/operator_feature.test new file mode 100644 index 0000000000..dec283644c --- /dev/null +++ b/tests/languages/lua/operator_feature.test @@ -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. \ No newline at end of file diff --git a/tests/languages/lua/string_feature.test b/tests/languages/lua/string_feature.test new file mode 100644 index 0000000000..ccd34e8ff2 --- /dev/null +++ b/tests/languages/lua/string_feature.test @@ -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. \ No newline at end of file