parseLiteral function

ValueIndex<LiteralNode>? parseLiteral(
  1. String input,
  2. List<Token> tokenList,
  3. int index,
  4. Settings settings,
)

Implementation

ValueIndex<LiteralNode>? parseLiteral(
    String input, List<Token> tokenList, int index, Settings settings) {
  // literal: STRING | NUMBER | TRUE | FALSE | NULL
  final token = tokenList[index];
  var value = null;

  switch (token.type) {
    case TokenType.STRING:
      {
        value = parseString(safeSubstring(
            input, token.loc!.start.offset + 1, token.loc!.end.offset - 1));
        break;
      }
    case TokenType.NUMBER:
      {
        if (token.value != null) {
          value = int.tryParse(token.value!) ?? null;
          if (value == null) {
            value = double.tryParse(token.value!) ?? null;
          }
        }
        break;
      }
    case TokenType.TRUE:
      {
        value = true;
        break;
      }
    case TokenType.FALSE:
      {
        value = false;
        break;
      }
    case TokenType.NULL:
      {
        value = null;
        break;
      }
    default:
      {
        return null;
      }
  }

  final literal = new LiteralNode(value, token.value);
  if (settings.loc != null) {
    literal.loc = token.loc;
  }
  return new ValueIndex(literal, index + 1);
}