BlueGit

timfa posprint

Files

posprint / docs / DSL-SPEC.md

DSL-SPEC.md

DSL-SPEC.md 7.7 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) so authors can pad columns manually.

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

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