Skip to content

Commit

Permalink
enh(cal) simplify using multi-matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgoebel committed Dec 16, 2021
1 parent 06c57ce commit 4284811
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 28 deletions.
97 changes: 79 additions & 18 deletions src/languages/cal.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,35 @@ Website: https://docs.microsoft.com/en-us/dynamics-nav/programming-in-c-al

/** @type LanguageFn */
export default function(hljs) {
const KEYWORDS =
'div mod in and or not xor asserterror begin case do downto else end exit for if of repeat then to ' +
'until while with var';
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,
Expand Down Expand Up @@ -52,33 +78,67 @@ export default function(hljs) {
};

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

const OBJECT_TYPES = [
"Table",
"Form",
"Report",
"Dataport",
"Codeunit",
"XMLport",
"MenuSuite",
"Page",
"Query"
];
const OBJECT = {
className: 'class',
begin: 'OBJECT (Table|Form|Report|Dataport|Codeunit|XMLport|MenuSuite|Page|Query) (\\d+) ([^\\r\\n]+)',
returnBegin: true,
contains: [
hljs.TITLE_MODE,
PROCEDURE
]
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 {
Expand All @@ -90,6 +150,7 @@ export default function(hljs) {
},
illegal: /\/\*/,
contains: [
PROPERTY,
STRING,
CHAR_STRING,
DATE,
Expand Down
9 changes: 8 additions & 1 deletion test/detect/cal/default.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
OBJECT Codeunit 11 Gen. Jnl.-Check Line
OBJECT Table 12 something
{
}
OBJECT XMLport 3000 something.other
{
}

OBJECT Codeunit 11 Gen. Jnl.-Chck Line
{
OBJECT-PROPERTIES
{
Expand Down
18 changes: 9 additions & 9 deletions test/markup/cal/default.expect.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<span class="hljs-class"><span class="hljs-title">OBJECT</span></span> Codeunit <span class="hljs-number">11</span> Gen. Jnl.-Check Line
<span class="hljs-keyword">OBJECT</span> <span class="hljs-type">Codeunit</span> <span class="hljs-number">11</span> <span class="hljs-title">Gen. Jnl.-Check Line</span>
{
OBJECT-PROPERTIES
{
Date=<span class="hljs-number">09</span>-<span class="hljs-number">09</span>-<span class="hljs-number">14</span>;
Time=<span class="hljs-number">12</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span>;
Version List=NAVW18.<span class="hljs-number">00</span>;
<span class="hljs-attribute">Date</span>=<span class="hljs-number">09</span>-<span class="hljs-number">09</span>-<span class="hljs-number">14</span>;
<span class="hljs-attribute">Time</span>=<span class="hljs-number">12</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span>;
Version <span class="hljs-attribute">List</span>=NAVW18.<span class="hljs-number">00</span>;
}
PROPERTIES
{
TableNo=<span class="hljs-number">81</span>;
Permissions=TableData <span class="hljs-number">252</span>=rimd;
OnRun=<span class="hljs-keyword">BEGIN</span>
<span class="hljs-attribute">TableNo</span>=<span class="hljs-number">81</span>;
<span class="hljs-attribute">Permissions</span>=TableData <span class="hljs-attribute">252</span>=rimd;
<span class="hljs-attribute">OnRun</span>=<span class="hljs-keyword">BEGIN</span>
GLSetup.GET;
RunCheck(Rec);
<span class="hljs-keyword">END</span>;
Expand All @@ -22,12 +22,12 @@
Text000@<span class="hljs-number">1000</span> : TextConst <span class="hljs-string">&#x27;ENU=can only be a closing date for G/L entries&#x27;</span>;
Text001@<span class="hljs-number">1001</span> : TextConst <span class="hljs-string">&#x27;ENU=is not within your range of allowed posting dates&#x27;</span>;

<span class="hljs-function"><span class="hljs-keyword">PROCEDURE</span> <span class="hljs-title">ErrorIfPositiveAmt</span>@2<span class="hljs-params">(GenJnlLine@1000 : Record 81)</span>;</span>
<span class="hljs-keyword">PROCEDURE</span> <span class="hljs-title function_">ErrorIfPositiveAmt@2</span><span class="hljs-params">(GenJnlLine@<span class="hljs-number">1000</span> : Record <span class="hljs-number">81</span>)</span>;
<span class="hljs-keyword">BEGIN</span>
<span class="hljs-keyword">IF</span> GenJnlLine.Amount &gt; <span class="hljs-number">0</span> <span class="hljs-keyword">THEN</span>
GenJnlLine.FIELDERROR(Amount,Text008);
<span class="hljs-keyword">END</span>;

LOCAL <span class="hljs-function"><span class="hljs-keyword">PROCEDURE</span> <span class="hljs-title">CheckGenJnlLineDocType</span>@7<span class="hljs-params">(GenJnlLine@1001 : Record 81)</span>;</span>
<span class="hljs-keyword">LOCAL</span> <span class="hljs-keyword">PROCEDURE</span> <span class="hljs-title function_">CheckGenJnlLineDocType@7</span><span class="hljs-params">(GenJnlLine@<span class="hljs-number">1001</span> : Record <span class="hljs-number">81</span>)</span>;
}
}

0 comments on commit 4284811

Please sign in to comment.