Created
July 17, 2022 02:06
-
-
Save Alan-Liang/07c65565dc8fce0e20d681357782c45b to your computer and use it in GitHub Desktop.
Heredoc in ANTLR 4 demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grammar Heredoc; | |
@lexer::members { | |
public boolean heredocValid (String str) { | |
String[] lines = str.split("\n"); | |
String first = lines[0], last = lines[lines.length - 1]; | |
return first.substring(2).equals(last); | |
} | |
} | |
Identifier: [a-z]+; | |
WS: ' ' -> skip; | |
Heredoc: | |
'<<' Identifier Newline | |
(.+? Newline)? | |
Identifier Newline {heredocValid(getText())}?; | |
Newline: '\n'; | |
program: (Identifier | Heredoc | Newline)+ EOF; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment