plcopen/structures.py
author lbessard
Wed, 18 Apr 2007 16:39:28 +0200
changeset 13 69075340d6a9
parent 9 b29105e29081
child 14 cd0133ed377b
permissions -rw-r--r--
Adding support for forbidding insertion of function block into function
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
#based on the plcopen standard. 
#
#Copyright (C): Edouard TISSERANT and Laurent BESSARD
#
#See COPYING file for copyrights details.
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


LANGUAGES = ["IL","ST","FBD","LD","SFC"]

#-------------------------------------------------------------------------------
#                        Function Block Types definitions
#-------------------------------------------------------------------------------

"""
Ordored list of common Function Blocks defined in the IEC 61131-3
Each block have this attributes:
    - "name" : The block name
    - "type" : The block type. It can be "function", "functionBlock" or "program"
    - "extensible" : Boolean that define if the block is extensible
    - "inputs" : List of the block inputs
    - "outputs" : List of the block outputs
    - "comment" : Comment that will be displayed in the block popup
Inputs and outputs are a tuple of characteristics that are in order:
    - The name
    - The data type
    - The default modifier which can be "none", "negated", "rising" or "falling"
"""

BlockTypes = [{"name" : "Numerical functions", "list":
               [{"name" : "ADD", "type" : "function", "extensible" : True, 
                    "inputs" : [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")], 
                    "outputs" : [("OUT","ANY_NUM","none")],
                    "comment" : "Addition,\nresult := I1 + I2 + ..."},
                {"name" : "MUL", "type" : "function", "extensible" : True, 
                    "inputs" : [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")], 
                    "outputs" : [("OUT","ANY_NUM","none")],
                    "comment" : "Multiplication,\nresult := I1 * I2 * ..."},
                {"name" : "SUB", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")], 
                    "outputs" : [("OUT","ANY_NUM","none")],
                    "comment" : "Substration,\nresult := I1 - I2"},
                {"name" : "DIV", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")], 
                    "outputs" : [("OUT","ANY_NUM","none")],
                    "comment" : "Division,\nresult := I1 / I2\n(with integer division, any fractional remainder is truncated)"},
                {"name" : "MOD", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN1","ANY_INT","none"),("IN2","ANY_INT","none")], 
                    "outputs" : [("OUT","ANY_INT","none")],
                    "comment" : "Modulus,\nresult := I1 MOD I2\n(only valid with integer values)"},
                {"name" : "EXPT", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN1","ANY_REAL","none"),("IN2","ANY_INT","none")], 
                    "outputs" : [("OUT","ANY_REAL","none")],
                    "comment" : "Exponential,\nresult := I1 ^ I2\n(can only take integer exponents)"},
                {"name" : "ABS", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN","ANY_NUM","none")], 
                    "outputs" : [("OUT","ANY_NUM","none")],
                    "comment" : "Absolute value (negative values become positive)"},
                {"name" : "SQRT", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN","ANY_REAL","none")], 
                    "outputs" : [("OUT","ANY_REAL","none")],
                    "comment" : "Square root"},
                {"name" : "LOG", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN","ANY_REAL","none")], 
                    "outputs" : [("OUT","ANY_REAL","none")],
                    "comment" : "Logarithm"},
                {"name" : "LN", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN","ANY_REAL","none")], 
                    "outputs" : [("OUT","ANY_REAL","none")],
                    "comment" : "Natural logarithm"},
                {"name" : "EXP", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN","ANY_REAL","none")], 
                    "outputs" : [("OUT","ANY_REAL","none")],
                    "comment" : "Natural exponential"},
                {"name" : "SIN", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN","ANY_REAL","none")], 
                    "outputs" : [("OUT","ANY_REAL","none")],
                    "comment" : "Sine of input as radians"},
                {"name" : "COS", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN","ANY_REAL","none")], 
                    "outputs" : [("OUT","ANY_REAL","none")],
                    "comment" : "Cosine of input as radians"},
                {"name" : "TAN", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN","ANY_REAL","none")], 
                    "outputs" : [("OUT","ANY_REAL","none")],
                    "comment" : "Tangent of input as radians"},
                {"name" : "ASIN", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN","ANY_REAL","none")], 
                    "outputs" : [("OUT","ANY_REAL","none")],
                    "comment" : "Principal arc-sine, result in radians"},
                {"name" : "ACOS", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN","ANY_REAL","none")], 
                    "outputs" : [("OUT","ANY_REAL","none")],
                    "comment" : "Principal arc-cosine, result in radians"},
                {"name" : "ATAN", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN","ANY_REAL","none")], 
                    "outputs" : [("OUT","ANY_REAL","none")],
                    "comment" : "Principal arc-tangent, result in radians"}
               ]},
              {"name" : "Boolean and bit functions", "list":
               [{"name" : "AND", "type" : "function", "extensible" : True, 
                    "inputs" : [("IN1","ANY_BIT","none"),("IN2","ANY_BIT","none")], 
                    "outputs" : [("OUT","ANY_BIT","none")],
                    "comment" : "Result := I1 & I2 & ..."},
                {"name" : "OR", "type" : "function", "extensible" : True, 
                    "inputs" : [("IN1","ANY_BIT","none"),("IN2","ANY_BIT","none")], 
                    "outputs" : [("OUT","ANY_BIT","none")],
                    "comment" : "Result := I1 OR I2 OR ..."},
                {"name" : "XOR", "type" : "function", "extensible" : True, 
                    "inputs" : [("IN1","ANY_BIT","none"),("IN2","ANY_BIT","none")], 
                    "outputs" : [("OUT","ANY_BIT","none")],
                    "comment" : "Result := I1 XOR I2 XOR ..."},
                {"name" : "NOT", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN","BIT","none")], 
                    "outputs" : [("OUT","BIT","none")],
                    "comment" : "Result := NOT I1"},
                {"name" : "SHL", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN","ANY_BIT","none"),("N","ANY_INT","none")], 
                    "outputs" : [("OUT","ANY_BIT","none")],
                    "comment" : "Shift bit-string n bit positions left, zero fill on the right."},
                {"name" : "SHR", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN","ANY_BIT","none"),("N","ANY_INT","none")], 
                    "outputs" : [("OUT","ANY_BIT")],
                    "comment" : "Shift bit-string right n bit positions, zero fill on the left."},
                {"name" : "ROR", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN","ANY_BIT","none"),("N","ANY_INT","none")], 
                    "outputs" : [("OUT","ANY_BIT","none")],
                    "comment" : "Shift bit-string right, rotate by n bit positions."},
                {"name" : "ROL", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN","ANY_BIT","none"),("N","ANY_INT","none")], 
                    "outputs" : [("OUT","ANY_BIT","none")],
                    "comment" : "Shift bit-string left, rotate by n bit positions."}
               ]},
              {"name" : "Selection functions", "list":
               [{"name" : "SEL", "type" : "function", "extensible" : False, 
                    "inputs" : [("G","BOOL","none"),("IN0","ANY","none"),("IN1","ANY","none")], 
                    "outputs" : [("OUT","ANY","none")],
                    "comment" : "Selection\nIf G = TRUE then\nResult := IN1 else\nResult := IN2"},
                {"name" : "MAX", "type" : "function", "extensible" : True, 
                    "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")], 
                    "outputs" : [("OUT","ANY","none")],
                    "comment" : "Maximum\nResult := maximum value of all inputs"},
                {"name" : "MIN", "type" : "function", "extensible" : True, 
                    "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")], 
                    "outputs" : [("","ANY","none")],
                    "comment" : "Minimum\nResult := minimum value of all inputs"},
                {"name" : "LIMIT", "type" : "function", "extensible" : False, 
                    "inputs" : [("MN","ANY","none"),("IN","ANY","none"),("MX","ANY","none")], 
                    "outputs" : [("OUT","ANY","none")],
                    "comment" : "Limit\nResult is the value of IN limited between a minimum value of MN and a maximum value of MX."},
                {"name" : "MUX", "type" : "function", "extensible" : True, 
                    "inputs" : [("K","ANY_INT","none"),("IN1","ANY","none"),("IN2","ANY","none")], 
                    "outputs" : [("OUT","ANY","none")],
                    "comment" : "Multiplexer\nResult is the value of the input selected by the value of K."}
               ]},
              {"name" : "Comparison functions", "list":
               [{"name" : "GT", "type" : "function", "extensible" : True, 
                    "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")], 
                    "outputs" : [("OUT","ANY","none")],
                    "comment" : "Greater than\nResult := IN1 > IN2"},
                {"name" : "GE", "type" : "function", "extensible" : True, 
                    "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")], 
                    "outputs" : [("OUT","ANY","none")],
                    "comment" : "Greater than or equal\nResult := IN1 >= IN2"},
                {"name" : "EQ", "type" : "function", "extensible" : True, 
                    "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")], 
                    "outputs" : [("OUT","ANY","none")],
                    "comment" : "Equality\nResult := IN1 = IN2"},
                {"name" : "LE", "type" : "function", "extensible" : True, 
                    "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")], 
                    "outputs" : [("OUT","ANY","none")],
                    "comment" : "Lesser than or equal\nResult := IN1 <= IN2"},
                {"name" : "LT", "type" : "function", "extensible" : True, 
                    "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")], 
                    "outputs" : [("OUT","ANY","none")],
                    "comment" : "Lesser than\nResult := IN1 < IN2"},
                {"name" : "NE", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")], 
                    "outputs" : [("OUT","ANY","none")],
                    "comment" : "Not equal\nResult := IN1 <> IN2"}
               ]},
              {"name" : "Character string functions", "list":
               [{"name" : "LEFT", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN","STRING","none"),("L","ANY_INT","none")], 
                    "outputs" : [("OUT","STRING","none")],
                    "comment" : "Extract left string\nResult is the string formed from L characters from the leftmost character of string IN."},
                {"name" : "RIGHT", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN","STRING","none"),("L","ANY_INT","none")], 
                    "outputs" : [("OUT","STRING","none")],
                    "comment" : "Extract right string\nResult is the string formed from L characters from the rightmost part of string IN."},
                {"name" : "MID", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN","ANY","none"),("L","ANY_INT","none"),("P","ANY_INT","none")], 
                    "outputs" : [("OUT","STRING","none")],
                    "comment" : "Extract mid string\nResult is a string extracted from the input string IN starting at character position P, and L characters long."},
                {"name" : "CONCAT", "type" : "function", "extensible" : True, 
                    "inputs" : [("IN1","STRING","none"),("IN2","STRING","none")], 
                    "outputs" : [("OUT","STRING","none")],
                    "comment" : "Concatenate strings\nResult is a string formed by joining the input strings together. This is an extensible function that can take two or more input strings."},
                {"name" : "INSERT", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN1","STRING","none"),("IN2","STRING","none"),("P","ANY_INT","none")], 
                    "outputs" : [("OUT","STRING","none")],
                    "comment" : "Insert string\nThe result is formed by the string IN2 being inserted into string IN1, P character positions from the start of IN1."},
                {"name" : "DELETE", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN","STRING","none"),("L","ANY_INT","none"),("P","ANY_INT","none")], 
                    "outputs" : [("OUT","STRING","none")],
                    "comment" : "Delete string\nThe result is formed by a string of characters L in length, being deleted from the input string IN, starting from character position P."},
                {"name" : "REPLACE", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN1","STRING","none"),("IN2","STRING","none"),("L","ANY_INT","none"),("P","ANY_INT","none")], 
                    "outputs" : [("OUT","STRING","none")],
                    "comment" : "Replace string\nThe result is formed by replacing L characters in string IN1, starting at position P, with character string in IN2."},
                {"name" : "LEN", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN","STRING","none")], 
                    "outputs" : [("OUT","INT","none")],
                    "comment" : "Length\nResult is length of the input string."},
                {"name" : "FIND", "type" : "function", "extensible" : False, 
                    "inputs" : [("IN1","STRING","none"),("IN2","STRING","none")], 
                    "outputs" : [("OUT","INT","none")],
                    "comment" : "Find string\nResult is the position where string IN2 is first found in string IN1.\nIf string IN2 is not found in IN1, the result is 0."},
               ]},
              {"name" : "Standard function blocks", "list":
               [{"name" : "SR", "type" : "functionBlock", "extensible" : False, 
                    "inputs" : [("S1","BOOL","none"),("R","BOOL","none")], 
                    "outputs" : [("Q1","BOOL","none")],
                    "comment" : "SR bistable\nThe SR bistable is a latch where the Set dominates."},
                {"name" : "RS", "type" : "functionBlock", "extensible" : False, 
                    "inputs" : [("S","BOOL","none"),("R1","BOOL","none")], 
                    "outputs" : [("Q1","BOOL","none")],
                    "comment" : "RS bistable\nThe RS bistable is a latch where the Reset dominates."},
                {"name" : "SEMA", "type" : "functionBlock", "extensible" : False, 
                    "inputs" : [("CLAIM","BOOL","none"),("RELEASE","BOOL","none")], 
                    "outputs" : [("BUSY","BOOL","none")],
                    "comment" : "Semaphore\nThe semaphore provides a mechanism to allow software elements mutually exclusive access to certain ressources."},
                {"name" : "R_TRIG", "type" : "functionBlock", "extensible" : False, 
                    "inputs" : [("CLK","BOOL","none")], 
                    "outputs" : [("Q","BOOL","none")],
                    "comment" : "Rising edge detector\nThe output produces a single pulse when a rising edge is detected."},
                {"name" : "F_TRIG", "type" : "functionBlock", "extensible" : False, 
                    "inputs" : [("CLK","BOOL","none")], 
                    "outputs" : [("Q","BOOL","none")],
                    "comment" : "Falling edge detector\nThe output produces a single pulse when a falling edge is detected."},
                {"name" : "CTU", "type" : "functionBlock", "extensible" : False, 
                    "inputs" : [("CU","BOOL","rising"),("R","BOOL","none"),("PV","INT","none")], 
                    "outputs" : [("Q","BOOL","none"),("CV","INT","none")],
                    "comment" : "Up-counter\nThe up-counter can be used to signal when a count has reached a maximum value."},
                {"name" : "CTD", "type" : "functionBlock", "extensible" : False, 
                    "inputs" : [("CD","BOOL","rising"),("LD","BOOL","none"),("PV","INT","none")], 
                    "outputs" : [("Q","BOOL","none"),("CV","INT","none")],
                    "comment" : "Down-counter\nThe down-counter can be used to signal when a count has reached zero, on counting down from a preset value."},
                {"name" : "CTUD", "type" : "functionBlock", "extensible" : False, 
                    "inputs" : [("CU","BOOL","rising"),("CD","BOOL","rising"),("R","BOOL","none"),("LD","BOOL","none"),("PV","INT","none")], 
                    "outputs" : [("QU","BOOL","none"),("QD","BOOL","none"),("CV","INT","none")],
                    "comment" : "Up-down counter\nThe up-down counter has two inputs CU and CD. It can be used to both count up on one input ans down on the other."},
                {"name" : "TP", "type" : "functionBlock", "extensible" : False, 
                    "inputs" : [("IN","BOOL","none"),("PT","TIME","none")], 
                    "outputs" : [("Q","BOOL","none"),("ET","TIME","none")],
                    "comment" : "Pulse timer\nThe pulse timer can be used to generate output pulses of a given time duration."},
                {"name" : "TOF", "type" : "functionBlock", "extensible" : False, 
                    "inputs" : [("IN","BOOL","none"),("PT","TIME","none")], 
                    "outputs" : [("Q","BOOL","none"),("ET","TIME","none")],
                    "comment" : "On-delay timer\nThe on-delay timer can be used to delay setting an output true, for fixed period after an input becomes true."},
                {"name" : "TON", "type" : "functionBlock", "extensible" : False, 
                    "inputs" : [("IN","BOOL","none"),("PT","TIME","none")], 
                    "outputs" : [("Q","BOOL","none"),("ET","TIME","none")],
                    "comment" : "Off-delay timer\nThe off-delay timer can be used to delay setting an output false, for fixed period after input goes false."},
                {"name" : "RTC", "type" : "functionBlock", "extensible" : False, 
                    "inputs" : [("EN","BOOL","none"),("PDT","DATE_AND_TIME","none")], 
                    "outputs" : [("Q","BOOL","none"),("CDT","DATE_AND_TIME","none")],
                    "comment" : "Real time clock\nThe real time clock has many uses including time stamping, setting dates and times of day in batch reports, in alarm messages and so on."},
                {"name" : "INTEGRAL", "type" : "functionBlock", "extensible" : False, 
                    "inputs" : [("RUN","BOOL","none"),("R1","BOOL","none"),("XIN","REAL","none"),("X0","REAL","none"),("CYCLE","TIME","none")], 
                    "outputs" : [("Q","BOOL","none"),("XOUT","REAL","none")],
                    "comment" : "Integral\nThe integral function block integrates the value of input XIN over time."},
                {"name" : "DERIVATIVE", "type" : "functionBlock", "extensible" : False, 
                    "inputs" : [("RUN","BOOL","none"),("XIN","REAL","none"),("CYCLE","TIME","none")], 
                    "outputs" : [("XOUT","REAL","none")],
                    "comment" : "Derivative\nThe derivative function block produces an output XOUT proportional to the rate of change of the input XIN."},
                {"name" : "PID", "type" : "functionBlock", "extensible" : False, 
                    "inputs" : [("AUTO","BOOL","none"),("PV","REAL","none"),("SP","REAL","none"),("X0","REAL","none"),("KP","REAL","none"),("TR","REAL","none"),("TD","REAL","none"),("CYCLE","TIME","none")], 
                    "outputs" : [("XOUT","REAL","none")],
                    "comment" : "PID\nThe PID (proportional, Integral, Derivative) function block provides the classical three term controller for closed loop control."},
                {"name" : "RAMP", "type" : "functionBlock", "extensible" : False, 
                    "inputs" : [("RUN","BOOL","none"),("X0","REAL","none"),("X1","REAL","none"),("TR","TIME","none"),("CYCLE","TIME","none"),("HOLDBACK","BOOL","none"),("ERROR","REAL","none"),("PV","REAL","none")], 
                    "outputs" : [("RAMP","BOOL","none"),("XOUT","REAL","none")],
                    "comment" : "Ramp\nThe RAMP function block is modelled on example given in the standard but with the addition of a 'Holdback' feature."},
                {"name" : "HYSTERESIS", "type" : "functionBlock", "extensible" : False, 
                    "inputs" : [("XIN1","REAL","none"),("XIN2","REAL","none"),("EPS","REAL","none")], 
                    "outputs" : [("Q","BOOL","none")],
                    "comment" : "Hysteresis\nThe hysteresis function block provides a hysteresis boolean output driven by the difference of two floating point (REAL) inputs XIN1 and XIN2."},
                {"name" : "RATIO_MONITOR", "type" : "functionBlock", "extensible" : False, 
                    "inputs" : [("PV1","REAL","none"),("PV2","REAL","none"),("RATIO","REAL","none"),("TIMON","TIME","none"),("TIMOFF","TIME","none"),("TOLERANCE","BOOL","none"),("RESET","BOOL","none"),("CYCLE","TIME","none")], 
                    "outputs" : [("ALARM","BOOL","none"),("TOTAL_ERR","BOOL","none")],
                    "comment" : "Ratio monitor\nThe ratio_monitor function block checks that one process value PV1 is always a given ratio (defined by input RATIO) of a second process value PV2."},
               ]}
             ]

