Прошло некоторое время, когда я пытался заставить себя написать парсер в 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 будет началом тега <p>
. Нумерация предназначена только для отслеживания вложенности и изменения списка.)
Я задавался вопросом о стратегии синтаксического анализа таких значительных блоков с белым пространством, очевидно, что я могу разбираться по строкам без каких-либо отступлений или ничего, поэтому это должно быть довольно просто, но по какой-то причине мне не удавалось сделай это. Я попытался получить вдохновение от парсеров Markdown или таких вещей, которые, как предполагается, будут иметь похожие функции, но они мне показались (для тех, которые я видел) очень хаки, полные регулярных выражений, и я надеялся, что смогу написать что-то более чистое (org -mode "грамматика" достаточно велика, когда вы задумываетесь об этом, она будет постепенно расти, и я бы хотел, чтобы все это было ремонтируемым и позволяло легко подключать новые функции).
Может ли кто-нибудь с опытом разбирать такие вещи, может дать мне несколько указателей?