Skip to content

Commit 9f4fb50

Browse files
committed
Add basic Structured Text parser
Includes support for simple arithmetic and boolean expressions as well as other ST language features, such as networks, functions and function blocks.
1 parent 3d9a71b commit 9f4fb50

16 files changed

+1784
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ src/json/json_lex.yy.cpp
8484
src/json/json_y.output
8585
src/json/json_y.tab.cpp
8686
src/json/json_y.tab.h
87+
src/structured-text/structured_text_lex.yy.cpp
88+
src/structured-text/structured_text_y.output
89+
src/structured-text/structured_text_y.tab.cpp
90+
src/structured-text/structured_text_y.tab.h
8791
src/xmllang/xml_lex.yy.cpp
8892
src/xmllang/xml_y.output
8993
src/xmllang/xml_y.tab.cpp
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*******************************************************************\
2+
3+
Module: Structured Text Language Conversion
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Structured Text Language Conversion
11+
12+
#include "convert_int_value.h"
13+
#include <util/std_expr.h>
14+
#include <util/std_types.h>
15+
16+
exprt convert_int_value(const std::string &src)
17+
{
18+
signed value = std::stoi(src);
19+
typet type(ID_structured_text_int_value);
20+
21+
exprt result(std::to_string(value), type);
22+
return result;
23+
}
24+
25+
exprt convert_bit_value(const std::string &src)
26+
{
27+
std::string value(src);
28+
typet type(ID_structured_text_bit_value);
29+
30+
exprt result(value, type);
31+
return result;
32+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*******************************************************************\
2+
3+
Module: Structured Text Language Conversion
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Structured Text Language Conversion
11+
12+
#ifndef CPROVER_STRUCTURED_TEXT_CONVERTERS_CONVERT_INT_VALUE_H
13+
#define CPROVER_STRUCTURED_TEXT_CONVERTERS_CONVERT_INT_VALUE_H
14+
15+
#include <string>
16+
17+
#include <util/expr.h>
18+
19+
/// Converts a string into the corresponding 'Int' expression.
20+
exprt convert_int_value(const std::string &src);
21+
/// Converts a string into the corresponding 'Bool' expression
22+
exprt convert_bit_value(const std::string &src);
23+
24+
#endif // CPROVER_STRUCTURED_TEXT_CONVERTERS_CONVERT_INT_VALUE_H
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*******************************************************************\
2+
3+
Module: Structured Text Language Conversion
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Structured Text Language Conversion
11+
12+
#include "convert_real_value.h"
13+
#include <util/std_expr.h>
14+
#include <util/std_types.h>
15+
16+
exprt convert_real_value(const std::string &src)
17+
{
18+
float value = std::stof(src);
19+
typet type(ID_structured_text_real_value);
20+
21+
exprt result(std::to_string(value), type);
22+
return result;
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*******************************************************************\
2+
3+
Module: Structured Text Language Conversion
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Structured Text Language Conversion
11+
12+
#ifndef CPROVER_STRUCTURED_TEXT_CONVERTERS_CONVERT_REAL_VALUE_H
13+
#define CPROVER_STRUCTURED_TEXT_CONVERTERS_CONVERT_REAL_VALUE_H
14+
15+
#include <string>
16+
17+
#include "util/expr.h"
18+
19+
/// Converts a string into the corresponding 'Real' expression.
20+
exprt convert_real_value(const std::string &src);
21+
22+
#endif // CPROVER_STRUCTURED_TEXT_CONVERTERS_CONVERT_REAL_VALUE_H
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*******************************************************************\
2+
3+
Module: Structured Text Language Conversion
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Structured Text Language Conversion
11+
12+
#include "convert_string_value.h"
13+
14+
exprt convert_identifier(const std::string &src)
15+
{
16+
// Cut the leading and trailing quotes
17+
std::string value = src.substr(1, src.length() - 2);
18+
19+
typet type(ID_structured_text_identifier);
20+
21+
exprt result(value, type);
22+
return result;
23+
}
24+
25+
exprt convert_title(const std::string &src)
26+
{
27+
std::string value(src);
28+
typet type(ID_structured_text_title);
29+
30+
exprt result(value, type);
31+
return result;
32+
}
33+
34+
exprt convert_label(const std::string &src)
35+
{
36+
// Cut the trailing colon
37+
std::string value = src.substr(0, src.length() - 1);
38+
39+
typet type(ID_structured_text_label);
40+
41+
exprt result(value, type);
42+
return result;
43+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*******************************************************************\
2+
3+
Module: Structured Text Language Conversion
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Structured Text Language Conversion
11+
12+
#ifndef CPROVER_STRUCTURED_TEXT_CONVERTERS_CONVERT_STRING_VALUE_H
13+
#define CPROVER_STRUCTURED_TEXT_CONVERTERS_CONVERT_STRING_VALUE_H
14+
15+
#include <util/expr.h>
16+
17+
/// Converts a string into a Structured Text identifier expression.
18+
exprt convert_identifier(const std::string &src);
19+
/// Converts a string into a Structured Text title expression.
20+
exprt convert_title(const std::string &src);
21+
/// Converts a string into a Structured Text label expression.
22+
exprt convert_label(const std::string &src);
23+
24+
#endif // CPROVER_STRUCTURED_TEXT_CONVERTERS_CONVERT_STRING_VALUE_H
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
structured-text
2+
structured-text/converters
3+
util
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
converters
2+
langapi
3+
linking
4+
structured-text
5+
util

0 commit comments

Comments
 (0)