lang-lua.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. * @fileoverview
  16. * Registers a language handler for Lua.
  17. *
  18. *
  19. * To use, include prettify.js and this file in your HTML page.
  20. * Then put your code in an HTML tag like
  21. * <pre class="prettyprint lang-lua">(my Lua code)</pre>
  22. *
  23. *
  24. * I used http://www.lua.org/manual/5.1/manual.html#2.1
  25. * Because of the long-bracket concept used in strings and comments, Lua does
  26. * not have a regular lexical grammar, but luckily it fits within the space
  27. * of irregular grammars supported by javascript regular expressions.
  28. *
  29. * @author mikesamuel@gmail.com
  30. */
  31. PR['registerLangHandler'](
  32. PR['createSimpleLexer'](
  33. [
  34. // Whitespace
  35. [PR['PR_PLAIN'], /^[\t\n\r \xA0]+/, null, '\t\n\r \xA0'],
  36. // A double or single quoted, possibly multi-line, string.
  37. [PR['PR_STRING'], /^(?:\"(?:[^\"\\]|\\[\s\S])*(?:\"|$)|\'(?:[^\'\\]|\\[\s\S])*(?:\'|$))/, null, '"\'']
  38. ],
  39. [
  40. // A comment is either a line comment that starts with two dashes, or
  41. // two dashes preceding a long bracketed block.
  42. [PR['PR_COMMENT'], /^--(?:\[(=*)\[[\s\S]*?(?:\]\1\]|$)|[^\r\n]*)/],
  43. // A long bracketed block not preceded by -- is a string.
  44. [PR['PR_STRING'], /^\[(=*)\[[\s\S]*?(?:\]\1\]|$)/],
  45. [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],
  46. // A number is a hex integer literal, a decimal real literal, or in
  47. // scientific notation.
  48. [PR['PR_LITERAL'],
  49. /^[+-]?(?:0x[\da-f]+|(?:(?:\.\d+|\d+(?:\.\d*)?)(?:e[+\-]?\d+)?))/i],
  50. // An identifier
  51. [PR['PR_PLAIN'], /^[a-z_]\w*/i],
  52. // A run of punctuation
  53. [PR['PR_PUNCTUATION'], /^[^\w\t\n\r \xA0][^\w\t\n\r \xA0\"\'\-\+=]*/]
  54. ]),
  55. ['lua']);