Skip to content

Commit dce2374

Browse files
committed
Add procedure for determining token values for tokens of types that do not feature a value attribute
Tokenization as defined in the specification effectively maps code points in the input text stream, to certain token types where the "value" is implied -- a comma token, for instance, is created when the `,` is encountered in the input stream. We envision needing the inverse function which returns the code point given a token's type. This commit adds an implementation of such function.
1 parent b930beb commit dce2374

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

expand/csspring/syntax/tokenizing.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from ..utils import CP, BufferedPeekingReader, is_surrogate_code_point_ordinal, IteratorReader, join, parser_error, PeekingUnreadingReader
1010

1111
from abc import ABC
12+
import builtins
1213
from collections.abc import Callable, Iterable, Iterator
1314
from dataclasses import dataclass
1415
from decimal import Decimal
@@ -502,3 +503,26 @@ def is_non_printable_code_point(cp: CP) -> bool:
502503
def is_whitespace(cp: CP) -> bool:
503504
"""See http://drafts.csswg.org/css-syntax/#whitespace."""
504505
return is_newline(cp) or cp in ('\t', ' ')
506+
507+
# Map of values by token type, for types of tokens which do _not_ have the `value` attribute
508+
token_values = { # For the `token_value` procedure to work as intended, subtypes should be listed _before_ their supertype(s)
509+
OpenBraceToken: '{',
510+
OpenBracketToken: '[',
511+
OpenParenToken: '(',
512+
CloseBraceToken: '}',
513+
CloseBracketToken: ']',
514+
CloseParenToken: ')',
515+
ColonToken: ':',
516+
CommaToken: ',',
517+
SemicolonToken: ';',
518+
CDCToken: '->',
519+
CDOToken: '!--',
520+
}
521+
522+
def token_value(type: builtins.type[Token]) -> str:
523+
"""Get the value of a token by its type for types of tokens that do _not_ feature a `value` attribute.
524+
525+
:param type: Type of token to get the value of
526+
:returns: The value common to the type of tokens
527+
"""
528+
return next(value for key, value in token_values.items() if issubclass(type, key))

0 commit comments

Comments
 (0)