public static string AsString(this TokenSpan span, string source) { var sb = new StringBuilder(span.Size); var reader = new CharReader(source, span.Left, span.Size); switch (span.Token) { case Token.String: reader.MoveNext(); while (reader.MoveNext() && reader.Current != '\'') { sb.Append(reader.Current); } break; default: //should use reader here as above!!! //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! return(source.Substring(span.Left, span.Right - span.Left)); } return(sb.ToString()); }
public TokenSpan Emit(Token type) { var token = TokenSpan.Of(type, Left, Right); Left = Right; return(token); }
public static int AsInt(this TokenSpan span, string source) { span.Token.MustBe(Token.Number); var reader = new CharReader(source, span.Left, span.Size); int acc = 0; while (reader.MoveNext()) { acc *= 10; acc += reader.Current.DecodeAsDecimal(); } return(acc); }
public static bool Match(this TokenSpan span, string from, string comp) => string.Compare(from, span.Left, comp, 0, span.Right - span.Left) == 0;