Block comment markup with ANTLR

This is what bothered me for a while. How can I parse the following text in the HTML below using ANTLR? I don’t seem to wrap my head around it.

Any ideas?

Markdown:

> first line
> second line
> & gt nested quote

HTML output:

<blockquote>
  <p>first line
  second line</p>
  <blockquote>
    <p>nested quote</p>
  </blockquote>
</blockquote>
+3
source share
1 answer

It's funny that you mentioned this because I dealt with this issue last week. See JMD, Markdown, and a brief overview of analysis and compilers . I am working on a true Markdown analyzer and I tried it with ANTLR.

There are several ways you can handle this.

-, :

BLOCK_QUOTE : '>' (' ' | '\t')? ;

, , .

- , , :

@members {
  int quoteDepth = 0;
}

BLOCK_QUOTE : '\n' (q+='>' (' ' | '\t')?)+
  { if ($q.size() > quoteDepth) /* emit one or more START_QUOTE tokens */
    else if ($q.size() < quoteDepth /* emit one or more END_QUOTE tokens */
    quoteDepth = $q.size(); }

, . .

, Markdown , .

, , , , , . ( ) The Definitive ANTLR Reference: .

ANTLR . , , .

+7

Source: https://habr.com/ru/post/1728120/


All Articles