library-ts/browser/text/lexer/parsing/Parser.ts

74 lines
1.8 KiB
TypeScript
Raw Permalink Normal View History

2025-03-08 08:16:54 +00:00
import { MultiString } from "../../../i18n/MultiString";
import { MessageType, MessageTypes } from "../../../messages/MessageType";
2025-03-08 12:22:18 +00:00
2025-03-08 08:16:54 +00:00
import { ParserMessage } from "./ParserMessage";
import { ParserPhase } from "./ParserPhase";
import { SourceInfo } from "./SourceInfo";
export abstract class Parser
{
_phases:ParserPhase<any>[] = [];
_source:string;
get source(){ return this._source; }
_messages:ParserMessage[] = [];
get messages():ParserMessage[]{ return this._messages }
_add( phase:ParserPhase<any> )
{
this._phases.push( phase );
}
get hasErrors()
{
return this._messages.find( m => MessageTypes.Error == m.type );
}
_message( type:MessageType, content:string|MultiString, sourceInfo:SourceInfo )
{
this._messages.push( ParserMessage.create( type, MultiString.resolve( content ), sourceInfo ) );
}
verbose( content:string|MultiString, sourceInfo?:SourceInfo )
{
this._message( MessageTypes.Verbose, content, sourceInfo );
}
info( content:string|MultiString, sourceInfo?:SourceInfo )
{
this._message( MessageTypes.Info, content, sourceInfo );
}
warn( content:string|MultiString, sourceInfo?:SourceInfo )
{
this._message( MessageTypes.Warning, content, sourceInfo );
}
error( content:string|MultiString, sourceInfo?:SourceInfo )
{
this._message( MessageTypes.Error, content, sourceInfo );
}
parse( source:string )
{
this._source = source;
for ( let phase of this._phases )
{
phase.reset();
}
for ( let phase of this._phases )
{
phase.process();
if ( this.hasErrors )
{
this.error( "Stopped at: " + phase.phaseInfo );
return;
}
}
this.info( "Parsed successfully" );
}
}