11 enum Type
{ Id
, Num
, Char
, String
, Def
, Bool
, Symbol
, EOL
};
16 unsigned int lineNumber
, colStart
, colEnd
;
18 FilePos
& operator=( const FilePos
& newPos
) {
19 filename
= newPos
.filename
;
20 lineNumber
= newPos
.lineNumber
;
21 colStart
= newPos
.colStart
;
22 colEnd
= newPos
.colEnd
;
29 TokenType::Type tokenType
;
34 Token(TokenType::Type tType
, std::string tData
) : tokenType(tType
), data(tData
) { }
35 Token(TokenType::Type tType
, std::string tData
, std::string tFilename
, int tLineNumber
, int tColStart
, int tColEnd
) :
36 tokenType(tType
), data(tData
) {
37 pos
.filename
= tFilename
;
38 pos
.lineNumber
= tLineNumber
;
39 pos
.colStart
= tColStart
;
44 class CompilerException
: public std::runtime_error
{
46 CompilerException(const std::string
&msg
, const int lineNumber
, const int colStart
, const int colEnd
, const std::string
&filename
)
47 : std::runtime_error(build_message(msg
, lineNumber
, colStart
, colEnd
, filename
)) {
50 CompilerException(const std::string
&msg
, const Token
*token
)
51 : std::runtime_error(build_message(msg
, token
->pos
.lineNumber
, token
->pos
.colStart
, token
->pos
.colEnd
, token
->pos
.filename
)) {
54 CompilerException(const std::string
&msg
, const FilePos
&pos
)
55 : std::runtime_error(build_message(msg
, pos
.lineNumber
, pos
.colStart
, pos
.colEnd
, pos
.filename
)) {
58 CompilerException(const std::string
&msg
)
59 : std::runtime_error(msg
) {
62 std::string
build_message(const std::string
&msg
, const int lineNumber
, const int colStart
, const int colEnd
, const std::string
&filename
) const {
63 std::ostringstream msg_builder
;
65 msg_builder
<< msg
<< " at line " << lineNumber
<< " column " << colStart
<< " in '" << filename
<< "'";
67 return msg_builder
.str();
71 std::vector
<Token
*> tokenize(std::string sourceText
, std::string filename
);