Objective-c 中的脚本阅读器
我正在尝试制作一种简单的标记语言,并且我需要我的 Cocoa 应用程序能够读取它。我更喜欢用 Objective-c 编写代码,但我也很乐意使用任何其他语言。
这是我目前的问题。我正在使用这段代码:
for (int character = 1; character < ([script length] - 1); character ++) {
NSLog(@"%@", [[script substringToIndex:character] substringFromIndex:([[script substringToIndex:character] length] - 2)]);
不幸的是,如果我通过它传递字符串 [button]
,它会在调试控制台中显示:
[
[b
[bu
[but
[butt
[__NSCFConstantString substringWithRange:]: Range or index out of bounds
我可以整理错误消息,但我需要知道如何阅读脚本并识别某些标签,例如 [button]
。有人能给我一个关于这个主题的有用的链接吗?
I am trying to make a simple markup language, and I need my Cocoa app to be able to read it. I would prefer to have that code written in Objective-c, but I am happy to use any other language.
Here is my current problem. I am using this code:
for (int character = 1; character < ([script length] - 1); character ++) {
NSLog(@"%@", [[script substringToIndex:character] substringFromIndex:([[script substringToIndex:character] length] - 2)]);
Unfortunately, if I pass the string [button]
through it, it shows this in the debug console:
[
[b
[bu
[but
[butt
[__NSCFConstantString substringWithRange:]: Range or index out of bounds
I can sort out the error message, but I need to know how to read the script and recognize certain tags like [button]
. Can anybody give me a link to something useful on this subject?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅举几个例子:
输出:
以及一个更有趣的案例:
产生:
Just to give you examples:
Output:
And a more interesting case:
Which produces:
这一切都取决于您的脚本语言的复杂程度,以及它的效率和健壮性。
一般来说,如果您不需要丰富的语法,则最好使用 XML 或 JSON 作为语法,然后将您自己的语义应用于其中之一。
否则,解析是编程中一个相当大、复杂的领域,你应该在深入研究之前先研究一下(网上和书籍上有很多很好的参考资料)。在我数十年的经验中,我已经编写了 20 个左右的“简单”解析器,我可以证明它永远不会像您期望的那么容易。
This all depends on how complex your scripting language is intended to be, and how efficient and robust it needs to be.
In general, if you don't need a rich syntax, you might be best using XML or JSON for your syntax, and just applying your own semantics to one of those.
Otherwise, parsing is a fairly large, complex field within programming, and you should study up a bit before you dive in (there are dozens of good references, on the web and in books). I've written 20 or so "simple" parsers over my decades of experience, and I can attest that it's never as easy as you expect.