// Special manipulation if we are going to write a new paragraph
function handleBeginningOfParagraph(bBeginningOfParagraph : node) {
	if bBeginningOfParagraph {
		@<text:p text:style-name="P1">@
		bBeginningOfParagraph = false;
	}
}

TrafficLight2OASIS ::=
	=> {
		// Invariant part of the document, such as styles
		@<?xml version="1.0" encoding="UTF-8"?>
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" office:version="1.0"><office:scripts/><office:font-face-decls><style:font-face style:name="Tahoma1" svg:font-family="Tahoma"/><style:font-face style:name="Courier New" svg:font-family="&apos;Courier New&apos;" style:font-family-generic="modern" style:font-pitch="fixed"/><style:font-face style:name="Lucida Sans Unicode" svg:font-family="&apos;Lucida Sans Unicode&apos;" style:font-pitch="variable"/><style:font-face style:name="Tahoma" svg:font-family="Tahoma" style:font-pitch="variable"/><style:font-face style:name="Times New Roman" svg:font-family="&apos;Times New Roman&apos;" style:font-family-generic="roman" style:font-pitch="variable"/></office:font-face-decls><office:automatic-styles><style:style style:name="P1" style:family="paragraph" style:parent-style-name="Standard"><style:paragraph-properties style:text-autospace="none"/><style:text-properties style:font-name="Courier New" fo:font-size="10pt" style:font-size-asian="10pt" style:font-size-complex="10pt"/></style:style><style:style style:name="P2" style:family="paragraph" style:parent-style-name="Standard"><style:text-properties style:font-name="Courier New" fo:font-size="10pt" style:font-size-asian="10pt" style:font-size-complex="10pt"/></style:style><style:style style:name="T1" style:family="text"><style:text-properties fo:color="#0000ff" fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/></style:style><style:style style:name="T2" style:family="text"><style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/></style:style><style:style style:name="T3" style:family="text"><style:text-properties fo:color="#0000ff"/></style:style><style:style style:name="T4" style:family="text"><style:text-properties fo:color="#008000"/></style:style><style:style style:name="T5" style:family="text"><style:text-properties fo:color="#0000ff" style:text-position="sub 58%" fo:font-size="12pt" style:font-size-asian="12pt" style:font-size-complex="12pt"/></style:style><style:style style:name="T6" style:family="text"><style:text-properties fo:color="#800000"/></style:style><style:style style:name="T7" style:family="text"><style:text-properties fo:color="#ff0000" fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/></style:style><style:style style:name="fr1" style:family="graphic" style:parent-style-name="Graphics"><style:graphic-properties style:mirror="none" fo:clip="rect(0in 0in 0in 0in)" draw:luminance="0%" draw:contrast="0%" draw:red="0%" draw:green="0%" draw:blue="0%" draw:gamma="100%" draw:color-inversion="false" draw:image-opacity="100%" draw:color-mode="standard"/></style:style></office:automatic-styles><office:body><office:text><office:forms form:automatic-focus="false" form:apply-design-mode="false"/><text:sequence-decls><text:sequence-decl text:display-outline-level="0" text:name="Illustration"/><text:sequence-decl text:display-outline-level="0" text:name="Table"/><text:sequence-decl text:display-outline-level="0" text:name="Text"/><text:sequence-decl text:display-outline-level="0" text:name="Drawing"/></text:sequence-decls>@
	}
	=> local bBeginningOfParagraph = true;
	// consume token by token
	[
			// an identifier: keyword or function of the language or sensor
			#readIdentifier:sWord
			=> handleBeginningOfParagraph(bBeginningOfParagraph);
			[
					// a keyword
					#check(sWord in {"activate", "desactivate", "duration", "start", "strategy"})
					=> {@<text:span text:style-name="T1">@sWord@</text:span>@}
				|
					// the sensor: 'hour' is displayed in subscript
					#check(sWord == "vehicles_hour")
					=> {@<text:span text:style-name="T3">vehicles</text:span><text:span text:style-name="T5">hour</text:span>@}
				|
					// a function
					#check(sWord == "time")
					=> {@<text:span text:style-name="T3">@sWord@</text:span>@}
				|
					// other
					=> {@<text:span text:style-name="T2">@sWord@</text:span>@}
			]
		|
			// a constant string
			#readCString:sText
			=> handleBeginningOfParagraph(bBeginningOfParagraph);
			=> {@<text:span text:style-name="T6">&quot;@sText.composeHTMLLikeString()@&quot;</text:span>@}
		|
			// symbols to highlight
			["=>" | "->"]:sSymbol
			=> handleBeginningOfParagraph(bBeginningOfParagraph);
			=> {@<text:span text:style-name="T7">@sSymbol.composeHTMLLikeString()@</text:span>@}
		|
			// multi-line comment
			!!"/*"
			=> handleBeginningOfParagraph(bBeginningOfParagraph);
			[->"*/"]:sComment
			=> {@<text:span text:style-name="T4">@sComment.composeHTMLLikeString()@</text:span>@}
		|
			// inline comment
			!!"//"
			=> handleBeginningOfParagraph(bBeginningOfParagraph);
			[~[['\r']? '\n']]*:sComment
			=> {@<text:span text:style-name="T4">@sComment.composeHTMLLikeString()@</text:span>@}
		|
			// special case: tabulation
			'\t'
			=> handleBeginningOfParagraph(bBeginningOfParagraph);
			=> {@<text:tab />@}
		|
			// special case: end of paragraph
			['\r']? '\n'
			=> {
				if bBeginningOfParagraph {
					@<text:p text:style-name="P1">@
				} else {
					bBeginningOfParagraph = true;
				}
				@</text:p>@
			}
		|
			// any other non-empty character
			#readChar:cChar
			=> handleBeginningOfParagraph(bBeginningOfParagraph);
			=>{@@cChar.composeHTMLLikeString()@@}
	]*
	=> {
		// may have a paragraph to close
		if !bBeginningOfParagraph {
			@</text:p>@
		}
		local sFileName = getShortFilename(getInputFilename());
		local sPictureFile = sFileName.rsubString(4) + ".png";
		if sFileName.endString(".tlc") && sPictureFile.existFile() {
			// reference the PNG diagram of the strategy in the document and
			// copy it to the directory devoted to pictures
			copyFile(sPictureFile, "OpenOffice2.0/Pictures/" + sPictureFile);
			@<text:p text:style-name="P1" /><text:p text:style-name="P1">@
			@<draw:frame draw:style-name="fr1" draw:name="graphics1" text:anchor-type="paragraph" @
			// extract the width and height of the picture, loaded as a binary file:
			// each byte is represented by two hexadecimal digits
			local sHexaDimensions = rightString(leftString(loadBinaryFile(sPictureFile), 48), 16);
			local dheight = $6.9*hexaToDecimal(sHexaDimensions.rightString(8)) / hexaToDecimal(sHexaDimensions.leftString(8))$;
			@svg:width="6.9in" @
			@svg:height="@$floor($1 + dheight*10$)/10$@in" @
			@draw:z-index="0"><draw:image xlink:href="Pictures/@sPictureFile@" xlink:type="simple"@
			@ xlink:show="embed" xlink:actuate="onLoad"/></draw:frame></text:p>@
		}
		// end of the document
		@</office:text></office:body></office:document-content>@
	}
	;

