- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经挪用并修改了以下代码,这些代码在使用 Java 的 StreamTokenizer 对 Java 代码进行标记化方面做得非常好。但是,它的数字处理是有问题的:
我很乐意完全关闭 StreamTokenizer 的数字解析并自己从单词标记中解析数字,但评论 st.parseNumbers() 似乎没有效果。
public class JavaTokenizer {
private String code;
private List<Token> tokens;
public JavaTokenizer(String c) {
code = c;
tokens = new ArrayList<>();
}
public void tokenize() {
try {
// Create the tokenizer
StringReader sr = new StringReader(code);
StreamTokenizer st = new StreamTokenizer(sr);
// Java-style tokenizing rules
st.parseNumbers();
st.wordChars('_', '_');
st.eolIsSignificant(false);
// Don't want whitespace tokens
//st.ordinaryChars(0, ' ');
// Strip out comments
st.slashSlashComments(true);
st.slashStarComments(true);
// Parse the file
int token;
do {
token = st.nextToken();
switch (token) {
case StreamTokenizer.TT_NUMBER:
// A number was found; the value is in nval
double num = st.nval;
if(num % 1 == 0)
tokens.add(new IntegerToken((int)num);
else
tokens.add(new FPNumberToken(num));
break;
case StreamTokenizer.TT_WORD:
// A word was found; the value is in sval
String word = st.sval;
tokens.add(new WordToken(word));
break;
case '"':
// A double-quoted string was found; sval contains the contents
String dquoteVal = st.sval;
tokens.add(new DoubleQuotedStringToken(dquoteVal));
break;
case '\'':
// A single-quoted string was found; sval contains the contents
String squoteVal = st.sval;
tokens.add(new SingleQuotedStringToken(squoteVal));
break;
case StreamTokenizer.TT_EOL:
// End of line character found
tokens.add(new EOLToken());
break;
case StreamTokenizer.TT_EOF:
// End of file has been reached
tokens. add(new EOFToken());
break;
default:
// A regular character was found; the value is the token itself
char ch = (char) st.ttype;
if(Character.isWhitespace(ch))
tokens.add(new WhitespaceToken(ch));
else
tokens.add(new SymbolToken(ch));
break;
}
} while (token != StreamTokenizer.TT_EOF);
sr.close();
} catch (IOException e) {
}
}
public List<Token> getTokens() {
return tokens;
}
}
最佳答案
parseNumbers() 默认处于“开启”状态。使用 resetSyntax() 关闭数字解析和所有其他预定义的字符类型,然后启用您需要的。
也就是说,手动数字解析在计算点和指数时可能会变得棘手……使用扫描仪和正则表达式,实现您自己的分词器应该相对简单,完全根据您的需要量身定制。例如,您可能想看看这里的 Tokenizer
内部类:https://github.com/stefanhaustein/expressionparser/blob/master/core/src/main/java/org/kobjects/expressionparser/ExpressionParser.java (最后大约120行)
关于java - StreamTokenizer 破坏整数和松散的句点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43502608/
我正在尝试在松散的 XAML 中使用 ResourceDictionary 并将其加载到运行时以向 WPF 应用程序提供模板和样式。我在本地目录中有可用的 XAML,并在应用启动时使用 URI 将新的
我正在尝试在松散的 XAML 中使用 ResourceDictionary 并将其加载到运行时以向 WPF 应用程序提供模板和样式。我在本地目录中提供了 XAML,并在应用程序启动时使用 URI 将新
下面这段代码会导致内存丢失,因为rA在构造时被初始化为无效。我什么时候可以解决这个问题? 使用 shared_ptr 还是希望 future 的编译器版本能够捕获这些错误代码? #include u
我有一个可扩展的UITableView,具有特定的单元格、页眉和页脚高度。当用户点击标题时,单元格开始显示在其下方(部分展开)。当用户再次点击时,部分会折叠。 我的问题是,当用户点击标题时,标题变为绿
我看过这个问题,没看出问题出在哪里。我不是 C++ 专家,所以对我来说这看起来还不错。我上次尝试时,这曾经毫无问题地编译。 namespace yaaf { /********************
问题陈述如下,假设你有一个包含三个元素的标题: ... ... ... Logo 和选项的绝对宽度分别为 220px 和 294px。元素布局安排是: .logo { float:left; } .s
我是一名优秀的程序员,十分优秀!