aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sksl/lex/RegexParser.h
blob: 7de546fc17284e39ee1d93a8eea175aaba7e1000 (plain)
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
/*
 * Copyright 2017 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SKSL_REGEXPARSER
#define SKSL_REGEXPARSER

#include "RegexNode.h"

#include <stack>
#include <string>

/**
 * Turns a simple regular expression into a parse tree. The regular expression syntax supports only
 * the basic quantifiers ('*', '+', and '?'), alternation ('|'), character sets ('[a-z]'), and
 * groups ('()').
 */
class RegexParser {
public:
    RegexNode parse(std::string source);

private:
    static constexpr char END = '\0';

    char peek();

    void expect(char c);

    RegexNode pop();

    /**
     * Matches a char literal, parenthesized group, character set, or dot ('.').
     */
    void term();

    /**
     * Matches a term followed by an optional quantifier ('*', '+', or '?').
     */
    void quantifiedTerm();

    /**
     * Matches a sequence of quantifiedTerms.
     */
    void sequence();

    /**
     * Returns a node representing the given escape character (e.g. escapeSequence('n') returns a
     * node which matches a newline character).
     */
    RegexNode escapeSequence(char c);

    /**
     * Matches a literal character or escape sequence.
     */
    void literal();

    /**
     * Matches a dot ('.').
     */
    void dot();

    /**
     * Matches a parenthesized group.
     */
    void group();

    /**
     * Matches a literal character, escape sequence, or character range from a character set.
     */
    void setItem();

    /**
     * Matches a character set.
     */
    void set();

    void regex();

    std::string fSource;

    size_t fIndex;

    std::stack<RegexNode> fStack;
};

#endif