Interpret everything after LeftBracket as a string until the next RightBracket

[Solution] bottom to bottom Edit3

I am currently developing a new grammar (from certain requirements that I cannot change), and the following requirement creates a problem that I cannot solve at the moment. I am using Antlr4 with a C # target.

The syntax is as follows:

print [blabla ]

therefore, everything inside the brackets is considered a string. So is it:

print [3 + 2]

will print

3 + 2

Now I have lexer rules that will obviously match 3 as a whole. So, how can I create a parser rule that will parse anything until it is found "]? I currently have the following production:

control
: 
| Print expr
| Print LeftBracket printArg RightBracket
    ;

, , , . (, ) . , Lexer , RightBracket, , , , , .

, ,

EDIT: : :

Print LeftBracket printArg RightBracket
Repeat IntegerConstant LeftBracket body RightBracket
While LeftBracket expr RightBracket LeftBracket body RightBracket
If expr LeftBracket body RightBracket LeftBracket body RightBracket
SetPos LeftBracket IntegerConstant IntegerConstant RightBracket

EDIT2: , . , :

mode printMode;
WhitespacePrint
    :   [ \t]+
        -> skip
    ;
LeftBracketPrint : '[' -> popMode, pushMode(stringMode);
NotLeftBracket : ~'[' -> popMode;

mode stringMode;
String : ~']'+;
RightBracketPrint: ']' -> popMode;

pushMode (printMode) Print lexer ( ) [1 + 2] , . , print 1 + 2 ( 3), "print1", "1" NotLeftBracket. , ?

EDIT3: lookahead, :

mode printMode;
LeftBracketPrint : [ \t]+ '[' -> popMode, pushMode(stringMode);
WhitespacePrint
    :   [ \t]+ {_input.La(1) != '['}?
        -> skip, popMode
    ;

mode stringMode;
String : ~']'+;
RightBracketPrint: ']' -> popMode;
+4
2

BracketLiteral .

LeftBracket : '[' -> pushMode(BracketLiteralMode);

mode BracketLiteralMode;

  BracketLiteral : ~']'+;
  RightBracket : ']' -> popMode;

, , , BracketLiteral . , , .

+3

, , , ( ).

2 :

  • lexer , , (.. )
  • lexer , , ( ),

, , -, , :

print [ a ++++ 2 ]
+1

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


All Articles