erlang.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. module.exports = function(hljs) {
  2. var BASIC_ATOM_RE = '[a-z\'][a-zA-Z0-9_\']*';
  3. var FUNCTION_NAME_RE = '(' + BASIC_ATOM_RE + ':' + BASIC_ATOM_RE + '|' + BASIC_ATOM_RE + ')';
  4. var ERLANG_RESERVED = {
  5. keyword:
  6. 'after and andalso|10 band begin bnot bor bsl bzr bxor case catch cond div end fun if ' +
  7. 'let not of orelse|10 query receive rem try when xor',
  8. literal:
  9. 'false true'
  10. };
  11. var COMMENT = hljs.COMMENT('%', '$');
  12. var NUMBER = {
  13. className: 'number',
  14. begin: '\\b(\\d+#[a-fA-F0-9]+|\\d+(\\.\\d+)?([eE][-+]?\\d+)?)',
  15. relevance: 0
  16. };
  17. var NAMED_FUN = {
  18. begin: 'fun\\s+' + BASIC_ATOM_RE + '/\\d+'
  19. };
  20. var FUNCTION_CALL = {
  21. begin: FUNCTION_NAME_RE + '\\(', end: '\\)',
  22. returnBegin: true,
  23. relevance: 0,
  24. contains: [
  25. {
  26. begin: FUNCTION_NAME_RE, relevance: 0
  27. },
  28. {
  29. begin: '\\(', end: '\\)', endsWithParent: true,
  30. returnEnd: true,
  31. relevance: 0
  32. // "contains" defined later
  33. }
  34. ]
  35. };
  36. var TUPLE = {
  37. begin: '{', end: '}',
  38. relevance: 0
  39. // "contains" defined later
  40. };
  41. var VAR1 = {
  42. begin: '\\b_([A-Z][A-Za-z0-9_]*)?',
  43. relevance: 0
  44. };
  45. var VAR2 = {
  46. begin: '[A-Z][a-zA-Z0-9_]*',
  47. relevance: 0
  48. };
  49. var RECORD_ACCESS = {
  50. begin: '#' + hljs.UNDERSCORE_IDENT_RE,
  51. relevance: 0,
  52. returnBegin: true,
  53. contains: [
  54. {
  55. begin: '#' + hljs.UNDERSCORE_IDENT_RE,
  56. relevance: 0
  57. },
  58. {
  59. begin: '{', end: '}',
  60. relevance: 0
  61. // "contains" defined later
  62. }
  63. ]
  64. };
  65. var BLOCK_STATEMENTS = {
  66. beginKeywords: 'fun receive if try case', end: 'end',
  67. keywords: ERLANG_RESERVED
  68. };
  69. BLOCK_STATEMENTS.contains = [
  70. COMMENT,
  71. NAMED_FUN,
  72. hljs.inherit(hljs.APOS_STRING_MODE, {className: ''}),
  73. BLOCK_STATEMENTS,
  74. FUNCTION_CALL,
  75. hljs.QUOTE_STRING_MODE,
  76. NUMBER,
  77. TUPLE,
  78. VAR1, VAR2,
  79. RECORD_ACCESS
  80. ];
  81. var BASIC_MODES = [
  82. COMMENT,
  83. NAMED_FUN,
  84. BLOCK_STATEMENTS,
  85. FUNCTION_CALL,
  86. hljs.QUOTE_STRING_MODE,
  87. NUMBER,
  88. TUPLE,
  89. VAR1, VAR2,
  90. RECORD_ACCESS
  91. ];
  92. FUNCTION_CALL.contains[1].contains = BASIC_MODES;
  93. TUPLE.contains = BASIC_MODES;
  94. RECORD_ACCESS.contains[1].contains = BASIC_MODES;
  95. var PARAMS = {
  96. className: 'params',
  97. begin: '\\(', end: '\\)',
  98. contains: BASIC_MODES
  99. };
  100. return {
  101. aliases: ['erl'],
  102. keywords: ERLANG_RESERVED,
  103. illegal: '(</|\\*=|\\+=|-=|/\\*|\\*/|\\(\\*|\\*\\))',
  104. contains: [
  105. {
  106. className: 'function',
  107. begin: '^' + BASIC_ATOM_RE + '\\s*\\(', end: '->',
  108. returnBegin: true,
  109. illegal: '\\(|#|//|/\\*|\\\\|:|;',
  110. contains: [
  111. PARAMS,
  112. hljs.inherit(hljs.TITLE_MODE, {begin: BASIC_ATOM_RE})
  113. ],
  114. starts: {
  115. end: ';|\\.',
  116. keywords: ERLANG_RESERVED,
  117. contains: BASIC_MODES
  118. }
  119. },
  120. COMMENT,
  121. {
  122. begin: '^-', end: '\\.',
  123. relevance: 0,
  124. excludeEnd: true,
  125. returnBegin: true,
  126. lexemes: '-' + hljs.IDENT_RE,
  127. keywords:
  128. '-module -record -undef -export -ifdef -ifndef -author -copyright -doc -vsn ' +
  129. '-import -include -include_lib -compile -define -else -endif -file -behaviour ' +
  130. '-behavior -spec',
  131. contains: [PARAMS]
  132. },
  133. NUMBER,
  134. hljs.QUOTE_STRING_MODE,
  135. RECORD_ACCESS,
  136. VAR1, VAR2,
  137. TUPLE,
  138. {begin: /\.$/} // relevance booster
  139. ]
  140. };
  141. };