OXYGENHUB
◆ main openmsx / src / commands / TclParser.hh
1.7 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
e8555c89 Andy McCall 1
#ifndef TCLPARSER_HH
2
#define TCLPARSER_HH
3
4
#include "tcl.hh"
5
6
#include <cstdint>
7
#include <span>
8
#include <string>
9
#include <string_view>
10
#include <vector>
11
12
#define DEBUG_TCLPARSER 0
13
14
class TclParser
15
{
16
public:
17
	/** Input: Tcl interpreter and command to parse	 */
18
	TclParser(Tcl_Interp* interp, std::string_view input);
19
20
	/** Ouput: a string of equal length of the input command where
21
	  * each character indicates the type of the corresponding
22
	  * character in the command. Currently the possible colors are:
23
	  *  'E' -> error
24
	  *  'c' -> comment
25
	  *  'v' -> variable
26
	  *  'l' -> literal (string or number)
27
	  *  'p' -> proc
28
	  *  'o' -> operator
29
	  *  '.' -> other
30
	  */
31
	[[nodiscard]] const std::string& getColors() const { return colors; }
32
33
	/** Get Start of the last subcommand. This is the command that should
34
	  * be completed by tab-completion. */
35
	[[nodiscard]] int getLast() const { return last.back(); }
36
37
	/** Is the given string a valid Tcl command. This also takes the
38
	  * 'lazy' Tcl scripts into account. */
39
	[[nodiscard]] static bool isProc(Tcl_Interp* interp, std::string_view str);
40
41
private:
42
	enum ParseType : uint8_t { COMMAND, EXPRESSION, OTHER };
43
44
	void parse(std::string parseStr, int offset, ParseType type);
45
	void printTokens(std::string_view parseStr, int offset, std::span<const Tcl_Token> tokens);
46
	[[nodiscard]] static ParseType guessSubType(std::span<const Tcl_Token> tokens, size_t i);
47
	void setColors(std::string_view parseStr, int offset, const char* p, int size, char c);
48
49
private:
50
	Tcl_Interp* interp;
51
	std::string colors;
52
	std::vector<int> last;
53
54
#if DEBUG_TCLPARSER
55
	void DEBUG_PRINT(const std::string& s);
56
	int level = 0;
57
#else
58
	#define DEBUG_PRINT(x)
59
#endif
60
};
61
62
#endif