"""
Function that returns the block definition associated to the block type given
"""

def GetBlockType(type):
    for category in BlockTypes:
        for blocktype in category["list"]:
            if blocktype["name"] == type:
                return blocktype
    return None


#-------------------------------------------------------------------------------
#                           Data Types definitions
#-------------------------------------------------------------------------------

"""
Ordored list of common data types defined in the IEC 61131-3
Each type is associated to his direct parent type. It defines then a hierarchy
between type that permits to make a comparison of two types
"""

TypeHierarchy = {"ANY" : None,
                 "ANY_DERIVED" : "ANY",
                 "ANY_ELEMENTARY" : "ANY",
                 "ANY_MAGNITUDE": "ANY_ELEMENTARY",
                 "ANY_BIT" : "ANY_ELEMENTARY",
                 "ANY_STRING" : "ANY_ELEMENTARY",
                 "ANY_DATE" : "ANY_ELEMENTARY",
                 "ANY_NUM" : "ANY_MAGNITUDE",
                 "ANY_REAL" : "ANY_NUM",
                 "ANY_INT" : "ANY_NUM",
                 "REAL" : "ANY_REAL",
                 "LREAL" : "ANY_REAL",
                 "SINT" : "ANY_INT",
                 "INT" : "ANY_INT",
                 "DINT" : "ANY_INT",
                 "LINT" : "ANY_INT",
                 "USINT" : "ANY_INT",
                 "UINT" : "ANY_INT",
                 "UDINT" : "ANY_INT",
                 "ULINT" : "ANY_INT",
                 "TIME" : "ANY_MAGNITUDE",
                 "BOOL" : "ANY_BIT",
                 "BYTE" : "ANY_BIT",
                 "WORD" : "ANY_BIT",
                 "DWORD" : "ANY_BIT",
                 "LWORD" : "ANY_BIT",
                 "STRING" : "ANY_STRING",
                 "WSTRING" : "ANY_STRING",
                 "DATE" : "ANY_DATE",
                 "TOD" : "ANY_DATE",
                 "DT" : "ANY_DATE"
}

