File: mdparser.cpp

package info (click to toggle)
codelite 17.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 136,204 kB
  • sloc: cpp: 491,547; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 804; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (255 lines) | stat: -rw-r--r-- 8,720 bytes parent folder | download | duplicates (2)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include "mdparser.hpp"

#include "wx/string.h"

#include <cctype>
#include <iostream>
#include <memory>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <wx/crt.h>

wxChar mdparser::Tokenizer::safe_get_char(size_t pos) const
{
    if(pos >= m_text.length()) {
        return 0;
    }
    return m_text[pos];
}

#define WORD_WRAP_AT_COL 100
#define RETURN_TYPE(TYPE, STR, INCR_POS)                                                                       \
    if(TYPE == Type::T_TEXT) {                                                                                 \
        m_text_sequence++;                                                                                     \
    } else {                                                                                                   \
        m_text_sequence = 0;                                                                                   \
    }                                                                                                          \
    /* implement word wrap */                                                                                  \
    if(m_text_sequence >= WORD_WRAP_AT_COL && (wxStrncmp(STR, " ", 1) == 0 || wxStrncmp(STR, "\t", 1) == 0)) { \
        m_text_sequence = 0;                                                                                   \
        --m_pos;                                                                                               \
        return { T_EOL, "" };                                                                                  \
    } else {                                                                                                   \
        m_pos += INCR_POS;                                                                                     \
        return { TYPE, STR };                                                                                  \
    }

#define IS_ONE_OF2(c, C1, C2) (c == C1 || c == C2)
#define IS_ONE_OF3(c, C1, C2, C3) (c == C1 || c == C2 || c == C3)

std::pair<mdparser::Type, wxString> mdparser::Tokenizer::next()
{
    while(m_pos < m_text.length()) {
        wxChar ch0 = m_text[m_pos];
        wxChar ch1 = safe_get_char(m_pos + 1);
        wxChar ch2 = safe_get_char(m_pos + 2);
        wxChar ch3 = safe_get_char(m_pos + 3);
        wxChar before_ch = safe_get_char(m_pos - 1);
        ++m_pos;
        switch(ch0) {
        case '\n':
            RETURN_TYPE(T_EOL, "\n", 0);
        case '*':
            // bold & italic
            if(ch1 == '*') {
                RETURN_TYPE(T_BOLD, "**", 1);
            } else {
                RETURN_TYPE(T_ITALIC, "*", 0);
            }
            break;
        case '~':
            // strikethrough
            if(ch1 == '~') {
                RETURN_TYPE(T_STRIKE, "~~", 1);
            } else {
                RETURN_TYPE(T_TEXT, ch0, 0);
            }
            break;
        case '`':
            // code blocks ``` & `
            if(ch1 == '`' && ch2 == '`') {
                RETURN_TYPE(T_CODEBLOCK, "```", 2);
            } else {
                RETURN_TYPE(T_CODE, "`", 0);
            }
            break;
        case '#':
            // Heading, up to 3 x #
            if(ch1 == '#' && ch2 == '#') {
                RETURN_TYPE(T_H3, "###", 2);
            } else if(ch1 == '#') {
                RETURN_TYPE(T_H2, "##", 1);
            } else {
                RETURN_TYPE(T_H1, "#", 0);
            }
            break;
        case '=':
            if(IS_ONE_OF2(before_ch, '\n', 0) && ch1 == '=' && ch2 == '=' && IS_ONE_OF3(ch3, '\n', '\r', 0)) {
                RETURN_TYPE(T_HR, "===", 2);
            } else {
                RETURN_TYPE(T_TEXT, "=", 0);
            }
            break;
        case '-':
            if(IS_ONE_OF2(before_ch, '\n', 0) && ch1 == '-' && ch2 == '-' && IS_ONE_OF3(ch3, '\n', '\r', 0)) {
                RETURN_TYPE(T_HR, "---", 2);
            } else if(ch1 != '-') {
                RETURN_TYPE(T_LI, "-", 0);
            } else {
                RETURN_TYPE(T_TEXT, ch0, 0);
            }
            break;
        case '\\':
            if(m_enable_backslash_esc) {
                RETURN_TYPE(T_TEXT, ch1, 1);
            } else {
                RETURN_TYPE(T_TEXT, ch0, 0);
            }
            break;
        default:
            RETURN_TYPE(T_TEXT, ch0, 0);
        }
    }
    RETURN_TYPE(T_EOF, "", 0);
}

