1#!/usr/bin/ruby 2# encoding: utf-8 3 4require 'antlr3/test/functional' 5 6class TestLexerRuleReference < ANTLR3::Test::Functional 7 8 inline_grammar( <<-'END' ) 9 lexer grammar RuleProperty; 10 options { 11 language = Ruby; 12 } 13 14 @lexer::init { 15 @properties = [] 16 } 17 @lexer::members { 18 attr_reader :properties 19 } 20 21 IDENTIFIER: 22 ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')* 23 { 24 @properties << [$text, $type, $line, $pos, $index, $channel, $start, $stop] 25 } 26 ; 27 WS: (' ' | '\n')+; 28 END 29 30 example "referencing lexer rule properties" do 31 lexer = RuleProperty::Lexer.new( "foobar _ab98 \n A12sdf" ) 32 tokens = lexer.map { |tk| tk } 33 34 lexer.properties.should have( 3 ).things 35 text, type, line, pos, index, channel, start, stop = lexer.properties[ 0 ] 36 text.should == 'foobar' 37 type.should == RuleProperty::TokenData::IDENTIFIER 38 line.should == 1 39 pos.should == 0 40 index.should == -1 41 channel.should == ANTLR3::DEFAULT_CHANNEL 42 start.should == 0 43 stop.should == 5 44 45 text, type, line, pos, index, channel, start, stop = lexer.properties[ 1 ] 46 text.should == '_ab98' 47 type.should == RuleProperty::TokenData::IDENTIFIER 48 line.should == 1 49 pos.should == 7 50 index.should == -1 51 channel.should == ANTLR3::DEFAULT_CHANNEL 52 start.should == 7 53 stop.should == 11 54 55 lexer.properties.should have( 3 ).things 56 text, type, line, pos, index, channel, start, stop = lexer.properties[ 2 ] 57 text.should == 'A12sdf' 58 type.should == RuleProperty::TokenData::IDENTIFIER 59 line.should == 2 60 pos.should == 1 61 index.should == -1 62 channel.should == ANTLR3::DEFAULT_CHANNEL 63 start.should == 15 64 stop.should == 20 65 end 66 67 68end 69 70class TestLexerRuleLabel < ANTLR3::Test::Functional 71 inline_grammar( <<-'END' ) 72 lexer grammar LexerRuleLabel; 73 options { 74 language = Ruby; 75 } 76 77 @members { attr_reader :token_text } 78 79 A: 'a'..'z' WS '0'..'9' 80 { 81 @token_text = $WS.text 82 } 83 ; 84 85 fragment WS : 86 ( ' ' 87 | '\t' 88 | ( '\n' 89 | '\r\n' 90 | '\r' 91 ) 92 )+ 93 { $channel = HIDDEN } 94 ; 95 END 96 97 example "referencing other token rule values with labels" do 98 lexer = LexerRuleLabel::Lexer.new 'a 2' 99 lexer.next_token 100 lexer.token_text.should == ' ' 101 end 102 103end 104