如何在不使用rg的情况下实现token?

问题描述 投票:0回答:1

我得到了一个令牌内容,我想完成

next()
类的
MDTokenizer
方法来执行令牌化过程。

next()
方法应识别所有标记及其关联值(例如,对于没有缩进(无前缀空格)的项目,
level
的相应值为
1
)。

每个令牌都有一个

tokenType
content
level
number
,如在
MDToken
类中实现的那样。

token
1个回答
0
投票

公共布尔值hasNext() { return this.buffer != null && !this.buffer.isEmpty(); // 如果需要,您可以更改此行 }

public MDToken next() {
    
    // Return null if there are no more tokens to process
    if (!hasNext()) {
        return null;
    }

    String line = buffer.split("\n", 2)[0]; // Get the first line from the buffer
    int newlineLength = line.length() < buffer.length() ? 1 : 0;
    buffer = buffer.substring(line.length() + newlineLength); // Remove the first line from the buffer

    int level = 1 + (line.length() - line.replaceFirst("^ *", "").length()) / 4;
    String trimmedLine = line.trim();
    if (trimmedLine.isEmpty()) {
        // Ignore empty lines and proceed to next token
        return next();
    }

    MDTokenType type;
    String text;
    int number = 0;

    // Check if the line is an ordered list item
    if (Character.isDigit(trimmedLine.charAt(0))) {
        type = MDTokenType.ORDERED_ITEM;
        int dotIndex = trimmedLine.indexOf('.');
        number = Integer.parseInt(trimmedLine.substring(0, dotIndex).trim());
        text = trimmedLine.substring(dotIndex + 1).trim();
    }
    // Check if the line is an unordered list item
    else if (trimmedLine.startsWith("-")) {
        type = MDTokenType.UNORDERED_ITEM;
        text = trimmedLine.substring(1).trim();
    }
    // If the line doesn't start with a digit or '-', it's not a valid list item
    else {
        current = null;
        return null;
    }

    // Create and return the new token
    current = new MDToken(type, text, level, number);
    return current;

    
}

你必须使用一些rg,例如

© www.soinside.com 2019 - 2024. All rights reserved.