1 /*  -*- c++ -*-
2     ksieve/lexer.h
3 
4     This file is part of KSieve,
5     the KDE internet mail/usenet news message filtering library.
6     SPDX-FileCopyrightText: 2003 Marc Mutz <mutz@kde.org>
7 
8     SPDX-License-Identifier: GPL-2.0-only
9 */
10 
11 #pragma once
12 
13 #include "ksieve_export.h"
14 
15 class QString;
16 
17 namespace KSieve
18 {
19 class Error;
20 
21 class KSIEVE_EXPORT Lexer
22 {
23 public:
24     enum Options { IncludeComments = 0, IgnoreComments = 1, IncludeLineFeeds = 0, IgnoreLineFeeds = 2 };
25 
26     Lexer(const char *scursor, const char *send, int options = 0);
27     ~Lexer();
28 
29     /** Return whether comments are returned by @ref
30         nextToken. Default is to not ignore comments. Ignoring them
31         can speed up script parsing a bit, and can be used when the
32         internal representation of the script won't be serialized into
33         string form again (or if you simply want to delete all
34         comments)
35     **/
36     bool ignoreComments() const;
37 
38     /** Return whether line feeds are returned by @ref
39         nextToken. Default is to not ignore line feeds. Ignoring them
40         can speed up script parsing a bit, and can be used when the
41         internal representation of the script won't be serialized into
42         string form again.
43     **/
44     bool ignoreLineFeeds() const;
45 
46     const Error &error() const;
47 
48     bool atEnd() const;
49     int column() const;
50     int line() const;
51 
52     enum Token {
53         None = 0,
54         Number, // 1, 100, 1M, 10k, 1G, 2g, 3m
55         Identifier, // atom
56         Tag, // :tag
57         Special, // {} [] () ,;
58         QuotedString, // "foo\"bar" -> foo"bar
59         MultiLineString, // text: \nfoo\n. -> foo
60         HashComment, // # foo
61         BracketComment, // /* foo */
62         LineFeeds // the number of line feeds encountered
63     };
64 
65     /** Parse the next token and return it's type. @p result will contain
66         the value of the token. */
67     Token nextToken(QString &result);
68 
69     void save();
70     void restore();
71 
72     class Impl;
73 
74 private:
75     Impl *i = nullptr;
76 
77 private:
78     const Lexer &operator=(const Lexer &);
79     Lexer(const Lexer &);
80 };
81 } // namespace KSieve
82 
83