/** * Number parser generates token of type TokenType.Number. * If successful, 'true' is returned, 'result' contains token, 'next' contains further index information. * If not successful, 'false' is returned, other values can be arbitrary. */ public static bool ParseNumber(string source, int index, out NumberToken result, out int next) { result = null; next = -1; var index2 = index; if (index2 >= source.Length) { return(false); } // parse sign (optional) if (source[index] == '+' || source[index] == '-') { index2++; } if (index2 >= source.Length) { return(false); } // parse unsigned number var ok = ParseUnsignedNumber(source, index2, out result, out var nnext); // process result if OK if (ok) { next = nnext; result._from = index; } return(ok); }
private static bool ParseUnsignedNumber(string source, int index, out NumberToken result, out int next) { if (ParseUnsignedReal(source, index, out result, out next)) { return(AllowedAfterParsedNumber(source, result)); } if (ParseUnsignedInteger(source, index, out result, out next)) { return(AllowedAfterParsedNumber(source, result)); } return(false); }
private static bool AllowedAfterParsedNumber(string source, NumberToken result) { if (result == null) { return(false); } // check last character if any if (result._to < source.Length && ( Char.IsLetterOrDigit(source[result._to]) || source[result._to] == '_') ) { return(false); } return(true); }
private static bool ParseUnsignedReal(string source, int index, out NumberToken result, out int next) { // parse digit sequence var ok = ParseDigitSequence(source, index, DEC, out result, out next); if (!ok) { return(ok); } var nextIndex = result._to; // and maybe .digit sequence if (nextIndex >= source.Length) { return(ok); } if (source[nextIndex] == '.') { var ok2 = ParseDigitSequence(source, nextIndex + 1, DEC, out var result2, out var nnext); if (ok2) { next = nnext; nextIndex = result2._to; } else { return(false); } } // and maybe scale factor if (nextIndex < source.Length && (source[nextIndex] == 'e' || source[nextIndex] == 'E')) { if (ParseScaleFactor(source, nextIndex, out var result3, out var nnnext)) { next = nnnext; } else { return(false); } }
private static bool ParseControlString(string source, int index, out StringToken result, out int next) { if (index >= source.Length || source[index] != '#') { result = null; next = -1; return(false); } var ok = NumberToken.ParseUnsignedInteger(source, index + 1, out var numberToken, out var nnext); if (ok) { result = new StringToken(); result._from = index; result._to = nnext; next = nnext; return(true); } result = null; next = -1; return(false); }