antlr4-native 2.1.0 → 2.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 15c061ecc5402e131b0bb299f653624452168b857a20010f6243e1415d74fe43
4
- data.tar.gz: c9c1dcbdcb46daedcdfbe219dee93d1f18512fbbeeb26808a5d2acb3281db73c
3
+ metadata.gz: 417a38bde6ca6dfdeca982ed80f0258c00c18feeda5cfecbaab7a5784e941501
4
+ data.tar.gz: 99531b3de905a08734dc2f421dc8b4e488850ab0021a6619c0581c6a810590d7
5
5
  SHA512:
6
- metadata.gz: 0c973edc74b86e8fbf2193832c3555aa1e93a52bc60dc9736752c0be35a18cee3a1dd5d68cb262603467e0586a4eb22c74b38589246eb6d34edba6d4e4fe54c2
7
- data.tar.gz: e6b89735c4083c6c51b6260f751048aeec4187da2a93d286af7cf2778d490fb5d82a9dff958af7285229f790f3a8e1f537581a5946feda441ebf95e1bd93ffa8
6
+ metadata.gz: bad5784b07eb34c221c4a1a7a8311dd9c6998a837c5b5f13605564367aa15269055c3046e4894d531b4e9512467200896f0eb0495f08f46972593367d1143012
7
+ data.tar.gz: a0f02bfbbbff0fb8d06849623f8ced4046ebb2feb28a1f21d48bd03f2d017887c41a289777f30f4016205d01a49e3c77d8df9a83352a9da6b05ba43dc81148b3
data/README.md CHANGED
@@ -79,6 +79,7 @@ Finally, if you override `#initialize` in your visitor subclasses, don't forget
79
79
 
80
80
  ## Caveats
81
81
 
82
+ 1. Avoid retaining references to contexts, tokens, etc anywhere in your Ruby code. Contexts (i.e. the `ctx` variables in the examples above) and other objects that are created by ANTLR's C++ runtime are automatically cleaned up without the Ruby interpreter's knowledge. You'll almost surely see a segfault if you retain a reference to one of these objects and try to use it after the call to `Parser#visit`.
82
83
  1. Due to an ANTLR limitation, parsers cannot be used in a multi-threaded environment, even if each parser instance is used entirely in the context of a single thread (i.e. parsers are not shared between threads). According to the ANTLR C++ developers, parsers should be threadsafe. Unfortunately firsthand experience has proven otherwise. Your mileage may vary.
83
84
  1. The description of this gem says "(almost) any ANTLR4 grammar" because many grammars contain target-specific code. For example, the Python3 grammar referenced in the examples above contains inline Java code that the C++ compiler won't understand. You'll need to port any such code to C++ before you'll be able to compile and use the native extension.
84
85
 
@@ -25,7 +25,7 @@ module Antlr4Native
25
25
  end
26
26
 
27
27
  def gem_name
28
- @gem_name ||= dasherize(parser_ns)
28
+ @gem_name ||= underscore(parser_ns)
29
29
  end
30
30
 
31
31
  def antlr_ns
@@ -316,7 +316,7 @@ module Antlr4Native
316
316
  <<~END
317
317
  extern "C"
318
318
  void Init_#{ext_name}() {
319
- Module rb_m#{parser_ns} = define_module("#{parser_ns}");
319
+ Module rb_m#{parser_ns} = define_module("#{capitalize(parser_ns)}");
320
320
 
