-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLispLexer.h
125 lines (107 loc) · 2.99 KB
/
LispLexer.h
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
#ifndef LISPLEXER_H
#define LISPLEXER_H
/*
* Copyright 2024 Rochus Keller <mailto:[email protected]>
*
* This file is part of the Interlisp project.
*
* The following is the license that applies to this copy of the
* library. For a license to use the library under conditions
* other than those described here, please email to [email protected].
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
// Adopted from the Luon project
#include <QObject>
#include "LispRowCol.h"
class QIODevice;
namespace Lisp
{
enum TokenType {
Tok_Invalid,
Tok_Eof,
Tok_lpar,
Tok_rpar,
Tok_lbrack,
Tok_rbrack,
Tok_string,
Tok_atom,
Tok_float,
Tok_integer,
Tok_comment,
Tok_Lattr, // (*
Tok_DblQuote, // "
};
struct Token
{
#ifdef _DEBUG
union
{
int type; // TokenType
TokenType tokenType;
};
#else
quint16 type; // TokenType
#endif
quint16 len;
RowCol pos;
QByteArray val;
QString sourcePath;
Token(quint16 t = Tok_Invalid, const RowCol& rc = RowCol(), quint16 len = 0, const QByteArray& val = QByteArray() ):
type(t),pos(rc),len(len),val(val){}
bool isValid() const;
bool isEof() const;
const char* getName() const;
const char* getString() const;
static QByteArray getSymbol( const QByteArray& );
static QByteArrayList getAllSymbols();
};
class Lexer : public QObject
{
public:
Lexer(QObject *parent = 0);
void setStream( QIODevice*, const QString& sourcePath );
void setStream(const QByteArray& code, const QString& sourcePath );
bool setStream(const QString& sourcePath);
Token nextToken();
Token readString();
void unget(const Token&);
QList<Token> tokens( const QString& code );
QList<Token> tokens( const QByteArray& code, const QString& path = QString() );
QString getSource() const { return sourcePath; }
void setEmitComments(bool on) { emitComments = on; }
void setPacked(bool on) { packed = on; }
RowCol getPos() const { return pos; }
void startQuote();
void endQuote();
static bool atom_delimiter(char);
protected:
Token nextTokenImp();
char readc();
void ungetc(char c);
QByteArray peek(int count);
void ungetstr(const QByteArray& str);
Token token(TokenType tt, int len = 0, const QByteArray &val = QByteArray());
Token number();
Token atom();
Token string();
Token comment();
void skipWhiteSpace();
private:
QIODevice* in;
RowCol pos, start;
QString sourcePath;
QList<Token> buffer;
bool emitComments;
bool packed;
bool inQuote;
};
}
#endif // LISPLEXER_H