1 /****************************************************************************
2 **
3 ** Copyright (C) Filippo Cucchetto <filippocucchetto@gmail.com>
4 ** Contact: http://www.qt.io/licensing
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #pragma once
27 
28 #include "sourcecodestream.h"
29 
30 #include <QString>
31 
32 namespace Nim {
33 
34 class NimLexer
35 {
36 public:
37     enum State {
38         Default = -1,
39         MultiLineString = 0,
40         MultiLineComment = 1
41     };
42 
43     struct TokenType {
44         enum Type {
45             Keyword = 0,
46             Identifier,
47             Comment,
48             Documentation,
49             StringLiteral,
50             MultiLineStringLiteral,
51             Operator,
52             Number,
53             EndOfText
54         };
55     };
56 
57     struct Token {
TokenToken58         Token()
59             : begin(0)
60             , length(0)
61             , type(TokenType::EndOfText)
62         {}
63 
TokenToken64         Token(int b, int l, TokenType::Type t)
65             : begin(b), length(l), type(t)
66         {}
67 
68         int begin;
69         int length;
70         TokenType::Type type;
71     };
72 
73     NimLexer(const QChar *text,
74              int length,
75              State state = State::Default );
76 
77     Token next();
78 
state()79     State state() const
80     {
81         return m_state;
82     }
83 
84 private:
85     Token onDefaultState();
86     Token onMultiLineStringState();
87     Token onMultiLineCommentState();
88 
89     bool isSkipChar();
90 
91     bool isOperator();
92     Token readOperator();
93 
94     bool matchCommentStart();
95     Token readComment();
96 
97     bool matchMultiLineCommentStart();
98     bool matchMultiLineCommentEnd();
99     Token readMultiLineComment(bool moveForward);
100 
101     bool matchDocumentationStart();
102     Token readDocumentation();
103 
104     bool matchNumber();
105     Token readNumber();
106 
107     bool matchIdentifierOrKeywordStart();
108     Token readIdentifierOrKeyword();
109 
110     bool matchStringLiteralStart();
111     Token readStringLiteral();
112 
113     bool matchMultiLineStringLiteralStart();
114     Token readMultiLineStringLiteral(bool moveForward = true);
115 
116     State m_state;
117     SourceCodeStream m_stream;
118 };
119 
120 }
121