lang-ml.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 OCaml, SML, F# and similar languages.
  17. *
  18. * Based on the lexical grammar at
  19. * http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/spec.html#_Toc270597388
  20. *
  21. * @author mikesamuel@gmail.com
  22. */
  23. PR['registerLangHandler'](
  24. PR['createSimpleLexer'](
  25. [
  26. // Whitespace is made up of spaces, tabs and newline characters.
  27. [PR['PR_PLAIN'], /^[\t\n\r \xA0]+/, null, '\t\n\r \xA0'],
  28. // #if ident/#else/#endif directives delimit conditional compilation
  29. // sections
  30. [PR['PR_COMMENT'],
  31. /^#(?:if[\t\n\r \xA0]+(?:[a-z_$][\w\']*|``[^\r\n\t`]*(?:``|$))|else|endif|light)/i,
  32. null, '#'],
  33. // A double or single quoted, possibly multi-line, string.
  34. // F# allows escaped newlines in strings.
  35. [PR['PR_STRING'], /^(?:\"(?:[^\"\\]|\\[\s\S])*(?:\"|$)|\'(?:[^\'\\]|\\[\s\S])(?:\'|$))/, null, '"\'']
  36. ],
  37. [
  38. // Block comments are delimited by (* and *) and may be
  39. // nested. Single-line comments begin with // and extend to
  40. // the end of a line.
  41. // TODO: (*...*) comments can be nested. This does not handle that.
  42. [PR['PR_COMMENT'], /^(?:\/\/[^\r\n]*|\(\*[\s\S]*?\*\))/],
  43. [PR['PR_KEYWORD'], /^(?:abstract|and|as|assert|begin|class|default|delegate|do|done|downcast|downto|elif|else|end|exception|extern|false|finally|for|fun|function|if|in|inherit|inline|interface|internal|lazy|let|match|member|module|mutable|namespace|new|null|of|open|or|override|private|public|rec|return|static|struct|then|to|true|try|type|upcast|use|val|void|when|while|with|yield|asr|land|lor|lsl|lsr|lxor|mod|sig|atomic|break|checked|component|const|constraint|constructor|continue|eager|event|external|fixed|functor|global|include|method|mixin|object|parallel|process|protected|pure|sealed|trait|virtual|volatile)\b/],
  44. // A number is a hex integer literal, a decimal real literal, or in
  45. // scientific notation.
  46. [PR['PR_LITERAL'],
  47. /^[+\-]?(?:0x[\da-f]+|(?:(?:\.\d+|\d+(?:\.\d*)?)(?:e[+\-]?\d+)?))/i],
  48. [PR['PR_PLAIN'], /^(?:[a-z_][\w']*[!?#]?|``[^\r\n\t`]*(?:``|$))/i],
  49. // A printable non-space non-special character
  50. [PR['PR_PUNCTUATION'], /^[^\t\n\r \xA0\"\'\w]+/]
  51. ]),
  52. ['fs', 'ml']);