在 Javascript 中解析组织模式文件
我已经有一段时间试图让自己用 Javascript 为 org-mode 编写一个解析器了。我在解析大纲时完全没有遇到任何问题(我在几分钟内就完成了),但解析实际内容要困难得多,例如,我在处理叠瓦式列表时遇到了麻烦。
* This is a heading
P1 Start a paragraph here but since it is the first indentation level
the paragraph may have a lower indentation on the next line
or a greater one for that matter.
+ LI1.1 I am beginning a list here
+ LI1.2 Here begins another list item
which continues here
and also here
P2 but is broken here (this line becomes a paragraph
outside of the first list).
+ LI2.1 P1 Second list item.
- LI2.1.1 Inner list with a simple item
- LI2.1.2 P1 and with an item containing several paragraphs.
Here is the second line in the item, and now
LI2.1.2 P2 I begin a new paragraph still in the same item.
The indentation can be only higher
LI2.1 P2 but if the indentation is lower, it breaks the item,
(and the whole list), and this is a paragraph in the LI2.1
list item
- LI 2.2.1 You get the picture
P3 Just plain text outside of the list.
(在上面的示例中,PX
和 LIX.Y
仅用于明确显示新块的开始,它们不会出现在实际文档中。P
代表段落,LI
代表列表项。在 HTML 世界中,PX 是
标记的开头。只是为了帮助跟踪列表的嵌套和更改。)
我想知道解析这种重要的空白叠瓦块的策略,显然我可以逐行解析而无需任何回溯或什么都不做,所以它一定非常简单,但由于某种原因我无法做到这一点。我试图从 Markdown 解析器或类似的东西中获得灵感,这些东西应该具有类似的叠瓦特征,但在我看来(对于我所看到的)它们非常老套,充满了正则表达式,我希望我可以写一些更干净的东西(org当你想到它时,模式“语法”是相当巨大的,它会一点一点地增长,我希望整个事情是可维护的,并允许轻松插入新功能)。
有解析此类事情经验的人可以给我一些指示吗?
It's been some time now that I am trying to get myself to write a parser in Javascript for org-mode. I had no trouble at all parsing the outline (which I did in a few minutes), but parsing the actual content is far more difficult, and I'm having trouble with imbricated lists, for example.
* This is a heading
P1 Start a paragraph here but since it is the first indentation level
the paragraph may have a lower indentation on the next line
or a greater one for that matter.
+ LI1.1 I am beginning a list here
+ LI1.2 Here begins another list item
which continues here
and also here
P2 but is broken here (this line becomes a paragraph
outside of the first list).
+ LI2.1 P1 Second list item.
- LI2.1.1 Inner list with a simple item
- LI2.1.2 P1 and with an item containing several paragraphs.
Here is the second line in the item, and now
LI2.1.2 P2 I begin a new paragraph still in the same item.
The indentation can be only higher
LI2.1 P2 but if the indentation is lower, it breaks the item,
(and the whole list), and this is a paragraph in the LI2.1
list item
- LI 2.2.1 You get the picture
P3 Just plain text outside of the list.
(In the above example, the PX
and LIX.Y
are only there to show explicitly the beginning of new blocks, they would not be present in the actual document. P
stand for paragraph and LI
for list item. In the HTML world, PX would be the beginning of a <p>
tag. The numbering are just to help keep track of the nesting and changes of list.)
I wondered about the strategy to parse this kind of significant white-space imbricated blocks, clearly I can parse line by line without any backtracking or nothing, so it must be quite simple, but for some reason I couldn't manage to do it. I tried to get inspiration from Markdown parsers, or such things that are supposed to have similar imbrication features but they appeared to me (for the ones I saw) to be very hacky, full of regexes and I hoped I could write something cleaner (org-mode "grammar" being quite huge when you come to think about it, it will grow little by little and I'd like the whole thing to be maintainable and allow to plug-in new features easily).
Can anyone with experience in parsing such things can give me some pointers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我喜欢解析器和编译器理论,所以我(手动)编写了一个小型解析器,它能够将示例片段解析为
XML DOM 文档对象。应该可以对其进行修改,以便生成其他类型的树结构,例如自定义 AST(抽象语法树)。
我尽力使代码易于阅读,以便您可以了解这样的解析器是如何工作的。
问我是否需要更多解释,或者希望我稍微修改一下。
使用示例代码片段作为输入,语句
result = new OrgModParser().parse(input); result.xml
返回:代码:
I like parsers and compiler theory, so I have written a small parser (by hand) that is able to parse your example snippet into
a XML DOM Document object. It shoul be possible to modify it so that it produces an other type of tree structure, like a custom AST (abstract syntax tree).
I've tried to keep the code easy to read, so that you can see how such a parser works.
Ask me if you need some more explanations, or want me to modify it a little.
With your example snippet as input, the statement
result = new OrgModParser().parse(input); result.xml
returned:The code:
就像我在评论中所说的那样,解析它会很痛苦,就像许多类似 Wiki 的语言一样。
如果您要编写语法并让解析器生成器为您创建解析器,而不是手动编写解析器,那么有多种选择。举几个:
我知道 ANTLR 可以做到这一点,但这不会是微不足道的,最重要的是,您需要掌握该工具(将需要一些时间!)。我没有在其他两个工具上花费太多时间,但怀疑它们能否胜任如此令人讨厌的语言的工作。
使用手写的解析器会让你快速入门,但调试、增强或重写它会很困难。编写语法并让解析器生成器为您创建解析器将使解析器的调试、增强和重写(通过语法)变得更加容易,但您将需要花费(相当)一些时间来学习使用该工具。
当然,如果编写正确的话,手写的解析器(很可能)会比生成的解析器更快。但它们之间的差异可能只有在使用大量源代码时才会明显。
抱歉,我没有如何使用手写解析器处理此问题的通用策略。
祝你好运!
Like I said in the comments, it'll be a pain to parse this, as are many of such Wiki-like languages.
If you're going to write a grammar and let a parser generator create a parser for you, opposed to hand-writing a parser, there are various options. To list a few:
I know ANTLR can do this, but it won't be trivial, and on top of that, you'll need to get to grips with the tool (which will take a bit of time!). I haven't spent too much time on the other two tools, but doubt they will be up to the job with such a nasty language.
Going for a hand-written parser will give you a quick start, but debugging, enhancing or rewriting it will be difficult. Writing a grammar and letting a parser generator create the parser for you will result in easier debugging, enhancing and rewriting of the parser (through the grammar), but you will need to spend (quite) some time learning to use the tool.
A hand-written parser will (most likely) be quicker than a generated one, if written properly, of course. But the difference between them might only be noticeable with large chunks of source.
Sorry, I don't have a general strategy how to handle this with a hand-written parser.
Best of luck!
此处提供了一个 Javascript 组织模式解析器。
There is a Javascript org-mode parser available here.