aboutsummaryrefslogtreecommitdiff
path: root/antlr-3.4/runtime/Ruby/lib/antlr3/test/grammar.rb
blob: 9c931495a645bd42cc33c6fdbad3d2c0f9f7ce1f (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/ruby
# encoding: utf-8

require 'antlr3'
require 'antlr3/test/core-extensions'
require 'antlr3/test/call-stack'

if RUBY_VERSION =~ /^1\.9/
  require 'digest/md5'
  MD5 = Digest::MD5
else
  require 'md5'
end

module ANTLR3
module Test
module DependantFile
  attr_accessor :path, :force
  alias force? force
  
  GLOBAL_DEPENDENCIES = []
  
  def dependencies
    @dependencies ||= GLOBAL_DEPENDENCIES.clone
  end
  
  def depends_on( path )
    path = File.expand_path path.to_s
    dependencies << path if test( ?f, path )
    return path
  end
  
  def stale?
    force and return( true )
    target_files.any? do |target|
      not test( ?f, target ) or
        dependencies.any? { |dep| test( ?>, dep, target ) }
    end
  end
end # module DependantFile

class Grammar
  include DependantFile

  GRAMMAR_TYPES = %w(lexer parser tree combined)
  TYPE_TO_CLASS = { 
    'lexer'  => 'Lexer',
    'parser' => 'Parser',
    'tree'   => 'TreeParser'
  }
  CLASS_TO_TYPE = TYPE_TO_CLASS.invert

  def self.global_dependency( path )
    path = File.expand_path path.to_s
    GLOBAL_DEPENDENCIES << path if test( ?f, path )
    return path
  end
  
  def self.inline( source, *args )
    InlineGrammar.new( source, *args )
  end
  
  ##################################################################
  ######## CONSTRUCTOR #############################################
  ##################################################################
  def initialize( path, options = {} )
    @path = path.to_s
    @source = File.read( @path )
    @output_directory = options.fetch( :output_directory, '.' )
    @verbose = options.fetch( :verbose, $VERBOSE )
    study
    build_dependencies
    
    yield( self ) if block_given?
  end
  
  ##################################################################
  ######## ATTRIBUTES AND ATTRIBUTE-ISH METHODS ####################
  ##################################################################
  attr_reader :type, :name, :source
  attr_accessor :output_directory, :verbose
  
  def lexer_class_name
    self.name + "::Lexer"
  end
  
  def lexer_file_name
    if lexer? then base = name
    elsif combined? then base = name + 'Lexer'
    else return( nil )
    end
    return( base + '.rb' )
  end
  
  def parser_class_name
    name + "::Parser"
  end
  
  def parser_file_name
    if parser? then base = name
    elsif combined? then base = name + 'Parser'
    else return( nil )
    end
    return( base + '.rb' )
  end
  
  def tree_parser_class_name
    name + "::TreeParser"
  end

  def tree_parser_file_name
    tree? and name + '.rb'
  end
  
  def has_lexer?
    @type == 'combined' || @type == 'lexer'
  end
  
  def has_parser?
    @type == 'combined' || @type == 'parser'
  end
  
  def lexer?
    @type == "lexer"
  end
  
  def parser?
    @type == "parser"
  end
  
  def tree?
    @type == "tree"
  end
  
  alias has_tree? tree?
  
  def combined?
    @type == "combined"
  end
  
  def target_files( include_imports = true )
    targets = []
    
    for target_type in %w(lexer parser tree_parser)
      target_name = self.send( :"#{ target_type }_file_name" ) and
        targets.push( output_directory / target_name )
    end
    
    targets.concat( imported_target_files ) if include_imports
    return targets
  end
  
  def imports
    @source.scan( /^\s*import\s+(\w+)\s*;/ ).
      tap { |list| list.flatten! }
  end
  
  def imported_target_files
    imports.map! do |delegate|
      output_directory / "#{ @name }_#{ delegate }.rb"
    end
  end

  ##################################################################
  ##### COMMAND METHODS ############################################
  ##################################################################
  def compile( options = {} )
    if options[ :force ] or stale?
      compile!( options )
    end
  end
  
  def compile!( options = {} )
    command = build_command( options )
    
    blab( command )
    output = IO.popen( command ) do |pipe|
      pipe.read
    end
    
    case status = $?.exitstatus
    when 0, 130
      post_compile( options )
    else compilation_failure!( command, status, output )
    end
    
    return target_files
  end
  
  def clean!
    deleted = []
    for target in target_files
      if test( ?f, target )
        File.delete( target )
        deleted << target
      end
    end
    return deleted
  end
  
  def inspect
    sprintf( "grammar %s (%s)", @name, @path )
  end
  
private
  
  def post_compile( options )
    # do nothing for now
  end
  
  def blab( string, *args )
    $stderr.printf( string + "\n", *args ) if @verbose
  end
  
  def default_antlr_jar
    ENV[ 'ANTLR_JAR' ] || ANTLR3.antlr_jar
  end
  
  def compilation_failure!( command, status, output )
    for f in target_files
      test( ?f, f ) and File.delete( f )
    end
    raise CompilationFailure.new( self, command, status, output )
  end

  def build_dependencies
    depends_on( @path )
    
    if @source =~ /tokenVocab\s*=\s*(\S+)\s*;/
      foreign_grammar_name = $1
      token_file = output_directory / foreign_grammar_name + '.tokens'
      grammar_file = File.dirname( path ) / foreign_grammar_name << '.g'
      depends_on( token_file )
      depends_on( grammar_file )
    end    
  end
  
  def shell_escape( token )
    token = token.to_s.dup
    token.empty? and return "''"
    token.gsub!( /([^A-Za-z0-9_\-.,:\/@\n])/n, '\\\1' )
    token.gsub!( /\n/, "'\n'" )
    return token
  end
  
  def build_command( options )
    parts = %w(java)
    jar_path = options.fetch( :antlr_jar, default_antlr_jar )
    parts.push( '-cp', jar_path )
    parts << 'org.antlr.Tool'
    parts.push( '-fo', output_directory )
    options[ :profile ] and parts << '-profile'
    options[ :debug ]   and parts << '-debug'
    options[ :trace ]   and parts << '-trace'
    options[ :debug_st ] and parts << '-XdbgST'
    parts << File.expand_path( @path )
    parts.map! { |part| shell_escape( part ) }.join( ' ' ) << ' 2>&1'
  end
  
  def study
    @source =~ /^\s*(lexer|parser|tree)?\s*grammar\s*(\S+)\s*;/ or
      raise Grammar::FormatError[ source, path ]
    @name = $2
    @type = $1 || 'combined'
  end
end # class Grammar

class Grammar::InlineGrammar < Grammar
  attr_accessor :host_file, :host_line
  
  def initialize( source, options = {} )
    host = call_stack.find { |call| call.file != __FILE__ }
    
    @host_file = File.expand_path( options[ :file ] || host.file )
    @host_line = ( options[ :line ] || host.line )
    @output_directory = options.fetch( :output_directory, File.dirname( @host_file ) )
    @verbose = options.fetch( :verbose, $VERBOSE )
    
    @source = source.to_s.fixed_indent( 0 )
    @source.strip!
    
    study
    write_to_disk
    build_dependencies
    
    yield( self ) if block_given?
  end
  
  def output_directory
    @output_directory and return @output_directory
    File.basename( @host_file )
  end
  
  def path=( v )
    previous, @path = @path, v.to_s
    previous == @path or write_to_disk
  end
  
  def inspect
    sprintf( 'inline grammar %s (%s:%s)', name, @host_file, @host_line )
  end
  
private
  
  def write_to_disk
    @path ||= output_directory / @name + '.g'
    test( ?d, output_directory ) or Dir.mkdir( output_directory )
    unless test( ?f, @path ) and MD5.digest( @source ) == MD5.digest( File.read( @path ) )
      open( @path, 'w' ) { |f| f.write( @source ) }
    end
  end
end # class Grammar::InlineGrammar

class Grammar::CompilationFailure < StandardError
  JAVA_TRACE = /^(org\.)?antlr\.\S+\(\S+\.java:\d+\)\s*/
  attr_reader :grammar, :command, :status, :output
  
  def initialize( grammar, command, status, output )
    @command = command
    @status = status
    @output = output.gsub( JAVA_TRACE, '' )
    
    message = <<-END.here_indent! % [ command, status, grammar, @output ]
    | command ``%s'' failed with status %s
    | %p
    | ~ ~ ~ command output ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
    | %s
    END
    
    super( message.chomp! || message )
  end
end # error Grammar::CompilationFailure

class Grammar::FormatError < StandardError
  attr_reader :file, :source
  
  def self.[]( *args )
    new( *args )
  end
  
  def initialize( source, file = nil )
    @file = file
    @source = source
    message = ''
    if file.nil? # inline
      message << "bad inline grammar source:\n"
      message << ( "-" * 80 ) << "\n"
      message << @source
      message[ -1 ] == ?\n or message << "\n"
      message << ( "-" * 80 ) << "\n"
      message << "could not locate a grammar name and type declaration matching\n"
      message << "/^\s*(lexer|parser|tree)?\s*grammar\s*(\S+)\s*;/"
    else
      message << 'bad grammar source in file %p' % @file
      message << ( "-" * 80 ) << "\n"
      message << @source
      message[ -1 ] == ?\n or message << "\n"
      message << ( "-" * 80 ) << "\n"
      message << "could not locate a grammar name and type declaration matching\n"
      message << "/^\s*(lexer|parser|tree)?\s*grammar\s*(\S+)\s*;/"
    end
    super( message )
  end
end # error Grammar::FormatError

end
end