Skip to content

Commit 064a3b6

Browse files
committed
parser without cabins
1 parent b68e408 commit 064a3b6

File tree

5 files changed

+520
-2
lines changed

5 files changed

+520
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@ stfListener.java
4747
src/main/antlr4/samples/*.xml
4848
src/main/antlr4/samples/*.cc
4949
src/main/antlr4/samples/*.h
50+
src/main/antlr4/samples/*.hpp
5051
src/main/antlr4/samples/*.sd
5152
src/main/antlr4/samples/*.sms

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# RTS strange structured text configs parser
2+
3+
## How to use
4+
5+
```bash
6+
java -jar parser.jar file1 file2
7+
```

src/main/java/net/unixcode/rts/parser/translator/XML2CXXTranslator.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
@Component
1919
public class XML2CXXTranslator implements ITranslator<ISTF2XMLListenerCtxt> {
2020
final Logger logger = LoggerFactory.getLogger(getClass());
21-
public static final String XSLT_FILENAME = "classpath:xml/xml2cxx.xsl";
22-
public static final String CXX_EXTENSION = "cc";
21+
public static final String XSLT_FILENAME = "classpath:xml/xml2cxx_no-cabins.xsl";
22+
public static final String CXX_EXTENSION = "hpp";
2323
protected Processor processor;
2424
protected XsltExecutable stylesheet;
2525
protected Xslt30Transformer transformer;

src/main/resources/xml/common.xsl

+235
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<xsl:stylesheet version="2.0"
3+
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
4+
xmlns:xs="http://www.w3.org/2001/XMLSchema">
5+
6+
<xsl:variable name='newline'>
7+
<xsl:text>&#xa;</xsl:text>
8+
</xsl:variable>
9+
10+
<xsl:template name="lights">
11+
<xsl:param name="lights"/>
12+
<xsl:for-each select="$lights">
13+
/**
14+
#### Источник света
15+
### <xsl:call-template name="dev_name">
16+
<xsl:with-param name="name" select="Name/string"/>
17+
</xsl:call-template>
18+
**/
19+
<xsl:value-of select="Name" />
20+
<xsl:text>_</xsl:text>
21+
<xsl:value-of select="position() - 1" />
22+
<xsl:text> = </xsl:text>
23+
<xsl:value-of select="position() - 1" />
24+
<xsl:text>,&#xa;</xsl:text>
25+
</xsl:for-each>
26+
</xsl:template>
27+
28+
<xsl:template name="anims">
29+
<xsl:param name="anims"/>
30+
<xsl:for-each select="$anims">
31+
/**
32+
#### Анимация
33+
### <xsl:call-template name="dev_name">
34+
<xsl:with-param name="name" select="Name/string"/>
35+
</xsl:call-template>
36+
**/
37+
<xsl:value-of select="Name" />
38+
<xsl:text>_</xsl:text>
39+
<xsl:value-of select="position() - 1" />
40+
<xsl:text> = </xsl:text>
41+
<xsl:value-of select="position() - 1" />
42+
<xsl:text>,&#xa;</xsl:text>
43+
</xsl:for-each>
44+
</xsl:template>
45+
46+
<xsl:template name="switch">
47+
<xsl:param name="switch"/>
48+
<xsl:param name="location"/>
49+
/**
50+
### <xsl:call-template name="dev_name"><xsl:with-param name="name" select="$switch/Hint/string"/></xsl:call-template>
51+
**<xsl:value-of select="$location" />**
52+
Type: <xsl:call-template name="switch_type_hint"/>
53+
Name: <xsl:value-of select="$switch/Name/word/text()" />
54+
Flags: <xsl:value-of select="$switch/Flags/word" separator=", "/>
55+
**/
56+
<xsl:text> </xsl:text>
57+
<xsl:call-template name="nameable">
58+
<xsl:with-param name="nameable" select="."/>
59+
</xsl:call-template>
60+
</xsl:template>
61+
62+
<xsl:template name="switch_type_hint">
63+
<xsl:choose>
64+
<xsl:when test="Flags/word[contains(text(), 'NONFIXED')]">
65+
<xsl:text>Кнопка</xsl:text>
66+
</xsl:when>
67+
<xsl:when test="States/number/text() = 2">
68+
<xsl:text>Выключатель/тумблер</xsl:text>
69+
</xsl:when>
70+
<xsl:when test="States/number/text() &gt; 2">
71+
<xsl:text>Мультипереключатель</xsl:text>
72+
</xsl:when>
73+
</xsl:choose>
74+
</xsl:template>
75+
76+
<xsl:template name="switches">
77+
<xsl:param name="switches"/>
78+
<xsl:param name="location"/>
79+
/**
80+
## Переключатели.
81+
### <xsl:value-of select="$location" />
82+
**/
83+
enum class sw : unsigned short {
84+
<xsl:for-each select="$switches">
85+
<xsl:call-template name="switch">
86+
<xsl:with-param name="switch" select="."/>
87+
<xsl:with-param name="location" select="$location"/>
88+
</xsl:call-template>
89+
</xsl:for-each>
90+
};
91+
</xsl:template>
92+
93+
<xsl:template name="dev_name">
94+
<xsl:param name="name" />
95+
<xsl:if test="string-length($name/text()) = 0">
96+
<xsl:value-of select="$unnamed"/>
97+
</xsl:if>
98+
<xsl:value-of select="$name/text()" />
99+
</xsl:template>
100+
101+
<xsl:template name="switch_state_hint">
102+
<xsl:param name="sw" />
103+
<xsl:param name="st" />
104+
<xsl:if test="not($sw/StateHints/(string|word)[number($st) + 1])">
105+
<xsl:value-of select="$unnamed"/>
106+
</xsl:if>
107+
<xsl:value-of select="$sw/StateHints/(string|word)[number($st) + 1]"/>
108+
</xsl:template>
109+
110+
<xsl:template name="switch_state">
111+
<xsl:param name="switch"/>
112+
<xsl:param name="location"/>
113+
/**
114+
#### Состояния переключателя
115+
### <xsl:call-template name="dev_name">
116+
<xsl:with-param name="name" select="$switch/Hint/string"/>
117+
</xsl:call-template>
118+
**<xsl:value-of select="$location" />**
119+
Name: `<xsl:value-of select="$switch/Name/word/text()" />`
120+
Default: `<xsl:value-of select="$switch/States/number[2]" />`
121+
States:<xsl:for-each select="1 to $switch/States/number[1]">
122+
- <xsl:number value=". - 1" format="`1` - "/>
123+
<xsl:call-template name="switch_state_hint">
124+
<xsl:with-param name="st" select=". - 1"/>
125+
<xsl:with-param name="sw" select="$switch"/>
126+
</xsl:call-template>
127+
</xsl:for-each>
128+
**/
129+
enum class <xsl:value-of select="$switch/Name/word/text()"/> : unsigned short {
130+
/**
131+
#### <xsl:call-template name="dev_name">
132+
<xsl:with-param name="name" select="$switch/Hint/string"/>
133+
</xsl:call-template>
134+
### Исходное состояние
135+
**/
136+
DEFAULT = <xsl:value-of select="$switch/States/number[2]"/><xsl:text>,</xsl:text>
137+
<xsl:for-each select="1 to $switch/States/number[1]">&#xa;
138+
/**
139+
#### <xsl:value-of select="if ($switch/Hint/string) then $switch/Hint/string else 'UNNAMED'"/>
140+
### <xsl:call-template name="switch_state_hint">
141+
<xsl:with-param name="st" select=". - 1"/>
142+
<xsl:with-param name="sw" select="$switch"/>
143+
</xsl:call-template>
144+
**/
145+
ST_<xsl:number value="position() - 1"/>
146+
<xsl:text> = </xsl:text>
147+
<xsl:number value="position() - 1"/>
148+
<xsl:text>,</xsl:text>
149+
</xsl:for-each>
150+
};
151+
</xsl:template>
152+
153+
<xsl:template name="states">
154+
<xsl:param name="location"/>
155+
<xsl:param name="switches"/>
156+
/**
157+
## Состояния приборов
158+
### <xsl:value-of select="$location" />
159+
**/
160+
namespace st {
161+
<xsl:for-each select="$switches">
162+
<xsl:call-template name="switch_state">
163+
<xsl:with-param name="switch" select="."/>
164+
<xsl:with-param name="location" select="$location"/>
165+
</xsl:call-template>
166+
</xsl:for-each>
167+
};
168+
</xsl:template>
169+
170+
171+
<xsl:template name="nameable">
172+
<xsl:param name="nameable"/>
173+
<xsl:value-of select="$nameable/Name/word/text()" />
174+
<xsl:text>_</xsl:text>
175+
<xsl:value-of select="$nameable/ID/number/text()" />
176+
<xsl:text> = </xsl:text>
177+
<xsl:value-of select="$nameable/ID/number/text()" />
178+
<xsl:text>,&#xa;</xsl:text>
179+
</xsl:template>
180+
181+
182+
<xsl:template name="display_values">
183+
<xsl:choose>
184+
<xsl:when test="DisplayType/word = $disp_lamp">
185+
#### Values:
186+
`0` - OFF
187+
`1` - ON</xsl:when>
188+
<xsl:when test="DisplayType/word = $disp_arrow">
189+
#### Values:
190+
MIN: `<xsl:value-of select="ValuesRange/number[1]"/>`
191+
MAX: `<xsl:value-of select="ValuesRange/number[2]"/>`
192+
ZERO: `<xsl:value-of select="ValuesRange/number[3]"/>
193+
<xsl:text>`</xsl:text>
194+
</xsl:when>
195+
</xsl:choose>
196+
</xsl:template>
197+
198+
<xsl:template name="display">
199+
<xsl:param name="display"/>
200+
<xsl:param name="location"/>
201+
/**
202+
### <xsl:call-template name="dev_name">
203+
<xsl:with-param name="name" select="$display/Hint/string"/>
204+
</xsl:call-template>
205+
#### <xsl:value-of select="$location" />
206+
Type: <xsl:value-of select="$display/DisplayType/word/text()" />
207+
Name: `<xsl:value-of select="$display/Name/word/text()" />
208+
<xsl:text>`</xsl:text>
209+
<xsl:call-template name="display_values" />
210+
**/
211+
<xsl:text> </xsl:text>
212+
<xsl:call-template name="nameable">
213+
<xsl:with-param name="nameable" select="."/>
214+
</xsl:call-template>
215+
</xsl:template>
216+
217+
218+
<xsl:template name="displays">
219+
<xsl:param name="displays"/>
220+
<xsl:param name="location"/>
221+
/**
222+
## Указатели.
223+
### <xsl:value-of select="$location" />
224+
**/
225+
enum class disp : unsigned short {
226+
<xsl:for-each select="$displays">
227+
<xsl:call-template name="display">
228+
<xsl:with-param name="display" select="."/>
229+
<xsl:with-param name="location" select="$location"/>
230+
</xsl:call-template>
231+
</xsl:for-each>
232+
};
233+
</xsl:template>
234+
235+
</xsl:stylesheet>

0 commit comments

Comments
 (0)