-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToken.h
More file actions
126 lines (103 loc) · 3.86 KB
/
Copy pathToken.h
File metadata and controls
126 lines (103 loc) · 3.86 KB
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
#ifndef __TOKEN_H__
#define __TOKEN_H__
/** @file
* Definition of the Token class.
*/
//#ident "@(#)Token.h 1.5 AKK - 20060526"
#ident "@(#)Token.h 2.1 AKK - 20121130"
#include <string> // std::string
#include <istream> // std::istream
using std::string; // import alleen std::string
using std::istream; // import alleen std::istream
using std::ostream; // import alleen std::ostream
/// @class Token
/// Describes a <a target='info'
/// href='http://en.wikipedia.org/wiki/Tokenizing'>token</a> from the input.
/// Which kinds of tokens will be recognized
/// can be choosen with setLevel().
/// By default the most advanced
/// BASH/KSH set is recognized.
class Token
{
public:
/// @enum Level
/// The constants that determine which tokens
/// will be recognized by the tokenizer.
enum Level
{
BASIC, //!< Basic tokens:
//!< ; | < > >> &
ADVANCED, //!< Advanced = BASIC plus:
//!< && || ( ) shell-variables simple-wildcard
SH, //!< Shell scripts = ADVANCED plus:
//!< << ` ;; { }
BASH //!< Bash/ksh scripts = SH plus:
//!< $( )
};
/// @enum token_t
/// The different kinds of tokens.
enum token_t
{
// == basic level ==
END, //!< end-of-input.
//!< @note We use END here because the code EOF is a macro
//!< already defined in some other include file.
EOL, //!< end-of-line (either \\n or \\r)
SEQUENCE, //!< ; e.g. foo ; bar
PIPE, //!< | e.g. run | in | tandem
INPUT, //!< < e.g. input < from ...
OUTPUT, //!< > e.g. output > to ....
APPEND, //!< >> e.g. output >> after ...
BACKGROUND, //!< & i.e. don't wait
// == advanced level ==
AND, //!< && in a conditional execution.
OR, //!< || in a conditional execution.
SUBS, //!< ( subshell begin, as in: ( ... ; ... )
ENDS, //!< ) subshell end, as in: ( ... ; ... )
VAR, //!< $word or one of $* $@ $# $0...$9 $? $! $$
//!< @note The token-text does NOT contain the $ character.
EXPR, //!< A simple wildcard containing * or ?
//!< @note The [...] wildcard is NOT recognized.
// == sh script level ==
HERE, //!< << start of an inline document
BQUOTE, //!< ` as in ` ... ` run a subshell & substitute the output here
BREAK, //!< ;; as in: case ... in ... ;; ... ;; ... esac
GROUP, //!< { group begin, as in: { ... ; ... }
GRPEND, //!< } group end, as in: { ... ; ... }
// == ksh/bash level ==
EVAL, //!< $( ... ) run a subshell & substitute the output here
WORD //!< anything else.
//!< @note Special words such as 'exit', 'cd' etc.
//!< are NOT handled here,
//!< because their meaning is position dependent.
};
private:
static Level level; // The current token level, by default BASH level
// per Token instance attributes
token_t type; // The code for the token type.
string text; // The corresponding text from the input.
explicit
Token(token_t t) : type(t) {}
Token(token_t t, const string &s) : type(t), text(s) {}
public:
/// Select which level of tokens will be recognized.
/// By default it is the most advanced BASH level.
static void setLevel(Level);
/// Returns the current token level.
static Level getLevel() { return level; }
// Per token query functions
token_t getType() const { return type; }
string& getText() { return text; }
/// This static function is the actual scanner
/// which returns the next token from the input.
/// @return a pointer to a new Token instance.
/// The caller must delete that object
/// when it is no longer needed.
static Token *nextToken(istream &is);
/// The output operator for tokens
friend ostream& operator<<(ostream& os, const Token& token);
};
/// The output operator for tokens
ostream& operator<<(ostream& os, const Token& token);
// vim:sw=4:ai:aw:ts=4:
#endif /*TOKEN_H*/