Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarShellParser.hpp
blob: b8e77d0445be42ed2cfb6d5cc9eff6d437edbd2c (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#pragma once

#include "StarString.hpp"
#include "StarEncode.hpp"
#include "StarBytes.hpp"
#include "StarFormat.hpp"

namespace Star {

// Currently the specification of the "language" is incredibly simple The only
// thing we process are quoted strings and backslashes Backslashes function as
// a useful subset of C++ This means: Newline: \n Tab: \t Backslash: \\ Single
// Quote: \' Double Quote: \" Null: \0 Space: "\ " (without quotes ofc, not
// actually C++) Also \v \b \a \f \r Plus Unicode \uxxxx Not implemented octal
// and hexadecimal, because it's possible to construct invalid unicode code
// points using them

STAR_EXCEPTION(ShellParsingException, StarException);

class ShellParser {
public:
  ShellParser();
  typedef String::Char Char;

  enum class TokenType {
    Word,
    // TODO: braces, brackets, actual shell stuff

  };

  struct Token {
    TokenType type;
    String token;
  };

  List<Token> tokenize(String const& command);
  StringList tokenizeToStringList(String const& command);

private:
  void init(String const& command);

  String word();
  Char parseBackslash();
  Char parseUnicodeEscapeSequence(Maybe<Char> previousCodepoint = {});

  bool isSpace(Char letter) const;
  bool isQuote(Char letter) const;

  bool inQuotedString() const;
  bool notDone() const;

  Maybe<Char> current() const;
  Maybe<Char> next();
  Maybe<Char> previous();

  String::const_iterator m_begin;
  String::const_iterator m_current;
  String::const_iterator m_end;

  Char m_quotedType;
};

}