import { MultiString } from "../../../i18n/MultiString"; import { MessageType, MessageTypes } from "../../../messages/MessageType"; import { ParserMessage } from "./ParserMessage"; import { ParserPhase } from "./ParserPhase"; import { SourceInfo } from "./SourceInfo"; export abstract class Parser { _phases:ParserPhase[] = []; _source:string; get source(){ return this._source; } _messages:ParserMessage[] = []; get messages():ParserMessage[]{ return this._messages } _add( phase:ParserPhase ) { 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" ); } }