summaryrefslogtreecommitdiff
path: root/Source/Core/InputCommon/ControlReference/ExpressionParser.h
blob: 8e3e6cc1a926115ffaa694203593e882edfe6d36 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#pragma once

#include <map>
#include <memory>
#include <string>

#include "InputCommon/ControllerInterface/Device.h"

namespace ciface::ExpressionParser
{
enum TokenType
{
  TOK_DISCARD,
  TOK_INVALID,
  TOK_EOF,
  TOK_LPAREN,
  TOK_RPAREN,
  TOK_FUNCTION,
  TOK_CONTROL,
  TOK_LITERAL,
  TOK_VARIABLE,
  // Binary Ops:
  TOK_BINARY_OPS_BEGIN,
  TOK_AND = TOK_BINARY_OPS_BEGIN,
  TOK_OR,
  TOK_ADD,
  TOK_SUB,
  TOK_MUL,
  TOK_DIV,
  TOK_MOD,
  TOK_ASSIGN,
  TOK_LTHAN,
  TOK_GTHAN,
  TOK_COMMA,
  TOK_BINARY_OPS_END,
};

class Token
{
public:
  TokenType type;
  std::string data;

  // Position in the input string:
  std::size_t string_position = 0;
  std::size_t string_length = 0;

  Token(TokenType type_);
  Token(TokenType type_, std::string data_);

  bool IsBinaryOperator() const;
  operator std::string() const;
};

enum class ParseStatus
{
  Successful,
  SyntaxError,
  EmptyExpression,
};

class Lexer
{
public:
  std::string expr;
  std::string::iterator it;

  Lexer(const std::string& expr_);

  ParseStatus Tokenize(std::vector<Token>& tokens);

private:
  template <typename F>
  std::string FetchCharsWhile(F&& func)
  {
    std::string value;
    while (it != expr.end() && func(*it))
    {
      value += *it;
      ++it;
    }
    return value;
  }

  std::string FetchDelimString(char delim);
  std::string FetchWordChars();
  Token GetFunction();
  Token GetDelimitedLiteral();
  Token GetVariable();
  Token GetFullyQualifiedControl();
  Token GetBarewordsControl(char c);
  Token GetRealLiteral(char c);

  Token NextToken();
};

class ControlQualifier
{
public:
  bool has_device;
  Core::DeviceQualifier device_qualifier;
  std::string control_name;

  ControlQualifier() : has_device(false) {}

  operator std::string() const
  {
    if (has_device)
      return device_qualifier.ToString() + ":" + control_name;
    else
      return control_name;
  }

  void FromString(const std::string& str)
  {
    const auto col_pos = str.find_last_of(':');

    has_device = (str.npos != col_pos);
    if (has_device)
    {
      device_qualifier.FromString(str.substr(0, col_pos));
      control_name = str.substr(col_pos + 1);
    }
    else
    {
      device_qualifier.FromString("");
      control_name = str;
    }
  }
};

class ControlEnvironment
{
public:
  using VariableContainer = std::map<std::string, ControlState>;

  ControlEnvironment(const Core::DeviceContainer& container_, const Core::DeviceQualifier& default_,
                     VariableContainer& vars)
      : m_variables(vars), container(container_), default_device(default_)
  {
  }

  std::shared_ptr<Core::Device> FindDevice(ControlQualifier qualifier) const;
  Core::Device::Input* FindInput(ControlQualifier qualifier) const;
  Core::Device::Output* FindOutput(ControlQualifier qualifier) const;
  ControlState* GetVariablePtr(const std::string& name);

private:
  VariableContainer& m_variables;
  const Core::DeviceContainer& container;
  const Core::DeviceQualifier& default_device;
};

class Expression
{
public:
  virtual ~Expression() = default;
  virtual ControlState GetValue() const = 0;
  virtual void SetValue(ControlState state) = 0;
  virtual int CountNumControls() const = 0;
  virtual void UpdateReferences(ControlEnvironment& finder) = 0;
  virtual operator std::string() const = 0;
};

std::pair<ParseStatus, std::unique_ptr<Expression>> ParseExpression(const std::string& expr);
}  // namespace ciface::ExpressionParser