"""
Function that returns if the given data type is the same that "reference" or one
of its children types
"""

def IsOfType(test, reference):
    while test != None:
        if test == reference:
            return True
        test = TypeHierarchy[test]
    return False


#-------------------------------------------------------------------------------
#                             Test identifier
#-------------------------------------------------------------------------------



# Test if identifier is valid
def TestIdentifier(identifier):
     if identifier[0].isdigit():
        return False
     words = identifier.split('_')
     for i, word in enumerate(words):
         if len(word) == 0 and i != 0:
             return False
         if len(word) != 0 and not word.isalnum():
             return False
     return True

#-------------------------------------------------------------------------------
#                            Languages Keywords
#-------------------------------------------------------------------------------



# Keywords for Pou Declaration
POU_KEYWORDS = ["FUNCTION", "END_FUNCTION", "FUNCTION_BLOCK", "END_FUNCTION_BLOCK",
 "PROGRAM", "END_PROGRAM", "EN", "ENO", "F_EDGE", "R_EDGE"]
for category in BlockTypes:
    for block in category["list"]:
        if block["name"] not in POU_KEYWORDS:
            POU_KEYWORDS.append(block["name"])


# Keywords for Type Declaration
TYPE_KEYWORDS = ["TYPE", "END_TYPE", "STRUCT", "END_STRUCT", "ARRAY", "OF", "T",
 "D", "TIME_OF_DAY", "DATE_AND_TIME"]
