Crate iec61131

Crate iec61131 

Source
Expand description

§iec61131

Complete IEC 61131-3 parser for PLC programming languages.

This crate provides a comprehensive parser for all 5 IEC 61131-3 programming languages:

  • ST (Structured Text)
  • IL (Instruction List)
  • LD (Ladder Diagram)
  • FBD (Function Block Diagram)
  • SFC (Sequential Function Chart)

§Quick Start

use iec61131::Parser;

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);
match parser.parse() {
    Ok(ast) => println!("Parsed {} declarations", ast.declarations.len()),
    Err(e) => eprintln!("Parse error: {}", e),
}

§Features

  • ✅ Complete IEC 61131-3:2013 support
  • ✅ All 5 languages (ST, IL, LD, FBD, SFC)
  • ✅ Functions, function blocks, programs
  • ✅ Classes and interfaces (OOP)
  • ✅ Namespaces and using directives
  • ✅ Detailed error reporting with source locations
  • ✅ Security limits to prevent DoS attacks

§Security

For untrusted input, use security limits to prevent denial-of-service attacks:

use iec61131::{Parser, security::ParserLimits};

let input = "FUNCTION Test : INT\n  VAR x : INT; END_VAR\n  Test := x;\nEND_FUNCTION";
 
// Use strict limits for untrusted input
let limits = ParserLimits::strict();
// Check input size before parsing
if input.len() > limits.max_input_size {
    panic!("Input too large");
}
 
let mut parser = Parser::new(input);
let ast = parser.parse()?;

§Analysis Features

This crate now includes static analysis features previously available in iecst:

use iec61131::{Parser, analysis::{CfgBuilder, max_nesting_depth}};

let code = r#"
FUNCTION Example : INT
    IF x > 0 THEN
        y := 1;
    ELSE
        y := 2;
    END_IF;
    Example := y;
END_FUNCTION
"#;
 
let mut parser = Parser::new(code);
let cu = parser.parse().unwrap();
 
// Extract function body and analyze
if let Some(iec61131::PouDeclaration::Function(func)) = cu.declarations.first() {
    let cfg = CfgBuilder::new().build(&func.body);
    println!("Cyclomatic complexity: {}", cfg.cyclomatic_complexity());
    println!("Nesting depth: {}", max_nesting_depth(&func.body));
}

Re-exports§

pub use security::ParserLimits;
pub use security::ParserState;
pub use security::SecurityError;

Modules§

analysis
Semantic analysis for IEC 61131-3
security
Security limits for IEC 61131-3 ST parser to prevent DoS attacks

Structs§

ClassDecl
Class declaration
CompilationUnit
Root compilation unit
FunctionBlockDecl
Function block declaration
FunctionDecl
Function declaration
InterfaceDecl
Interface declaration
Lexer
MethodDecl
Method declaration
ParseError
Parser
ProgramDecl
Program declaration
Span
VarDecl
Variable declaration

Enums§

Argument
BinaryOp
Expression
Expression
Literal
PouDeclaration
Program Organization Unit declarations
Statement
Statement
Token
TypeSpec
Type specification
UnaryOp
Variable
Variable (can be simple or complex with member access, array indexing)

Type Aliases§

StatementList
Statement list