Skip to content

Commit 1f37cf1

Browse files
committed
as antlr3 runtime has no independent repo or pypi release just self contains the codes
1 parent 958bb8d commit 1f37cf1

File tree

15 files changed

+9228
-14
lines changed

15 files changed

+9228
-14
lines changed

antlr3/__init__.py

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
""" @package antlr3
2+
@brief ANTLR3 runtime package
3+
4+
This module contains all support classes, which are needed to use recognizers
5+
generated by ANTLR3.
6+
7+
@mainpage
8+
9+
\note Please be warned that the line numbers in the API documentation do not
10+
match the real locations in the source code of the package. This is an
11+
unintended artifact of doxygen, which I could only convince to use the
12+
correct module names by concatenating all files from the package into a single
13+
module file...
14+
15+
Here is a little overview over the most commonly used classes provided by
16+
this runtime:
17+
18+
@section recognizers Recognizers
19+
20+
These recognizers are baseclasses for the code which is generated by ANTLR3.
21+
22+
- BaseRecognizer: Base class with common recognizer functionality.
23+
- Lexer: Base class for lexers.
24+
- Parser: Base class for parsers.
25+
- tree.TreeParser: Base class for %tree parser.
26+
27+
@section streams Streams
28+
29+
Each recognizer pulls its input from one of the stream classes below. Streams
30+
handle stuff like buffering, look-ahead and seeking.
31+
32+
A character stream is usually the first element in the pipeline of a typical
33+
ANTLR3 application. It is used as the input for a Lexer.
34+
35+
- ANTLRStringStream: Reads from a string objects. The input should be a unicode
36+
object, or ANTLR3 will have trouble decoding non-ascii data.
37+
- ANTLRFileStream: Opens a file and read the contents, with optional character
38+
decoding.
39+
- ANTLRInputStream: Reads the date from a file-like object, with optional
40+
character decoding.
41+
42+
A Parser needs a TokenStream as input (which in turn is usually fed by a
43+
Lexer):
44+
45+
- CommonTokenStream: A basic and most commonly used TokenStream
46+
implementation.
47+
- TokenRewriteStream: A modification of CommonTokenStream that allows the
48+
stream to be altered (by the Parser). See the 'tweak' example for a usecase.
49+
50+
And tree.TreeParser finally fetches its input from a tree.TreeNodeStream:
51+
52+
- tree.CommonTreeNodeStream: A basic and most commonly used tree.TreeNodeStream
53+
implementation.
54+
55+
56+
@section tokenstrees Tokens and Trees
57+
58+
A Lexer emits Token objects which are usually buffered by a TokenStream. A
59+
Parser can build a Tree, if the output=AST option has been set in the grammar.
60+
61+
The runtime provides these Token implementations:
62+
63+
- CommonToken: A basic and most commonly used Token implementation.
64+
- ClassicToken: A Token object as used in ANTLR 2.x, used to %tree
65+
construction.
66+
67+
Tree objects are wrapper for Token objects.
68+
69+
- tree.CommonTree: A basic and most commonly used Tree implementation.
70+
71+
A tree.TreeAdaptor is used by the parser to create tree.Tree objects for the
72+
input Token objects.
73+
74+
- tree.CommonTreeAdaptor: A basic and most commonly used tree.TreeAdaptor
75+
implementation.
76+
77+
78+
@section Exceptions
79+
80+
RecognitionException are generated, when a recognizer encounters incorrect
81+
or unexpected input.
82+
83+
- RecognitionException
84+
- MismatchedRangeException
85+
- MismatchedSetException
86+
- MismatchedNotSetException
87+
.
88+
- MismatchedTokenException
89+
- MismatchedTreeNodeException
90+
- NoViableAltException
91+
- EarlyExitException
92+
- FailedPredicateException
93+
.
94+
.
95+
96+
A tree.RewriteCardinalityException is raised, when the parsers hits a
97+
cardinality mismatch during AST construction. Although this is basically a
98+
bug in your grammar, it can only be detected at runtime.
99+
100+
- tree.RewriteCardinalityException
101+
- tree.RewriteEarlyExitException
102+
- tree.RewriteEmptyStreamException
103+
.
104+
.
105+
106+
"""
107+
108+
# tree.RewriteRuleElementStream
109+
# tree.RewriteRuleSubtreeStream
110+
# tree.RewriteRuleTokenStream
111+
# CharStream
112+
# DFA
113+
# TokenSource
114+
115+
# [The "BSD licence"]
116+
# Copyright (c) 2005-2008 Terence Parr
117+
# All rights reserved.
118+
#
119+
# Redistribution and use in source and binary forms, with or without
120+
# modification, are permitted provided that the following conditions
121+
# are met:
122+
# 1. Redistributions of source code must retain the above copyright
123+
# notice, this list of conditions and the following disclaimer.
124+
# 2. Redistributions in binary form must reproduce the above copyright
125+
# notice, this list of conditions and the following disclaimer in the
126+
# documentation and/or other materials provided with the distribution.
127+
# 3. The name of the author may not be used to endorse or promote products
128+
# derived from this software without specific prior written permission.
129+
#
130+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
131+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
132+
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
133+
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
134+
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
135+
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
136+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
137+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
138+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
139+
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
140+
141+
__version__ = '3.1.3'
142+
143+
def version_str_to_tuple(version_str):
144+
import re
145+
import sys
146+
147+
if version_str == 'HEAD':
148+
return (sys.maxint, sys.maxint, sys.maxint, sys.maxint)
149+
150+
m = re.match(r'(\d+)\.(\d+)(\.(\d+))?(b(\d+))?', version_str)
151+
if m is None:
152+
raise ValueError("Bad version string %r" % version_str)
153+
154+
major = int(m.group(1))
155+
minor = int(m.group(2))
156+
patch = int(m.group(4) or 0)
157+
beta = int(m.group(6) or sys.maxint)
158+
159+
return (major, minor, patch, beta)
160+
161+
162+
runtime_version_str = __version__
163+
runtime_version = version_str_to_tuple(runtime_version_str)
164+
165+
166+
from constants import *
167+
from dfa import *
168+
from exceptions import *
169+
from recognizers import *
170+
from streams import *
171+
from tokens import *

