@@ -262,12 +262,22 @@ fn extract_formatted_text(ft: &FormattedText) -> Option<String> {
262262 ft. text . clone ( )
263263}
264264
265+ /// Extract variable and POU references from ST (Structured Text) code.
266+ ///
267+ /// This function:
268+ /// - Removes comments (both `(* ... *)` and `//` style)
269+ /// - Splits code into identifiers
270+ /// - Filters out keywords and non-identifiers
271+ /// - Adds potential variable/POU references to the analysis
265272fn extract_references_from_st ( code : & str , analysis : & mut PlcopenAnalysis ) {
273+ // Remove comments: (* ... *) and // ...
274+ let code = remove_plc_comments ( code) ;
275+
266276 // Simple extraction: find identifiers that could be variables
267277 for word in code. split ( |c : char | !c. is_alphanumeric ( ) && c != '_' ) {
268278 let word = word. trim ( ) ;
269279 if !word. is_empty ( )
270- && word . chars ( ) . next ( ) . map ( |c| c . is_alphabetic ( ) ) . unwrap_or ( false )
280+ && is_identifier ( word )
271281 && !is_st_keyword ( word)
272282 {
273283 // Could be a variable or POU call
@@ -276,19 +286,45 @@ fn extract_references_from_st(code: &str, analysis: &mut PlcopenAnalysis) {
276286 }
277287}
278288
289+ /// Extract variable references from IL (Instruction List) code.
290+ ///
291+ /// This function:
292+ /// - Removes comments
293+ /// - Parses IL format: `OPCODE OPERAND`
294+ /// - Filters out IL opcodes (LD, ST, ADD, etc.)
295+ /// - Extracts operands as variable references
279296fn extract_references_from_il ( code : & str , analysis : & mut PlcopenAnalysis ) {
297+ // Remove comments: (* ... *) and // ...
298+ let code = remove_plc_comments ( code) ;
299+
280300 // IL format: OPCODE OPERAND
281301 for line in code. lines ( ) {
282302 let parts: Vec < & str > = line. split_whitespace ( ) . collect ( ) ;
283303 if parts. len ( ) >= 2 {
304+ // First part is opcode, second is operand (unless opcode takes no args)
305+ let opcode = parts[ 0 ] . to_uppercase ( ) ;
306+
307+ // Skip if first token is not an opcode (e.g., label)
308+ if !is_il_opcode ( & opcode) {
309+ continue ;
310+ }
311+
284312 let operand = parts[ 1 ] ;
285- if operand . chars ( ) . next ( ) . map ( |c| c . is_alphabetic ( ) ) . unwrap_or ( false ) {
313+ if is_identifier ( operand ) && ! is_il_opcode ( & operand . to_uppercase ( ) ) {
286314 analysis. used_variables . insert ( operand. to_string ( ) ) ;
287315 }
288316 }
289317 }
290318}
291319
320+ /// Extract references from FBD (Function Block Diagram) bodies.
321+ ///
322+ /// Extracts:
323+ /// - Block type names (function/FB calls)
324+ /// - Variable references (inVariable, outVariable, inOutVariable)
325+ /// - Label names for jumps
326+ ///
327+ /// FBD is a graphical language where logic is expressed as interconnected blocks.
292328fn extract_references_from_fbd ( fbd : & plcopen:: Body_FBD_Inline , analysis : & mut PlcopenAnalysis ) {
293329 // Extract from blocks (function blocks, function calls)
294330 for block in & fbd. block {
@@ -353,6 +389,15 @@ fn extract_references_from_fbd(fbd: &plcopen::Body_FBD_Inline, analysis: &mut Pl
353389 }
354390}
355391
392+ /// Extract references from LD (Ladder Diagram) bodies.
393+ ///
394+ /// Extracts:
395+ /// - Contact variable names (input conditions)
396+ /// - Coil variable names (output actions)
397+ /// - Block type names (function/FB calls)
398+ /// - Other variables used in the ladder logic
399+ ///
400+ /// LD is a graphical language resembling electrical ladder diagrams.
356401fn extract_references_from_ld ( ld : & plcopen:: Body_LD_Inline , analysis : & mut PlcopenAnalysis ) {
357402 // LD shares many elements with FBD, plus ladder-specific coils and contacts
358403
@@ -434,6 +479,15 @@ fn extract_references_from_ld(ld: &plcopen::Body_LD_Inline, analysis: &mut Plcop
434479 }
435480}
436481
482+ /// Extract references from SFC (Sequential Function Chart) bodies.
483+ ///
484+ /// Extracts:
485+ /// - Step names (states in the sequence)
486+ /// - Action block qualifiers and references
487+ /// - Transition conditions
488+ /// - Jump step targets
489+ ///
490+ /// SFC is a graphical language for sequential control processes.
437491fn extract_references_from_sfc ( sfc : & plcopen:: Body_SFC_Inline , analysis : & mut PlcopenAnalysis ) {
438492 // SFC shares FBD/LD elements plus SFC-specific steps, transitions, actions
439493
@@ -530,8 +584,56 @@ fn extract_references_from_sfc(sfc: &plcopen::Body_SFC_Inline, analysis: &mut Pl
530584 }
531585}
532586
587+ /// Remove PLC-style comments from code: `(* ... *)` and `// ...`
588+ ///
589+ /// This prevents comment text from being incorrectly identified as
590+ /// variable references or POU calls.
591+ ///
592+ /// # Example
593+ /// ```ignore
594+ /// let code = "XIC(Tag1) (* Comment *) OTE(Tag2)";
595+ /// let cleaned = remove_plc_comments(code);
596+ /// // Result: "XIC(Tag1) OTE(Tag2)"
597+ /// ```
598+ fn remove_plc_comments ( code : & str ) -> String {
599+ let mut result = String :: with_capacity ( code. len ( ) ) ;
600+ let mut chars = code. chars ( ) . peekable ( ) ;
601+
602+ while let Some ( ch) = chars. next ( ) {
603+ if ch == '(' && chars. peek ( ) == Some ( & '*' ) {
604+ // Block comment (* ... *)
605+ chars. next ( ) ; // consume *
606+ let mut prev = ' ' ;
607+ while let Some ( c) = chars. next ( ) {
608+ if prev == '*' && c == ')' {
609+ break ;
610+ }
611+ prev = c;
612+ }
613+ result. push ( ' ' ) ; // Replace comment with space
614+ } else if ch == '/' && chars. peek ( ) == Some ( & '/' ) {
615+ // Line comment // ...
616+ chars. next ( ) ; // consume second /
617+ while let Some ( c) = chars. next ( ) {
618+ if c == '\n' {
619+ result. push ( c) ;
620+ break ;
621+ }
622+ }
623+ } else {
624+ result. push ( ch) ;
625+ }
626+ }
627+
628+ result
629+ }
630+
533631/// Check if a string is a valid identifier (not a literal or complex expression)
534632fn is_identifier ( s : & str ) -> bool {
633+ if s. is_empty ( ) {
634+ return false ;
635+ }
636+
535637 // Skip numeric literals
536638 if s. chars ( ) . next ( ) . map ( |c| c. is_ascii_digit ( ) ) . unwrap_or ( false ) {
537639 return false ;
@@ -547,13 +649,13 @@ fn is_identifier(s: &str) -> bool {
547649 return false ;
548650 }
549651
550- // Must start with letter or underscore and contain only alphanumeric/underscore
652+ // Must start with letter or underscore
551653 let first_char = s. chars ( ) . next ( ) ;
552654 if !matches ! ( first_char, Some ( c) if c. is_alphabetic( ) || c == '_' ) {
553655 return false ;
554656 }
555657
556- // Simple identifier check - no dots, brackets, or operators
658+ // Must contain ONLY alphanumeric and underscore - reject anything with punctuation
557659 s. chars ( ) . all ( |c| c. is_alphanumeric ( ) || c == '_' )
558660}
559661
@@ -578,6 +680,35 @@ fn is_st_keyword(word: &str) -> bool {
578680 )
579681}
580682
683+ /// Check if a word is an IL (Instruction List) opcode.
684+ ///
685+ /// IL opcodes include:
686+ /// - Load/Store: LD, LDN, ST, STN, S, R
687+ /// - Arithmetic: ADD, SUB, MUL, DIV, MOD
688+ /// - Bitwise: AND, OR, XOR, NOT
689+ /// - Comparison: GT, GE, EQ, NE, LE, LT
690+ /// - Flow: JMP, JMPC, CAL, RET
691+ ///
692+ /// This prevents opcodes from being flagged as undefined variables.
693+ fn is_il_opcode ( word : & str ) -> bool {
694+ matches ! ( word,
695+ // Load/Store
696+ "LD" | "LDN" | "ST" | "STN" | "S" | "R" |
697+ // Stack operations
698+ "PUSH" | "POP" |
699+ // Arithmetic
700+ "ADD" | "SUB" | "MUL" | "DIV" | "MOD" |
701+ // Bitwise
702+ "AND" | "ANDN" | "OR" | "ORN" | "XOR" | "XORN" | "NOT" |
703+ // Comparison
704+ "GT" | "GE" | "EQ" | "NE" | "LE" | "LT" |
705+ // Jump/Call
706+ "JMP" | "JMPC" | "JMPCN" | "CAL" | "CALC" | "CALCN" | "RET" | "RETC" | "RETCN" |
707+ // Other
708+ "NOP"
709+ )
710+ }
711+
581712fn is_builtin ( name : & str ) -> bool {
582713 // Common IEC 61131-3 standard functions/function blocks
583714 matches ! ( name. to_uppercase( ) . as_str( ) ,
0 commit comments