void mdparser::Tokenizer::consume_until(wxChar ch)
{
    for(; m_pos < m_text.length(); ++m_pos) {
        if(m_text[m_pos] == ch) {
            ++m_pos;
            break;
        }
    }
}

void mdparser::Parser::parse(const wxString& input_str, write_callback_t on_write)
{
    constexpr int STATE_NORMAL = 0;
    constexpr int STATE_CODE = 1;
    constexpr int STATE_CODEBLOCK_HEADER = 2;
    constexpr int STATE_CODEBLOCK = 3;
    int state = STATE_NORMAL;

    write_cb = std::move(on_write);
    Tokenizer tokenizer(input_str);
    Style style;
    Type last_state = T_EOF;
    wxString buffer;
    while(true) {
        auto tok = tokenizer.next();
        if(tok.first == T_EOF) {
            flush_buffer(buffer, style, false);
            break;
        }

        switch(state) {
        case STATE_NORMAL:
            switch(tok.first) {
            case T_LI:
                if(last_state == T_EOL || last_state == T_EOF) {
                    buffer << L"\u2022"; // bullet
                } else {
                    buffer << "-";
                }
                break;
            // below are style styles
            case T_BOLD:
            case T_ITALIC:
            case T_STRIKE:
                flush_buffer(buffer, style, false);
                style.toggle_property(tok.first);
                break;
            case T_H1:
            case T_H2:
            case T_H3:
                if(last_state == T_EOL || last_state == T_EOF) {
                    flush_buffer(buffer, style, false);
                    style.toggle_property(tok.first);
                } else {
                    // handle it as text
                    buffer << tok.second;
                }
                break;
            case T_CODE:
            case T_CODEBLOCK:
                state = (tok.first == T_CODE ? STATE_CODE : STATE_CODEBLOCK_HEADER);
                flush_buffer(buffer, style, false);
                style.toggle_property(tok.first);
                tokenizer.enable_backslash_esc(false);
                break;
            case T_EOL:
                // clear heading
                flush_buffer(buffer, style, true);
                style.clear_property(T_H1);
                style.clear_property(T_H2);
                style.clear_property(T_H3);
                break;
            case T_TEXT:
                buffer << tok.second;
                break;
            case T_HR:
                flush_buffer(buffer, style, false);
                notify_hr();
                tokenizer.consume_until('\n');
                break;
            case T_EOF:
                break;
            }
            break;
        case STATE_CODEBLOCK_HEADER:
            // we are looking for the first EOL
            switch(tok.first) {
            case T_EOL:
                state = STATE_CODEBLOCK;
                break;
            default:
                // ignore anything else
                break;
            }
            break;
        case STATE_CODE:
        case STATE_CODEBLOCK:
            if((state == STATE_CODE && tok.first == T_CODE) || (state == STATE_CODEBLOCK && tok.first == T_CODEBLOCK)) {
                state = STATE_NORMAL;
                flush_buffer(buffer, style, false);
                style.toggle_property(tok.first);
                tokenizer.enable_backslash_esc(true);
            } else if(tok.first == T_EOL) {
                flush_buffer(buffer, style, true);
            } else {
                if(buffer.empty() && tok.second == '\n') {
                    // skip it
                } else {
                    buffer << tok.second;
                }
            }
            break;
        }

        if(tok.first == T_TEXT && (tok.second == ' ' || tok.second == '\t')) {
            // dont change the lasta seen state for whitespace
            continue;
        }
        last_state = tok.first;
    }
}

void mdparser::Parser::flush_buffer(wxString& buffer, const Style& style, bool is_eol)
{
    if(!buffer.empty() || is_eol) {
        write_cb(buffer, style, is_eol);
    }
    buffer.clear();
}

void mdparser::Parser::notify_hr()
{
    Style style;
    style.toggle_property(T_HR);
    write_cb("", style, true);
}