xref: /aosp_15_r20/external/antlr/runtime/Ruby/lib/antlr3.rb (revision 16467b971bd3e2009fad32dd79016f2c7e421deb)
1*16467b97STreehugger Robot#!/usr/bin/ruby
2*16467b97STreehugger Robot# encoding: utf-8
3*16467b97STreehugger Robot
4*16467b97STreehugger Robot=begin LICENSE
5*16467b97STreehugger Robot
6*16467b97STreehugger Robot[The "BSD licence"]
7*16467b97STreehugger RobotCopyright (c) 2009-2010 Kyle Yetter
8*16467b97STreehugger RobotAll rights reserved.
9*16467b97STreehugger Robot
10*16467b97STreehugger RobotRedistribution and use in source and binary forms, with or without
11*16467b97STreehugger Robotmodification, are permitted provided that the following conditions
12*16467b97STreehugger Robotare met:
13*16467b97STreehugger Robot
14*16467b97STreehugger Robot 1. Redistributions of source code must retain the above copyright
15*16467b97STreehugger Robot    notice, this list of conditions and the following disclaimer.
16*16467b97STreehugger Robot 2. Redistributions in binary form must reproduce the above copyright
17*16467b97STreehugger Robot    notice, this list of conditions and the following disclaimer in the
18*16467b97STreehugger Robot    documentation and/or other materials provided with the distribution.
19*16467b97STreehugger Robot 3. The name of the author may not be used to endorse or promote products
20*16467b97STreehugger Robot    derived from this software without specific prior written permission.
21*16467b97STreehugger Robot
22*16467b97STreehugger RobotTHIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23*16467b97STreehugger RobotIMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24*16467b97STreehugger RobotOF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25*16467b97STreehugger RobotIN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26*16467b97STreehugger RobotINCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27*16467b97STreehugger RobotNOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28*16467b97STreehugger RobotDATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29*16467b97STreehugger RobotTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30*16467b97STreehugger Robot(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31*16467b97STreehugger RobotTHIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*16467b97STreehugger Robot
33*16467b97STreehugger Robot=end
34*16467b97STreehugger Robot
35*16467b97STreehugger Robot=begin rdoc ANTLR3
36*16467b97STreehugger Robot
37*16467b97STreehugger RobotThe main namespace for the ANTLR runtime libraries, which are used by
38*16467b97STreehugger RobotRuby-targeted recognizers generated by ANTLR. The entire library is segmented
39*16467b97STreehugger Robotinto several main components, as well as a few additional utility components,
40*16467b97STreehugger Roboteach contained within a separate script.
41*16467b97STreehugger Robot
42*16467b97STreehugger Robot== Library Components
43*16467b97STreehugger Robot
44*16467b97STreehugger RobotNot all components of the ANTLR3 library are necessary within ANTLR generated
45*16467b97STreehugger Robotcode. Some components are only used within specific types of recognizers and
46*16467b97STreehugger Robotsome are simply extra utilities for use by anyone working with ANTLR code. Thus,
47*16467b97STreehugger Robotwhen requiring 'antlr3', only the essential core components are loaded
48*16467b97STreehugger Robotimmediately. The rest are configured to autoload when any of the constant names
49*16467b97STreehugger Robotthey define are referenced.
50*16467b97STreehugger Robot
51*16467b97STreehugger RobotThe following list gives a brief introduction to each component of the ANTLR3
52*16467b97STreehugger Robotruntime library. The items are loosely ordered by importance.
53*16467b97STreehugger Robot
54*16467b97STreehugger Robotantlr3/recognizers.rb::
55*16467b97STreehugger Robot  contains the base classes for ANTLR-generated recognizers, and thus, is one of
56*16467b97STreehugger Robot  the most important components of the runtime library. loaded by default
57*16467b97STreehugger Robotantlr3/dfa.rb::
58*16467b97STreehugger Robot  defines a single DFA class that is used to simulate state machines for certain
59*16467b97STreehugger Robot  decisions recognizers must make in code generated by ANTLR
60*16467b97STreehugger Robotantlr3/streams.rb::
61*16467b97STreehugger Robot  defines the stream classes used by ANTLR recognizers to walk sequentially
62*16467b97STreehugger Robot  through strings, tokens, and tree nodes loaded by default
63*16467b97STreehugger Robotantlr3/token.rb::
64*16467b97STreehugger Robot  contains all modules and classes concerned with making tokens, the chunks of
65*16467b97STreehugger Robot  text and symbol information produced by lexers and used by parsers and ASTs
66*16467b97STreehugger Robot  loaded by default
67*16467b97STreehugger Robotantlr3/error.rb::
68*16467b97STreehugger Robot  defines the Error module, which contains definitions for most of the many
69*16467b97STreehugger Robot  error classes used through the runtime library and ANTLR generated
70*16467b97STreehugger Robot  recognizers. loaded by default
71*16467b97STreehugger Robotantlr3/constants.rb::
72*16467b97STreehugger Robot  just a module used as a namespace for the named constant values used
73*16467b97STreehugger Robot  throughout the library. loaded by default
74*16467b97STreehugger Robotantlr3/tree.rb::
75*16467b97STreehugger Robot  contains everything pertaining to Abstract Syntax Trees (ASTs). This script is
76*16467b97STreehugger Robot  not loaded by default when 'antlr3' is required, but it is autloaded on demand
77*16467b97STreehugger Robot  when any constant defined in the script is referenced. contents are autoloaded
78*16467b97STreehugger Robot  on demand
79*16467b97STreehugger Robotantlr3/debug.rb::
80*16467b97STreehugger Robot  when code is generated by ANTLR using the '-debug' option, all of the
81*16467b97STreehugger Robot  additional classes and mixins required by the debug code are contained within
82*16467b97STreehugger Robot  the Debug module defined by this library. the Debug module is autoloaded on
83*16467b97STreehugger Robot  demand
84*16467b97STreehugger Robotantlr3/main.rb::
85*16467b97STreehugger Robot  defines the Main module. When ANTLR-generated recognizer code is run directly
86*16467b97STreehugger Robot  as a script (not loaded as a module), the code will behave as a full
87*16467b97STreehugger Robot  command-line script by using functionality implemented in the Main module. the
88*16467b97STreehugger Robot  Main module is autloaded on demand
89*16467b97STreehugger Robotantlr3/tree-wizard.rb::
90*16467b97STreehugger Robot  contains extra tools to easily construct ASTs by parsing descriptions written
91*16467b97STreehugger Robot  in a special DSL
92*16467b97STreehugger Robotantlr3/dot.rb::
93*16467b97STreehugger Robot  extra utilities to generate DOT map specifications for graphical.
94*16467b97STreehugger Robot  representations of ASTs
95*16467b97STreehugger Robot
96*16467b97STreehugger Robot@author Kyle Yetter
97*16467b97STreehugger Robot
98*16467b97STreehugger Robot=end
99*16467b97STreehugger Robot
100*16467b97STreehugger Robotmodule ANTLR3
101*16467b97STreehugger Robot
102*16467b97STreehugger Robot  # :stopdoc:
103*16467b97STreehugger Robot  # BEGIN PATHS -- do not modify
104*16467b97STreehugger Robot
105*16467b97STreehugger Robot  LIBRARY_PATH  = ::File.expand_path( ::File.dirname( __FILE__ ) ).freeze
106*16467b97STreehugger Robot  PROJECT_PATH  = ::File.dirname( LIBRARY_PATH ).freeze
107*16467b97STreehugger Robot  DATA_PATH     = ::File.join( PROJECT_PATH, 'java' ).freeze
108*16467b97STreehugger Robot
109*16467b97STreehugger Robot  # END PATHS
110*16467b97STreehugger Robot  # :startdoc:
111*16467b97STreehugger Robot
112*16467b97STreehugger Robot  # Returns the library path for the module. If any arguments are given,
113*16467b97STreehugger Robot  # they will be joined to the end of the libray path using
114*16467b97STreehugger Robot  # <tt>File.join</tt>.
115*16467b97STreehugger Robot  #
116*16467b97STreehugger Robot  def self.library_path( *args )
117*16467b97STreehugger Robot    ::File.expand_path( ::File.join( LIBRARY_PATH, *args ) )
118*16467b97STreehugger Robot  end
119*16467b97STreehugger Robot
120*16467b97STreehugger Robot  # Returns the lpath for the module. If any arguments are given,
121*16467b97STreehugger Robot  # they will be joined to the end of the path using
122*16467b97STreehugger Robot  # <tt>File.join</tt>.
123*16467b97STreehugger Robot  #
124*16467b97STreehugger Robot  def self.data_path( *args )
125*16467b97STreehugger Robot    ::File.expand_path( ::File.join( DATA_PATH, *args ) )
126*16467b97STreehugger Robot  end
127*16467b97STreehugger Robot
128*16467b97STreehugger Robot  # This is used internally in a handful of locations in the runtime library
129*16467b97STreehugger Robot  # where assumptions have been made that a condition will never happen
130*16467b97STreehugger Robot  # under normal usage conditions and thus an ANTLR3::Bug error will be
131*16467b97STreehugger Robot  # raised if the condition does occur.
132*16467b97STreehugger Robot  def self.bug!( message = nil )
133*16467b97STreehugger Robot    bug = Bug.new( message )
134*16467b97STreehugger Robot    bug.set_backtrace( caller )
135*16467b97STreehugger Robot    raise( bug )
136*16467b97STreehugger Robot  end
137*16467b97STreehugger Robot
138*16467b97STreehugger Robot  @antlr_jar = nil
139*16467b97STreehugger Robot
140*16467b97STreehugger Robot  def self.antlr_jar=( path )
141*16467b97STreehugger Robot    @antlr_jar = path ? File.expand_path( path.to_s ) : path
142*16467b97STreehugger Robot  end
143*16467b97STreehugger Robot
144*16467b97STreehugger Robot  def self.antlr_jar
145*16467b97STreehugger Robot    @antlr_jar and return( @antlr_jar )
146*16467b97STreehugger Robot
147*16467b97STreehugger Robot    path = data_path "antlr-full-#{ ANTLR_VERSION_STRING }.jar"
148*16467b97STreehugger Robot    if env_path = ENV[ 'RUBY_ANTLR_JAR' ]
149*16467b97STreehugger Robot      if File.file?( env_path ) then return File.expand_path( env_path ) end
150*16467b97STreehugger Robot
151*16467b97STreehugger Robot      warn(
152*16467b97STreehugger Robot        "#{ __FILE__ }:#{ __LINE__ }: " <<
153*16467b97STreehugger Robot        "ignoring environmental variable RUBY_ANTLR_JAR (=%p) " % env_path <<
154*16467b97STreehugger Robot        "as it is not the path to an existing file\n" <<
155*16467b97STreehugger Robot        "  -> trying default jar path %p instead" % path
156*16467b97STreehugger Robot      )
157*16467b97STreehugger Robot    end
158*16467b97STreehugger Robot
159*16467b97STreehugger Robot    File.exists?( path ) ? path : nil
160*16467b97STreehugger Robot  end
161*16467b97STreehugger Robot
162*16467b97STreehugger Robot  ##############################################################################################
163*16467b97STreehugger Robot  ############################### Namespace and Load Path Setup ################################
164*16467b97STreehugger Robot  ##############################################################################################
165*16467b97STreehugger Robot
166*16467b97STreehugger Robot  # Tree classes are only used by tree parsers or AST-building parsers
167*16467b97STreehugger Robot  # Thus, they're not essential for everything ANTLR generates and
168*16467b97STreehugger Robot  # are autoloaded on-demand
169*16467b97STreehugger Robot  autoload :AST, 'antlr3/tree'
170*16467b97STreehugger Robot
171*16467b97STreehugger Robot  tree_classes = [
172*16467b97STreehugger Robot    :Tree, :TreeAdaptor, :BaseTree, :BaseTreeAdaptor,
173*16467b97STreehugger Robot    :CommonTree, :CommonErrorNode, :CommonTreeAdaptor,
174*16467b97STreehugger Robot    :TreeNodeStream, :CommonTreeNodeStream, :TreeParser,
175*16467b97STreehugger Robot    :TreeVisitor, :RewriteRuleElementStream,
176*16467b97STreehugger Robot    :RewriteRuleTokenStream, :RewriteRuleSubtreeStream,
177*16467b97STreehugger Robot    :RewriteRuleNodeStream
178*16467b97STreehugger Robot  ]
179*16467b97STreehugger Robot
180*16467b97STreehugger Robot  for klass in tree_classes
181*16467b97STreehugger Robot    autoload klass, 'antlr3/tree'
182*16467b97STreehugger Robot  end
183*16467b97STreehugger Robot
184*16467b97STreehugger Robot  # Set up non-essential components to be loaded on-demand
185*16467b97STreehugger Robot  autoload :TokenRewriteStream, 'antlr3/streams/rewrite'
186*16467b97STreehugger Robot  autoload :FilterMode, 'antlr3/modes/filter'
187*16467b97STreehugger Robot  autoload :ASTBuilder, 'antlr3/modes/ast-builder'
188*16467b97STreehugger Robot  autoload :Main,  'antlr3/main'
189*16467b97STreehugger Robot  autoload :Debug, 'antlr3/debug'
190*16467b97STreehugger Robot  autoload :Profile, 'antlr3/profile'
191*16467b97STreehugger Robot  autoload :DOT, 'antlr3/dot'
192*16467b97STreehugger Robot  autoload :InteractiveStringStream, 'antlr3/streams/interactive'
193*16467b97STreehugger Robot
194*16467b97STreehugger Robot  autoload :Template, 'antlr3/template'
195*16467b97STreehugger Robot
196*16467b97STreehugger Robot  $LOAD_PATH.include?( library_path ) or $LOAD_PATH.unshift( library_path )
197*16467b97STreehugger Robot
198*16467b97STreehugger Robotend  # module ANTLR3
199*16467b97STreehugger Robot
200*16467b97STreehugger Robot
201*16467b97STreehugger Robotrequire 'set'
202*16467b97STreehugger Robotrequire 'antlr3/util'
203*16467b97STreehugger Robotrequire 'antlr3/version'
204*16467b97STreehugger Robot
205*16467b97STreehugger Robotunless $0 == 'antlr4ruby'
206*16467b97STreehugger Robot  require 'antlr3/constants'
207*16467b97STreehugger Robot  require 'antlr3/error'
208*16467b97STreehugger Robot  require 'antlr3/token'
209*16467b97STreehugger Robot  require 'antlr3/recognizers'
210*16467b97STreehugger Robot  require 'antlr3/dfa'
211*16467b97STreehugger Robot  require 'antlr3/streams'
212*16467b97STreehugger Robotend
213