antlr3/compat.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""Compatibility stuff"""
2+
3+
# begin[licence]
4+
#
5+
# [The "BSD licence"]
6+
# Copyright (c) 2005-2008 Terence Parr
7+
# All rights reserved.
8+
#
9+
# Redistribution and use in source and binary forms, with or without
10+
# modification, are permitted provided that the following conditions
11+
# are met:
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. The name of the author may not be used to endorse or promote products
18+
# derived from this software without specific prior written permission.
19+
#
20+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22+
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23+
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24+
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25+
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29+
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
#
31+
# end[licence]
32+
33+
try:
34+
set = set
35+
frozenset = frozenset
36+
except NameError:
37+
from sets import Set as set, ImmutableSet as frozenset
38+
39+
40+
try:
41+
reversed = reversed
42+
except NameError:
43+
def reversed(l):
44+
l = l[:]
45+
l.reverse()
46+
return l
47+
48+

antlr3/constants.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""ANTLR3 runtime package"""
2+
3+
# begin[licence]
4+
#
5+
# [The "BSD licence"]
6+
# Copyright (c) 2005-2008 Terence Parr
7+
# All rights reserved.
8+
#
9+
# Redistribution and use in source and binary forms, with or without
10+
# modification, are permitted provided that the following conditions
11+
# are met:
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. The name of the author may not be used to endorse or promote products
18+
# derived from this software without specific prior written permission.
19+
#
20+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22+
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23+
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24+
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25+
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29+
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
#
31+
# end[licence]
32+
33+
EOF = -1
34+
35+
## All tokens go to the parser (unless skip() is called in that rule)
36+
# on a particular "channel". The parser tunes to a particular channel
37+
# so that whitespace etc... can go to the parser on a "hidden" channel.
38+
DEFAULT_CHANNEL = 0
39+
40+
## Anything on different channel than DEFAULT_CHANNEL is not parsed
41+
# by parser.
42+
HIDDEN_CHANNEL = 99
43+
44+
# Predefined token types
45+
EOR_TOKEN_TYPE = 1
46+
47+
##
48+
# imaginary tree navigation type; traverse "get child" link
49+
DOWN = 2
50+
##
51+
#imaginary tree navigation type; finish with a child list
52+
UP = 3
53+
54+
MIN_TOKEN_TYPE = UP+1
55+
56+
INVALID_TOKEN_TYPE = 0
57+

0 commit comments

Comments
 (0)