开发者

How to have unstructured sections in a file parsed using Antlr

开发者 https://www.devze.com 2023-03-19 07:48 出处:网络
I am creating a translator from my language into many (all?) other object oriented languages.As part of the language I want to support being able to insert target language code sections into the file.

I am creating a translator from my language into many (all?) other object oriented languages. As part of the language I want to support being able to insert target language code sections into the file. This is actually rather similar to how Antlr supports actions in rules.

So I would like to be able to have the sections begin and end with curlies like this:

{ ...target lang code... }

The issue is that it is quite possible { ... } can show up in the target language code so I need to be able match pairs of curlies.

What I want to be able to do is something like this fragment that I've pulled into its own grammar:

grammar target_lang_block;

options
{
    output = AST;
}

entry   
    :   target_lang_block;

target_lang_block
    :   '{' target_lang_code* '}'
    ;


target_lang_code
    :   target_lang_block
    |   NO_CURLIES 
    ;       

WS
    :  (' ' | '开发者_运维知识库\r' | '\t' | '\n')+ {$channel = HIDDEN;}
    ;

NO_CURLIES  
    :   ~('{'|'}')+
    ;

This grammar works by itself (at least to the extent I have tested it).

However, when I put these rules into the larger language, NO_CURLIES seems to eat everything and cause MismatchedTokenExceptions.

I'm not sure how to deal with this situation, but it seems that what I want is to be able to turn NO_CURILES on and off based on if I'm in target_lang_block, but it does not seem that is possible.

Is it possible? Is there another way?

Thanks


Handle the target_lang_block inside the lexer instead:

Target_lang_block
  :  '{' (~('{' | '}') | Target_lang_block)* '}'
  ;

And remove NO_CURLIES, of course.

0

精彩评论

暂无评论...
验证码 换一张
取 消