Skip to content

Commit 54c7b15

Browse files
authored
feat: migration to use parser based on lezer (#34)
1 parent 52fe399 commit 54c7b15

32 files changed

Lines changed: 173 additions & 2520 deletions

bun.lock

Lines changed: 15 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

functional_tests/features/executable.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Feature: Executable
88
Scenario: Validate invalid SDL
99
When the executable is launched with arguments "validate --input features/sample_specifications/invalid.sdl"
1010
Then the executable should complete with exit code 0
11-
And the executable should have output "Unable to consume token: _packet"
11+
And the executable should have output "SYNTACTIC ERROR: Missing expected token => { row: 6, column: 17, position: 206 }"
1212

1313
Scenario: Validate valid SDL
1414
When the executable is launched with arguments "validate -i features/sample_specifications/valid.sdl"
@@ -18,7 +18,7 @@ Feature: Executable
1818
Scenario: Prettify invalid SDL
1919
When the executable is launched with arguments "prettify --input features/sample_specifications/invalid.sdl"
2020
Then the executable should complete with exit code 0
21-
And the executable should have output "Unable to consume token: _packet"
21+
And the executable should have output "SYNTACTIC ERROR: Parse error => { row: 6, column: 17, position: 206 }"
2222

2323
Scenario: Prettify valid SDL
2424
When the executable is launched with arguments "prettify -i features/sample_specifications/valid.sdl"
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
class transport _packet {
1+
// repeated packets in bitstream
2+
class transport_packet {
23
unsigned int(8) sync_byte = 0x47;
34
bit(1) transport_error_indicator;
45
bit(1) payload_unit_start_indicator;
5-
bit(1) transport_priority;
6+
bit transport_priority;
67
unsigned int(13) PID;
78
bit(2) transport_scrambling_control;
89
bit(2) adaptation_field_control;
910

10-
computed unsigned int N = 184;
11+
unsigned int N = 184;
1112

1213
if (adaptation_field_control == 0b10 || adaptation_field_control == 0b11) {
13-
adaptation_field data;
14+
adaptation_field data; // previously declared class
15+
16+
// N is 184 minus the number of bytes in the adaptation_field
1417
N = N - 1 - data.adaptation_field_length;
1518
}
1619

17-
if (adaptation_field_control == 0b01 || adaptation_field_control == 0b11) {
20+
if (adaptation_ field_control == 0b01 || adaptation_field_control == 0b11) {
1821
bit(8) data_byte[N];
1922
}
2023
}
24+
// until EOF

nick.sdl

Lines changed: 0 additions & 109 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@
2929
},
3030
"dependencies": {
3131
"@flowscripter/dynamic-cli-framework": "^1.1.6",
32-
"@flowscripter/mpeg-sdl-parser": "^1.1.13"
32+
"@flowscripter/mpeg-sdl-parser": "^1.2.4"
3333
}
3434
}

src/commands/prettify/prettify.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ import {
1212
type PrinterService,
1313
type SubCommand,
1414
} from "@flowscripter/dynamic-cli-framework";
15-
import prettierPluginSdl from "../../prettier/prettierPluginSdl";
1615
import sdlHighlight from "../../util/sdl_highlight";
1716
import outputError from "../../util/output_error";
17+
import prettierPluginSdl from "@flowscripter/mpeg-sdl-parser/src/prettier/prettierPluginSdl";
18+
import { SyntacticParseError } from "@flowscripter/mpeg-sdl-parser";
1819

1920
/**
2021
* Command to parse and prettify an SDL file.
@@ -62,8 +63,8 @@ const prettify: SubCommand = {
6263
`SDL file ${inputSdlFilePath} could not be parsed\n`,
6364
Icon.FAILURE,
6465
);
65-
if (error instanceof Error) {
66-
await outputError(error, sdlSpecification, context);
66+
if (error instanceof SyntacticParseError) {
67+
await outputError(error, context);
6768
} else {
6869
await printerService.warn(String(error) + "\n");
6970
}
@@ -72,7 +73,7 @@ const prettify: SubCommand = {
7273
}
7374

7475
await printerService.print(
75-
sdlHighlight(prettifiedSdlSpecification, context),
76+
sdlHighlight(prettifiedSdlSpecification + "\n", context),
7677
);
7778
},
7879
};

src/commands/validate/validate.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import {
1010
PRINTER_SERVICE_ID,
1111
type PrinterService,
1212
type SubCommand,
13-
SYNTAX_HIGHLIGHTER_SERVICE_ID,
14-
type SyntaxHighlighterService,
1513
} from "@flowscripter/dynamic-cli-framework";
16-
import { Parser } from "@flowscripter/mpeg-sdl-parser";
14+
import {
15+
collateParseErrors,
16+
createLenientSdlParser,
17+
SdlStringInput,
18+
} from "@flowscripter/mpeg-sdl-parser";
1719
import outputError from "../../util/output_error";
1820

1921
/**
@@ -38,43 +40,45 @@ const validate: SubCommand = {
3840
const printerService = context.getServiceById(
3941
PRINTER_SERVICE_ID,
4042
) as PrinterService;
41-
const parser = new Parser();
43+
const parser = await createLenientSdlParser();
4244

4345
const inputSdlFilePath = argumentValues.input as string;
4446

4547
const sdlSpecification = await fs.readFile(
4648
path.join(process.cwd(), inputSdlFilePath),
4749
).then((buffer) => buffer.toString());
4850

49-
try {
50-
const parsedSdlSpecification = parser.parse(sdlSpecification);
51+
// Prepare the SDL input
52+
const sdlStringInput = new SdlStringInput(sdlSpecification);
53+
54+
const sdlParseTree = parser.parse(sdlStringInput);
5155

52-
if (printerService.getLevel() === Level.DEBUG) {
53-
const highlighterService = context.getServiceById(
54-
SYNTAX_HIGHLIGHTER_SERVICE_ID,
55-
) as SyntaxHighlighterService;
56+
if (printerService.getLevel() === Level.DEBUG) {
57+
// Traverse and print the parse tree
58+
const cursor = sdlParseTree.cursor();
59+
do {
5660
await printerService.debug(
57-
highlighterService.highlight(
58-
JSON.stringify(parsedSdlSpecification, undefined, 2),
59-
"json",
60-
) + "\n",
61+
`Node ${cursor.name} from ${cursor.from} to ${cursor.to}\n`,
6162
);
62-
}
63+
} while (cursor.next());
64+
}
6365

64-
await printerService.info(
65-
`SDL file ${inputSdlFilePath} is valid\n`,
66-
Icon.SUCCESS,
67-
);
68-
} catch (error) {
66+
// Check for any parse errors
67+
const parseErrors = collateParseErrors(sdlParseTree, sdlStringInput);
68+
69+
if (parseErrors.length > 0) {
6970
await printerService.error(
7071
`SDL file ${inputSdlFilePath} is invalid\n`,
7172
Icon.FAILURE,
7273
);
73-
if (error instanceof Error) {
74-
await outputError(error, sdlSpecification, context);
75-
} else {
76-
await printerService.warn(String(error) + "\n");
74+
for (const error of parseErrors) {
75+
await outputError(error, context);
7776
}
77+
} else {
78+
await printerService.info(
79+
`SDL file ${inputSdlFilePath} is valid\n`,
80+
Icon.SUCCESS,
81+
);
7882
}
7983
},
8084
};

src/prettier/prettierPluginSdl.ts

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)