TYPE_KEYWORDS.extend([keyword for keyword in TypeHierarchy.keys() if keyword not in TYPE_KEYWORDS])


# Keywords for Variable Declaration
VAR_KEYWORDS = ["VAR", "VAR_INPUT", "VAR_OUTPUT", "VAR_IN_OUT", "VAR_TEMP", 
 "VAR_EXTERNAL", "END_VAR", "AT", "CONSTANT", "RETAIN", "NON_RETAIN"]


# Keywords for Configuration Declaration
CONFIG_KEYWORDS = ["CONFIGURATION", "END_CONFIGURATION", "RESOURCE", "ON", "END_RESOURCE",
 "PROGRAM", "WITH", "READ_ONLY", "READ_WRITE", "TASK", "VAR_ACCESS", "VAR_CONFIG", 
 "VAR_GLOBAL", "END_VAR"]


# Keywords for Structured Function Chart
SFC_KEYWORDS = ["ACTION", "END_ACTION", "INITIAL_STEP", "STEP", "END_STEP", "TRANSITION",
 "FROM", "TO", "END_TRANSITION"]


# Keywords for Instruction List
IL_KEYWORDS = ["LD", "LDN", "ST", "STN", "S", "R", "AND", "ANDN", "OR", "ORN",
 "XOR", "XORN", "NOT", "ADD", "SUB", "MUL", "DIV", "MOD", "GT", "GE", "EQ", "NE",
 "LE", "LT", "JMP", "JMPC", "JMPNC", "CAL", "CALC", "CALNC", "RET", "RETC", "RETNC"]


