Class: ANTLR3::Test::Grammar

Inherits:
Object
  • Object
show all
Includes:
DependantFile
Defined in:
lib/antlr3/test/grammar.rb

Overview

module DependantFile

Direct Known Subclasses

InlineGrammar

Defined Under Namespace

Classes: CompilationFailure, FormatError, InlineGrammar

Constant Summary collapse

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

Constants included from DependantFile

DependantFile::GLOBAL_DEPENDENCIES

Instance Attribute Summary collapse

Attributes included from DependantFile

#force, #path

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DependantFile

#dependencies, #depends_on, #stale?

Constructor Details

#initialize(path, options = {}) {|_self| ... } ⇒ Grammar

CONSTRUCTOR #############################################

Yields:

  • (_self)

Yield Parameters:



66
67
68
69
70
71
72
73
74
75
# File 'lib/antlr3/test/grammar.rb', line 66

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

Instance Attribute Details

#nameObject (readonly)

ATTRIBUTES AND ATTRIBUTE-ISH METHODS ####################



80
81
82
# File 'lib/antlr3/test/grammar.rb', line 80

def name
  @name
end

#output_directoryObject

Returns the value of attribute output_directory.



81
82
83
# File 'lib/antlr3/test/grammar.rb', line 81

def output_directory
  @output_directory
end

#sourceObject (readonly)

ATTRIBUTES AND ATTRIBUTE-ISH METHODS ####################



80
81
82
# File 'lib/antlr3/test/grammar.rb', line 80

def source
  @source
end

#typeObject (readonly)

ATTRIBUTES AND ATTRIBUTE-ISH METHODS ####################



80
81
82
# File 'lib/antlr3/test/grammar.rb', line 80

def type
  @type
end

#verboseObject

Returns the value of attribute verbose.



81
82
83
# File 'lib/antlr3/test/grammar.rb', line 81

def verbose
  @verbose
end

Class Method Details

.global_dependency(path) ⇒ Object



53
54
55
56
57
# File 'lib/antlr3/test/grammar.rb', line 53

def self.global_dependency( path )
  path = File.expand_path path.to_s
  GLOBAL_DEPENDENCIES << path if test( ?f, path )
  return path
end

.inline(source, *args) ⇒ Object



59
60
61
# File 'lib/antlr3/test/grammar.rb', line 59

def self.inline( source, *args )
  InlineGrammar.new( source, *args )
end

Instance Method Details

#clean!Object



190
191
192
193
194
195
196
197
198
199
# File 'lib/antlr3/test/grammar.rb', line 190

def clean!
  deleted = []
  for target in target_files
    if test( ?f, target )
      File.delete( target )
      deleted << target
    end
  end
  return deleted
end

#combined?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/antlr3/test/grammar.rb', line 137

def combined?
  @type == "combined"
end

#compile(options = {}) ⇒ Object

COMMAND METHODS ############################################



167
168
169
170
171
# File 'lib/antlr3/test/grammar.rb', line 167

def compile( options = {} )
  if options[ :force ] or stale?
    compile!( options )
  end
end

#compile!(options = {}) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/antlr3/test/grammar.rb', line 173

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

#has_lexer?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/antlr3/test/grammar.rb', line 115

def has_lexer?
  @type == 'combined' || @type == 'lexer'
end

#has_parser?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/antlr3/test/grammar.rb', line 119

def has_parser?
  @type == 'combined' || @type == 'parser'
end

#imported_target_filesObject



158
159
160
161
162
# File 'lib/antlr3/test/grammar.rb', line 158

def imported_target_files
  imports.map! do |delegate|
    output_directory / "#{ @name }_#{ delegate }.rb"
  end
end

#importsObject



153
154
155
156
# File 'lib/antlr3/test/grammar.rb', line 153

def imports
  @source.scan( /^\s*import\s+(\w+)\s*;/ ).
    tap { |list| list.flatten! }
end

#inspectObject



201
202
203
# File 'lib/antlr3/test/grammar.rb', line 201

def inspect
  sprintf( "grammar %s (%s)", @name, @path )
end

#lexer?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/antlr3/test/grammar.rb', line 123

def lexer?
  @type == "lexer"
end

#lexer_class_nameObject



83
84
85
# File 'lib/antlr3/test/grammar.rb', line 83

def lexer_class_name
  self.name + "::Lexer"
end

#lexer_file_nameObject



87
88
89
90
91
92
93
# File 'lib/antlr3/test/grammar.rb', line 87

def lexer_file_name
  if lexer? then base = name
  elsif combined? then base = name + 'Lexer'
  else return( nil )
  end
  return( base + '.rb' )
end

#parser?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/antlr3/test/grammar.rb', line 127

def parser?
  @type == "parser"
end

#parser_class_nameObject



95
96
97
# File 'lib/antlr3/test/grammar.rb', line 95

def parser_class_name
  name + "::Parser"
end

#parser_file_nameObject



99
100
101
102
103
104
105
# File 'lib/antlr3/test/grammar.rb', line 99

def parser_file_name
  if parser? then base = name
  elsif combined? then base = name + 'Parser'
  else return( nil )
  end
  return( base + '.rb' )
end

#target_files(include_imports = true) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/antlr3/test/grammar.rb', line 141

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

#tree?Boolean Also known as: has_tree?

Returns:

  • (Boolean)


131
132
133
# File 'lib/antlr3/test/grammar.rb', line 131

def tree?
  @type == "tree"
end

#tree_parser_class_nameObject



107
108
109
# File 'lib/antlr3/test/grammar.rb', line 107

def tree_parser_class_name
  name + "::TreeParser"
end

#tree_parser_file_nameObject



111
112
113
# File 'lib/antlr3/test/grammar.rb', line 111

def tree_parser_file_name
  tree? and name + '.rb'
end