BlueGit

timfa posprint

Files

posprint / docs / DSL-SPEC.md

DSL-SPEC.md

DSL-SPEC.md 7.8 KiB Download Raw

Document DSL specification

A lightweight, markdown-inspired syntax for POS receipt templates. The DSL compiles to an AST and then to ESC/POS bytes or a screen preview.

> Implementation: Parser in src/dsl/parser.rs. All constructs below are implemented unless listed under Future extensions.

Design goals: easy to author, line-oriented, explicit about POS-specific features (barcodes, cut, feed), parameter-friendly.

---

Document structure

A document is a sequence of blocks. Blank lines become paper feeds (@feed 1). Blocks are not nested except for ${parameters} inside text.

---

Text blocks

Paragraphs

Plain text (and ${parameters}). One or more consecutive non-empty lines form a paragraph. There is no inline bold/italic/code markup. Consecutive spaces are preserved (not collapsed), including leading indentation, so authors can pad columns manually. Trailing spaces on a line are trimmed.

Thank you for your purchase!
Visit us again soon.
Item               12.00
                          rightish note

Headings

# Store Name
## Receipt
  • # → large / double-height text (ESC/POS emphasis)
  • ## → bold emphasis
  • Maximum two levels in v1

Alignment

Prefix a paragraph with alignment markers:

:center:
Your Store Name

:right:
Total: ${Amount}
MarkerEffect
:left:Left (default)
:center:Centered
:right:Right-aligned

Bullet lists

- Groceries
  - Milk
  - Eggs
- Wash car

Each line must start with optional indent (2 spaces or 1 tab per level) then - . Consecutive list lines form one list block. A blank line ends the list.

DepthPrinted bullet
0 (top)*
≥ 1indented -

Wrapped lines use a hanging indent matching the bullet prefix width:

* this is a
  long line
  - nested item that
    wraps too

Parameters

SyntaxEffect
${Name}Text parameter placeholder (see Parameters)
$[Name]Bool checkbox parameter → [ ] / [x]

${Name} and $[Name] must not share the same name.

Checkboxes

SyntaxPrinted result
[] or [ ][ ]
[x] / [X][x]
$[Name][ ] or [x] from a bool parameter

ASCII brackets + x stay inline in the native CP437 font. Do not use Unicode ☐ / √ — many ESC/POS printers map those to CJK glyphs. ${Name} and $[Name] cannot share a name.

Parameters work inside any text, and inside barcode/QR/image directives (below).

Example:

Receipt #${DocumentNumber}
Date: ${Date}

$[Paid] Paid
[] Pending

Item: ${ItemName}     ${LineTotal}

Horizontal rule

---

Renders a dashed or solid separator line across the print width.

---

Directives (block-level)

Directives start with @ at the beginning of a line.

Feed (blank lines)

@feed 3

Advances paper by n lines.

Cut

@cut

Partial cut after content (if printer supports). Often placed at document end.

Image

@image path/to/logo.png
@image ${LogoPath} width=50%
@image assets/qr-promo.png width=384
AttributeDescription
path (positional)File path relative to .pos file or absolute; may be ${Param}
widthNN% of printable width, or absolute dots (e.g. 384)
alignleft (default), center, right

Images are converted to monochrome raster data and scaled to width (never wider than printable_dots).

Date / time

@date
@date DD-MM-YYYY
@time
@time 12h
@time 24h
DirectiveBehaviour
@dateSystem regional short date (%x)
@date PATTERNCustom pattern (YYYY, MM, DD, …)
@time / @time 24h24-hour HH:MM
@time 12h12-hour with AM/PM

Barcode

@barcode CODE128 ${Sku}
@barcode EAN13 ${EanCode} height=80
@barcode RASTER CODE128 ${Sku}
@barcode RASTER CODE93 ABC-1234
FieldValues
ModeOptional RASTER prefix; otherwise native ESC/POS
SymbologyNative: CODE128, EAN13, EAN8, CODE39, UPC-A, UPC-E, ITF, CODABAR. Raster-only: CODE93, CODE11
DataLiteral or ${Param}
heightModule / PNG height in dots (default: 64)
alignleft, center, right
show_textNative only: HRI below barcode

Raster barcodes stretch horizontally to full printable width.

QR code

