1#!/usr/bin/ruby
2# encoding: utf-8
3
4require 'antlr3/test/functional'
5
6class TestPredicateHoist < ANTLR3::Test::Functional
7
8  inline_grammar( <<-'END' )
9    grammar TestHoist;
10    options {
11        language = Ruby;
12    }
13
14    /* With this true, enum is seen as a keyword.  False, it's an identifier */
15    @parser::init {
16      @enable_enum = false
17    }
18    @members {
19      attr_accessor :enable_enum
20    }
21
22    stat returns [enumIs]
23        : identifier    {$enumIs = "ID"}
24        | enumAsKeyword {$enumIs = "keyword"}
25        ;
26
27    identifier
28        : ID
29        | enumAsID
30        ;
31
32    enumAsKeyword : {@enable_enum}? 'enum' ;
33
34    enumAsID : {!@enable_enum}? 'enum' ;
35
36    ID  :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
37        ;
38
39    INT :	('0'..'9')+
40        ;
41
42    WS  :   (   ' '
43            |   '\t'
44            |   '\r'
45            |   '\n'
46            )+
47            {$channel=HIDDEN}
48        ;
49  END
50
51
52  example "'enum' is a keyword" do
53    lexer = TestHoist::Lexer.new 'enum'
54    parser = TestHoist::Parser.new lexer
55    parser.enable_enum = true
56    parser.stat.should == 'keyword'
57  end
58
59  example "'enum' is an ID" do
60    lexer = TestHoist::Lexer.new 'enum'
61    parser = TestHoist::Parser.new lexer
62    parser.enable_enum = false
63    parser.stat.should == 'ID'
64  end
65
66end
67
68
69class TestSyntacticPredicate < ANTLR3::Test::Functional
70
71  inline_grammar( <<-'END' )
72    grammar SyntacticPredicate;
73    options {
74      language = Ruby;
75    }
76
77    @parser::members {
78      def emit_error_message(msg)
79        # do nothing
80      end
81      def report_error(error)
82        raise error
83      end
84    }
85
86    a: ((s+ P)=> s+ b)? E;
87    b: P 'foo';
88
89    s: S;
90
91
92    S: ' ';
93    P: '+';
94    E: '>';
95  END
96
97  example "rule with syntactic predicate" do
98    lexer = SyntacticPredicate::Lexer.new( '   +foo>' )
99    parser = SyntacticPredicate::Parser.new lexer
100    events = parser.a
101  end
102end
103