UniSet 2.45.1
Пространство имен st2js.parser

Классы

class  GVLBlock
class  ParseResult

Функции

str _preprocess_st (str source)
ParseResult parse_st (str source, str filename="<stdin>")
ParseResult parse_file (str path)
ParseResult _parse_twincat_file (Path file_path)
ParseResult _parse_plcopen_xml (Path file_path)

Переменные

 _TWINCAT_EXTENSIONS = frozenset(('.tcpou', '.tcdut', '.tcgvl'))
 _PLCOPEN_EXTENSIONS = frozenset(('.xml',))

Подробное описание

ST parser wrapper around blark.

Provides parse_st() which parses IEC 61131-3 Structured Text source code
and returns a ParseResult containing the blark typed AST, original source,
and filename for error reporting.

Provides parse_file() which auto-detects file format by extension and
supports plain ST (.st, .txt) and TwinCAT/PLCopen XML (.xml, .TcPOU,
.TcDUT, .TcGVL) input files.

Функции

◆ _parse_plcopen_xml()

ParseResult st2js.parser._parse_plcopen_xml ( Path file_path)
protected
Parse a PLCopen XML file by reconstructing ST source from XML structure.

Codesys PLCopen XML exports store POU declarations (PROGRAM,
FUNCTION_BLOCK, FUNCTION), data types, global variables, and actions
as XML elements.  The ST body code is embedded inside ``<ST>`` elements,
either as direct text/CDATA or within an ``<xhtml>`` child element.

This function reconstructs syntactically valid IEC 61131-3 Structured
Text source code from the XML structure, then parses it with blark.

Two styles are supported:

1. **CDATA style** -- ``<ST><![CDATA[PROGRAM Main ... END_PROGRAM]]></ST>``
   The ST text already contains full POU wrappers; we just extract & join.

2. **Codesys xhtml style** -- ``<pou pouType="program" name="Main">``
   with ``<interface>`` holding variable sections and ``<body><ST><xhtml>``
   holding only the body statements.  We reconstruct the ST wrapper from
   the XML structure.

Args:
    file_path: Path to the PLCopen XML file.

Returns:
    ParseResult with the typed AST.

Raises:
    ParseError: If the file cannot be read, is not valid XML, or
        contains no parseable ST code.

◆ _parse_twincat_file()

ParseResult st2js.parser._parse_twincat_file ( Path file_path)
protected
Parse a TwinCAT XML file (.TcPOU, .TcDUT, .TcGVL) using blark.

Collects all successfully parsed items from the XML file and merges
them into a single SourceCode AST.  If there are parse errors in some
items but at least one succeeds, the successful items are returned.

Args:
    file_path: Path to the TwinCAT XML file.

Returns:
    ParseResult with merged SourceCode tree.

Raises:
    ParseError: If no items could be parsed from the file.

◆ _preprocess_st()

str st2js.parser._preprocess_st ( str source)
protected
Preprocess ST source to work around blark grammar limitations.

Transforms:
- Typed literals: CHAR#'a' -> 'a', LDT#... -> ..., SINT#5 -> 5
- Boolean function calls: AND(x1,x2) -> (x1 AND x2), OR(x1,x2) -> (x1 OR x2)

◆ parse_file()

ParseResult st2js.parser.parse_file ( str path)
Parse an ST source file, auto-detecting format by extension.

Supports plain ST files (.st, .txt) and TwinCAT/PLCopen XML files
(.xml, .TcPOU, .TcDUT, .TcGVL).  For XML files, blark.parse.parse()
handles extraction of ST code from CDATA sections.

Args:
    path: Filesystem path to the input file.

Returns:
    ParseResult with the typed AST, source, and filename.

Raises:
    ParseError: If the file cannot be read or parsed.

◆ parse_st()

ParseResult st2js.parser.parse_st ( str source,
str filename = "<stdin>" )
Parse IEC 61131-3 Structured Text source code.

Uses blark to parse the source and transform the lark parse tree into
blark's typed AST dataclasses (Program, FunctionBlock, etc.).

Args:
    source: The ST source code string.
    filename: Filename for error messages (default: "<stdin>").

Returns:
    ParseResult with the typed AST, source, and filename.

Raises:
    ParseError: If the source cannot be parsed, with line/col when available.