1- import { type ErrorLevel , ErrorType , MetaBindError } from '../utils/errors/MetaBindErrors' ;
1+ import { ErrorLevel , ErrorType , MetaBindError } from '../utils/errors/MetaBindErrors' ;
22import { type ParseFailure , type ParsingRange } from '@lemons_dev/parsinom/lib/HelperTypes' ;
3+ import { type Parser } from '@lemons_dev/parsinom/lib/Parser' ;
4+
5+ export function runParser < T > ( parser : Parser < T > , str : string ) : T {
6+ const result = parser . tryParse ( str ) ;
7+ if ( result . success ) {
8+ return result . value ;
9+ } else {
10+ throw new ParsingError ( ErrorLevel . ERROR , 'parsiNOM parser' , str , result ) ;
11+ }
12+ }
313
414export class ParsingError extends MetaBindError {
515 str : string ;
616 parseFailure : ParseFailure ;
717 source : string ;
818
919 constructor ( errorLevel : ErrorLevel , source : string , str : string , parseFailure : ParseFailure ) {
10- super ( { errorLevel : errorLevel , effect : 'failed to parse' , cause : 'expected' + parseFailure . expected . join ( ' or ' ) , context : { } } ) ;
20+ super ( {
21+ errorLevel : errorLevel ,
22+ effect : 'failed to parse' ,
23+ cause : `expected ${ parseFailure . expected . sort ( ) . join ( ' or ' ) } ` ,
24+ } ) ;
1125
1226 this . str = str ;
1327 this . parseFailure = parseFailure ;
@@ -17,7 +31,7 @@ export class ParsingError extends MetaBindError {
1731 }
1832
1933 public getErrorType ( ) : ErrorType {
20- return ErrorType . PARSING ;
34+ return ErrorType . PARSINOM ;
2135 }
2236
2337 protected updateMessage2 ( ) : void {
@@ -31,8 +45,17 @@ export class ParsingError extends MetaBindError {
3145 const failedLine = lines [ this . parseFailure . furthest . line - 1 ] ; // line is a one based index
3246
3347 const linePrefix = `${ this . parseFailure . furthest . line } | ` ;
34- this . message += `\n${ linePrefix } ${ failedLine } ` ;
35- this . message += `\n${ ' ' . repeat ( this . parseFailure . furthest . column - 1 + linePrefix . length ) } ^ (${ this . cause } )\n` ;
48+ this . positionContext = `${ linePrefix } ${ failedLine } ` ;
49+ this . positionContext += `\n${ this . getUnderline ( linePrefix . length ) } \n` ;
50+
51+ this . message += '\n' + this . positionContext ;
52+ }
53+
54+ private getUnderline ( offset : number ) : string {
55+ const spacing = ' ' . repeat ( this . parseFailure . furthest . column + offset - 1 ) ;
56+ const underline = `^ (${ this . cause } )` ;
57+
58+ return spacing + underline ;
3659 }
3760}
3861
@@ -41,8 +64,8 @@ export class ParsingValidationError extends MetaBindError {
4164 position ?: ParsingRange ;
4265 source : string ;
4366
44- constructor ( errorLevel : ErrorLevel , source : string , cause : string , str ?: string , position ?: ParsingRange ) {
45- super ( { errorLevel : errorLevel , effect : 'failed to validate parser result' , cause : cause } ) ;
67+ constructor ( errorLevel : ErrorLevel , source : string , cause : string , str ?: string , position ?: ParsingRange , docs ?: string [ ] ) {
68+ super ( { errorLevel : errorLevel , effect : 'failed to validate parser result' , cause : cause , docs : docs } ) ;
4669
4770 this . str = str ;
4871 this . position = position ;
0 commit comments