parser
Location
dataclass
Where a token starts in the source text. offset is 0-based, and line
and column are 1-based.
Source code in src/syntactes/parser/token.py
from_object(obj)
classmethod
Builds a location from a mapping with the keys offset, line and
column, or from any other object with those attributes, such as a
lexer's own location. Other keys and attributes are ignored, and each
value is converted with int.
Raises syntactes.parser.LocationObjectError if a key or attribute is
missing, or if its value can't be converted to an integer, with the
conversion's exception as its cause.
Source code in src/syntactes/parser/token.py
Token
A token of the stream a parser reads: a terminal symbol, the Location it
starts at, and the value it was read with, e.g.
Token(NUMBER, Location(0, 1, 1), "42"). Callbacks receive one token per
right-hand side symbol. A non-terminal's token holds the value computed for
its rule, and the location of its first token (for an empty rule, of the
token that follows it).
Tokens are equal if their symbols and values are. Hashing uses only the symbol, so a token with an unhashable value can still be hashed.
Source code in src/syntactes/parser/token.py
LR0Parser
LR1Parser
Parser
Bases: ABC
Base class of the LR parsers. Parses a stream of tokens with a parsing table,
and on each reduction computes the rule's value: with the callback registered
with execute_on, else with the rule's primitive from the grammar.
Source code in src/syntactes/parser/parser.py
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 | |
execute_on(rule)
Decorate a function to be executed when this parser reduces by rule.
rule is a Rule or its text, as str(rule) prints it: the left-hand
side, ->, then the right-hand side symbols separated by whitespace,
e.g. "E -> T + E". An empty rule is "A -> ε" or "A ->", and the
starting rule ends with $. Symbols are matched by name. The text can
start with the rule's primitive, as in Grammar.from_text
("int % T -> x", "add(1,3) % E -> E + T"), which must then match
the grammar's; without operands, "add % E -> E + T" matches any.
The function is called with one token per right-hand side symbol of the rule, and returns the value of the left-hand side token. The decorated function is returned unchanged.
Raises ValueError if rule is not a rule of the parser's grammar, if
its text is malformed, matches more than one rule, or gives a primitive
the rule doesn't have.
Source code in src/syntactes/parser/parser.py
from_grammar(grammar)
classmethod
Create a parser for the given grammar.
Source code in src/syntactes/parser/parser.py
parse(stream)
Parses the given stream of tokens. Expects a token with the EOF symbol,
Token(Symbol.eof(), location), as the last one.
On each reduction the parser pushes a token for the left-hand side. Its
location is that of the rule's first token, or for an empty rule, of
the token that follows it. Its value is the rule's value, which is, in
order of precedence:
- what the callback registered for the rule returns, called with one token per right-hand side symbol, in order;
- the result of the rule's primitive (
Grammar.primitives): a binary operation gets the values of the tokens atGrammar.operands,Nonegives None, and any other primitive converts its single token's value; - the value of the rule's token, if it has exactly one;
- None.
On accept the starting rule's value is computed the same way, from its right-hand side tokens except the trailing EOF, and returned. So a starting rule with neither a callback nor a primitive returns the start symbol's value.
Raises syntactes.parser.PrimitiveError if a primitive raises, with the
original exception as its cause. Exceptions raised by callbacks
propagate unchanged.
Raises syntactes.parser.UnexpectedTokenError if an unexpected token is
received.
Raises syntactes.parser.NotAcceptedError if the stream of tokens ends
before the parser receives an accept action.
Source code in src/syntactes/parser/parser.py
LocationObjectError
Bases: ParserError
Location.from_object received an object it can't build a location from:
it lacks offset, line or column, or one of them isn't an integer. In
the latter case the original exception is available as __cause__.
Source code in src/syntactes/parser/exception.py
NotAcceptedError
Bases: ParserError
The parser did not receive an accept action. The stream of tokens is syntactically invalid.
ParserError
PrimitiveError
Bases: ParserError
A rule's primitive raised while the parser applied it, e.g. int on a
token whose value isn't a number. The rule and primitive are available as
rule and primitive, and the original exception as __cause__.
Source code in src/syntactes/parser/exception.py
UnexpectedTokenError
Bases: ParserError
A token was received that does not map to an action. The stream of tokens
is syntactically invalid. The token is received_token, and the terminals
that would have been valid are expected_symbols.