chore: Update bits syntax to use +<count>

This commit is contained in:
Sidharth Vinod
2025-04-15 12:14:06 +05:30
parent d7730aba46
commit ea321168bd
5 changed files with 56 additions and 38 deletions

View File

@@ -25,14 +25,15 @@ start-end: "Block name" %% Multi-bit blocks
### Bits Syntax (v\<MERMAID_RELEASE_VERSION>+) ### Bits Syntax (v\<MERMAID_RELEASE_VERSION>+)
Using start and end bit counts can be difficult, especially when modifying a design. For this we add a bit count field, which starts from the end of the previous field automagically. Use `bit` or `bits` interchangeably to set the number of bits, thus: Using start and end bit counts can be difficult, especially when modifying a design. For this we add a bit count field, which starts from the end of the previous field automagically. Use `+<count>` to set the number of bits, thus:
````md ```md
packet-beta packet-beta
1bit: "Block name" %% Single-bit block +1: "Block name" %% Single-bit block
8bits: "Block name" %% 8-bit block +8: "Block name" %% 8-bit block
9-15: "Manually set start and end, it's fine to mix and match" 9-15: "Manually set start and end, it's fine to mix and match"
... More Fields ... ... More Fields ...
```
## Examples ## Examples
@@ -41,8 +42,32 @@ packet-beta
title: "TCP Packet" title: "TCP Packet"
--- ---
packet-beta packet-beta
16bits: "Source Port" 0-15: "Source Port"
16bits: "Destination Port" 16-31: "Destination Port"
32-63: "Sequence Number"
64-95: "Acknowledgment Number"
96-99: "Data Offset"
100-105: "Reserved"
106: "URG"
107: "ACK"
108: "PSH"
109: "RST"
110: "SYN"
111: "FIN"
112-127: "Window"
128-143: "Checksum"
144-159: "Urgent Pointer"
160-191: "(Options and Padding)"
192-255: "Data (variable length)"
```
```mermaid
---
title: "TCP Packet"
---
packet-beta
0-15: "Source Port"
16-31: "Destination Port"
32-63: "Sequence Number" 32-63: "Sequence Number"
64-95: "Acknowledgment Number" 64-95: "Acknowledgment Number"
96-99: "Data Offset" 96-99: "Data Offset"
@@ -59,13 +84,12 @@ packet-beta
160-191: "(Options and Padding)" 160-191: "(Options and Padding)"
192-255: "Data (variable length)" 192-255: "Data (variable length)"
``` ```
````
```mermaid-example ```mermaid-example
packet-beta packet-beta
title UDP Packet title UDP Packet
0-15: "Source Port" +16: "Source Port"
16-31: "Destination Port" +16: "Destination Port"
32-47: "Length" 32-47: "Length"
48-63: "Checksum" 48-63: "Checksum"
64-95: "Data (variable length)" 64-95: "Data (variable length)"
@@ -74,8 +98,8 @@ title UDP Packet
```mermaid ```mermaid
packet-beta packet-beta
title UDP Packet title UDP Packet
0-15: "Source Port" +16: "Source Port"
16-31: "Destination Port" +16: "Destination Port"
32-47: "Length" 32-47: "Length"
48-63: "Checksum" 48-63: "Checksum"
64-95: "Data (variable length)" 64-95: "Data (variable length)"

View File

