1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
# SPDX-License-Identifier: BSD-2-Clause
# Copyright (c) 2024 Phil Thompson <[email protected]>
from ..specification import CodeBlock
from ply.lex import TOKEN
# The lexer states.
states = (
('code', 'exclusive'),
('ccomment', 'exclusive'),
('directive', 'inclusive'),
('needeol', 'inclusive'),
)
# The single character tokens.
literals = '(){}.,;:=!- */&|~<>[]%^'
# The non-code directives.
directives = {
'AutoPyName', 'CompositeModule', 'DefaultDocstringFormat',
'DefaultDocstringSignature', 'DefaultEncoding', 'DefaultMetatype',
'DefaultSupertype', 'End', 'Exception', 'Feature', 'HideNamespace', 'If',
'Import', 'Include', 'License', 'MappedType', 'Module', 'Platforms',
'Property', 'Timeline',
# Remove in SIP v7.
'Plugin',
}
# The code directives.
code_directives = {
'AccessCode', 'BIGetBufferCode', 'BIReleaseBufferCode',
'ConvertFromTypeCode', 'ConvertToSubClassCode', 'ConvertToTypeCode',
'Copying', 'Docstring', 'ExportedHeaderCode', 'ExportedTypeHintCode',
'Extract', 'FinalisationCode', 'GCClearCode', 'GCTraverseCode', 'GetCode',
'InitialisationCode', 'InstanceCode', 'MethodCode', 'ModuleCode',
'ModuleHeaderCode', 'PickleCode', 'PostInitialisationCode',
'PreInitialisationCode', 'PreMethodCode', 'RaiseCode', 'ReleaseCode',
'SetCode', 'TypeCode', 'TypeHeaderCode', 'TypeHintCode', 'UnitCode',
'UnitPostIncludeCode', 'VirtualCallCode', 'VirtualCatcherCode',
'VirtualErrorHandler',
# Remove in SIP v7.
'BIGetCharBufferCode', 'BIGetReadBufferCode', 'BIGetSegCountCode',
'BIGetWriteBufferCode',
}
# The plain keywords.
keywords = {
'bool', 'char', 'class', 'const', 'double', 'enum', 'explicit', 'false',
'final', 'float', 'int', 'long', 'namespace', 'noexcept', 'NULL',
'operator', 'private', 'protected', 'public', 'Py_hash_t', 'Py_ssize_t',
'Q_SIGNAL', 'Q_SIGNALS', 'Q_SLOT', 'Q_SLOTS', 'short', 'signals', 'signed',
'SIP_PYBUFFER', 'SIP_PYCALLABLE', 'SIP_PYDICT', 'SIP_PYENUM', 'SIP_PYLIST',
'SIP_PYOBJECT', 'SIP_PYSLICE', 'SIP_PYTUPLE', 'SIP_PYTYPE', 'size_t',
'slots', 'static', 'struct', 'template', 'throw', 'true', 'typedef',
'union', 'unsigned', 'virtual', 'void', 'wchar_t',
# Remove in SIP v7.
'SIP_SSIZE_T',
}
# The directive keywords.
directive_keywords = {
'all_raise_py_exception', 'call_super_init', 'default_VirtualErrorHandler',
'False', 'format', 'get', 'id', 'keyword_arguments', 'language',
'licensee', 'name', 'optional', 'order', 'remove_leading', 'set',
'signature', 'timestamp', 'True', 'type', 'py_ssize_t_clean',
'use_argument_names', 'use_limited_api',
}
# The lexer tokens.
tokens = [
'CODE_BLOCK', 'DOTTED_NAME', 'ELLIPSIS', 'EOF', 'EOL', 'FILE_PATH',
'LOGICAL_OR', 'NAME', 'NUMBER', 'QUOTED_CHAR', 'REAL', 'SCOPE', 'STRING',
]
tokens.extend(directives)
tokens.extend(code_directives)
tokens.extend(keywords)
tokens.extend(directive_keywords)
# Handle EOF.
def t_eof(t):
try:
t.lexer.pm.pop_file()
except IndexError:
return None
# Return an explicit EOF token. This stops the parser looking too far into
# the popped file.
t.type = 'EOF'
return t
# Handle errors.
def t_ANY_error(t):
t.lexer.pm.lexer_error(t, "'{0}' is unexpected".format(t.value[0]))
t.lexer.skip(1)
# Ignore whitespace except when reading code blocks.
t_ANY_ignore = ' \t\r'
t_code_ignore = ''
# Handle newlines outside of code blocks and comments.
def t_newline(t):
r'\n'
lexer = t.lexer
pm = lexer.pm
# Maintain the line number.
lexer.lineno = 1
# Enter the 'code' state if we are at the end of a code directive name and
# arguments.
if pm.code_block is not None and pm.paren_depth == 0:
pm.code_block.line_nr = lexer.lineno
pm.set_lexer_state('code')
# Maintain the parenthesis depth.
def t_LPAREN(t):
r'\('
t.lexer.pm.paren_depth = 1
t.type = '('
return t
def t_RPAREN(t):
r'\)'
t.lexer.pm.paren_depth -= 1
t.type = ')'
return t
# Handle directives.
def t_DIRECTIVE(t):
r'%[a-zA-Z][a-zA-Z]*'
# The name of the directive is used as its type.
name = t.value[t.value.index('%') 1:]
if name in code_directives:
t.lexer.pm.code_block = CodeBlock(t.lexer.pm.raw_sip_file)
t.type = name
elif name in directives:
t.type = name
return t
# Handle the %End of a code directive.
def t_code_END(t):
r'%End'
code_block = t.lexer.pm.code_block
t.lexer.pm.code_block = None
# Ignore any indentation preceding the # %End.
code_block.text = code_block.text.rstrip(' \t')
t.type = 'CODE_BLOCK'
t.value = code_block
t.lexer.begin('INITIAL')
return t
# Handle a newline when an end-of-line needs to be reported to the parser.
def t_needeol_newline(t):
r'\n'
# Maintain the line number.
t.lexer.lineno = 1
t.lexer.pm.set_lexer_state()
t.type = 'EOL'
return t
# Handle a newline in a code directive.
def t_code_newline(t):
r'\n'
# Maintain the line number.
t.lexer.lineno = 1
t.lexer.pm.code_block.text = t.value
# Discard the token.
return None
# Handle a character in a code directive.
def t_code_CH(t):
r'.'
t.lexer.pm.code_block.text = t.value
# Discard the token.
return None
# Handle keywords, ellipsis, names, dotted name and file paths.
ambiguous = r'[._A-Za-z][._/A-Za-z\d\-]*[._A-Za-z\d]'
@TOKEN(ambiguous)
def t_AMBIGUOUS(t):
t.type = t.lexer.pm.disambiguate_token(t.value, keywords)
return t
# Handle directive keywords (ie. keywords that are only recognised in the
# context of a directive), ellipsis, names, dotted name and file paths.
@TOKEN(ambiguous)
def t_directive_AMBIGUOUS(t):
t.type = t.lexer.pm.disambiguate_token(t.value, directive_keywords)
return t
# Handle a C -style comment.
def t_CPPCOMMENT(t):
r'//.*'
# Discard the token.
return None
# Handle the start of a C-style comment.
def t_COMMENTSTART(t):
r'/\*'
t.lexer.push_state('ccomment')
# Discard the token.
return None
# Handle the end of a C-style comment.
def t_ccomment_COMMENTEND(t):
r'\*/'
t.lexer.pop_state()
# Discard the token.
return None
# Handle a newline in a C-style comment.
def t_ccomment_newline(t):
r'\n'
# Maintain the line number.
t.lexer.lineno = 1
# Discard the token.
return None
# Handle the content of a C-style comment.
def t_ccomment_CH(t):
r'.'
# Maintain the line number.
if t.value == '\n':
t.lexer.lineno = 1
# Discard the token.
return None
# Handle an unsigned hexadecimal number.
def t_HEXNUMBER(t):
r'0x[\da-fA-F] '
t.type = 'NUMBER'
t.value = int(t.value, base=16)
return t
# Handle a number.
def t_NUMBER(t):
r'-?(\d (\.\d*)?|\.\d )([eE][- ]?\d )?[fFlLuU]?'
# Remove any suffix character.
value = t.value
if not value[-1].isdigit():
value = value[:-1]
try:
t.type = 'NUMBER'
t.value = int(value)
except ValueError:
t.type = 'REAL'
t.value = float(value)
return t
# Handle a double-quoted string.
def t_STRING(t):
r'"(\\.|[^\\"])*"'
# Strip the quotes and handle any standard escape characters.
value = t.value.strip('"')
value = value.replace(r'\n', '\n')
value = value.replace(r'\r', '\r')
value = value.replace(r'\t', '\t')
value = value.replace(r'\\', '\\')
t.type = 'STRING'
t.value = value
return t
# Handle a single-quoted hex encoded character.
def t_QHEXCH(t):
r"'\\x[\da-fA-F] '"
t.type = 'QUOTED_CHAR'
t.value = chr(int(t.value.strip("'")[2:], base=16))
return t
# Handle a single-quoted character.
def t_QCH(t):
r"'[^'\n]*['\n]"
# Make sure these is only one quoted character. If not then report the
# error and carry on with a fudged value.
n_ch = len(t.value)
if n_ch != 3:
t.lexer.pm.lexer_error(t,
"exactly one character expected between single quotes")
if n_ch == 0:
t.value = '?'
t.type = 'QUOTED_CHAR'
t.value = t.value[1]
return t
# The remaining trivial token definitions.
t_LOGICAL_OR = r'\|\|'
t_SCOPE = r'::'
# We only deal with a single character as everything else is handled by
# AMBIGUOUS.
t_NAME = r'[_A-Za-z]'
|