# Keywords for Instruction List and Structured Text
ST_KEYWORDS = ["IF", "THEN", "ELSIF", "ELSE", "END_IF", "CASE", "OF", "END_CASE", 
 "FOR", "TO", "BY", "DO", "END_FOR", "WHILE", "DO", "END_WHILE", "REPEAT", "UNTIL", 
 "END_REPEAT", "EXIT", "RETURN", "NOT", "MOD", "AND", "XOR", "OR"]

 
# All the keywords of IEC
IEC_KEYWORDS = ["E", "TRUE", "FALSE"]
IEC_KEYWORDS.extend([keyword for keyword in POU_KEYWORDS if keyword not in IEC_KEYWORDS])
IEC_KEYWORDS.extend([keyword for keyword in TYPE_KEYWORDS if keyword not in IEC_KEYWORDS])
IEC_KEYWORDS.extend([keyword for keyword in VAR_KEYWORDS if keyword not in IEC_KEYWORDS])
IEC_KEYWORDS.extend([keyword for keyword in CONFIG_KEYWORDS if keyword not in IEC_KEYWORDS])
IEC_KEYWORDS.extend([keyword for keyword in SFC_KEYWORDS if keyword not in IEC_KEYWORDS])
IEC_KEYWORDS.extend([keyword for keyword in IL_KEYWORDS if keyword not in IEC_KEYWORDS])
IEC_KEYWORDS.extend([keyword for keyword in ST_KEYWORDS if keyword not in IEC_KEYWORDS])