antlr3 1.8.0 → 1.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. data/History.txt +35 -0
  2. data/Manifest.txt +73 -0
  3. data/README.txt +6 -13
  4. data/java/RubyTarget.java +43 -19
  5. data/java/antlr-full-3.2.1.jar +0 -0
  6. data/lib/antlr3/debug.rb +2 -0
  7. data/lib/antlr3/debug/event-hub.rb +55 -55
  8. data/lib/antlr3/debug/record-event-listener.rb +2 -2
  9. data/lib/antlr3/debug/rule-tracer.rb +14 -14
  10. data/lib/antlr3/debug/socket.rb +47 -47
  11. data/lib/antlr3/debug/trace-event-listener.rb +8 -8
  12. data/lib/antlr3/main.rb +29 -9
  13. data/lib/antlr3/modes/ast-builder.rb +7 -7
  14. data/lib/antlr3/modes/filter.rb +19 -17
  15. data/lib/antlr3/profile.rb +34 -6
  16. data/lib/antlr3/recognizers.rb +50 -1
  17. data/lib/antlr3/streams.rb +19 -15
  18. data/lib/antlr3/streams/rewrite.rb +241 -229
  19. data/lib/antlr3/template/group-file-lexer.rb +6 -8
  20. data/lib/antlr3/template/group-file-parser.rb +16 -16
  21. data/lib/antlr3/template/group-file.rb +1 -1
  22. data/lib/antlr3/test/call-stack.rb +13 -13
  23. data/lib/antlr3/test/core-extensions.rb +69 -69
  24. data/lib/antlr3/test/functional.rb +0 -4
  25. data/lib/antlr3/test/grammar.rb +70 -70
  26. data/lib/antlr3/token.rb +41 -17
  27. data/lib/antlr3/tree.rb +11 -14
  28. data/lib/antlr3/tree/debug.rb +53 -53
  29. data/lib/antlr3/tree/visitor.rb +11 -11
  30. data/lib/antlr3/tree/wizard.rb +35 -35
  31. data/lib/antlr3/util.rb +18 -0
  32. data/lib/antlr3/version.rb +1 -1
  33. data/rakefile +1 -0
  34. data/samples/ANTLRv3Grammar.g +3 -3
  35. data/samples/JavaScript.g +702 -0
  36. data/samples/standard/C/C.g +543 -0
  37. data/samples/standard/C/C.tokens +175 -0
  38. data/samples/standard/C/C__testrig.st +0 -0
  39. data/samples/standard/C/c.rb +12 -0
  40. data/samples/standard/C/input +3479 -0
  41. data/samples/standard/C/output +171 -0
  42. data/samples/standard/LL-star/LLStar.g +101 -0
  43. data/samples/standard/LL-star/input +12 -0
  44. data/samples/standard/LL-star/ll-star.rb +12 -0
  45. data/samples/standard/LL-star/output +2 -0
  46. data/samples/standard/calc/Calculator.g +47 -0
  47. data/samples/standard/calc/Calculator.py +16 -0
  48. data/samples/standard/calc/Calculator.rb +28 -0
  49. data/samples/standard/cminus/CMinus.g +141 -0
  50. data/samples/standard/cminus/bytecode.group +80 -0
  51. data/samples/standard/cminus/cminus.rb +16 -0
  52. data/samples/standard/cminus/input +9 -0
  53. data/samples/standard/cminus/java.group +91 -0
  54. data/samples/standard/cminus/output +11 -0
  55. data/samples/standard/cminus/python.group +48 -0
  56. data/samples/standard/dynamic-scope/DynamicScopes.g +50 -0
  57. data/samples/standard/dynamic-scope/dynamic-scopes.rb +12 -0
  58. data/samples/standard/dynamic-scope/input +7 -0
  59. data/samples/standard/dynamic-scope/output +4 -0
  60. data/samples/standard/fuzzy/FuzzyJava.g +89 -0
  61. data/samples/standard/fuzzy/fuzzy.py +11 -0
  62. data/samples/standard/fuzzy/fuzzy.rb +9 -0
  63. data/samples/standard/fuzzy/input +13 -0
  64. data/samples/standard/fuzzy/output +12 -0
  65. data/samples/standard/hoisted-predicates/HoistedPredicates.g +40 -0
  66. data/samples/standard/hoisted-predicates/hoisted-predicates.rb +13 -0
  67. data/samples/standard/hoisted-predicates/input +1 -0
  68. data/samples/standard/hoisted-predicates/output +1 -0
  69. data/samples/standard/island-grammar/Javadoc.g +46 -0
  70. data/samples/standard/island-grammar/Simple.g +104 -0
  71. data/samples/standard/island-grammar/input +11 -0
  72. data/samples/standard/island-grammar/island.rb +12 -0
  73. data/samples/standard/island-grammar/output +16 -0
  74. data/samples/standard/java/Java.g +827 -0
  75. data/samples/standard/java/input +80 -0
  76. data/samples/standard/java/java.rb +13 -0
  77. data/samples/standard/java/output +1 -0
  78. data/samples/standard/python/Python.g +718 -0
  79. data/samples/standard/python/PythonTokenSource.rb +107 -0
  80. data/samples/standard/python/input +210 -0
  81. data/samples/standard/python/output +24 -0
  82. data/samples/standard/python/python.rb +14 -0
  83. data/samples/standard/rakefile +18 -0
  84. data/samples/standard/scopes/SymbolTable.g +66 -0
  85. data/samples/standard/scopes/input +12 -0
  86. data/samples/standard/scopes/output +3 -0
  87. data/samples/standard/scopes/scopes.rb +12 -0
  88. data/samples/standard/simplecTreeParser/SimpleC.g +113 -0
  89. data/samples/standard/simplecTreeParser/SimpleCWalker.g +64 -0
  90. data/samples/standard/simplecTreeParser/input +12 -0
  91. data/samples/standard/simplecTreeParser/output +1 -0
  92. data/samples/standard/simplecTreeParser/simplec.rb +18 -0
  93. data/samples/standard/treeparser/Lang.g +24 -0
  94. data/samples/standard/treeparser/LangDumpDecl.g +17 -0
  95. data/samples/standard/treeparser/input +1 -0
  96. data/samples/standard/treeparser/output +2 -0
  97. data/samples/standard/treeparser/treeparser.rb +18 -0
  98. data/samples/standard/tweak/Tweak.g +68 -0
  99. data/samples/standard/tweak/input +9 -0
  100. data/samples/standard/tweak/output +16 -0
  101. data/samples/standard/tweak/tweak.rb +13 -0
  102. data/samples/standard/xml/README +16 -0
  103. data/samples/standard/xml/XML.g +123 -0
  104. data/samples/standard/xml/input +21 -0
  105. data/samples/standard/xml/output +39 -0
  106. data/samples/standard/xml/xml.rb +9 -0
  107. data/templates/Ruby.stg +4 -4
  108. data/test/functional/ast-output/auto-ast.rb +0 -5
  109. data/test/functional/ast-output/rewrites.rb +4 -4
  110. data/test/unit/test-scope.rb +45 -0
  111. metadata +96 -8
