63 lines
1.2 KiB
C#
63 lines
1.2 KiB
C#
|
|
using System.Collections.Generic;
|
||
|
|
|
||
|
|
namespace Rokojori;
|
||
|
|
|
||
|
|
public abstract class Parser
|
||
|
|
{
|
||
|
|
public string source;
|
||
|
|
public List<LexerEvent> lexerEvents = [];
|
||
|
|
public ASTNode root;
|
||
|
|
public List<ParserPhase> phases = [];
|
||
|
|
public List<ParserMessage> messages = [];
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
public ParserPhase stoppedParsingPhase = null;
|
||
|
|
|
||
|
|
public virtual ExpressionParser GetExpressionParser()
|
||
|
|
{
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Parse( string source )
|
||
|
|
{
|
||
|
|
this.source = source;
|
||
|
|
|
||
|
|
foreach ( var p in phases )
|
||
|
|
{
|
||
|
|
p.Process( this );
|
||
|
|
|
||
|
|
if ( p.hasStopError )
|
||
|
|
{
|
||
|
|
stoppedParsingPhase = p;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
bool _hasErrors = false;
|
||
|
|
public bool hasErrors => _hasErrors;
|
||
|
|
|
||
|
|
public void AddError( string message, params TextSelection[] textSelections )
|
||
|
|
{
|
||
|
|
AddMessage( MessageType.Error, message, textSelections );
|
||
|
|
}
|
||
|
|
|
||
|
|
public void AddMessage( MessageType type, string message, params TextSelection[] textSelections )
|
||
|
|
{
|
||
|
|
var pm = new ParserMessage();
|
||
|
|
pm.message = Message.Create( type, message );
|
||
|
|
|
||
|
|
if ( textSelections != null && textSelections.Length > 0 )
|
||
|
|
{
|
||
|
|
pm.selections.AddRange( textSelections );
|
||
|
|
}
|
||
|
|
|
||
|
|
messages.Add( pm );
|
||
|
|
|
||
|
|
if ( type == MessageType.Error )
|
||
|
|
{
|
||
|
|
_hasErrors = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|