提问人:donutcheese 提问时间:11/12/2023 最后编辑:Holger Justdonutcheese 更新时间:11/12/2023 访问量:31
如何在不使用 rg 的情况下实现代币?[关闭]
How to implement tokens without using rg? [closed]
问:
我得到了一个令牌内容,我想完成类的方法以执行令牌化过程。next()
MDTokenizer
该方法应标识所有标记及其关联值(例如,对于没有缩进(没有前缀空格)的项目,对应的值为 is )。next()
level
1
每个标记都有一个 、 和,如在类中实现的那样。tokenType
content
level
number
MDToken
答:
-1赞
david
11/12/2023
#1
public boolean hasNext() {
return this.buffer != null && !this.buffer.isEmpty(); // You may make changes to this line if needed
}
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,例如\n
评论
0赞
Holger Just
11/12/2023
你能编辑你的答案,用你自己的话解释你的代码是做什么的,以及它如何以及为什么有助于解决问题吗?这样,它可能对未来的读者更有帮助。
评论