CSS 2.1 parsing with correct CSS syntax conventions in ANTLR

CSS2.1 grammar includes strong consulting so as not to parse CSS directly in this way, "because it does not express conventions , only CSS 2.1 syntax.

Indeed, any parser that ignores these parsing conventions (as we tried to do), encounters problems when working with pages containing errors or unknown constructs.

Therefore, we would like our ANTLR CSS2.1 parser, which currently does not comply with cross-compatibility and error handling compatibility conventions, to somehow use the parse tree generated by the underlying grammar, which includes parsing conventions. (Perhaps the latter could have been created by another ANTLR parser.)

Is this a smart approach? Are there any well-understood methods for doing this?

Again, the goal is to create a robust CSS2.1 parser that intelligently handles errors and new constructs in accordance with CSS parsing conventions.

+4
source share
1 answer

We went with the general approach above, which we thought might work; he did.

In short, we have two ANTLR parsers: one for basic CSS grammar and the other for CSS2.1 grammar. The CSS2.1 parser can be executed independently of the main CSS parser. However, this is not how it is actually used.

The main CSS parser is used to build the main parsing tree. The rule actions re-process the text using the appropriate entry points into the CSS 2.1 grammar to create the same C # objects that the CSS2.1 grammar could create when it was executed offline. For example, the rule set action in the CSS parser core reanalyzes the consistent text using the rule set entry point in CSS 2.1 grammar, and adds the results to the result.

A few important points that took a long time to figure out:

  • ANTLR Parser rules called from external code are different in how they handle EOF compared to entry points called other rules.

  • The basic CSS grammar should be supplemented depending on what level CSS is actually translated without violating parsing rules. One example is the @media at-rule, whose block contains a set of rules that you need to analyze as much as possible using parsing conventions before passing them to the CSS2.1 parser.

Hope this helps others who want to do the same.

+2
source

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


All Articles