@@ -0,0 +1,171 @@
1
+ define type size_t
2
+ define type wchar_t
3
+ define type _Bool
4
+ define type div_t
5
+ define type ldiv_t
6
+ define type __u_char
7
+ define type __u_short
8
+ define type __u_int
9
+ define type __u_long
10
+ define type __int8_t
11
+ define type __uint8_t
12
+ define type __int16_t
13
+ define type __uint16_t
14
+ define type __int32_t
15
+ define type __uint32_t
16
+ define type __int64_t
17
+ define type __uint64_t
18
+ define type __quad_t
19
+ define type __u_quad_t
20
+ define type __dev_t
21
+ define type __uid_t
22
+ define type __gid_t
23
+ define type __ino_t
24
+ define type __ino64_t
25
+ define type __mode_t
26
+ define type __nlink_t
27
+ define type __off_t
28
+ define type __off64_t
29
+ define type __pid_t
30
+ define type __fsid_t
31
+ define type __clock_t
32
+ define type __rlim_t
33
+ define type __rlim64_t
34
+ define type __id_t
35
+ define type __time_t
36
+ define type __useconds_t
37
+ define type __suseconds_t
38
+ define type __daddr_t
39
+ define type __swblk_t
40
+ define type __key_t
41
+ define type __clockid_t
42
+ define type __timer_t
43
+ define type __blksize_t
44
+ define type __blkcnt_t
45
+ define type __blkcnt64_t
46
+ define type __fsblkcnt_t
47
+ define type __fsblkcnt64_t
48
+ define type __fsfilcnt_t
49
+ define type __fsfilcnt64_t
50
+ define type __ssize_t
51
+ define type __loff_t
52
+ define type __qaddr_t
53
+ define type __caddr_t
54
+ define type __intptr_t
55
+ define type __socklen_t
56
+ define type u_char
57
+ define type u_short
58
+ define type u_int
59
+ define type u_long
60
+ define type quad_t
61
+ define type u_quad_t
62
+ define type fsid_t
63
+ define type loff_t
64
+ define type ino_t
65
+ define type dev_t
66
+ define type gid_t
67
+ define type mode_t
68
+ define type nlink_t
69
+ define type uid_t
70
+ define type off_t
71
+ define type pid_t
72
+ define type id_t
73
+ define type ssize_t
74
+ define type daddr_t
75
+ define type caddr_t
76
+ define type key_t
77
+ define type time_t
78
+ define type clockid_t
79
+ define type timer_t
80
+ define type ulong
81
+ define type ushort
82
+ define type uint
83
+ define type int8_t
84
+ define type int16_t
85
+ define type int32_t
86
+ define type int64_t
87
+ define type u_int8_t
88
+ define type u_int16_t
89
+ define type u_int32_t
90
+ define type u_int64_t
91
+ define type register_t
92
+ define type __sig_atomic_t
93
+ define type __sigset_t
94
+ define type sigset_t
95
+ define type suseconds_t
96
+ define type __fd_mask
97
+ define type fd_set
98
+ define type fd_mask
99
+ define type blkcnt_t
100
+ define type fsblkcnt_t
101
+ define type fsfilcnt_t
102
+ define type _pthread_descr
103
+ define type pthread_attr_t
104
+ define type __pthread_cond_align_t
105
+ define type pthread_cond_t
106
+ define type pthread_condattr_t
107
+ define type pthread_key_t
108
+ define type pthread_mutex_t
109
+ define type pthread_mutexattr_t
110
+ define type pthread_once_t
111
+ define type pthread_t
112
+ define type __compar_fn_t
113
+ define type sig_atomic_t
114
+ define type __sighandler_t
115
+ define type sig_t
116
+ define type sigval_t
117
+ define type siginfo_t
118
+ define type sigevent_t
119
+ define type stack_t
120
+ define type chtype
121
+ define type FILE
122
+ define type __FILE
123
+ define type wint_t
124
+ define type __mbstate_t
125
+ define type _G_fpos_t
126
+ define type _G_fpos64_t
127
+ define type __gconv_fct
128
+ define type __gconv_btowc_fct
129
+ define type __gconv_init_fct
130
+ define type __gconv_end_fct
131
+ define type __gconv_trans_fct
132
+ define type __gconv_trans_context_fct
133
+ define type __gconv_trans_query_fct
134
+ define type __gconv_trans_init_fct
135
+ define type __gconv_trans_end_fct
136
+ define type __gconv_t
137
+ define type _G_iconv_t
138
+ define type _G_int16_t
139
+ define type _G_int32_t
140
+ define type _G_uint16_t
141
+ define type _G_uint32_t
142
+ define type __gnuc_va_list
143
+ define type _IO_lock_t
144
+ define type _IO_FILE
145
+ define type __io_read_fn
146
+ define type __cookie
147
+ define type __buf
148
+ define type __nbytes
149
+ define type __io_write_fn
150
+ define type __cookie
151
+ define type __buf
152
+ define type __n
153
+ define type __io_seek_fn
154
+ define type __cookie
155
+ define type __pos
156
+ define type __w
157
+ define type __io_close_fn
158
+ define type __cookie
159
+ define type fpos_t
160
+ define type va_list
161
+ define type NCURSES_BOOL
162
+ define type SCREEN
163
+ define type WINDOW
164
+ define type attr_t
165
+ define type mmask_t
166
+ define type MEVENT
167
+ define type cc_t
168
+ define type speed_t
169
+ define type tcflag_t
170
+ define type TERMTYPE
171
+ define type TERMINAL
@@ -0,0 +1,101 @@
1
+ grammar LLStar;
2
+
3
+ options { language = Ruby; }
4
+
5
+ program
6
+ : declaration+
7
+ ;
8
+
9
+ /** In this rule, the functionHeader left prefix on the last two
10
+ * alternatives is not LL(k) for a fixed k. However, it is
11
+ * LL(*). The LL(*) algorithm simply scans ahead until it sees
12
+ * either the ';' or the '{' of the block and then it picks
13
+ * the appropriate alternative. Lookhead can be arbitrarily
14
+ * long in theory, but is <=10 in most cases. Works great.
15
+ * Use ANTLRWorks to see the look use (step by Location)
16
+ * and look for blue tokens in the input window pane. :)
17
+ */
18
+ declaration
19
+ : variable
20
+ | functionHeader ';'
21
+ { puts( $functionHeader.name + " is a declaration") }
22
+ | functionHeader block
23
+ { puts( $functionHeader.name + " is a definition") }
24
+ ;
25
+
26
+ variable
27
+ : type declarator ';'
28
+ ;
29
+
30
+ declarator
31
+ : ID
32
+ ;
33
+
34
+ functionHeader returns [name]
35
+ : type ID '(' ( formalParameter ( ',' formalParameter )* )? ')'
36
+ {$name = $ID.text}
37
+ ;
38
+
39
+ formalParameter
40
+ : type declarator
41
+ ;
42
+
43
+ type
44
+ : 'int'
45
+ | 'char'
46
+ | 'void'
47
+ | ID
48
+ ;
49
+
50
+ block
51
+ : '{'
52
+ variable*
53
+ stat*
54
+ '}'
55
+ ;
56
+
57
+ stat: forStat
58
+ | expr ';'
59
+ | block
60
+ | assignStat ';'
61
+ | ';'
62
+ ;
63
+
64
+ forStat
65
+ : 'for' '(' assignStat ';' expr ';' assignStat ')' block
66
+ ;
67
+
68
+ assignStat
69
+ : ID '=' expr
70
+ ;
71
+
72
+ expr: condExpr
73
+ ;
74
+
75
+ condExpr
76
+ : aexpr ( ('==' | '<') aexpr )?
77
+ ;
78
+
79
+ aexpr
80
+ : atom ( '+' atom )*
81
+ ;
82
+
83
+ atom
84
+ : ID
85
+ | INT
86
+ | '(' expr ')'
87
+ ;
88
+
89
+ ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
90
+ ;
91
+
92
+ INT : ('0'..'9')+
93
+ ;
94
+
95
+ WS : ( ' '
96
+ | '\t'
97
+ | '\r'
98
+ | '\n'
99
+ )+
100
+ {$channel=HIDDEN}
101
+ ;
@@ -0,0 +1,12 @@
1
+ char c;
2
+ int x;
3
+
4
+ void bar(int x);
5
+
6
+ int foo(int y, char d) {
7
+ int i;
8
+ for (i=0; i<3; i=i+1) {
9
+ x=3;
10
+ y=5;
11
+ }
12
+ }
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/ruby
2
+ # encoding: utf-8
3
+ $:.unshift( File.dirname( __FILE__ ) )
4
+ require 'LLStarLexer'
5
+ require 'LLStarParser'
6
+
7
+ for file in ARGV
8
+ input = ANTLR3::FileStream.new( file )
9
+ lexer = LLStar::Lexer.new( input )
10
+ parser = LLStar::Parser.new( lexer )
11
+ parser.program
12
+ end
@@ -0,0 +1,2 @@
1
+ bar is a declaration
2
+ foo is a definition
@@ -0,0 +1,47 @@
1
+ grammar Calculator;
2
+ options {
3
+ language = Ruby;
4
+ }
5
+
6
+ evaluate returns [result]
7
+ : r=expression { $result = $r.result }
8
+ ;
9
+
10
+ expression returns [result]
11
+ : r=mult
12
+ ( '+' r2=mult { $r.result += $r2.result }
13
+ | '-' r2=mult { $r.result -= $r2.result }
14
+ )* { $result = $r.result }
15
+ ;
16
+
17
+ mult returns [result]
18
+ : r=log
19
+ ( '*' r2=log { $r.result *= $r2.result }
20
+ | '/' r2=log { $r.result /= $r2.result } //
21
+ )* { $result = $r.result };
22
+
23
+ log returns [result]
24
+ : 'ln' r=exp { $result = Math.log( r ) }
25
+ | r=exp { $result = $r.result }
26
+ ;
27
+
28
+ exp returns [result]
29
+ : r=atom ( '^' r2=atom { $r.result **= $r2.result } )? { $result = $r.result }
30
+ ;
31
+
32
+ atom returns [result]
33
+ : n=INTEGER { $result = Integer( $n.text ) }
34
+ | n=DECIMAL { $result = $n.text.to_f }
35
+ | '(' r=expression { $result = $r.result } ')'
36
+ | 'PI' { $result = Math::PI }
37
+ | 'E' { $result = Math::E }
38
+ ;
39
+
40
+ INTEGER: DIGIT+;
41
+
42
+ DECIMAL: DIGIT+ '.' DIGIT+;
43
+
44
+ fragment
45
+ DIGIT: '0'..'9';
46
+
47
+ WS: (' ' | '\n' | '\t')+ { $channel = HIDDEN };
@@ -0,0 +1,16 @@
1
+ import antlr3
2
+ from CalculatorLexer import CalculatorLexer
3
+ from CalculatorParser import CalculatorParser
4
+
5
+
6
+ while True:
7
+ expr = raw_input('>>> ')
8
+ if expr == '':
9
+ break
10
+
11
+ cStream = antlr3.StringStream(expr)
12
+ lexer = CalculatorLexer(cStream)
13
+ tStream = antlr3.CommonTokenStream(lexer)
14
+ parser = CalculatorParser(tStream)
15
+ result = parser.evaluate()
16
+ print result
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/ruby
2
+ # encoding: utf-8
3
+
4
+ $:.unshift( File.dirname( __FILE__ ) )
5
+ require 'CalculatorLexer'
6
+ require 'CalculatorParser'
7
+
8
+ fetch =
9
+ begin
10
+ require 'readline'
11
+ proc do
12
+ Readline.readline( ">>> ", true )
13
+ end
14
+ rescue LoadError
15
+ proc do
16
+ print( ">>> " )
17
+ l = $stdin.gets and l.chomp
18
+ end
19
+ end
20
+
21
+ puts( "enter calculator expressions or `quit' to exit")
22
+ loop do
23
+ case line = fetch.call
24
+ when /q(uit)?/i, /e(xit)?/i, nil then break
25
+ else
26
+ printf( "\t=> %s\n", Calculator::Parser.new( line ).evaluate )
27
+ end
28
+ end
@@ -0,0 +1,141 @@
1
+ grammar CMinus;
2
+ options {
3
+ language = Ruby;
4
+ output = template;
5
+ }
6
+
7
+ scope slist {
8
+ locals;
9
+ stats;
10
+ }
11
+
12
+ @members {
13
+ load_templates( 'java.group' )
14
+ }
15
+
16
+ program
17
+ scope {
18
+ globals;
19
+ functions;
20
+ }
21
+ @init {
22
+ $program::globals = []
23
+ $program::functions = []
24
+ }
25
+ : declaration+
26
+ -> program(globals={$program::globals},functions={$program::functions})
27
+ ;
28
+
29
+ declaration
30
+ : variable { $program::globals << $variable.st }
31
+ | f=function { $program::functions << $f.st }
32
+ ;
33
+
34
+ // ack is $function.st ambig? It can mean the rule's dyn scope or
35
+ // the ref in this rule. Ack.
36
+
37
+ variable
38
+ : type declarator ';'
39
+ -> { $function.length > 0 and $function::name.nil? }?
40
+ globalVariable(type={$type.st},name={$declarator.st})
41
+ -> variable(type={$type.st},name={$declarator.st})
42
+ ;
43
+
44
+ declarator
45
+ : ID -> { $ID.text }
46
+ ;
47
+
48
+ function
49
+ scope {
50
+ name;
51
+ }
52
+ scope slist;
53
+ @init {
54
+ $slist::locals = []
55
+ $slist::stats = []
56
+ }
57
+ : type ID { $function::name = $ID.text }
58
+ '(' ( p+=formalParameter ( ',' p+=formalParameter )* )? ')'
59
+ block
60
+ -> function(type={$type.st}, name={$function::name},
61
+ locals={$slist::locals},
62
+ stats={$slist::stats},
63
+ args={$p})
64
+ ;
65
+
66
+ formalParameter
67
+ : type declarator
68
+ -> parameter(type={$type.st},name={$declarator.st})
69
+ ;
70
+
71
+ type
72
+ : 'int' -> type_int()
73
+ | 'char' -> type_char()
74
+ | ID -> type_user_object(name={$ID.text})
75
+ ;
76
+
77
+ block
78
+ : '{'
79
+ ( variable { $slist::locals << $variable.st } )*
80
+ ( stat { $slist::stats << $stat.st } )*
81
+ '}'
82
+ ;
83
+
84
+ stat
85
+ scope slist;
86
+ @init {
87
+ $slist::locals = []
88
+ $slist::stats = []
89
+ }
90
+ : forStat -> { $forStat.st }
91
+ | expr ';' -> statement( expr={$expr.st} )
92
+ | block -> statementList( locals={$slist::locals}, stats={$slist::stats} )
93
+ | assignStat ';' -> { $assignStat.st }
94
+ | ';' -> { ';' }
95
+ ;
96
+
97
+ forStat
98
+ scope slist;
99
+ @init {
100
+ $slist::locals = []
101
+ $slist::stats = []
102
+ }
103
+ : 'for' '(' e1=assignStat ';' e2=expr ';' e3=assignStat ')' block
104
+ -> forLoop(e1={$e1.st},e2={$e2.st},e3={$e3.st},locals={$slist::locals}, stats={$slist::stats})
105
+ ;
106
+
107
+ assignStat
108
+ : ID '=' expr -> assign(lhs={$ID.text}, rhs={$expr.st})
109
+ ;
110
+
111
+ expr: condExpr -> {$condExpr.st}
112
+ ;
113
+
114
+ condExpr
115
+ : a=aexpr
116
+ ( ( '==' b=aexpr -> equals(left={$a.st},right={$b.st})
117
+ | '<' b=aexpr -> lessThan(left={$a.st},right={$b.st})
118
+ )
119
+ | -> {$a.st} // else just aexpr
120
+ )
121
+ ;
122
+
123
+ aexpr
124
+ : (a=atom -> {$a.st})
125
+ ( '+' b=atom -> add(left={$aexpr.st}, right={$b.st}) )*
126
+ ;
127
+
128
+ atom
129
+ : ID -> refVar(id={$ID.text})
130
+ | INT -> iconst(value={$INT.text})
131
+ | '(' expr ')' -> {$expr.st}
132
+ ;
133
+
134
+ ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
135
+ ;
136
+
137
+ INT : ('0'..'9')+
138
+ ;
139
+
140
+ WS : (' ' | '\t' | '\r' | '\n')+ {$channel=HIDDEN;}
141
+ ;