@qr https://example.com/receipt/${DocumentNumber}
@qr ${PaymentUrl} size=6 ecc=H
@qr RASTER ${PaymentUrl} version=5 scale=4 ecc=M
AttributeNativeRaster
DataURL/text; may include ${Param}Same
sizeModule size 1–16 (default 4)—
scale—Pixels per module (default 4)
version—QR version 1–40 (optional)
eccL, M, Q, H (default M)Same
alignleft, center, rightSame

---

Parameters

Syntax

${ParameterName}   # text
$[ParameterName]   # boolean checkbox → [ ] / [x]
  • Names: [A-Za-z_][A-Za-z0-9_]* (ASCII letters, digits, underscore)
  • Case-sensitive: ${DocNo} and ${docno} are different
  • No mixed types: the same name must not appear as both ${…} and $[…]
  • Uniqueness at input: all occurrences of the same name share one value entered at print time

Where parameters are allowed

LocationExample
TextOrder ${OrderId} / $[Paid] Paid
Barcode data@barcode CODE128 ${OrderId} (${…} only)
QR data@qr ${VerifyUrl} (${…} only)
Image path@image ${LogoPath} (${…} only)

Print-time behavior

1. Before print, scan AST for all ${…} and $[…] tokens. 2. Deduplicate by name (kinds must not conflict — enforced at parse). 3. Show dialog: text field per ${…}, checkbox per $[…] (or require CLI --set). 4. Bool values: true/false (also 1/0, yes/no). 4. Pre-fill from last print session (in-memory). 5. Substitute into AST, then encode/print.

Missing parameter at print time: block print and highlight unresolved names.

---

Comments

<!-- this is not printed -->

HTML-style comments are stripped before parse.

---

Blank lines

A blank line (or whitespace-only line) inserts Feed(1) — same paper advance as @feed 1. On print this becomes ESC d 1 (reliable after raster graphics).

---

Error reporting

Parse errors include 1-based line and column. Typical causes:

  • Invalid barcode symbology
  • Invalid ${} name
  • Missing required directive argument
  • Unknown @time format

---

ESC/POS mapping (summary)

