Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Structured text language support #2310

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions components.json
Expand Up @@ -458,6 +458,10 @@
"title": "Icon",
"owner": "Golmote"
},
"iecst": {
"title": "Structured Text (IEC 61131-3)",
"owner": "serhioromano"
},
"inform7": {
"title": "Inform 7",
"owner": "Golmote"
Expand Down
30 changes: 30 additions & 0 deletions components/prism-iecst.js
@@ -0,0 +1,30 @@
Prism.languages.iecst = {
comment: [
{
pattern: /(^|[^\\])(?:\/\*[\s\S]*?(?:\*\/|$)|\(\*[\s\S]*?(?:\*\)|$)|\{[\s\S]*?(?:\}|$))/,
lookbehind: true,
},
{
pattern: /(^|[^\\:])\/\/.*/,
lookbehind: true,
greedy: true,
},
],
string: {
pattern: /(["'])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
greedy: true,
},
"class-name": /\b(?:END_)?(?:PROGRAM|CONFIGURATION|INTERFACE|FUNCTION_BLOCK|FUNCTION|ACTION|TRANSITION|TYPE|STRUCT|(?:INITIAL_)?STEP|NAMESPACE|LIBRARY|CHANNEL|FOLDER|RESOURCE|VAR_(?:GLOBAL|INPUT|PUTPUT|IN_OUT|ACCESS|TEMP|EXTERNAL|CONFIG)|VAR|METHOD|PROPERTY)\b/i,
keyword: /\b(?:(?:END_)?(?:IF|WHILE|REPEAT|CASE|FOR)|ELSE|FROM|THEN|ELSIF|DO|TO|BY|PRIVATE|PUBLIC|PROTECTED|CONSTANT|RETURN|EXIT|CONTINUE|GOTO|JMP|AT|RETAIN|NON_RETAIN|TASK|WITH|UNTIL|USING|EXTENDS|IMPLEMENTS|GET|SET|__TRY|__CATCH|__FINALLY|__ENDTRY)\b/,
variable: /\b(?:AT|BOOL|BYTE|(?:D|L)?WORD|U?(?:S|D|L)?INT|L?REAL|TIME(?:_OF_DAY)?|TOD|DT|DATE(?:_AND_TIME)?|STRING|ARRAY|ANY|POINTER)\b/,
symbol: /%[IQM][XBWDL][\d.]*|%[IQ][\d.]*/,
number: /\b(?:16#[\da-f]+|2#[01_]+|0x[\da-f]+)\b|\b(?:T|D|DT|TOD)#[\d_shmd:]*|\b[A-Z]*\#[\d.,_]*|(?:\b\d+\.?\d*|\B\.\d+)(?:e[+-]?\d+)?/i,
boolean: /\b(?:TRUE|FALSE|NULL)\b/,
function: /\w+(?=\()/,
operator: /(?:\+|S?R?:?\=>?|:|\^|\-|&&?|\*\*?|\/|<=?|>=?|OR|AND|MOD|NOT|XOR|LE|GE|EQ|NE|GE|LT)/,
punctuation: /[();]/,
type: {
pattern: /#/,
alias: "selector",
},
};
1 change: 1 addition & 0 deletions components/prism-iecst.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions examples/prism-iecst.html
@@ -0,0 +1,40 @@
<h2>Code</h2>
<pre><code>
CONFIGURATION DefaultCfg
VAR_GLOBAL
Start_Stop AT %IX0.0: BOOL; (* This is a comment *)
END_VAR
TASK NewTask (INTERVAL := T#20ms);
PROGRAM Main WITH NewTask : PLC_PRG;
END_CONFIGURATION

PROGRAM demo
VAR_EXTERNAL
Start_Stop: BOOL;
StringVar: STRING[250] := "Test String"
END_VAR
VAR
a : REAL; // Another comment
todTest: TIME_OF_DAY := TOD#12:55;
END_VAR
a := csq(12.5);
IF a > REAL#100 - 16#FAC0 + 2#1001_0110 THEN
Start_Stop := TRUE;
END_IF
END_PROGRAM;

FUNCTION_BLOCK PRIVATE MyName EXTENDS AnotherName

END_FUNCTION_BLOCK

/* Get a square of the circle */
FUNCTION csq : REAL
VAR_INPUT
r: REAL;
END_VAR
VAR CONSTANT
c_pi: REAL := 3.14;
END_VAR
csq := ABS(c_pi * (r * 2));
END_FUNCTION
</code></pre>
25 changes: 15 additions & 10 deletions plugins/show-language/prism-show-language.js
@@ -1,12 +1,10 @@
(function () {

if (typeof self === 'undefined' || !self.Prism || !self.document) {
if (typeof self === "undefined" || !self.Prism || !self.document) {
return;
}

if (!Prism.plugins.toolbar) {
console.warn('Show Languages plugin loaded before Toolbar plugin.');

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert these changes. While the file might need a cleanup, it shouldn't be part of this PR.

The only change to this file should be this:

 		"ichigojam": "IchigoJam",
+		"iecst": "Structured Text (IEC 61131-3)",
 		"inform7": "Inform 7",

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How? when I run npx gulp it rebuild this file and make them all one line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And yet, I am not sure how to revert a single file change. Out of my knowledge. I'll try to google it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The easiest way would probably to copy-paste the content of the current prism-show-language.js in master and run npx gulp.

There's probably also some fancy git-way of doing it, but I'm not a git expert...

console.warn("Show Languages plugin loaded before Toolbar plugin.");
return;
}

Expand Down Expand Up @@ -76,6 +74,7 @@
"hpkp": "HTTP Public-Key-Pins",
"hsts": "HTTP Strict-Transport-Security",
"ichigojam": "IchigoJam",
"iecst": "Structured Text (IEC 61131-3)",
"inform7": "Inform 7",
"javadoc": "JavaDoc",
"javadoclike": "JavaDoc-like",
Expand Down Expand Up @@ -171,9 +170,10 @@
"xquery": "XQuery",
"yaml": "YAML",
"yml": "YAML"
}/*]*/;
}/*]*/

Prism.plugins.toolbar.registerButton("show-language", function (env) {

Prism.plugins.toolbar.registerButton('show-language', function (env) {
var pre = env.element.parentNode;
if (!pre || !/pre/i.test(pre.nodeName)) {
return;
Expand All @@ -189,18 +189,23 @@
if (!id) {
return id;
}
return (id.substring(0, 1).toUpperCase() + id.substring(1)).replace(/s(?=cript)/, 'S');
return (id.substring(0, 1).toUpperCase() + id.substring(1)).replace(
/s(?=cript)/,
"S"
);
}

var language = pre.getAttribute('data-language') || Languages[env.language] || guessTitle(env.language);
var language =
pre.getAttribute("data-language") ||
Languages[env.language] ||
guessTitle(env.language);

if (!language) {
return;
}
var element = document.createElement('span');
var element = document.createElement("span");
element.textContent = language;

return element;
});

})();
2 changes: 1 addition & 1 deletion plugins/show-language/prism-show-language.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions tests/languages/iecst/block.test
@@ -0,0 +1,23 @@
csq := ABS(c_pi * (r * 2));

----------------------------------------------------

[
"csq ",
["operator", ":="],
["function", "ABS"],
["punctuation", "("],
"c_pi ",
["operator", "*"],
["punctuation", "("],
"r ",
["operator", "*"],
["number", "2"],
["punctuation", ")"],
["punctuation", ")"],
["punctuation", ";"]
]

----------------------------------------------------

Checks expression.
13 changes: 13 additions & 0 deletions tests/languages/iecst/number.test
@@ -0,0 +1,13 @@
a := 100

----------------------------------------------------

[
"a ",
["operator", ":="],
["number", "100"]
]

----------------------------------------------------

Checks number.
22 changes: 22 additions & 0 deletions tests/languages/iecst/symbol.test
@@ -0,0 +1,22 @@
VAR
varname AT %QX1.0.0: BOOL := TRUE;
END_VAR

----------------------------------------------------

[
["class-name", "VAR"],
"\n varname ",
["keyword", "AT"],
["symbol", "%QX1.0.0"],
["operator", ":"],
["variable", "BOOL"],
["operator", ":="],
["boolean", "TRUE"],
["punctuation", ";"],
["class-name", "END_VAR"]
]

----------------------------------------------------

Checks expression.