@@ -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" ;
1719import 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} ;
0 commit comments