1/*description:
2{
3	It takes in charge of translating any \CodeWorker\ script to HTML.
4
5	The source file is highlighted and keywords plus particular symbols of the
6	language are linked to the online-documentation.
7
8	Note that \samp{this.docURL} designates the URL or the directory where both the CSS
9	and the	HTML documentation of \CodeWorker\ are stored. If not populated, it will resolve
10	links from the directory where is the target HTML file. Don't forget to end the URL or
11	directory with a trailing slash.
12
13	To highlight any script, type:\\
14	\texttt{CodeWorker \textbf{-translate} CWscript2HTML.cws \textit{<CW-script.cw>} \textit{<CW-script.html>}}\\
15	to consider that the documentation and the CSS are in the same directory, and:\\
16	\texttt{CodeWorker \textbf{-translate} CWscript2HTML.cws \textit{<CW-script.cw>} \textit{<CW-script.html>} \textbf{-insert} docURL \textit{HTML-doc-URL}}\\
17	otherwise.
18}
19*/
20
21#include "CodeWorker_grammar.cwp"
22
23#overload #ignore	::=
24		[
25				' ' | '\t' | '\r' | '\n'
26			|
27				"/*"
28				=> insertText($getOutputLocation() - 2$, "<span class=\"comment\">");
29				ignoreEmbeddedComment
30				=> {@</span>@}
31			|
32				"//"
33				=> insertText($getOutputLocation() - 2$, "<span class=\"comment\">");
34				[~[['\r']? '\n']]*
35				#explicitCopy
36				['\r']? '\n'
37				=> {@</span><br/>
38		@}
39		]*;
40
41function transformChar(cChar : value) {
42	if cChar == ' ' return "&nbsp;";
43	if cChar == '\t' return "&nbsp;&nbsp;&nbsp;&nbsp;";
44	if cChar == '\n' return "<br/>" + endl();
45	if cChar != '\r' return composeHTMLLikeString(cChar);
46}
47
48#implicitCopy(transformChar)
49
50#overload translation_unit	::=
51		=> {@<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
52<html>
53	<head>
54		<link rel="stylesheet" type="text/css" href="@this.docURL@CodeWorker.css" />
55	</head>
56	<body class="script">
57		@}
58		script<this>
59		=> {@<hr />
60		Generated by <I class="CodeWorker">CodeWorker</I> v@getVersion()@ from <a href="@this.docURL@CWscript2HTML.html">CWscript2HTML.cws</a>.
61	</body>
62</html>
63@}
64		;
65
66#overload script<"template-based">	::=
67		#continue
68		[
69			=> {@<span class="raw_text">@}
70			->['@' | "<%"]
71			=> {@</span>@}
72			#continue
73			#ignore
74			[
75					expression
76					=> {@<span class="raw_text">@}
77					['@' | "%>" | #empty]
78				|
79					[instruction]*
80					=> {@<span class="raw_text">@}
81					['@' | "%>" | #empty]
82			]
83			=> {@</span>@}
84		]+
85		=> {@</span>@}
86		;
87
88#overload instruction	::=
89		'{' #continue [instruction]* '}'
90	|
91		#readIdentifier:sKeyword
92		=> local iLocation = getOutputLocation();
93		[
94				instruction<sKeyword>
95				=> insertText(iLocation, "</A>");
96				=> insertText($iLocation - sKeyword.length()$, "<A class=\"instruction\" href=\"" + this.docURL + "manual_The_scripting_language.html#" + sKeyword + "\">");
97			|
98				predefined_function_call<sKeyword> ';'
99				=> insertText(iLocation, "</A>");
100				=> insertText($iLocation - sKeyword.length()$, "<A class=\"function\" href=\"" + this.docURL + "manual_The_scripting_language.html#" + sKeyword + "\">");
101			|
102				predefined_procedure_call<sKeyword> ';'
103				=> insertText(iLocation, "</A>");
104				=> insertText($iLocation - sKeyword.length()$, "<A class=\"procedure\" href=\"" + this.docURL + "manual_The_scripting_language.html#" + sKeyword + "\">");
105			|
106				user_function_call ';'
107		]
108	|
109		=> local iLocation = getOutputLocation();
110		'#'
111		#readIdentifier:sKeyword
112		=> insertText(iLocation, "<a class=\"directive\" href=\"" + this.docURL + "manual_The_scripting_language.html#_" + sKeyword + "\">");
113		=> {@</a>@}
114		preprocessor<sKeyword>
115	|
116		#check(this != "procedural")
117		=> {@<span class="raw_text">@}
118		['@' | "%>"]
119		#!ignore
120		#continue ->['@' | "<%" | #empty]
121		=>{@</span>@}
122		#ignore
123		[expression ![!'@' !"%>" !#empty]]?
124		;
125
126#overload literal_expression<bNumeric>	::=
127		=> local iLocation = getOutputLocation();
128		[
129				CONSTANT_STRING
130			|
131				'(' #continue expression<bNumeric> ')'
132			|
133				'$'
134				=> insertText(iLocation, "<a class=\"constant\" href=\"" + this.docURL + "manual_The_scripting_language.html#$\">");
135				=> {@</a>@}
136				#continue #check(!bNumeric) expression<true>
137				=> {@<a class="constant" href="@this.docURL@manual_The_scripting_language.html#$">@}
138				'$'
139				=> {@</a>@}
140			|
141				'~'
142				=> insertText(iLocation, "<a class=\"constant\" href=\"" + this.docURL + "manual_The_scripting_language.html#numeric_expression\">");
143				=> {@</a>@}
144				#continue #check(bNumeric) literal_expression<true>
145			|
146				CONSTANT_CHAR
147			|
148				'!'
149				=> insertText(iLocation, "<a class=\"constant\" href=\"" + this.docURL + "manual_The_scripting_language.html#boolean_expression\">");
150				=> {@</a>@}
151				#continue literal_expression<bNumeric>
152			|
153				#readNumeric
154				=> insertText(iLocation, "<span class=\"numeric\">");
155				=> {@</span>@}
156			|
157				#readIdentifier:{"true", "false"}
158				=> insertText(iLocation, "<a class=\"constant\" href=\"" + this.docURL + "manual_The_scripting_language.html#boolean_literals\">");
159				=> {@</a>@}
160			|
161				function_call
162			|
163				variable_expression ['.' #continue method_call]?
164		]
165		;
166
167#overload function_call	::=
168		#readIdentifier:sFunctionName
169		=> local iLocation = getOutputLocation();
170		[
171				predefined_function_call<sFunctionName>
172				=> insertText(iLocation, "</A>");
173				=> insertText($iLocation - sFunctionName.length()$, "<A class=\"function\" href=\"" + this.docURL + "manual_The_scripting_language.html#" + sFunctionName + "\">");
174			|
175				user_function_call
176		];
177
178#overload method_call	::=
179		#readIdentifier:sMethodName
180		=> local iLocation = getOutputLocation();
181		[
182				predefined_method_call<sMethodName>:sFunctionName
183				=> insertText(iLocation, "</A>");
184				=> insertText($iLocation - sMethodName.length()$, "<A class=\"function\" href=\"" + this.docURL + "manual_The_scripting_language.html#" + sFunctionName + "\">");
185			|
186				user_method_call<sMethodName>
187		];
188
189#overload variable_expression<sIdentifier> : #!ignore	::=
190		=> if sIdentifier == "project" || sIdentifier == "this" {
191			insertText($getOutputLocation() - sIdentifier.length()$, "<a class = \"constant\" href=\"" + this.docURL + "manual_The_scripting_language.html#" + sIdentifier + "\">");
192			@</a>@
193		}
194		#super::variable_expression<sIdentifier>
195		;
196
197
198#overload DEFAULT	::=
199		=> {@<B>@}
200		#readIdentifier:"default"
201		=> {@</B>@};
202
203#overload CASE	::=
204		=> {@<B>@}
205		#readIdentifier:"case"
206		=> {@</B>@};
207
208#overload START	::=
209		=> {@<B>@}
210		#readIdentifier:"start"
211		=> {@</B>@};
212
213#overload CASCADING	::=
214		=> {@<B>@}
215		#readIdentifier:"cascading"
216		=> {@</B>@};
217
218#overload ELSE	::=
219		=> {@<B>@}
220		#readIdentifier:"else"
221		=> {@</B>@};
222
223#overload IN		::=
224		=> {@<B>@}
225		#readIdentifier:"in"
226		=> {@</B>@};
227
228#overload NO_CASE	::=
229		=> {@<B>@}
230		#readIdentifier:"no_case"
231		=> {@</B>@};
232
233#overload SORTED	::=
234		=> {@<B>@}
235		#readIdentifier:"sorted"
236		=> {@</B>@};
237
238#overload WHILE	::=
239		=> {@<B>@}
240		#readIdentifier:"while"
241		=> {@</B>@};
242
243#overload CONSTANT_STRING	::=
244		=> local iLocation = getOutputLocation();
245		#readCString
246		=> insertText(iLocation, "<span class=\"string\">");
247		=> {@</span>@}
248		;
249
250#overload CONSTANT_CHAR	::=
251		=> local iLocation = getOutputLocation();
252		'\'' #!ignore #continue ['\\']? #readChar '\''
253		=> insertText(iLocation, "<span class=\"string\">");
254		=> {@</span>@}
255		;
256
257#overload PRULE_SYMBOL	::=
258		=> {@<a class="constant" href="@this.docURL@manual_The_scripting_language.html#parsing_BNF_syntax">@}
259		"::="
260		=> {@</a>@}
261		;
262
263#overload NON_TERMINAL	::=
264		=> {@<i>@}
265		#readIdentifier
266		=> {@</i>@}
267		;
268
269#overload ALTERNATION	::=
270		=> local iLocation = getOutputLocation();
271		'|'
272		=> insertText(iLocation, "<a class=\"instruction\" href=\"" + this.docURL + "manual_The_scripting_language.html#parsing_alternation\">");
273		=> {@</a>@}
274		;
275
276#overload TR_BEGIN		::=
277		=> local iLocation = getOutputLocation();
278		'<'
279		=> insertText(iLocation, "<a class=\"instruction\" href=\"" + this.docURL + "manual_The_scripting_language.html#BNF_declaring_a_clause\">");
280		=> {@</a>@}
281		;
282
283#overload TR_END		::=
284		=> local iLocation = getOutputLocation();
285		'>'
286		=> insertText(iLocation, "<a class=\"instruction\" href=\"" + this.docURL + "manual_The_scripting_language.html#BNF_declaring_a_clause\">");
287		=> {@</a>@}
288		;
289
290
291#overload BNF_general_directive	::=
292		=> local iLocation = getOutputLocation();
293		'#'
294		#readIdentifier:sKeyword
295		=> insertText(iLocation, "<a class=\"directive\" href=\"" + this.docURL + "manual_The_scripting_language.html#_" + sKeyword + "\">");
296		=> {@</a>@}
297		BNF_general_directive<sKeyword>
298		;
299
300#overload BNF_general_directive<"overload">	::=
301		'#'
302		=> insertText($getOutputLocation() - 1$, "<a class=\"directive\" href=\"" + this.docURL + "manual_The_scripting_language.html#_ignore\">");
303		#continue #readIdentifier:"ignore"
304		=> {@</a>@}
305		BNF_general_directive<"ignore">
306	|
307		production_rule;
308
309#overload BNF_catch	::=
310		'#'
311		=> insertText($getOutputLocation() - 1$, "<a class=\"directive\" href=\"" + this.docURL + "manual_The_scripting_language.html#_try\">");
312		#continue
313		"catch"
314		=> {@</a>@}
315		'(' variable_expression ')';
316
317#overload BNF_clause_preprocessing	::=
318		':' '#'
319		=> insertText($getOutputLocation() - 1$, "<a class=\"directive\" href=\"" + this.docURL + "manual_The_scripting_language.html#BNF_preprocessing_of_a_clause\">");
320		#continue
321		[
322				'!'
323				#continue
324				#readIdentifier:"ignore"
325			|
326				#readIdentifier:"ignore"
327		]
328		=> {@</a>@};
329
330#overload BNF_literal<bTokenCondition>	::=
331		CONSTANT_STRING
332		[#check(bTokenCondition) ':' #continue variable_expression]?
333	|
334		CONSTANT_CHAR
335		[
336				".." #continue CONSTANT_CHAR
337				[#check(bTokenCondition) BNF_token_post_processing]?
338			|
339				[#check(bTokenCondition) ':' #continue variable_expression]?
340		]
341	|
342		[
343				'~'
344				=> insertText($getOutputLocation() - 1$, "<a class=\"instruction\" href=\"" + this.docURL + "manual_The_scripting_language.html#BNF_token_complementary\">");
345				=> {@</a>@}
346			|
347				'^'
348				=> insertText($getOutputLocation() - 1$, "<a class=\"instruction\" href=\"" + this.docURL + "manual_The_scripting_language.html#BNF_token_complementary\">");
349				=> {@</a>@}
350			|
351				'!'
352				=> insertText($getOutputLocation() - 1$, "<a class=\"instruction\" href=\"" + this.docURL + "manual_The_scripting_language.html#BNF_token_negation\">");
353				=> {@</a>@}
354			|
355				"->"
356				=> insertText($getOutputLocation() - 10$, "<a class=\"instruction\" href=\"" + this.docURL + "manual_The_scripting_language.html#-_amp_gt_\">");
357				=> {@</a>@}
358		]
359		#continue BNF_literal<false>
360		[#check(bTokenCondition) BNF_token_post_processing]?
361	|
362		'['
363		=> insertText($getOutputLocation() - 1$, "<a class=\"instruction\" href=\"" + this.docURL + "manual_The_scripting_language.html#BNF_token_repeating_a_token\">");
364		=> {@</a>@}
365		#continue BNF_sequence [ALTERNATION #continue BNF_sequence]*
366		=> {@<a class="instruction" href="@this.docURL@manual_The_scripting_language.html#BNF_token_repeating_a_token">@}
367		']'
368		[
369			'?' | '+' | '*' | #readInteger
370			[".." #continue [#readInteger | '*']]?
371		]?
372		=> {@</a>@}
373		[#check(bTokenCondition) BNF_token_post_processing]?
374	|
375		'#'
376		=> local iLocation = $getOutputLocation() - 1$;
377		#continue
378		[
379				'!'
380				#continue
381				#readIdentifier:"ignore"
382				=> insertText(iLocation, "<a class=\"directive\" href=\"" + this.docURL + "manual_The_scripting_language.html#_ignore\">");
383				=> {@</a>@}
384			|
385				#readIdentifier:sDirective
386				=> insertText(iLocation, "<a class=\"directive\" href=\"" + this.docURL + "manual_The_scripting_language.html#_" + sDirective + "\">");
387				=> {@</a>@}
388				BNF_directive<sDirective>:bTokenConditionAllowed
389				[
390					#check(bTokenCondition && bTokenConditionAllowed)
391					BNF_token_post_processing
392				]?
393		]
394	|
395		"=>"
396		=> insertText($getOutputLocation() - 5$, "<a class=\"instruction\" href=\"" + this.docURL + "manual_The_scripting_language.html#BNF_operator__eq__amp_gt_\">");
397		=> {@</a>@}
398		#continue instruction
399	|
400		BNF_clause_call
401		[#check(bTokenCondition) BNF_token_post_processing]?
402	;
403
404