cal.js 2.6 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
/*
Language: C/AL
Author: Kenneth Fuglsang Christensen <kfuglsang@gmail.com>
Description: Provides highlighting of Microsoft Dynamics NAV C/AL code files
Website: https://docs.microsoft.com/en-us/dynamics-nav/programming-in-c-al
*/

/** @type LanguageFn */
function cal(hljs) {
  const regex = hljs.regex;
  const KEYWORDS = [
    "div",
    "mod",
    "in",
    "and",
    "or",
    "not",
    "xor",
    "asserterror",
    "begin",
    "case",
    "do",
    "downto",
    "else",
    "end",
    "exit",
    "for",
    "local",
    "if",
    "of",
    "repeat",
    "then",
    "to",
    "until",
    "while",
    "with",
    "var"
  ];
  const LITERALS = 'false true';
  const COMMENT_MODES = [
    hljs.C_LINE_COMMENT_MODE,
    hljs.COMMENT(
      /\{/,
      /\}/,
      { relevance: 0 }
    ),
    hljs.COMMENT(
      /\(\*/,
      /\*\)/,
      { relevance: 10 }
    )
  ];
  const STRING = {
    className: 'string',
    begin: /'/,
    end: /'/,
    contains: [ { begin: /''/ } ]
  };
  const CHAR_STRING = {
    className: 'string',
    begin: /(#\d+)+/
  };
  const DATE = {
    className: 'number',
    begin: '\\b\\d+(\\.\\d+)?(DT|D|T)',
    relevance: 0
  };
  const DBL_QUOTED_VARIABLE = {
    className: 'string', // not a string technically but makes sense to be highlighted in the same style
    begin: '"',
    end: '"'
  };

  const PROCEDURE = {
    match: [
      /procedure/,
      /\s+/,
      /[a-zA-Z_][\w@]*/,
      /\s*/
    ],
    scope: {
      1: "keyword",
      3: "title.function"
    },
    contains: [
      {
        className: 'params',
        begin: /\(/,
        end: /\)/,
        keywords: KEYWORDS,
        contains: [
          STRING,
          CHAR_STRING,
          hljs.NUMBER_MODE
        ]
      },
      ...COMMENT_MODES
    ]
  };

  const OBJECT_TYPES = [
    "Table",
    "Form",
    "Report",
    "Dataport",
    "Codeunit",
    "XMLport",
    "MenuSuite",
    "Page",
    "Query"
  ];
  const OBJECT = {
    match: [
      /OBJECT/,
      /\s+/,
      regex.either(...OBJECT_TYPES),
      /\s+/,
      /\d+/,
      /\s+(?=[^\s])/,
      /.*/,
      /$/
    ],
    relevance: 3,
    scope: {
      1: "keyword",
      3: "type",
      5: "number",
      7: "title"
    }
  };

  const PROPERTY = {
    match: /[\w]+(?=\=)/,
    scope: "attribute",
    relevance: 0
  };

  return {
    name: 'C/AL',
    case_insensitive: true,
    keywords: {
      keyword: KEYWORDS,
      literal: LITERALS
    },
    illegal: /\/\*/,
    contains: [
      PROPERTY,
      STRING,
      CHAR_STRING,
      DATE,
      DBL_QUOTED_VARIABLE,
      hljs.NUMBER_MODE,
      OBJECT,
      PROCEDURE
    ]
  };
}

module.exports = cal;