@@ -68,8 +68,8 @@ describe('packet diagrams', () => {
it('should handle bit counts', async () => { it('should handle bit counts', async () => {
const str = `packet-beta const str = `packet-beta
8bits: "byte" +8: "byte"
16bits: "word" +16: "word"
`; `;
await expect(parser.parse(str)).resolves.not.toThrow(); await expect(parser.parse(str)).resolves.not.toThrow();
expect(getPacket()).toMatchInlineSnapshot(` expect(getPacket()).toMatchInlineSnapshot(`
@@ -94,8 +94,8 @@ describe('packet diagrams', () => {
it('should handle bit counts with bit or bits', async () => { it('should handle bit counts with bit or bits', async () => {
const str = `packet-beta const str = `packet-beta
8bit: "byte" +8: "byte"
16bits: "word" +16: "word"
`; `;
await expect(parser.parse(str)).resolves.not.toThrow(); await expect(parser.parse(str)).resolves.not.toThrow();
expect(getPacket()).toMatchInlineSnapshot(` expect(getPacket()).toMatchInlineSnapshot(`
@@ -206,7 +206,7 @@ describe('packet diagrams', () => {
it('should throw error if numbers are not continuous with bit counts', async () => { it('should throw error if numbers are not continuous with bit counts', async () => {
const str = `packet-beta const str = `packet-beta
16bits: "test" +16: "test"
18-20: "error" 18-20: "error"
`; `;
await expect(parser.parse(str)).rejects.toThrowErrorMatchingInlineSnapshot( await expect(parser.parse(str)).rejects.toThrowErrorMatchingInlineSnapshot(
@@ -226,7 +226,7 @@ describe('packet diagrams', () => {
it('should throw error if numbers are not continuous for single packets with bit counts', async () => { it('should throw error if numbers are not continuous for single packets with bit counts', async () => {
const str = `packet-beta const str = `packet-beta
16 bits: "test" +16: "test"
18: "error" 18: "error"
`; `;
await expect(parser.parse(str)).rejects.toThrowErrorMatchingInlineSnapshot( await expect(parser.parse(str)).rejects.toThrowErrorMatchingInlineSnapshot(
@@ -257,7 +257,7 @@ describe('packet diagrams', () => {
it('should throw error if bit count is 0', async () => { it('should throw error if bit count is 0', async () => {
const str = `packet-beta const str = `packet-beta
0bits: "test" +0: "test"
`; `;
await expect(parser.parse(str)).rejects.toThrowErrorMatchingInlineSnapshot( await expect(parser.parse(str)).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: Packet block 0 is invalid. Cannot have a zero bit field.]` `[Error: Packet block 0 is invalid. Cannot have a zero bit field.]`

View File

@@ -19,9 +19,7 @@ const populate = (ast: Packet) => {
if (start !== undefined && end !== undefined && end < start) { if (start !== undefined && end !== undefined && end < start) {
throw new Error(`Packet block ${start} - ${end} is invalid. End must be greater than start.`); throw new Error(`Packet block ${start} - ${end} is invalid. End must be greater than start.`);
} }
if (start == undefined) { start ??= lastBit + 1;
start = lastBit + 1;
}
if (start !== lastBit + 1) { if (start !== lastBit + 1) {
throw new Error( throw new Error(
`Packet block ${start} - ${end ?? start} is not contiguous. It should start from ${ `Packet block ${start} - ${end ?? start} is not contiguous. It should start from ${
@@ -32,12 +30,8 @@ const populate = (ast: Packet) => {
if (bits === 0) { if (bits === 0) {
throw new Error(`Packet block ${start} is invalid. Cannot have a zero bit field.`); throw new Error(`Packet block ${start} is invalid. Cannot have a zero bit field.`);
} }
if (end == undefined) { end ??= start + (bits ?? 1) - 1;
end = start + (bits ?? 1) - 1; bits ??= end - start + 1;
}
if (bits == undefined) {
bits = end - start + 1;
}
lastBit = end; lastBit = end;
log.debug(`Packet block ${start} - ${lastBit} with label ${label}`); log.debug(`Packet block ${start} - ${lastBit} with label ${label}`);

View File

@@ -19,14 +19,15 @@ start-end: "Block name" %% Multi-bit blocks
### Bits Syntax (v<MERMAID_RELEASE_VERSION>+) ### Bits Syntax (v<MERMAID_RELEASE_VERSION>+)
Using start and end bit counts can be difficult, especially when modifying a design. For this we add a bit count field, which starts from the end of the previous field automagically. Use `bit` or `bits` interchangeably to set the number of bits, thus: Using start and end bit counts can be difficult, especially when modifying a design. For this we add a bit count field, which starts from the end of the previous field automagically. Use `+<count>` to set the number of bits, thus:
````md ```md
packet-beta packet-beta
1bit: "Block name" %% Single-bit block +1: "Block name" %% Single-bit block
8bits: "Block name" %% 8-bit block +8: "Block name" %% 8-bit block
9-15: "Manually set start and end, it's fine to mix and match" 9-15: "Manually set start and end, it's fine to mix and match"
... More Fields ... ... More Fields ...
```
## Examples ## Examples
@@ -35,8 +36,8 @@ packet-beta
title: "TCP Packet" title: "TCP Packet"
--- ---
packet-beta packet-beta
16bits: "Source Port" 0-15: "Source Port"
16bits: "Destination Port" 16-31: "Destination Port"
32-63: "Sequence Number" 32-63: "Sequence Number"
64-95: "Acknowledgment Number" 64-95: "Acknowledgment Number"
96-99: "Data Offset" 96-99: "Data Offset"
@@ -53,13 +54,12 @@ packet-beta
160-191: "(Options and Padding)" 160-191: "(Options and Padding)"
192-255: "Data (variable length)" 192-255: "Data (variable length)"
``` ```
````
```mermaid-example ```mermaid-example
packet-beta packet-beta
title UDP Packet title UDP Packet
0-15: "Source Port" +16: "Source Port"
16-31: "Destination Port" +16: "Destination Port"
32-47: "Length" 32-47: "Length"
48-63: "Checksum" 48-63: "Checksum"
64-95: "Data (variable length)" 64-95: "Data (variable length)"

View File

@@ -13,8 +13,8 @@ entry Packet:
PacketBlock: PacketBlock:
( (
start=INT('-' (end=INT | bits=INT'bit''s'?))? start=INT('-' end=INT)?
| bits=INT'bit''s'? | '+' bits=INT
) )
':' label=STRING ':' label=STRING
EOL EOL