Poison

type()

SetTypeLexer.g4:

1
2
3
4
5
6
7
lexer grammar SetTypeLexer;

tokens { STRING }

DOUBLE: '"' .*? '"' -> type(STRING);
SINGLE: '\'' .*? '\'' -> type(STRING);
WS: [ \r\t\n]+ -> skip;

SetTypeParser.g4:

1
2
3
4
5
6
7
parser grammar SetTypeParser;

options { tokenVocab=SetTypeLexer; }

root
: STRING STRING EOF
;

Java Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.tree.ParseTree;

import java.io.IOException;
import java.net.URISyntaxException;

public class SetTypeParseTest {

public static void main(String[] args) throws URISyntaxException, IOException {
CharStream charStream = CharStreams.fromString(" \"aaa\" 'bbb' ");
Lexer lexer = new SetTypeLexer(charStream);

CommonTokenStream tokens = new CommonTokenStream(lexer);
SetTypeParser parser = new SetTypeParser(tokens);
ParseTree parseTree = parser.root();

System.out.println(parseTree.toStringTree(parser));
}

}

Output:

1
(root "aaa" 'bbb' <EOF>)
Reference

Lexer Rules - type()
antlrworks2/TemplateLexer.g4 at master · sharwell/antlrworks2 · GitHub