Skip to content

Commit

Permalink
Add PLC Structured Text IEC 61131-3 standard syntax highlighting supp…
Browse files Browse the repository at this point in the history
…ort. (#430)

* WIP: added some basic patterns and the manifest entries.

* WIP: updating todo

* Updated some keywords.

* Fixed elseif keyword.

* Updated some patterns.

* Updated some patterns, again.

* WIP: fixing numbers, types and other things.

* WIP: a few different things.

* Last commit before final review.

* WIP: fixing regex patterns vulnerable to REDOs.

* Fixed name.

* Fix...

* Removed comment,

* WIP: fixing patterns.

* Fixed function pattern.

* Fixed function pattern.

* Update plugins/language_st.lua

Co-authored-by: Guldoman <[email protected]>

* Update plugins/language_st.lua

Co-authored-by: Guldoman <[email protected]>

* Fixed some patterns and removed some unexplainable patterns.

* Updated manifest

* Replaced some regex patterns with equivalent lua patterns.

* Rewritten function pattern with lua.

* Rewritten type pattern with regex.

* Replaced type regex pattern with lua pattern.

* Fixed some typos.

* Update manifest.json
  • Loading branch information
PerilousBooklet authored Jan 1, 2025
1 parent 7b15ce8 commit 1245246
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 1 deletion.
13 changes: 12 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
"language_scala": {},
"language_sh": {},
"language_ssh_config": {},
"language_st": {},
"language_swift": {},
"language_tal": {},
"language_tcl": {},
Expand All @@ -169,7 +170,7 @@
"id": "meta_languages",
"mod_version": "3",
"type": "meta",
"version": "0.1.19"
"version": "0.1.20"
},
{
"description": "Align multiple carets and selections *([clip](https://user-images.githubusercontent.com/2798487/165631951-532f8d24-d596-4dd0-9d21-ff53c71ed32f.mp4))*",
Expand Down Expand Up @@ -1591,6 +1592,16 @@
],
"version": "0.1"
},
{
"description": "Syntax for PLC Structured Text IEC 61131-3 standard.",
"id": "language_st",
"mod_version": "3",
"path": "plugins/language_st.lua",
"tags": [
"language"
],
"version": "0.2"
},
{
"description": "Syntax for the [Uxntal](https://wiki.xxiivv.com/site/uxntal) assembly language",
"id": "language_tal",
Expand Down
125 changes: 125 additions & 0 deletions plugins/language_st.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
-- mod-version:3
local syntax = require "core.syntax"

-- Language Syntax References
-- https://pdhonline.com/courses/e334/e334content.pdf

syntax.add {
name = "PLC Structured Text IEC 61131-3",
files = { "%.stx?$", "%.iecst$" },
comment = "//",
block_comment = { "%(%*", "%*%)" },
patterns = {
{ pattern = "//.*", type = "comment" }, -- Single-line comment
{ pattern = { "%(%*", "%*%)" }, type = "comment" }, -- Multi-line comment
{ pattern = { '"', '"', '\\' }, type = "string" }, -- String, quotation marks
{ pattern = { "'", "'", '\\' }, type = "string" }, -- String, apices
{ pattern = "%w+#[0-9]+m?s?", type = "number" }, -- Time/Date formats
{ pattern = "[%a_][%w_]*%f[(]", type = "function" }, -- Function
{ pattern = "[%a_][%w_]*", type = "symbol" }, -- Symbols
{ pattern = ":%s*%w[%w_]*", type = "keyword2" }, -- Variable/Method Type
{ regex = [[[-+]?\d+\.{2,4}[-+]?\d+]], type = "number" }, -- Number Range
{ pattern = "[%+%-=/%*%^%%<>!~|&:]", type = "operator" }, -- Operators
{ pattern = "-?0x%x+", type = "number" }, -- Number
{ pattern = "-?%d+[%deE]*f?", type = "number" }, -- Number
{ pattern = "-?%.?%d+f?", type = "number" }, -- Number
},
symbols = {
["PROGRAM"] = "keyword",
["PROGRAM_INIT"] = "keyword",
["PROGRAM_CYCLIC"] = "keyword",

["PROGRAM_CLOSE"] = "keyword",
["END_PROGRAM"] = "keyword",
["FUNCTION_BLOCK"] = "keyword",
["END_FUNCTION_BLOCK"] = "keyword",
["FUNCTION"] = "keyword",
["END_FUNCTION"] = "keyword",
["METHOD"] = "keyword",
["END_METHOD"] = "keyword",
["IMPLEMENTATION"] = "keyword",
["END_IMPLEMENTATION"] = "keyword",
["INTERFACE"] = "keyword",
["END_INTERFACE"] = "keyword",

["IF"] = "keyword",
["THEN"] = "keyword",
["ELSE"] = "keyword",
["ELSIF"] = "keyword",
["END_IF"] = "keyword",
["CASE"] = "keyword",
["END_CASE"] = "keyword",
["OF"] = "keyword",
["FOR"] = "keyword",
["TO"] = "keyword",
["BY"] = "keyword",
["DO"] = "keyword",
["END_FOR"] = "keyword",
["WHILE"] = "keyword",
["END_WHILE"] = "keyword",
["REPEAT"] = "keyword",
["END_REPEAT"] = "keyword",

["MOD"] = "operator",
["UNTIL"] = "operator",
["EXIT"] = "keyword",
["RETURN"] = "keyword",

["AND"] = "keyword",
["NOT"] = "keyword",
["OR"] = "keyword",
["XOR"] = "keyword",

["NAMESPACE"] = "keyword",
["END_NAMESPACE"] = "keyword",

["VAR"] = "keyword",
["VAR_GLOBAL"] = "keyword",
["VAR_INPUT"] = "keyword",
["VAR_OUTPUT"] = "keyword",
["VAR_IN_OUT"] = "keyword",
["VAR_ACCESS"] = "keyword",
["VAR_EXTERNAL"] = "keyword",
["VAR_TEMP"] = "keyword",
["AT"] = "keyword",
["RETAIN"] = "keyword",
["END_VAR"] = "keyword",
["CONST"] = "keyword",
["END_CONST"] = "keyword",

["TYPE"] = "keyword",
["END_TYPE"] = "keyword",
["STRUCT"] = "keyword",
["END_STRUCT"] = "keyword",

["ORGANIZATION_BLOCK"] = "keyword",
["END_ORGANIZATION_BLOCK"] = "keyword",

["TRUE"] = "literal",
["FALSE"] = "literal",
["true"] = "literal",
["false"] = "literal",

["SINT"] = "keyword2",
["INT"] = "keyword2",
["DINT"] = "keyword2",
["LINT"] = "keyword2",
["USINT"] = "keyword2",
["UINT"] = "keyword2",
["UDINT"] = "keyword2",
["ULINT"] = "keyword2",
["LDINT"] = "keyword2",
["REAL"] = "keyword2",
["LREAL"] = "keyword2",
["TIME"] = "keyword2",
["DATE"] = "keyword2",
["TIME_OF_DAY"] = "keyword2",
["DATE_AND_TIME"] = "keyword2",
["STRING"] = "keyword2",
["BOOL"] = "keyword2",
["BYTE"] = "keyword2",
["WORD"] = "keyword2",
["DWORD"] = "keyword2",
["LWORD"] = "keyword2",
}
}

0 comments on commit 1245246

Please sign in to comment.