library-ts/browser/text/lexer/LexerEvent.ts

80 lines
1.6 KiB
TypeScript
Raw Permalink Normal View History

2025-03-08 08:16:54 +00:00
export class LexerEvent
{
static readonly LENGTH_ERROR_ID = -1;
static readonly LENGTH_DONE_ID = -2;
private _type:string;
get type(){ return this._type; }
private _offset:number;
get offset(){ return this._offset; }
private _length:number;
get length(){ return this._length; }
private _match:string = null;
get match(){ return this._match;}
get exclusiveEnd()
{
return this.offset + this.length;
}
get lastSourceIndex()
{
return this.exclusiveEnd - 1;
}
extendLength( num:number )
{
this._length += num;
}
constructor( type:string, offset:number, length:number )
{
this.set( type, offset, length );
}
set( type:string, offset:number, length:number )
{
this._type = type;
this._offset = offset;
this._length = length;
}
isType( type:string )
{
return this._type == type;
}
get isError(){ return this._length === LexerEvent.LENGTH_ERROR_ID; }
get isDone(){ return this._length === LexerEvent.LENGTH_DONE_ID; }
get isDoneOrError(){ return this.isDone || this.isError }
get end() { return this._offset + this._length; }
toString()
{
return "Token{ '" + this._type + "' (" + this._offset + "-" + this.end + ") }";
}
clone()
{
return new LexerEvent( this._type, this._offset, this._length );
}
getMatch( source:string )
{
if ( this._match )
{
return this._match;
}
this.setMatch( source.substring( this._offset, this.end ) );
return this._match;
}
setMatch( source:string )
{
this._match = source;
}
}