Comments and annotations
Comments that appear in diagrams, labels that replace code in them, annotations that steer the layout, and pragmas for the whole file.
A chart is read far more often than it is written, and much of the reading happens in the diagram.
This chapter is about the parts of a .sctx file that are there for the reader: comments, some of which
KIELER draws in the diagram; labels, which replace code in the diagram with words; annotations, which
steer the layout (and occasionally the compiler); and pragmas, which apply to the whole file. Along the
way it covers two comment traps that can quietly change what a chart does.
Ordinary comments
Section titled “Ordinary comments”// starts a comment that runs to the end of the line, and /* ... */ encloses a comment that can span
lines. Both are ignored by the compiler and by the diagram. In SCCharts Lab, a // comment on the line
above a declaration also shows up when you hover over the declared name.
Doc comments in the diagram
Section titled “Doc comments in the diagram”A comment that starts with /** is a doc comment. It belongs to the element written right after
it: the chart, a declaration, a state, a region, a transition, an action or an equation. KIELER draws
the doc comments of the chart, its states and its regions as yellow comment boxes attached to their
element, and the doc comment of a transition as an extra label on its arrow:
Diagram
Code
/** Switches the lamp on and off with one button. */scchart Lamp { /** The push button, true while pressed. */ input bool button output bool light = false
/** Waiting for a press. */ initial state Off /** Press: switch on. */ if button do light = true go to On
/** The lamp is lit. */ state On if button do light = false go to Off}Comment boxes are an option of the diagram, “Comment Nodes”, which is on by default. The annotation
@diagram[commentNodes] false before scchart turns it off for one chart (more on annotations below).
Doc comments change nothing in the behaviour.
A doc comment must stand where an element can follow. Inside a transition (if button /** x */ go to On) it is a syntax error, no viable alternative at input '/** x */', and so is a doc comment after the
last state of a region, with nothing left for it to describe.
There is also a one-line form for declarations, the declaration comment: //* at the end of a
declaration attaches the rest of the line to it.
input bool button //* the push button, true while pressedoutput bool light = false //* the lampSCCharts Lab shows these in its hover card for the declared name. The form is allowed only at the end of
a declaration: at the end of a transition line, say, it is a syntax error (no viable alternative at input '//* switch on\n').
Two comment traps
Section titled “Two comment traps”Both traps come from the same detail of KIELER’s grammar: a // comment must be followed by at least one
character that is not *, and the line break itself counts as that character.
An empty // swallows the next line
Section titled “An empty // swallows the next line”Diagram
Code
scchart Swallowed { input bool button output bool light = false
initial state Off // if button do light = true go to On
state On if button do light = false go to Off}| Tick | input bool button | output bool light | Active state of the chart |
|---|
Guided tour, 3 ticks:
- button = falseTick 1 enters
Off. KIELER warns thatOnis not reachable, which is the only hint that something is wrong. - button = trueThe button is pressed, but
lightstays false: the lineif button do light = true go to Onwas swallowed by the empty//above it, soOffhas no outgoing transition. - button = falseNothing will ever happen in this chart.
1 warning and 1 note
- Note, line 6, column 1KIELER treats this line as part of the empty // comment on the line aboveAn empty // comment at the end of a line also comments out the whole next line in KIELER. Write something after the //, or delete it.
- Warning, line 8, column 3State
Oncan never be enteredKIELER says:The state is not reachable.
No transition leads to it from a reachable state, and it is not initial. Add a transition to it, or delete it.About this message
// is not part of the chart.initial state Off // ends with an empty comment. KIELER’s comment rule takes the line break after //
as the comment’s first character and then continues to the end of the next line. So the whole line
if button do light = true go to On becomes part of the comment, Off has no transition, and the only
hint is the warning The state is not reachable. on On.
A //* banner is not a comment
Section titled “A //* banner is not a comment”Many C programmers frame headings with lines of asterisks. In SCTX a line starting with //* is the
declaration comment from above, and before the chart there is nothing for it to belong to:
Diagram
Code
//**********************************// Lamp controller//**********************************scchart LampBanner { input bool button output bool light = false
initial state Off if button do light = true go to On
state On if button do light = false go to Off}KIELER rejects this chart
- Line 1, column 1Expected an scchart but found a //* commentKIELER says:
missing EOF at '//**********************************\n'
A file contains imports, pragmas and charts written as scchart Name { ... }.About this message
scchart makes the whole file unreadable for KIELER. (This site still draws the chart, so that you can see what was meant.)KIELER stops at the first line with missing EOF at '//**********************************\n'. That is
all official KIELER says; SCCharts Lab adds
There is no SCChart to compile. Add a scchart declaration first.Inside a chart, a banner line that follows a declaration does not fail: it silently becomes that
declaration’s comment. Put a space
after the slashes (// ****) or use /* ... */ for banners.
One relative of these traps: /**/, an empty block comment, is read as the start of a doc comment
(/**) that only ends at the next */ in the file.
Labels
Section titled “Labels”A diagram is easiest to read when it says what a transition means rather than how it is computed. Labels replace the name of a state or region, or the trigger and effects of a transition or action, with your own words:
Diagram
Code
scchart Kettle "Kettle controller" { input bool lever, boiling output bool heater = false
initial state Idle "Waiting" if lever do heater = true go to Heating label "lever down"
state Heating { exit do heater = false label "heater off" } if boiling go to Idle label "water boils" if !lever go to Idle label "lever up"}| Element | Syntax |
|---|---|
| chart | scchart Kettle "Kettle controller" { ... } |
| state | state Idle "Waiting" |
| region | region Lamp "The lamp" { ... } |
| transition | if lever do heater = true go to Heating label "lever down" |
| entry, during or exit action | exit do heater = false label "heater off" |
A label of a state or region is shown in place of its name; the name is still what transitions and
references use. A label of a transition or action is shown in place of its trigger and effects, and
KIELER reminds you of that with the notes The trigger of this action is hidden by the label. and
The effects of this action are hidden by the label. In KIELER the “User Labels” option of the diagram
turns the labels off again. Labels never change behaviour.
Annotations
Section titled “Annotations”An annotation is a name starting with @, written before the element it applies to. It comes in
three shapes:
// a tag:@HVLayout// a name with a value:@DefaultSleep 50// a name with a key and a value:@layout[elk.direction] DOWNMost annotations only affect the diagram:
| Annotation | Where | Effect |
|---|---|---|
@layout[elk.direction] DOWN |
chart, state, region | lays out that level DOWN, UP, LEFT or RIGHT |
@HVLayout, @VHLayout |
chart, state, region | the order in which the levels alternate between horizontal and vertical layout (the default is @HVLayout) |
@hide |
state, region, transition, declaration | leaves the element out of the diagram, along with transitions from or to a hidden state |
@collapse, @expand |
region | draws the region collapsed, or expanded |
@diagram[userLabels] false |
chart | sets one of the diagram’s options for this chart, here “User Labels” |
@foreground, @background |
state | colours (this site’s diagrams ignore them, so that they suit both themes) |
Hidden is not switched off. In this chart the region that counts presses carries @hide:
Diagram
Code
scchart Layout { input bool button output bool light = false output int presses = 0
@layout[elk.direction] DOWN region Lamp { initial state Off if button do light = true go to On
state On if button do light = false go to Off }
@hide region Counter { initial state Count if button do presses++ go to Count }}| Tick | input bool button | output bool light | output int presses | Active state of region Lamp | Active state of region Counter |
|---|
Guided tour, 4 ticks:
- button = falseAnnotations change the diagram, not the behaviour. Tick 1 enters
Offand, in the hidden region,Count. - button = true
light= true, andpresses= 1: the region you cannot see still runs. - button = falseNothing fires.
- button = true
light= false,presses= 2.
presses goes up with every press.A few annotations change what the compiler does. They appear in the chapters about their features:
@HardReset, @SimulateSleep, @DefaultSleep, @IntegerClockType and @NoSleep in
Time, and @C / @Java on extern names in Host code.
Pragmas
Section titled “Pragmas”A pragma is a line starting with # that applies to the whole file. Pragmas and import lines go at
the top, in any order, before the first scchart; a pragma after a chart is a syntax error
(missing EOF at '#'). The ones you are likely to meet:
| Pragma | Purpose | Chapter |
|---|---|---|
#hostcode "...", #hostcode-c "...", #hostcode-c-header "...", #hostcode-java "..." |
C or Java text pasted into the generated files | Host code |
#resource "file" |
copies a file next to the generated code and compiles it with it | Host code |
#code.naming "prefix" |
renames the generated functions and data struct | From chart to C |
The KIELER wiki’s page on annotations and pragmas lists a few more for special set-ups.
Try it
Section titled “Try it”A diagram that explains itself
Document the doorbell: give the chart and both states doc comments, put a //* comment on the input,
and label the two transitions “pressed” and “after one tick”. Check your work in the diagram: the
comment boxes and the two labels should appear there. Comments and labels never change behaviour, so
the playground’s comparison with the model solution matches even before you start; run it at the end
to make sure that you did not change anything else.
Show the starter chart
scchart Doorbell { input bool press output bool ring = false
initial state Quiet if press do ring = true go to Ringing
state Ringing do ring = false go to Quiet}Show a solution
/** Rings for one tick per press. */scchart Doorbell { input bool press //* the bell push output bool ring = false
/** Waiting for someone at the door. */ initial state Quiet if press do ring = true go to Ringing label "pressed"
/** The bell is ringing. */ state Ringing do ring = false go to Quiet label "after one tick"}Check your understanding
Section titled “Check your understanding”Show the answer
Answer D: //***** Lamp controller *****
A line starting with //* is a declaration comment, and there is no declaration before scchart for it
to belong to: missing EOF at .... An ordinary // or /* */ comment is ignored, and a /** */ doc
comment before scchart belongs to the chart.
Show the answer
Answer A: Nothing: labels only change the diagram
A label replaces the trigger and effects in the diagram only. KIELER notes that they are hidden, but the transition behaves exactly as before.
Key ideas
//and/* */comments are ignored./** */doc comments belong to the next element and KIELER draws them in the diagram;//*at the end of a declaration is a one-line doc comment.- Never end a line with a bare
//: in LF files it swallows the next line. Never start a banner with//*. - Labels (
state S "...",label "...") replace names, triggers and effects in the diagram, never the behaviour. - Annotations (
@...) steer the layout and a few compiler features; unknown or misspelt names are ignored without a warning. - Pragmas (
#...) apply to the whole file and go before the firstscchart.
Next, Host code: calling C or Java from a chart.