| Crates.io | iec61131 |
| lib.rs | iec61131 |
| version | 0.7.0 |
| created_at | 2025-12-11 10:55:59.035771+00 |
| updated_at | 2025-12-11 15:47:42.77007+00 |
| description | IEC 61131-3 Structured Text (ST) parser with static analysis |
| homepage | https://github.com/radevgit/plc |
| repository | https://github.com/radevgit/plc |
| max_upload_size | |
| id | 1979497 |
| size | 129,295 |
IEC 61131-3 Structured Text (ST) parser for PLC programming.
iec61131 is a parser for IEC 61131-3 Structured Text (ST), the international standard for PLC (Programmable Logic Controller) programming. Currently supports:
Planned support for additional IEC 61131-3 languages:
Add to your Cargo.toml:
[dependencies]
iec61131 = "0.7"
use iec61131::Parser;
// Parse a Structured Text function
let code = r#"
FUNCTION Add : INT
VAR_INPUT
a : INT;
b : INT;
END_VAR
Add := a + b;
END_FUNCTION
"#;
let mut parser = Parser::new(code);
let ast = parser.parse().expect("Parse error");
println!("Parsed {} declarations", ast.declarations.len());
FUNCTION / END_FUNCTIONFUNCTION_BLOCK / END_FUNCTION_BLOCKPROGRAM / END_PROGRAMCLASS / END_CLASS (OOP)INTERFACE / END_INTERFACE (OOP)METHOD / END_METHOD (OOP)VAR, VAR_INPUT, VAR_OUTPUT, VAR_IN_OUTVAR_TEMP, VAR_EXTERNAL, VAR_GLOBALVAR_ACCESS, VAR_CONFIGCONSTANT, RETAIN, NON_RETAINAT locations (direct variables)variable := expressionIF...THEN...ELSIF...ELSE...END_IFCASE...OF...END_CASEFOR...TO...BY...DO...END_FOR, WHILE...DO...END_WHILE, REPEAT...UNTIL...END_REPEATEXIT, CONTINUE, RETURN+, -, *, /, MOD, ** (power)=, <>, <, <=, >, >=AND, OR, XOR, NOT, &EXTENDS)IMPLEMENTS)PUBLIC, PROTECTED, PRIVATE, INTERNAL)FINAL, ABSTRACT, OVERRIDE)THIS and SUPER keywordsNAMESPACE...END_NAMESPACE)USING)%IX0.0, %QW10)use iec61131::Parser;
let code = r#"
FUNCTION_BLOCK Counter
VAR_INPUT
reset : BOOL;
END_VAR
VAR_OUTPUT
count : INT;
END_VAR
IF reset THEN
count := 0;
ELSE
count := count + 1;
END_IF
END_FUNCTION_BLOCK
"#;
let mut parser = Parser::new(code);
let ast = parser.parse()?;
For untrusted input, use security limits to prevent denial-of-service attacks:
use iec61131::{Parser, ParserLimits};
let input = load_untrusted_file()?;
// Use strict limits for untrusted input
let limits = ParserLimits::strict();
if input.len() > limits.max_input_size {
return Err("Input too large");
}
let mut parser = Parser::new(&input);
let ast = parser.parse()?;
Available security profiles:
ParserLimits::strict() - For untrusted/external input (10MB max, 64 depth)ParserLimits::balanced() - For general use (100MB max, 256 depth) [default]ParserLimits::relaxed() - For trusted/internal files (500MB max, 512 depth)The limits protect against:
use iec61131::Parser;
let code = r#"
TYPE
Color : (Red, Green, Blue);
END_TYPE
FUNCTION_BLOCK Motor
VAR_INPUT
enable : BOOL;
speed : INT;
END_VAR
// ...
END_FUNCTION_BLOCK
PROGRAM Main
VAR
motor1 : Motor;
currentColor : Color;
END_VAR
motor1(enable := TRUE, speed := 100);
CASE currentColor OF
Red: motor1.speed := 50;
Green: motor1.speed := 100;
Blue: motor1.speed := 150;
END_CASE
END_PROGRAM
"#;
let mut parser = Parser::new(code);
let ast = parser.parse()?;
The parser uses a two-stage approach:
The AST fully represents the structure of IEC 61131-3 programs and can be used for:
iecstThis crate (iec61131) supersedes the older iecst crate with several improvements:
| Feature | iecst | iec61131 |
|---|---|---|
| Languages | ST only | ST only (currently) |
| IEC Version | Partial | IEC 61131-3:2013 ST |
| OOP Support | Limited | Full (classes, interfaces) |
| Namespaces | No | Yes |
| Specification | Manual implementation | Based on official specification |
| AST | Basic | Comprehensive |
For new projects, use iec61131. The iecst crate remains available for backward compatibility.
This crate is generated from the official IEC 61131-3:2013 EBNF specification using the plcp/iec61131 parser generator.
# Run tests
cargo test
# Build documentation
cargo doc --open
MIT