BjAlvestad / vscode-simatic-scl

VS Code extensions for the SCL language used in TIA Portal
4 stars 0 forks source link

Parse numeric litterals (Integer and Real numbers) #2

Closed BjAlvestad closed 1 year ago

BjAlvestad commented 1 year ago

Ref. section 11.3 of SCLV4_e

Examples of valid formats for decimal digit strings in literals: 1000 1_120_200 666_999_400_311

image

Integer literals

Integer literals can be assigned to BOOL, BYTE, INT, DINT, WORD, DWORD (depending on their length).

image

Integer literals can also be specified in binary, octal or hexadecimal with the prefixes 2#, 8# and 16#

image

Real number literals

This is nubers with decimal places, and can only be assigned to the REAL data type.

image

image

Note that eksponent is denoted with e followed by +, - or just a number directly. E.g. 3.0E+10, 3.0E10, 3e+10, 3E10, 0.3E+11, 0.3e11, 30.0E+9, 30e9 are all representing 3*10^10.

Summary of various alternatives

// Integer literals
NUMBER1:= 10 ;
NUMBER2:= 2#1010 ;
NUMBER3:= 16#1A2B ;
// Real number literals
NUMBER4:= -3.4 ;
NUMBER5:= 4e2 ;
NUMBER6:= 40_123E10;
BjAlvestad commented 1 year ago

Suggestion to try out

Add terminal factors for digits:

Add terminal factors for exponent;

And terminal for Integer literal and Real number:

BjAlvestad commented 1 year ago

Landed on

terminal fragment BINARY_DIGIT: /2#([0-1](_[0-1])?)+/;
terminal fragment OCTAL_DIGIT: /8#([0-7](_[0-7])?)+/;
terminal fragment HEX_DIGIT: /16#([0-9a-fA-F](_[0-9a-fA-F])?)+/;
terminal fragment DECIMAL_DIGIT: /([0-9](_[0-9])?)+/;
terminal fragment EXPONENT: /[eE][+-]?/DECIMAL_DIGIT;
terminal REAL: /[+-]?/((DECIMAL_DIGIT('.'DECIMAL_DIGIT)?EXPONENT)|(DECIMAL_DIGIT'.'DECIMAL_DIGIT));
terminal INTEGER: /[+-]?/BINARY_DIGIT|OCTAL_DIGIT|HEX_DIGIT|DECIMAL_DIGIT;