Skip to content

Instantly share code, notes, and snippets.

@Alan-Liang
Created July 17, 2022 02:06
Show Gist options
  • Save Alan-Liang/07c65565dc8fce0e20d681357782c45b to your computer and use it in GitHub Desktop.
Save Alan-Liang/07c65565dc8fce0e20d681357782c45b to your computer and use it in GitHub Desktop.
Heredoc in ANTLR 4 demo
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