Skip to content

Commit

Permalink
Added a basic string escape (not 100% foolproof, but should catch pra…
Browse files Browse the repository at this point in the history
…ctical cases)
  • Loading branch information
RobertoRoos committed Nov 28, 2024
1 parent 8801ea1 commit e114904
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/plcdoc/st_declaration.tx
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,21 @@ SemiColon:
Anything that is considered a value: a literal, a variable, or e.g. a sum
*/
Expression:
ExpressionString | ExpressionAnything
;

/*
Because a string expression could use a syntax character, we need to make an effort match string content, to
escape the content.

We use a literal string match, instead of TextX's `STRING`, because we need to keep the quotes so we can later
still distinguish a literal string type (over e.g. a variable name).
*/
ExpressionString:
/'.*'/
;

ExpressionAnything:
/[^;]*/
// Match anything, including parentheses, up to (but excluding) the semicolon
;
Expand Down
4 changes: 4 additions & 0 deletions tests/plc_code/FB_Variables.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ myfloat_no_ws:REAL;
mystring_size5 : STRING(35) := 'Unknown';
mystring_size6 : STRING[Module.SIZE] := 'Unknown';

mystring_escape : STRING := ':;"';

mystring_concat : STRING := CONCAT('abc', 'xyz');

myint : INT := SomeConstant;
myint2 : INT := E_Error.NoError;

Expand Down
4 changes: 3 additions & 1 deletion tests/test_st_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ def test_grammar_variables(meta_model):
("mystring_size4", "STRING[SIZE]", None, None, None, None),
("mystring_size5", "STRING(35)", "'Unknown'", None, None, None),
("mystring_size6", "STRING[Module.SIZE]", "'Unknown'", None, None, None),
("mystring_escape", "STRING", "':;\"'", None, None, None),
("mystring_concat", "STRING", "CONCAT('abc','xyz')", None, None, None),
("myint", "INT", "SomeConstant", None, None, None),
("myint2", "INT", "E_Error.NoError", None, None, None),
("mylist", "BOOL", None, None, "0..4", None),
Expand Down Expand Up @@ -151,7 +153,7 @@ def test_grammar_variables(meta_model):
("inline1", "INT", None, None, None, None),
]

assert len(variables) == 45
assert len(variables) == 47

for i, expected in enumerate(expected_list):
assert_variable(variables[i], expected)
Expand Down

0 comments on commit e114904

Please sign in to comment.