fix(packet): Grammar whitespace

Move populate into parser

Co-authored-by: Reda Al Sulais <u.yokozuna@gmail.com>
This commit is contained in:
Sidharth Vinod 2023-11-14 11:59:58 +05:30
parent 7c79bbd6b0
commit b88d1dfaa9
No known key found for this signature in database
GPG Key ID: FB5CCD378D3907CD
3 changed files with 72 additions and 69 deletions

View File

@ -1,6 +1,5 @@
import type { Block, PacketDB, PacketData, Row } from './types.js';
import type { PacketDiagramConfig } from '../../config.type.js';
import { log } from '../../logger.js';
import DEFAULT_CONFIG from '../../defaultConfig.js';
import { getConfig as commonGetConfig } from '../../config.js';
import { cleanAndMerge } from '../../utils.js';
@ -10,6 +9,7 @@ const defaultPacketData: PacketData = {
};
let data: PacketData = structuredClone(defaultPacketData);
export const DEFAULT_PACKET_CONFIG: Required<PacketDiagramConfig> = DEFAULT_CONFIG.packet;
export const getConfig = (): Required<PacketDiagramConfig> => {
@ -25,71 +25,7 @@ export const getConfig = (): Required<PacketDiagramConfig> => {
export const getPacket = (): Row[] => data.packet;
export 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 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);
}
}
export const pushWord = (word: Row) => {
if (word.length > 0) {
data.packet.push(word);
}

View File

@ -1,9 +1,77 @@
import type { Packet } from 'mermaid-parser';
import type { ParserDefinition } from '../../diagram-api/types.js';
import { parse } from 'mermaid-parser';
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 = {
parse: (input: string): void => {

View File

@ -15,6 +15,5 @@ PacketBlock:
start=INT('-' end=INT)? ':' label=STRING NEWLINE+
;
hidden terminal WS: /\s+/;
terminal INT returns number: /0|[1-9][0-9]*/;
terminal STRING: /"[^"]*"|'[^']*'/;