BlueGit

timfa posprint

Files

posprint / docs / DSL-SPEC.md

DSL-SPEC.md

DSL-SPEC.md 7.3 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.

Thank you for your purchase!
Visit us again soon.

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

- Milk
- Bread
- Eggs

Each line must start with - (dash + space). Consecutive list lines form one list block. A blank line ends the list.

Wrapped lines use a hanging indent:

* this is a
  long line

Parameters

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

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

Checkboxes

SyntaxPrinted result
[] or [ ][ ]
[x] / [X] / [√][√] (CP437 byte 0xFB)
$[Name][ ] or [√] from a bool parameter

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