aboutsummaryrefslogtreecommitdiff
path: root/runtime/Ruby/test/functional/parser/predicates.rb
blob: 3b4fb2976013c6a6b4edcb61d5c32542700fdfc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/ruby
# encoding: utf-8

require 'antlr3/test/functional'

class TestPredicateHoist < ANTLR3::Test::Functional

  inline_grammar( <<-'END' )
    grammar TestHoist;
    options {
        language = Ruby;
    }
    
    /* With this true, enum is seen as a keyword.  False, it's an identifier */
    @parser::init {
      @enable_enum = false
    }
    @members {
      attr_accessor :enable_enum
    }
    
    stat returns [enumIs]
        : identifier    {$enumIs = "ID"}
        | enumAsKeyword {$enumIs = "keyword"}
        ;
    
    identifier
        : ID
        | enumAsID
        ;
    
    enumAsKeyword : {@enable_enum}? 'enum' ;
    
    enumAsID : {!@enable_enum}? 'enum' ;
    
    ID  :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
        ;
    
    INT :	('0'..'9')+
        ;
    
    WS  :   (   ' '
            |   '\t'
            |   '\r'
            |   '\n'
            )+
            {$channel=HIDDEN}
        ;
  END
  
  
  example "'enum' is a keyword" do
    lexer = TestHoist::Lexer.new 'enum'
    parser = TestHoist::Parser.new lexer
    parser.enable_enum = true
    parser.stat.should == 'keyword'
  end
  
  example "'enum' is an ID" do
    lexer = TestHoist::Lexer.new 'enum'
    parser = TestHoist::Parser.new lexer
    parser.enable_enum = false
    parser.stat.should == 'ID'
  end
  
end


class TestSyntacticPredicate < ANTLR3::Test::Functional

  inline_grammar( <<-'END' )
    grammar SyntacticPredicate;
    options {
      language = Ruby;
    }
    
    @parser::members {
      def emit_error_message(msg)
        # do nothing
      end
      def report_error(error)
        raise error
      end
    }
    
    a: ((s+ P)=> s+ b)? E;
    b: P 'foo';
    
    s: S;
    
    
    S: ' ';
    P: '+';
    E: '>';
  END
  
  example "rule with syntactic predicate" do
    lexer = SyntacticPredicate::Lexer.new( '   +foo>' )
    parser = SyntacticPredicate::Parser.new lexer
    events = parser.a
  end
end