简体   繁体   English

Objective-c中的脚本阅读器

[英]Script reader in Objective-c

I am trying to make a simple markup language, and I need my Cocoa app to be able to read it. 我正在尝试制作一种简单的标记语言,并且我需要我的Cocoa应用程序能够阅读它。 I would prefer to have that code written in Objective-c, but I am happy to use any other language. 我希望用Objective-c编写该代码,但是我很高兴使用任何其他语言。

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: 不幸的是,如果我将字符串[button]传递给它,它将在调试控制台中显示出来:

[
[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] . 我可以整理出错误消息,但是我需要知道如何阅读脚本并识别某些标签,例如[button] Can anybody give me a link to something useful on this subject? 谁能给我链接到该主题的有用信息?

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. 通常,如果您不需要丰富的语法,则最好使用XML或JSON作为语法,并仅将自己的语义应用于其中之一。

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. 在过去数十年的经验中,我已经编写了20个左右的“简单”解析器,我可以证明它从未像您期望的那样简单。

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
       ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM