mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-16 13:59:54 +02:00
fix(packet): Grammar whitespace
Move populate into parser Co-authored-by: Reda Al Sulais <u.yokozuna@gmail.com>
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import type { Block, PacketDB, PacketData, Row } from './types.js';
|
import type { Block, PacketDB, PacketData, Row } from './types.js';
|
||||||
import type { PacketDiagramConfig } from '../../config.type.js';
|
import type { PacketDiagramConfig } from '../../config.type.js';
|
||||||
import { log } from '../../logger.js';
|
|
||||||
import DEFAULT_CONFIG from '../../defaultConfig.js';
|
import DEFAULT_CONFIG from '../../defaultConfig.js';
|
||||||
import { getConfig as commonGetConfig } from '../../config.js';
|
import { getConfig as commonGetConfig } from '../../config.js';
|
||||||
import { cleanAndMerge } from '../../utils.js';
|
import { cleanAndMerge } from '../../utils.js';
|
||||||
@@ -10,6 +9,7 @@ const defaultPacketData: PacketData = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let data: PacketData = structuredClone(defaultPacketData);
|
let data: PacketData = structuredClone(defaultPacketData);
|
||||||
|
|
||||||
export const DEFAULT_PACKET_CONFIG: Required<PacketDiagramConfig> = DEFAULT_CONFIG.packet;
|
export const DEFAULT_PACKET_CONFIG: Required<PacketDiagramConfig> = DEFAULT_CONFIG.packet;
|
||||||
|
|
||||||
export const getConfig = (): Required<PacketDiagramConfig> => {
|
export const getConfig = (): Required<PacketDiagramConfig> => {
|
||||||
@@ -25,71 +25,7 @@ export const getConfig = (): Required<PacketDiagramConfig> => {
|
|||||||
|
|
||||||
export const getPacket = (): Row[] => data.packet;
|
export const getPacket = (): Row[] => data.packet;
|
||||||
|
|
||||||
export const getNextFittingBlock = (
|
export const pushWord = (word: Row) => {
|
||||||
block: Block,
|
|
||||||
row: number,
|
|
||||||
bitsPerRow: number
|
|
||||||
): [Required<Block>, Block | undefined] => {
|
|
||||||
if (block.end === undefined) {
|
|
||||||
block.end = block.start;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (block.start > block.end) {
|
|
||||||
throw new Error(`Block start ${block.start} is greater than block end ${block.end}.`);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (block.end + 1 <= row * bitsPerRow) {
|
|
||||||
return [block as Required<Block>, undefined];
|
|
||||||
}
|
|
||||||
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
start: block.start,
|
|
||||||
end: row * bitsPerRow - 1,
|
|
||||||
label: block.label,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
start: row * bitsPerRow,
|
|
||||||
end: block.end,
|
|
||||||
label: block.label,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
};
|
|
||||||
|
|
||||||
export const populate = ({ blocks }: { blocks: Block[] }) => {
|
|
||||||
clear();
|
|
||||||
let lastByte = -1;
|
|
||||||
let word: Row = [];
|
|
||||||
let row = 1;
|
|
||||||
const { bitsPerRow } = getConfig();
|
|
||||||
for (let { start, end, label } of blocks) {
|
|
||||||
if (end && end < start) {
|
|
||||||
throw new Error(`Packet block ${start} - ${end} is invalid. End must be greater than start.`);
|
|
||||||
}
|
|
||||||
if (start != lastByte + 1) {
|
|
||||||
throw new Error(
|
|
||||||
`Packet block ${start} - ${end ?? start} is not contiguous. It should start from ${
|
|
||||||
lastByte + 1
|
|
||||||
}.`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
lastByte = end ?? start;
|
|
||||||
log.debug(`Packet block ${start} - ${lastByte} with label ${label}`);
|
|
||||||
|
|
||||||
while (word.length <= bitsPerRow + 1 && data.packet.length < 10_000) {
|
|
||||||
const [block, nextBlock] = getNextFittingBlock({ start, end, label }, row, bitsPerRow);
|
|
||||||
word.push(block);
|
|
||||||
if (block.end + 1 === row * bitsPerRow) {
|
|
||||||
data.packet.push(word);
|
|
||||||
word = [];
|
|
||||||
row++;
|
|
||||||
}
|
|
||||||
if (!nextBlock) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
({ start, end, label } = nextBlock);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (word.length > 0) {
|
if (word.length > 0) {
|
||||||
data.packet.push(word);
|
data.packet.push(word);
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,77 @@
|
|||||||
import type { Packet } from 'mermaid-parser';
|
import type { Packet } from 'mermaid-parser';
|
||||||
import type { ParserDefinition } from '../../diagram-api/types.js';
|
import type { ParserDefinition } from '../../diagram-api/types.js';
|
||||||
|
|
||||||
import { parse } from 'mermaid-parser';
|
import { parse } from 'mermaid-parser';
|
||||||
import { log } from '../../logger.js';
|
import { log } from '../../logger.js';
|
||||||
import { populate } from './db.js';
|
import type { Block, Row } from './types.js';
|
||||||
|
import { clear, getConfig, getPacket, pushWord } from './db.js';
|
||||||
|
|
||||||
|
const populate = ({ blocks }: { blocks: Block[] }) => {
|
||||||
|
clear();
|
||||||
|
let lastByte = -1;
|
||||||
|
let word: Row = [];
|
||||||
|
let row = 1;
|
||||||
|
const { bitsPerRow } = getConfig();
|
||||||
|
for (let { start, end, label } of blocks) {
|
||||||
|
if (end && end < start) {
|
||||||
|
throw new Error(`Packet block ${start} - ${end} is invalid. End must be greater than start.`);
|
||||||
|
}
|
||||||
|
if (start != lastByte + 1) {
|
||||||
|
throw new Error(
|
||||||
|
`Packet block ${start} - ${end ?? start} is not contiguous. It should start from ${
|
||||||
|
lastByte + 1
|
||||||
|
}.`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
lastByte = end ?? start;
|
||||||
|
log.debug(`Packet block ${start} - ${lastByte} with label ${label}`);
|
||||||
|
|
||||||
|
while (word.length <= bitsPerRow + 1 && getPacket().length < 10_000) {
|
||||||
|
const [block, nextBlock] = getNextFittingBlock({ start, end, label }, row, bitsPerRow);
|
||||||
|
word.push(block);
|
||||||
|
if (block.end + 1 === row * bitsPerRow) {
|
||||||
|
pushWord(word);
|
||||||
|
word = [];
|
||||||
|
row++;
|
||||||
|
}
|
||||||
|
if (!nextBlock) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
({ start, end, label } = nextBlock);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pushWord(word);
|
||||||
|
};
|
||||||
|
|
||||||
|
const getNextFittingBlock = (
|
||||||
|
block: Block,
|
||||||
|
row: number,
|
||||||
|
bitsPerRow: number
|
||||||
|
): [Required<Block>, Block | undefined] => {
|
||||||
|
if (block.end === undefined) {
|
||||||
|
block.end = block.start;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (block.start > block.end) {
|
||||||
|
throw new Error(`Block start ${block.start} is greater than block end ${block.end}.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (block.end + 1 <= row * bitsPerRow) {
|
||||||
|
return [block as Required<Block>, undefined];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
start: block.start,
|
||||||
|
end: row * bitsPerRow - 1,
|
||||||
|
label: block.label,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
start: row * bitsPerRow,
|
||||||
|
end: block.end,
|
||||||
|
label: block.label,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
};
|
||||||
|
|
||||||
export const parser: ParserDefinition = {
|
export const parser: ParserDefinition = {
|
||||||
parse: (input: string): void => {
|
parse: (input: string): void => {
|
||||||
|
@@ -15,6 +15,5 @@ PacketBlock:
|
|||||||
start=INT('-' end=INT)? ':' label=STRING NEWLINE+
|
start=INT('-' end=INT)? ':' label=STRING NEWLINE+
|
||||||
;
|
;
|
||||||
|
|
||||||
hidden terminal WS: /\s+/;
|
|
||||||
terminal INT returns number: /0|[1-9][0-9]*/;
|
terminal INT returns number: /0|[1-9][0-9]*/;
|
||||||
terminal STRING: /"[^"]*"|'[^']*'/;
|
terminal STRING: /"[^"]*"|'[^']*'/;
|
||||||
|
Reference in New Issue
Block a user