DSLESC/POS
# headingDouble width/height ON → text → OFF
## headingBold ON → text → OFF
- item / - nestedTop: ASCII * ; nested: indented - + hanging wrap
[] / [ ] / [x] / $[Name]ASCII [ ] / [x] (CP437-safe, inline)
:center:Justification center
@feed n / blank lineESC d n
@cutGS V partial cut
@barcodeGS k or GS v 0 raster
@qrGS ( k or GS v 0 raster
@imageGS v 0 raster bit image
@date / @timePlain text lines

Full command details: PRINTING.md.

---

Future extensions (not implemented)

  • @table for columnar line items
  • Numbered lists
  • Bluetooth / network printer transports
  • Syntax highlighting in the editor

---

Example document

# ${StoreName}
:center:

123 Main Street

---
@date
@time
Receipt: ${DocumentNumber}

- Item one
- Item two

:right:
TOTAL: ${GrandTotal}

@barcode CODE128 ${DocumentNumber}
@feed 2
@qr RASTER https://shop.example.com/r/${DocumentNumber} scale=4

Thank you!
@feed 4
@cut
Source
1# Document DSL specification
2
3A lightweight, markdown-inspired syntax for POS receipt templates. The DSL compiles to an AST and then to ESC/POS bytes or a screen preview.
4
5> **Implementation:** Parser in `src/dsl/parser.rs`. All constructs below are implemented unless listed under [Future extensions](#future-extensions-not-implemented).
6
7**Design goals:** easy to author, line-oriented, explicit about POS-specific features (barcodes, cut, feed), parameter-friendly.
8
9---
10
11## Document structure
12
13A document is a sequence of **blocks**. Blank lines become paper feeds (`@feed 1`). Blocks are not nested except for `${parameters}` inside text.
14
15---
16
17## Text blocks
18
19### Paragraphs
20
21Plain text (and `${parameters}`). One or more consecutive non-empty lines form a paragraph. There is **no** inline bold/italic/code markup. Consecutive spaces are preserved (not collapsed), including leading indentation, so authors can pad columns manually. Trailing spaces on a line are trimmed.
22
23```markdown
24Thank you for your purchase!
25Visit us again soon.
26Item 12.00
27 rightish note
28```
29
30### Headings
31
32```markdown
33# Store Name
34## Receipt
35```
36
37- `#` → large / double-height text (ESC/POS emphasis)
38- `##` → bold emphasis
39- Maximum two levels in v1
40
41### Alignment
42
43Prefix a paragraph with alignment markers:
44
45```markdown
46:center:
47Your Store Name
48
49:right:
50Total: ${Amount}
51```
52
53| Marker | Effect |
54|--------|--------|
55| `:left:` | Left (default) |
56| `:center:` | Centered |
57| `:right:` | Right-aligned |
58
59### Bullet lists
60
61```markdown
62- Groceries
63 - Milk
64 - Eggs
65- Wash car
66```
67
68Each line must start with optional indent (2 spaces or 1 tab per level) then `- `. Consecutive list lines form one list block. A blank line ends the list.
69
70| Depth | Printed bullet |
71|-------|----------------|
72| 0 (top) | `* ` |
73| ≥ 1 | indented `- ` |
74
75Wrapped lines use a hanging indent matching the bullet prefix width:
76
77```text
78* this is a
79 long line
80 - nested item that
81 wraps too
82```
83
84### Parameters
85
86| Syntax | Effect |
87|--------|--------|
88| `${Name}` | Text parameter placeholder (see Parameters) |
89| `$[Name]` | Bool checkbox parameter → `[ ]` / `[x]` |
90
91`${Name}` and `$[Name]` must not share the same name.
92
93### Checkboxes
94
95| Syntax | Printed result |
96|--------|----------------|
97| `[]` or `[ ]` | `[ ]` |
98| `[x]` / `[X]` | `[x]` |
99| `$[Name]` | `[ ]` or `[x]` from a bool parameter |
100
101ASCII brackets + `x` stay inline in the native CP437 font. Do not use Unicode ☐ / √ — many ESC/POS printers map those to CJK glyphs. `${Name}` and `$[Name]` cannot share a name.
102
103Parameters work inside any text, and inside barcode/QR/image directives (below).
104
105**Example:**
106
107```markdown
108Receipt #${DocumentNumber}
109Date: ${Date}
110
111$[Paid] Paid
112[] Pending
113
114Item: ${ItemName} ${LineTotal}
115```
116
117### Horizontal rule
118
119```markdown
120---
121```
122
123Renders a dashed or solid separator line across the print width.
124
125---
126
127## Directives (block-level)
128
129Directives start with `@` at the beginning of a line.
130
131### Feed (blank lines)
132
133```markdown
134@feed 3
135```
136
137Advances paper by *n* lines.
138
139### Cut
140
141```markdown
142@cut
143```
144
145Partial cut after content (if printer supports). Often placed at document end.
146
147### Image
148
149```markdown
150@image path/to/logo.png
151@image ${LogoPath} width=50%
152@image assets/qr-promo.png width=384
153```
154
155| Attribute | Description |
156|-----------|-------------|
157| `path` (positional) | File path relative to `.pos` file or absolute; may be `${Param}` |
158| `width` | `NN%` of printable width, or absolute dots (e.g. `384`) |
159| `align` | `left` (default), `center`, `right` |
160
161Images are converted to monochrome raster data and scaled to width (never wider than `printable_dots`).
162
163### Date / time
164
165```markdown
166@date
167@date DD-MM-YYYY
168@time
169@time 12h
170@time 24h
171```
172
173| Directive | Behaviour |
174|-----------|-----------|
175| `@date` | System regional short date (`%x`) |
176| `@date PATTERN` | Custom pattern (`YYYY`, `MM`, `DD`, …) |
177| `@time` / `@time 24h` | 24-hour `HH:MM` |
178| `@time 12h` | 12-hour with AM/PM |
179
180### Barcode
181
182```markdown
183@barcode CODE128 ${Sku}
184@barcode EAN13 ${EanCode} height=80
185@barcode RASTER CODE128 ${Sku}
186@barcode RASTER CODE93 ABC-1234
187```
188
189| Field | Values |
190|-------|--------|
191| Mode | Optional `RASTER` prefix; otherwise native ESC/POS |
192| Symbology | Native: `CODE128`, `EAN13`, `EAN8`, `CODE39`, `UPC-A`, `UPC-E`, `ITF`, `CODABAR`. Raster-only: `CODE93`, `CODE11` |
193| Data | Literal or `${Param}` |
194| `height` | Module / PNG height in dots (default: 64) |
195| `align` | `left`, `center`, `right` |
196| `show_text` | Native only: HRI below barcode |
197
198Raster barcodes stretch horizontally to full printable width.
199
200### QR code
201
202```markdown
203@qr https://example.com/receipt/${DocumentNumber}
204@qr ${PaymentUrl} size=6 ecc=H
205@qr RASTER ${PaymentUrl} version=5 scale=4 ecc=M
206```
207
208| Attribute | Native | Raster |
209|-----------|--------|--------|
210| Data | URL/text; may include `${Param}` | Same |
211| `size` | Module size 1–16 (default 4) | — |
212| `scale` | — | Pixels per module (default 4) |
213| `version` | — | QR version 1–40 (optional) |
214| `ecc` | `L`, `M`, `Q`, `H` (default `M`) | Same |
215| `align` | `left`, `center`, `right` | Same |
216
217---
218
219## Parameters
220
221### Syntax
222
223```
224${ParameterName} # text
225$[ParameterName] # boolean checkbox → [ ] / [x]
226```
227
228- **Names:** `[A-Za-z_][A-Za-z0-9_]*` (ASCII letters, digits, underscore)
229- **Case-sensitive:** `${DocNo}` and `${docno}` are different
230- **No mixed types:** the same name must not appear as both `${…}` and `$[…]`
231- **Uniqueness at input:** all occurrences of the same name share one value entered at print time
232
233### Where parameters are allowed
234
235| Location | Example |
236|----------|---------|
237| Text | `Order ${OrderId}` / `$[Paid] Paid` |
238| Barcode data | `@barcode CODE128 ${OrderId}` (`${…}` only) |
239| QR data | `@qr ${VerifyUrl}` (`${…}` only) |
240| Image path | `@image ${LogoPath}` (`${…}` only) |
241
242### Print-time behavior
243
2441. Before print, scan AST for all `${…}` and `$[…]` tokens.
2452. Deduplicate by name (kinds must not conflict — enforced at parse).
2463. Show dialog: text field per `${…}`, checkbox per `$[…]` (or require CLI `--set`).
2474. Bool values: `true`/`false` (also `1`/`0`, `yes`/`no`).
2484. Pre-fill from last print session (in-memory).
2495. Substitute into AST, then encode/print.
250
251Missing parameter at print time: block print and highlight unresolved names.
252
253---
254
255## Comments
256
257```markdown
258<!-- this is not printed -->
259```
260
261HTML-style comments are stripped before parse.
262
263---
264
265## Blank lines
266
267A blank line (or whitespace-only line) inserts `Feed(1)` — same paper advance as `@feed 1`. On print this becomes `ESC d 1` (reliable after raster graphics).
268
269---
270
271## Error reporting
272
273Parse errors include 1-based line and column. Typical causes:
274
275- Invalid barcode symbology
276- Invalid `${}` name
277- Missing required directive argument
278- Unknown `@time` format
279
280---
281
282## ESC/POS mapping (summary)
283
284| DSL | ESC/POS |
285|-----|---------|
286| `# heading` | Double width/height ON → text → OFF |
287| `## heading` | Bold ON → text → OFF |
288| `- item` / ` - nested` | Top: ASCII `* `; nested: indented `- ` + hanging wrap |
289| `[]` / `[ ]` / `[x]` / `$[Name]` | ASCII `[ ]` / `[x]` (CP437-safe, inline) |
290| `:center:` | Justification center |
291| `@feed n` / blank line | ESC d n |
292| `@cut` | GS V partial cut |
293| `@barcode` | GS k or GS v 0 raster |
294| `@qr` | GS ( k or GS v 0 raster |
295| `@image` | GS v 0 raster bit image |
296| `@date` / `@time` | Plain text lines |
297
298Full command details: [PRINTING.md](PRINTING.md).
299
300---
301
302## Future extensions (not implemented)
303
304- `@table` for columnar line items
305- Numbered lists
306- Bluetooth / network printer transports
307- Syntax highlighting in the editor
308
309---
310
311## Example document
312
313```markdown
314# ${StoreName}
315:center:
316
317123 Main Street
318
319---
320@date
321@time
322Receipt: ${DocumentNumber}
323
324- Item one
325- Item two
326
327:right:
328TOTAL: ${GrandTotal}
329
330@barcode CODE128 ${DocumentNumber}
331@feed 2
332@qr RASTER https://shop.example.com/r/${DocumentNumber} scale=4
333
334Thank you!
335@feed 4
336@cut
337```