321
321
  rb_cToken = define_class_under<Token>(rb_m#{parser_ns}, "Token")
322
322
  .define_method("text", &Token::getText)
@@ -346,8 +346,8 @@ module Antlr4Native
346
346
  rb_cParser = define_class_under<ParserProxy>(rb_m#{parser_ns}, "Parser")
347
347
  .define_singleton_function("parse", &ParserProxy::parse)
348
348
  .define_singleton_function("parse_file", &ParserProxy::parseFile)
349
- .define_method("#{parser_root_method}", &ParserProxy::#{parser_root_method}, Return().keepAlive())
350
- .define_method("visit", &ParserProxy::visit, Return().keepAlive());
349
+ .define_method("#{parser_root_method}", &ParserProxy::#{parser_root_method})
350
+ .define_method("visit", &ParserProxy::visit);
351
351
 
352
352
  #{class_wrappers_str(' ')}
353
353
  }
@@ -1,3 +1,3 @@
1
1
  module Antlr4Native
2
- VERSION = '2.1.0'
2
+ VERSION = '2.2.1'
3
3
  end
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem 'pry-byebug'
7
+ gem 'antlr4-native', path: '../../../'
8
+ end
9
+
10
+ group :development, :test do
11
+ gem 'rake'
12
+ end
@@ -0,0 +1,335 @@
1
+ /*
2
+ BSD License
3
+
4
+ Copyright (c) 2013, Kazunori Sakamoto
5
+ Copyright (c) 2016, Alexander Alexeev
6
+ All rights reserved.
7
+
8
+ Redistribution and use in source and binary forms, with or without
9
+ modification, are permitted provided that the following conditions
10
+ are met:
11
+
12
+ 1. Redistributions of source code must retain the above copyright
13
+ notice, this list of conditions and the following disclaimer.
14
+ 2. Redistributions in binary form must reproduce the above copyright
15
+ notice, this list of conditions and the following disclaimer in the
16
+ documentation and/or other materials provided with the distribution.
17
+ 3. Neither the NAME of Rainer Schuster nor the NAMEs of its contributors
18
+ may be used to endorse or promote products derived from this software
19
+ without specific prior written permission.
20
+
21
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25
+ HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
+
33
+ This grammar file derived from:
34
+
35
+ Lua 5.3 Reference Manual
36
+ http://www.lua.org/manual/5.3/manual.html
37
+
38
+ Lua 5.2 Reference Manual
39
+ http://www.lua.org/manual/5.2/manual.html
40
+
41
+ Lua 5.1 grammar written by Nicolai Mainiero
42
+ http://www.antlr3.org/grammar/1178608849736/Lua.g
43
+
44
+ Tested by Kazunori Sakamoto with Test suite for Lua 5.2 (http://www.lua.org/tests/5.2/)
45
+
46
+ Tested by Alexander Alexeev with Test suite for Lua 5.3 http://www.lua.org/tests/lua-5.3.2-tests.tar.gz
47
+ */
48
+
49
+ grammar Lua;
50
+
51
+ chunk
52
+ : block EOF
53
+ ;
54
+
55
+ block
56
+ : stat* retstat?
57
+ ;
58
+
59
+ stat
60
+ : ';'
61
+ | varlist '=' explist
62
+ | functioncall
63
+ | label
64
+ | 'break'
65
+ | 'goto' NAME
66
+ | 'do' block 'end'
67
+ | 'while' exp 'do' block 'end'
68
+ | 'repeat' block 'until' exp
69
+ | 'if' exp 'then' block ('elseif' exp 'then' block)* ('else' block)? 'end'
70
+ | 'for' NAME '=' exp ',' exp (',' exp)? 'do' block 'end'
71
+ | 'for' namelist 'in' explist 'do' block 'end'
72
+ | 'function' funcname funcbody
73
+ | 'local' 'function' NAME funcbody
74
+ | 'local' namelist ('=' explist)?
75
+ ;
76
+
77
+ retstat
78
+ : 'return' explist? ';'?
79
+ ;
80
+
81
+ label
82
+ : '::' NAME '::'
83
+ ;
84
+
85
+ funcname
86
+ : NAME ('.' NAME)* (':' NAME)?
87
+ ;
88
+
89
+ varlist
90
+ : var (',' var)*
91
+ ;
92
+
93
+ namelist
94
+ : NAME (',' NAME)*
95
+ ;
96
+
97
+ explist
98
+ : exp (',' exp)*
99
+ ;
100
+
101
+ exp
102
+ : 'nil' | 'false' | 'true'
103
+ | number
104
+ | string
105
+ | '...'
106
+ | functiondef
107
+ | prefixexp
108
+ | tableconstructor
109
+ | <assoc=right> exp operatorPower exp
110
+ | operatorUnary exp
111
+ | exp operatorMulDivMod exp
112
+ | exp operatorAddSub exp
113
+ | <assoc=right> exp operatorStrcat exp
114
+ | exp operatorComparison exp
115
+ | exp operatorAnd exp
116
+ | exp operatorOr exp
117
+ | exp operatorBitwise exp
118
+ ;
119
+
120
+ prefixexp
121
+ : varOrExp nameAndArgs*
122
+ ;
123
+
124
+ functioncall
125
+ : varOrExp nameAndArgs+
126
+ ;
127
+
128
+ varOrExp
129
+ : var | '(' exp ')'
130
+ ;
131
+
132
+ var
133
+ : (NAME | '(' exp ')' varSuffix) varSuffix*
134
+ ;
135
+
136
+ varSuffix
137
+ : nameAndArgs* ('[' exp ']' | '.' NAME)
138
+ ;
139
+
140
+ nameAndArgs
141
+ : (':' NAME)? args
142
+ ;
143
+
144
+ /*
145
+ var
146
+ : NAME | prefixexp '[' exp ']' | prefixexp '.' NAME
147
+ ;
148
+
149
+ prefixexp
150
+ : var | functioncall | '(' exp ')'
151
+ ;
152
+
153
+ functioncall
154
+ : prefixexp args | prefixexp ':' NAME args
155
+ ;
156
+ */
157
+
158
+ args
159
+ : '(' explist? ')' | tableconstructor | string
160
+ ;
161
+
162
+ functiondef
163
+ : 'function' funcbody
164
+ ;
165
+
166
+ funcbody
167
+ : '(' parlist? ')' block 'end'
168
+ ;
169
+
170
+ parlist
171
+ : namelist (',' '...')? | '...'
172
+ ;
173
+
174
+ tableconstructor
175
+ : '{' fieldlist? '}'
176
+ ;
177
+
178
+ fieldlist
179
+ : field (fieldsep field)* fieldsep?
180
+ ;
181
+
182
+ field
183
+ : '[' exp ']' '=' exp | NAME '=' exp | exp
184
+ ;
185
+
186
+ fieldsep
187
+ : ',' | ';'
188
+ ;
189
+
190
+ operatorOr
191
+ : 'or';
192
+
193
+ operatorAnd
194
+ : 'and';
195
+
196
+ operatorComparison
197
+ : '<' | '>' | '<=' | '>=' | '~=' | '==';
198
+
199
+ operatorStrcat
200
+ : '..';
201
+
202
+ operatorAddSub
203
+ : '+' | '-';
204
+
205
+ operatorMulDivMod
206
+ : '*' | '/' | '%' | '//';
207
+
208
+ operatorBitwise
209
+ : '&' | '|' | '~' | '<<' | '>>';
210
+
211
+ operatorUnary
212
+ : 'not' | '#' | '-' | '~';
213
+
214
+ operatorPower
215
+ : '^';
216
+
217
+ number
218
+ : INT | HEX | FLOAT | HEX_FLOAT
219
+ ;
220
+
221
+ string
222
+ : NORMALSTRING | CHARSTRING | LONGSTRING
223
+ ;
224
+
225
+ // LEXER
226
+
227
+ NAME
228
+ : [a-zA-Z_][a-zA-Z_0-9]*
229
+ ;
230
+
231
+ NORMALSTRING
232
+ : '"' ( EscapeSequence | ~('\\'|'"') )* '"'
233
+ ;
234
+
235
+ CHARSTRING
236
+ : '\'' ( EscapeSequence | ~('\''|'\\') )* '\''
237
+ ;
238
+
239
+ LONGSTRING
240
+ : '[' NESTED_STR ']'
241
+ ;
242
+
243
+ fragment
244
+ NESTED_STR
245
+ : '=' NESTED_STR '='
246
+ | '[' .*? ']'
247
+ ;
248
+
249
+ INT
250
+ : Digit+
251
+ ;
252
+
253
+ HEX
254
+ : '0' [xX] HexDigit+
255
+ ;
256
+
257
+ FLOAT
258
+ : Digit+ '.' Digit* ExponentPart?
259
+ | '.' Digit+ ExponentPart?
260
+ | Digit+ ExponentPart
261
+ ;
262
+
263
+ HEX_FLOAT
264
+ : '0' [xX] HexDigit+ '.' HexDigit* HexExponentPart?
265
+ | '0' [xX] '.' HexDigit+ HexExponentPart?
266
+ | '0' [xX] HexDigit+ HexExponentPart
267
+ ;
268
+
269
+ fragment
270
+ ExponentPart
271
+ : [eE] [+-]? Digit+
272
+ ;
273
+
274
+ fragment
275
+ HexExponentPart
276
+ : [pP] [+-]? Digit+
277
+ ;
278
+
279
+ fragment
280
+ EscapeSequence
281
+ : '\\' [abfnrtvz"'\\]
282
+ | '\\' '\r'? '\n'
283
+ | DecimalEscape
284
+ | HexEscape
285
+ | UtfEscape
286
+ ;
287
+
288
+ fragment
289
+ DecimalEscape
290
+ : '\\' Digit
291
+ | '\\' Digit Digit
292
+ | '\\' [0-2] Digit Digit
293
+ ;
294
+
295
+ fragment
296
+ HexEscape
297
+ : '\\' 'x' HexDigit HexDigit
298
+ ;
299
+
300
+ fragment
301
+ UtfEscape
302
+ : '\\' 'u{' HexDigit+ '}'
303
+ ;
304
+
305
+ fragment
306
+ Digit
307
+ : [0-9]
308
+ ;
309
+
310
+ fragment
311
+ HexDigit
312
+ : [0-9a-fA-F]
313
+ ;
314
+
315
+ COMMENT
316
+ : '--[' NESTED_STR ']' -> channel(HIDDEN)
317
+ ;
318
+
319
+ LINE_COMMENT
320
+ : '--'
321
+ ( // --
322
+ | '[' '='* // --[==
323
+ | '[' '='* ~('='|'['|'\r'|'\n') ~('\r'|'\n')* // --[==AA
324
+ | ~('['|'\r'|'\n') ~('\r'|'\n')* // --AAA
325
+ ) ('\r\n'|'\r'|'\n'|EOF)
326
+ -> channel(HIDDEN)
327
+ ;
328
+
329
+ WS
330
+ : [ \t\u000C\r\n]+ -> skip
331
+ ;
332
+
333
+ SHEBANG
334
+ : '#' '!' ~('\n'|'\r')* -> channel(HIDDEN)
335
+ ;
@@ -0,0 +1,28 @@
1
+ require 'bundler'
2
+
3
+ require 'antlr4-native'
4
+ require 'etc'
5
+
6
+ def ruby_installer?
7
+ Object.const_defined?(:RubyInstaller)
8
+ end
9
+
10
+ Bundler::GemHelper.install_tasks
11
+
12
+ task :generate do
13
+ generator = Antlr4Native::Generator.new(
14
+ grammar_files: ["./Lua.g4"],
15
+ output_dir: 'ext/',
16
+ parser_root_method: 'chunk'
17
+ )
18
+
19
+ generator.generate
20
+ end
21
+
22
+ task :compile do
23
+ Dir.chdir(File.join(%w(ext lua_parser))) do
24
+ load 'extconf.rb'
25
+ RubyInstaller::Runtime.enable_msys_apps if ruby_installer?
26
+ exec "make -j #{Etc.nprocessors}"
27
+ end
28
+ end
@@ -0,0 +1,38 @@
1
+ require 'mkmf-rice'
2
+
3
+ extension_name = 'lua_parser'
4
+ dir_config(extension_name)
5
+
6
+ have_library('stdc++')
7
+
8
+ $CFLAGS << ' -std=c++14'
9
+
10
+ if enable_config('static')
11
+ $defs.push '-DANTLR4CPP_STATIC' unless $defs.include?('-DANTLR4CPP_STATIC')
12
+ end
13
+
14
+ include_paths = [
15
+ '.',
16
+ 'antlrgen',
17
+ 'antlr4-upstream/runtime/Cpp/runtime/src',
18
+ 'antlr4-upstream/runtime/Cpp/runtime/src/atn',
19
+ 'antlr4-upstream/runtime/Cpp/runtime/src/dfa',
20
+ 'antlr4-upstream/runtime/Cpp/runtime/src/misc',
21
+ 'antlr4-upstream/runtime/Cpp/runtime/src/support',
22
+ 'antlr4-upstream/runtime/Cpp/runtime/src/tree',
23
+ 'antlr4-upstream/runtime/Cpp/runtime/src/tree/pattern',
24
+ 'antlr4-upstream/runtime/Cpp/runtime/src/tree/xpath'
25
+ ]
26
+
27
+ $srcs = []
28
+
29
+ include_paths.each do |include_path|
30
+ $INCFLAGS << " -I#{include_path}"
31
+ $VPATH << include_path
32
+
33
+ Dir.glob("#{include_path}/*.cpp").each do |path|
34
+ $srcs << path
35
+ end
36
+ end
37
+
38
+ create_makefile(extension_name)
@@ -0,0 +1,3 @@
1
+ module LuaParser
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,2 @@
1
+ # load the native extension
2
+ require File.expand_path(File.join('..', 'ext', 'lua_parser', 'lua_parser'), __dir__)
@@ -0,0 +1,31 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), 'lib')
2
+ require 'lua_parser/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'lua_parser'
6
+ s.version = ::LuaParser::VERSION
7
+ s.authors = ['Mickey Mouse']
8
+ s.email = ['mickey@disney.com']
9
+ s.homepage = 'https://github.com/mickeymouse/lua-parser-rb'
10
+
11
+ s.description = s.summary = 'A Lua parser for Ruby'
12
+
13
+ s.platform = Gem::Platform::RUBY
14
+
15
+ s.add_dependency 'rice', '~> 4.0'
16
+
17
+ s.extensions = File.join(*%w(ext lua_parser extconf.rb))
18
+
19
+ s.require_path = 'lib'
20
+ s.files = Dir[
21
+ '{lib,spec}/**/*',
22
+ 'ext/lua_parser/*.{cpp,h}',
23
+ 'ext/lua_parser/extconf.rb',
24
+ 'ext/lua_parser/antlrgen/*',
25
+ 'ext/lua_parser/antlr4-upstream/runtime/Cpp/runtime/src/**/*.{cpp,h}',
26
+ 'Gemfile',
27
+ 'README.md',
28
+ 'Rakefile',
29
+ 'lua_parser.gemspec'
30
+ ]
31
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lua_parser"
4
+
5
+ class FuncVisitor < LuaParser::Visitor
6
+ def visit_functioncall(ctx)
7
+ puts ctx.var_or_exp.text
8
+ visit_children(ctx)
9
+ end
10
+ end
11
+
12
+ Dir.glob('lua/**/*.lua').each do |file_name|
13
+ # this file contains some weird non-UTF8 strings, so let's just skip it
14
+ next if File.basename(file_name) == "strings.lua"
15
+
16
+ lua_code = File.read(file_name)
17
+ parser = LuaParser::Parser.parse(lua_code)
18
+ visitor = FuncVisitor.new
19
+ parser.visit(visitor)
20
+ end
@@ -0,0 +1,5 @@
1
+ #! /bin/bash
2
+
3
+ antlr_version=$(bundle exec ruby -Ilib -rantlr4-native -e "puts Antlr4Native::Generator::ANTLR_VERSION")
4
+ docker build --build-arg ANTLR_VERSION="${antlr_version}" -t antlr4-native-rb:latest .
5
+ docker run -t antlr4-native-rb:latest bundle exec ruby parse_test.rb
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: antlr4-native
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Dutro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-12 00:00:00.000000000 Z
11
+ date: 2023-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rice
@@ -43,6 +43,15 @@ files:
43
43
  - lib/antlr4-native/string_helpers.rb
44
44
  - lib/antlr4-native/version.rb
45
45
  - lib/antlr4-native/visitor_generator.rb
46
+ - spec/lua-parser-rb/Gemfile
47
+ - spec/lua-parser-rb/Lua.g4
48
+ - spec/lua-parser-rb/Rakefile
49
+ - spec/lua-parser-rb/ext/lua_parser/extconf.rb
50
+ - spec/lua-parser-rb/lib/lua_parser.rb
51
+ - spec/lua-parser-rb/lib/lua_parser/version.rb
52
+ - spec/lua-parser-rb/lua_parser.gemspec
53
+ - spec/lua-parser-rb/parse_test.rb
54
+ - spec/run-lua-test.sh
46
55
  - vendor/antlr-4.10.1-complete.jar
47
56
  homepage: http://github.com/camertron/antlr4-native-rb
48
57
  licenses: []
@@ -62,7 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
71
  - !ruby/object:Gem::Version
63
72
  version: '0'
64
73
  requirements: []
65
- rubygems_version: 3.2.22
74
+ rubygems_version: 3.4.5
66
75
  signing_key:
67
76
  specification_version: 4
68
77
  summary: Create a Ruby native extension from any ANTLR4 grammar.