BlueGit

timfa posprint

Files

posprint / docs / DSL-SPEC.md

DSL-SPEC.md

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

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