aboutsummaryrefslogtreecommitdiff
path: root/runtime/Ruby/test/functional/parser/predicates.rb
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/Ruby/test/functional/parser/predicates.rb')
-rw-r--r--runtime/Ruby/test/functional/parser/predicates.rb102
1 files changed, 102 insertions, 0 deletions
diff --git a/runtime/Ruby/test/functional/parser/predicates.rb b/runtime/Ruby/test/functional/parser/predicates.rb
new file mode 100644
index 0000000..3b4fb29
--- /dev/null
+++ b/runtime/Ruby/test/functional/parser/predicates.rb
@@ -0,0 +1,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