|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright 2007 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +""" @package google.appengine._internal.antlr3 |
| 18 | +@brief ANTLR3 runtime package |
| 19 | +
|
| 20 | +This module contains all support classes, which are needed to use recognizers |
| 21 | +generated by ANTLR3. |
| 22 | +
|
| 23 | +@mainpage |
| 24 | +
|
| 25 | +\note Please be warned that the line numbers in the API documentation do not |
| 26 | +match the real locations in the source code of the package. This is an |
| 27 | +unintended artifact of doxygen, which I could only convince to use the |
| 28 | +correct module names by concatenating all files from the package into a single |
| 29 | +module file... |
| 30 | +
|
| 31 | +Here is a little overview over the most commonly used classes provided by |
| 32 | +this runtime: |
| 33 | +
|
| 34 | +@section recognizers Recognizers |
| 35 | +
|
| 36 | +These recognizers are baseclasses for the code which is generated by ANTLR3. |
| 37 | +
|
| 38 | +- BaseRecognizer: Base class with common recognizer functionality. |
| 39 | +- Lexer: Base class for lexers. |
| 40 | +- Parser: Base class for parsers. |
| 41 | +- tree.TreeParser: Base class for %tree parser. |
| 42 | +
|
| 43 | +@section streams Streams |
| 44 | +
|
| 45 | +Each recognizer pulls its input from one of the stream classes below. Streams |
| 46 | +handle stuff like buffering, look-ahead and seeking. |
| 47 | +
|
| 48 | +A character stream is usually the first element in the pipeline of a typical |
| 49 | +ANTLR3 application. It is used as the input for a Lexer. |
| 50 | +
|
| 51 | +- ANTLRStringStream: Reads from a string objects. The input should be a unicode |
| 52 | + object, or ANTLR3 will have trouble decoding non-ascii data. |
| 53 | +- ANTLRFileStream: Opens a file and read the contents, with optional character |
| 54 | + decoding. |
| 55 | +- ANTLRInputStream: Reads the date from a file-like object, with optional |
| 56 | + character decoding. |
| 57 | +
|
| 58 | +A Parser needs a TokenStream as input (which in turn is usually fed by a |
| 59 | +Lexer): |
| 60 | +
|
| 61 | +- CommonTokenStream: A basic and most commonly used TokenStream |
| 62 | + implementation. |
| 63 | +- TokenRewriteStream: A modification of CommonTokenStream that allows the |
| 64 | + stream to be altered (by the Parser). See the 'tweak' example for a usecase. |
| 65 | +
|
| 66 | +And tree.TreeParser finally fetches its input from a tree.TreeNodeStream: |
| 67 | +
|
| 68 | +- tree.CommonTreeNodeStream: A basic and most commonly used tree.TreeNodeStream |
| 69 | + implementation. |
| 70 | +
|
| 71 | +
|
| 72 | +@section tokenstrees Tokens and Trees |
| 73 | +
|
| 74 | +A Lexer emits Token objects which are usually buffered by a TokenStream. A |
| 75 | +Parser can build a Tree, if the output=AST option has been set in the grammar. |
| 76 | +
|
| 77 | +The runtime provides these Token implementations: |
| 78 | +
|
| 79 | +- CommonToken: A basic and most commonly used Token implementation. |
| 80 | +- ClassicToken: A Token object as used in ANTLR 2.x, used to %tree |
| 81 | + construction. |
| 82 | +
|
| 83 | +Tree objects are wrapper for Token objects. |
| 84 | +
|
| 85 | +- tree.CommonTree: A basic and most commonly used Tree implementation. |
| 86 | +
|
| 87 | +A tree.TreeAdaptor is used by the parser to create tree.Tree objects for the |
| 88 | +input Token objects. |
| 89 | +
|
| 90 | +- tree.CommonTreeAdaptor: A basic and most commonly used tree.TreeAdaptor |
| 91 | +implementation. |
| 92 | +
|
| 93 | +
|
| 94 | +@section Exceptions |
| 95 | +
|
| 96 | +RecognitionException are generated, when a recognizer encounters incorrect |
| 97 | +or unexpected input. |
| 98 | +
|
| 99 | +- RecognitionException |
| 100 | + - MismatchedRangeException |
| 101 | + - MismatchedSetException |
| 102 | + - MismatchedNotSetException |
| 103 | + . |
| 104 | + - MismatchedTokenException |
| 105 | + - MismatchedTreeNodeException |
| 106 | + - NoViableAltException |
| 107 | + - EarlyExitException |
| 108 | + - FailedPredicateException |
| 109 | + . |
| 110 | +. |
| 111 | +
|
| 112 | +A tree.RewriteCardinalityException is raised, when the parsers hits a |
| 113 | +cardinality mismatch during AST construction. Although this is basically a |
| 114 | +bug in your grammar, it can only be detected at runtime. |
| 115 | +
|
| 116 | +- tree.RewriteCardinalityException |
| 117 | + - tree.RewriteEarlyExitException |
| 118 | + - tree.RewriteEmptyStreamException |
| 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 | +__version__ = '3.1.1' |
| 158 | + |
| 159 | +def version_str_to_tuple(version_str): |
| 160 | + import re |
| 161 | + import sys |
| 162 | + |
| 163 | + if version_str == 'HEAD': |
| 164 | + return (sys.maxsize, sys.maxsize, sys.maxsize, sys.maxsize) |
| 165 | + |
| 166 | + m = re.match(r'(\d+)\.(\d+)(\.(\d+))?(b(\d+))?', version_str) |
| 167 | + if m is None: |
| 168 | + raise ValueError('Bad version string %r' % version_str) |
| 169 | + |
| 170 | + major = int(m.group(1)) |
| 171 | + minor = int(m.group(2)) |
| 172 | + patch = int(m.group(4) or 0) |
| 173 | + beta = int(m.group(6) or sys.maxsize) |
| 174 | + |
| 175 | + return (major, minor, patch, beta) |
| 176 | + |
| 177 | + |
| 178 | +runtime_version_str = __version__ |
| 179 | +runtime_version = version_str_to_tuple(runtime_version_str) |
| 180 | + |
| 181 | +from .exceptions import * |
| 182 | + |
| 183 | +from .constants import * |
| 184 | +from .dfa import * |
| 185 | +from .recognizers import * |
| 186 | +from .streams import * |
| 187 | +from .tokens import * |
0 commit comments