1use strict;
2use warnings;
3
4use FindBin;
5use lib qw( t/lib );
6
7use File::Slurp;
8
9use Test::More;
10use ANTLR::Runtime::Test;
11
12plan tests => 2;
13
14sub grammar_file {
15    my ($file) = @_;
16    return read_file("t/$file");
17}
18
19# A simple test: try to lex one possible token.
20g_test_output_is({ grammar => <<'GRAMMAR', test_program => <<'CODE', expected => <<'OUTPUT' });
21/* This is a comment.  Note that we're in the ANTLR grammar here, so it's not
22   a Perl '#' comment, and may be multi line... */
23// ... or a single line comment
24lexer grammar INTLexer;
25/* Set target language to Perl5. */
26options { language = Perl5; }
27
28/* Lexer rule for an integer. */
29INT : '0'..'9'+;
30GRAMMAR
31use strict;
32use warnings;
33
34use ANTLR::Runtime::ANTLRStringStream;
35use INTLexer;
36
37my $input = ANTLR::Runtime::ANTLRStringStream->new({ input => '123' });
38my $lexer = INTLexer->new({ input => $input });
39while ((my $_ = $lexer->next_token())) {
40    print $_->get_text(), "\n";
41}
42CODE
43123
44OUTPUT
45
46# Multiple choice, including 'skip' and 'hide' actions.
47g_test_output_is({ grammar => <<'GRAMMAR', test_program => <<'CODE', expected => <<'OUTPUT' });
48lexer grammar IDLexer;
49options { language = Perl5; }
50
51ID      : ('a'..'z'|'A'..'Z')+ ;
52INT     : '0'..'9'+ ;
53NEWLINE : '\r'? '\n'  { $self->skip() } ;
54WS      : (' '|'\t')+ { $channel = HIDDEN } ;
55GRAMMAR
56use strict;
57use warnings;
58
59use ANTLR::Runtime::ANTLRStringStream;
60use IDLexer;
61
62my $input = ANTLR::Runtime::ANTLRStringStream->new({ input => "Hello World!\n42\n" });
63my $lexer = IDLexer->new({ input => $input });
64
65while (1) {
66    my $token = $lexer->next_token();
67    last if $token->get_type() == IDLexer->EOF;
68
69    print "text: '", $token->get_text(), "'\n";
70    print "type: ",  $token->get_type(), "\n";
71    print "pos: ",   $token->get_line(), ':', $token->get_char_position_in_line(), "\n";
72    print "channel: ",     $token->get_channel(), "\n";
73    print "token index: ", $token->get_token_index(), "\n";
74    print "\n";
75}
76CODE
77text: 'Hello'
78type: 4
79pos: 1:0
80channel: 0
81token index: -1
82
83text: ' '
84type: 7
85pos: 1:5
86channel: 99
87token index: -1
88
89text: 'World'
90type: 4
91pos: 1:6
92channel: 0
93token index: -1
94
95text: '42'
96type: 5
97pos: 2:0
98channel: 0
99token index: -1
100
101OUTPUT
102
103=begin SKIP doesn't compile yet
104
105g_test_output_is({ grammar => scalar grammar_file('XMLLexer.g'), test_program => <<'CODE', expected => <<'OUTPUT' });
106use English qw( -no_match_vars );
107use ANTLR::Runtime::ANTLRStringStream;
108use XMLLexer;
109
110use strict;
111use warnings;
112
113my $input = ANTLR::Runtime::ANTLRStringStream->new(<< 'XML');
114<?xml version='1.0'?>
115<test>foo</test>
116XML
117my $lexer = IDLexer->new($input);
118while ((my $_ = $lexer->next_token())) {
119}
120CODE
121XML declaration
122PCDATA: "foo"
123OUTPUT
124}
125
126=end SKIP
127