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