Skip to content

Commit 86f9df8

Browse files
authored
Merge pull request #114 from alajovic/increase-priority-of-constant-rule
Increase priority of constant rule in grammar (#108)
2 parents c8adbe8 + c1193b9 commit 86f9df8

3 files changed

Lines changed: 35 additions & 53 deletions

File tree

blark/iec.lark

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ _library_element_declaration: data_type_declaration
3636
IDENTIFIER: /[A-Za-z_][A-Za-z0-9_]*/i
3737

3838
// B.1.2
39-
constant: time_literal
40-
| numeric_literal
41-
| string_literal
42-
| bit_string_literal
43-
| boolean_literal
39+
constant.1: time_literal
40+
| numeric_literal
41+
| string_literal
42+
| bit_string_literal
43+
| boolean_literal
4444

4545
// B.1.2.1
4646
BIT_STRING: /(1|0)(_?(1|0))*/
@@ -430,7 +430,6 @@ edge_declaration: var1_list ":" _BOOL ( R_EDGE | F_EDGE )
430430

431431
?var_init_decl: array_var_init_decl
432432
| structured_var_init_decl
433-
| string_var_declaration
434433
| var1_init_decl
435434
| fb_decl
436435

@@ -499,20 +498,13 @@ location: _AT direct_variable
499498

500499
global_var_list: global_var_name ( "," global_var_name )*
501500

502-
?string_var_declaration: single_byte_string_var_declaration
503-
| double_byte_string_var_declaration
504-
505-
single_byte_string_var_declaration: var1_list ":" single_byte_string_spec
506-
507501
bracketed_expression: "[" expression "]"
508502

509503
string_spec_length: parenthesized_expression
510504
| bracketed_expression
511505

512506
single_byte_string_spec: STRING [ string_spec_length ] [ ":=" SINGLE_BYTE_CHARACTER_STRING ]
513507

514-
double_byte_string_var_declaration: var1_list ":" double_byte_string_spec
515-
516508
double_byte_string_spec: WSTRING [ string_spec_length ] [ ":=" DOUBLE_BYTE_CHARACTER_STRING ]
517509

518510
incomplete_located_var_declarations: _VAR [ variable_attributes ] incomplete_located_var_decl* _END_VAR ";"*

blark/tests/test_parsing.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pathlib
22

3+
import lark
34
import pytest
45

56
from ..parse import parse, parse_source_code, summarize
@@ -130,3 +131,31 @@ def test_comment_parsing(code: str, expected_snippets: list[str]):
130131
result.source_code[token.start_pos: token.end_pos] for token in result.comments
131132
]
132133
assert snippets == expected_snippets
134+
135+
136+
def tree_contains_token(tree, token):
137+
"""
138+
Checks whether the given lark tree contains the given token, either as a node
139+
or as a leaf.
140+
"""
141+
return any((
142+
tree.data == token,
143+
token in tree.children,
144+
any(tree_contains_token(child, token)
145+
for child in tree.children
146+
if isinstance(child, lark.Tree))))
147+
148+
149+
@pytest.mark.parametrize(
150+
"start_rule, code, token_type, token_value",
151+
[
152+
pytest.param("unary_expression", "TRUE", "RULE", "constant"),
153+
pytest.param("unary_expression", "TRUE", "TRUE_VALUE", "TRUE"),
154+
pytest.param("unary_expression", "FALSE", "RULE", "constant"),
155+
pytest.param("unary_expression", "FALSE", "FALSE_VALUE", "FALSE"),
156+
],
157+
)
158+
def test_key_token(start_rule: str, code: str, token_type: str, token_value: str):
159+
"""Test whether the parse tree contains the key token."""
160+
result = conftest.get_grammar(start=start_rule).parse(code)
161+
assert tree_contains_token(result, lark.Token(token_type, token_value))

blark/transform.py

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2494,7 +2494,7 @@ def __str__(self) -> str:
24942494
EnumeratedTypeInitialization,
24952495
ArrayTypeInitialization,
24962496
InitializedStructure,
2497-
_GenericInit, # StringVariableInitDeclaration, EdgeDeclaration
2497+
_GenericInit, # EdgeDeclaration
24982498
]
24992499

25002500

@@ -2568,44 +2568,6 @@ class StructuredVariableInitDeclaration(InitDeclaration):
25682568
meta: Optional[Meta] = meta_field()
25692569

25702570

2571-
@dataclass
2572-
@_rule_handler(
2573-
"single_byte_string_var_declaration",
2574-
"double_byte_string_var_declaration",
2575-
comments=True
2576-
)
2577-
class StringVariableInitDeclaration(InitDeclaration):
2578-
"""
2579-
A declaration of one or more variables using single/double byte strings,
2580-
with an optinoal initialization value.
2581-
2582-
Examples::
2583-
2584-
sVar1 : STRING(2_500_000) := 'test1'
2585-
sVar2, sVar3 : STRING(Param.iLower) := 'test2'
2586-
sVar4, sVar5 : WSTRING(Param.iLower) := "test3"
2587-
"""
2588-
variables: List[DeclaredVariable]
2589-
spec: StringTypeSpecification
2590-
value: Optional[lark.Token]
2591-
init: _GenericInit
2592-
meta: Optional[Meta] = meta_field()
2593-
2594-
@staticmethod
2595-
def from_lark(variables: List[DeclaredVariable], string_info: StringTypeInitialization):
2596-
return StringVariableInitDeclaration(
2597-
variables=variables,
2598-
spec=string_info.spec,
2599-
value=string_info.value,
2600-
init=_GenericInit(
2601-
base_type_name=str(string_info.spec.base_type_name),
2602-
full_type_name=str(string_info.spec.full_type_name),
2603-
value=str(string_info.value),
2604-
repr=join_if(string_info.spec, " := ", string_info.value),
2605-
)
2606-
)
2607-
2608-
26092571
@dataclass
26102572
@_rule_handler("edge_declaration", comments=True)
26112573
class EdgeDeclaration(InitDeclaration):
@@ -3294,7 +3256,6 @@ def __str__(self) -> str:
32943256

32953257
VariableInitDeclaration = Union[
32963258
ArrayVariableInitDeclaration,
3297-
StringVariableInitDeclaration,
32983259
VariableOneInitDeclaration,
32993260
FunctionBlockDeclaration,
33003261
EdgeDeclaration,

0 commit comments

Comments
 (0)