Objective-c 中的脚本阅读器

发布于 2024-12-12 18:58:29 字数 610 浏览 1 评论 0原文

我正在尝试制作一种简单的标记语言,并且我需要我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是暖光i 2024-12-19 18:58:29

仅举几个例子:

NSString *str = @"[Button]";
NSUInteger i, len = [str length];

for (i = 1; i <= len; ++i)
    NSLog(@"%@", [str substringToIndex:i]);

for (i = 0; i < len; ++i)
    NSLog(@"%c", [str characterAtIndex:i]);

输出:

[
[B
[Bu
[But
[Butt
[Butto
[Button
[Button]
[
B
u
t
t
o
n
]

以及一个更有趣的案例:

for (i = 0; i < len; ++i)
    NSLog([NSString stringWithFormat:@"%%%dc", i+1],
          [str characterAtIndex:i]);

产生:

[
 B
  u
   t
    t
     o
      n
       ]

Just to give you examples:

NSString *str = @"[Button]";
NSUInteger i, len = [str length];

for (i = 1; i <= len; ++i)
    NSLog(@"%@", [str substringToIndex:i]);

for (i = 0; i < len; ++i)
    NSLog(@"%c", [str characterAtIndex:i]);

Output:

[
[B
[Bu
[But
[Butt
[Butto
[Button
[Button]
[
B
u
t
t
o
n
]

And a more interesting case:

for (i = 0; i < len; ++i)
    NSLog([NSString stringWithFormat:@"%%%dc", i+1],
          [str characterAtIndex:i]);

Which produces:

[
 B
  u
   t
    t
     o
      n
       ]
请你别敷衍 2024-12-19 18:58:29

这一切都取决于您的脚本语言的复杂程度,以及它的效率和健壮性。

一般来说,如果您不需要丰富的语法,则最好使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文