75 lines
1.8 KiB
C#
75 lines
1.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Rokojori
|
|
{
|
|
public class LexerMatcher
|
|
{
|
|
string _mode;
|
|
public string mode => _mode;
|
|
|
|
string _type;
|
|
public string type => _type;
|
|
|
|
string _nextMode;
|
|
public string nextMode => _nextMode;
|
|
|
|
Regex _matcher;
|
|
Regex matcher
|
|
{
|
|
get { return this._matcher; }
|
|
}
|
|
|
|
public bool Matches( LexerEvent le )
|
|
{
|
|
return type == le.type;
|
|
}
|
|
|
|
public LexerMatcher( string type, Regex matcher, string mode = "", string nextMode = "" )
|
|
{
|
|
_type = type;
|
|
_mode = mode;
|
|
_nextMode = nextMode;
|
|
_matcher = RegexUtility.MakeSticky( matcher );
|
|
}
|
|
|
|
public LexerMatcher( string type, string matcher, string mode = "", string nextMode = "" )
|
|
{
|
|
_type = type;
|
|
_mode = mode;
|
|
_nextMode = nextMode;
|
|
_matcher = RegexUtility.MakeSticky( new Regex( matcher ) );
|
|
}
|
|
|
|
public static LexerMatcher CreateExtended( string type, string matcher, string mode = "", string nextMode = "" )
|
|
{
|
|
return new LexerMatcher( type, RegexExtensions.Extend( matcher ), mode, nextMode );
|
|
}
|
|
|
|
public LexerMatcher CloneWithMode( string mode, string nextMode )
|
|
{
|
|
return new LexerMatcher( _type, _matcher, mode, nextMode );
|
|
}
|
|
|
|
public int MatchLength( string source, int offset )
|
|
{
|
|
var match = matcher.Match( source, offset );
|
|
|
|
if ( match.Success && match.Index != offset )
|
|
{
|
|
var message = "Illegal match index! Not a sticky matcher: " + matcher + ".";
|
|
message += "Offset: " + offset + " Matched Index: " + match.Index ;
|
|
throw new System.Exception( message );
|
|
}
|
|
|
|
if ( match.Success )
|
|
{
|
|
return match.Groups[ 0 ].Length;
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
}
|
|
} |