|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
| 3 | +import logging |
3 | 4 | import os
|
4 | 5 | import pprint
|
| 6 | +import sys |
5 | 7 |
|
6 | 8 | import json5
|
7 | 9 |
|
8 | 10 | from .helper_functions import only_dirs, resolve_globs
|
9 | 11 | from .jsonrpc import JSONRPC2Connection, ReadWriter, path_from_uri
|
10 | 12 | from .langserver import LangServer
|
11 |
| -from .parsers.internal.parser import FortranFile |
| 13 | +from .parsers.internal.parser import FortranFile, preprocess_file |
12 | 14 |
|
13 | 15 |
|
14 | 16 | class DebugError(Exception):
|
@@ -439,6 +441,56 @@ def debug_parser(args):
|
439 | 441 | separator()
|
440 | 442 |
|
441 | 443 |
|
| 444 | +def debug_preprocessor(args): |
| 445 | + """Debug the preprocessor of the Language Server |
| 446 | + Triggered by `--debug_preprocessor` option. |
| 447 | +
|
| 448 | + Parameters |
| 449 | + ---------- |
| 450 | + args : Namespace |
| 451 | + The arguments parsed from the `ArgumentParser` |
| 452 | + """ |
| 453 | + |
| 454 | + def sep_lvl2(heading: str): |
| 455 | + print("\n" + "=" * 75 + f"\n{heading}\n" + "=" * 75) |
| 456 | + |
| 457 | + print("\nTesting preprocessor") |
| 458 | + separator() |
| 459 | + |
| 460 | + logging.basicConfig(level=logging.DEBUG, stream=sys.stdout, format="%(message)s") |
| 461 | + |
| 462 | + file = args.debug_filepath |
| 463 | + ensure_file_accessible(file) |
| 464 | + with open(file, encoding="utf-8") as f: |
| 465 | + lines = f.readlines() |
| 466 | + |
| 467 | + root = args.debug_rootpath if args.debug_rootpath else os.path.dirname(file) |
| 468 | + _, pp_defs, include_dirs = read_config(root, args.config) |
| 469 | + |
| 470 | + sep_lvl2("Preprocessor Pass:") |
| 471 | + output, skips, defines, defs = preprocess_file( |
| 472 | + lines, file, pp_defs, include_dirs, debug=True |
| 473 | + ) |
| 474 | + |
| 475 | + sep_lvl2("Preprocessor Skipped Lines:") |
| 476 | + for line in skips: |
| 477 | + print(f" {line}") |
| 478 | + |
| 479 | + sep_lvl2("Preprocessor Macros:") |
| 480 | + for key, value in defs.items(): |
| 481 | + print(f" {key} = {value}") |
| 482 | + |
| 483 | + sep_lvl2("Preprocessor Defines (#define):") |
| 484 | + for line in defines: |
| 485 | + print(f" {line}") |
| 486 | + |
| 487 | + sep_lvl2("Preprocessor Final Output:") |
| 488 | + for line in output: |
| 489 | + print(rf" {line.rstrip()}") |
| 490 | + |
| 491 | + separator() |
| 492 | + |
| 493 | + |
442 | 494 | def ensure_file_accessible(filepath: str):
|
443 | 495 | """Ensure the file exists and is accessible, raising an error if not."""
|
444 | 496 | if not os.path.isfile(filepath):
|
|
0 commit comments