aboutsummaryrefslogtreecommitdiffstats
path: root/lib/libtmi8/include/tmi8/kv1_lexer.hpp
blob: 38f2ece464ed9d1386614a0656b546b707176f5a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// vim:set sw=2 ts=2 sts et:
//
// Copyright 2024 Rutger Broekhoff. Licensed under the EUPL.

#ifndef OEUF_LIBTMI8_KV1_LEXER_HPP
#define OEUF_LIBTMI8_KV1_LEXER_HPP

#include <cstdint>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <variant>

enum Kv1TokenType {
  KV1_TOKEN_CELL,
  KV1_TOKEN_ROW_END,
};
struct Kv1Token { Kv1TokenType type; std::string data; };

struct Kv1Lexer {
  std::vector<std::string> errors;
  std::vector<Kv1Token> tokens;
  
  explicit Kv1Lexer(std::string_view input);

  void lex();

 private:
  // Does not eat newline character.
  void eatRestOfLine();
  void lexOptionalHeader();
  void lexOptionalComment();
  
  static bool isWhitespace(int c);

  void readQuotedColumn();
  void readUnquotedColumn();
  void lexRow();
  // Returns true when a line ending was consumed.
  bool eatWhitespace();
  
  std::string_view input;
  std::string_view slice;
  std::string colbuf;
};

#endif // OEUF_LIBTMI8_KV1_LEXER_HPP