-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pygments plugins for Gerber code and PyGerber API
- Loading branch information
Showing
8 changed files
with
172 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "pygerber" | ||
version = "3.0.0a0" | ||
version = "3.0.0a2" | ||
description = "Parsing, formatting and rendering toolkit for Gerber X3 file format" | ||
authors = ["Krzysztof Wisniewski <[email protected]>"] | ||
license = "MIT" | ||
|
@@ -59,6 +59,7 @@ pygls = { version = "^1.0.2", optional = true } | |
lsprotocol = { version = "^2023.0.0a3", optional = true } | ||
drawsvg = { version = "^2.3.0", optional = true } | ||
typing-extensions = "^4.12.2" | ||
pygments = { version = "^2.18.0", optional = true } | ||
|
||
[tool.poetry.group.dev.dependencies] | ||
mypy = "^1.6.1" | ||
|
@@ -100,15 +101,19 @@ black = "^24.4.0" | |
[tool.poetry.extras] | ||
language_server = ["pygls", "lsprotocol"] | ||
svg = ["drawsvg"] | ||
all = ["pygls", "lsprotocol", "drawsvg"] | ||
pygments = ["pygments"] | ||
all = ["pygls", "lsprotocol", "drawsvg", "pygments"] | ||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
build-backend = "poetry.core.masonry.api" | ||
|
||
[tool.poetry.scripts] | ||
pygerber = "pygerber.__main__:main" | ||
pygerber_language_server = "pygerber.gerber.language_server.__main__:main" | ||
|
||
[tool.poetry.plugins."pygments.lexers"] | ||
pygerber_docs_lexer = "pygerber.gerber.pygments:PyGerberDocsLexer" | ||
gerber_lexer = "pygerber.gerber.pygments:GerberLexer" | ||
|
||
[tool.poe.tasks] | ||
# ------------------------------------------------------------------------------------- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
|
||
from __future__ import annotations | ||
|
||
__version__ = "3.0.0a1" | ||
__version__ = "3.0.0a2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
"""The `pygments` module provides Pygments lexer for Gerber files.""" | ||
|
||
from __future__ import annotations | ||
|
||
import importlib.util | ||
import os | ||
from typing import Any, ClassVar, Dict, List, Optional | ||
|
||
_IS_PYGMENTS_AVAILABLE: Optional[bool] = None | ||
|
||
|
||
def is_pygments_available() -> bool: | ||
"""Check if the language server feature is available.""" | ||
global _IS_PYGMENTS_AVAILABLE # noqa: PLW0603 | ||
|
||
if _IS_PYGMENTS_AVAILABLE is None: | ||
try: | ||
_spec_pygls = importlib.util.find_spec("pygls") | ||
_spec_lsprotocol = importlib.util.find_spec("lsprotocol") | ||
|
||
except (ImportError, ValueError): | ||
return False | ||
|
||
else: | ||
_IS_PYGMENTS_AVAILABLE = (_spec_pygls is not None) and ( | ||
_spec_lsprotocol is not None | ||
) | ||
|
||
return _IS_PYGMENTS_AVAILABLE | ||
|
||
|
||
if is_pygments_available(): | ||
from pygments.lexer import RegexLexer | ||
from pygments.lexers.python import PythonLexer | ||
from pygments.token import Keyword, Name, Number | ||
|
||
class GerberLexer(RegexLexer): | ||
"""The `GerberLexer` class is a Pygments lexer for Gerber files.""" | ||
|
||
name = "Gerber" | ||
aliases: ClassVar[List[str]] = ["gerber", "gbr", "grb", "gerberx3"] | ||
filenames: ClassVar[List[str]] = ["*.grb", "*.gbr"] | ||
|
||
tokens: ClassVar[Dict[str, Any]] = { | ||
"root": [ | ||
(r"X(?=[0-9]+)", Keyword), | ||
(r"Y(?=[0-9]+)", Keyword), | ||
(r"I(?=[0-9]+)", Keyword), | ||
(r"J(?=[0-9]+)", Keyword), | ||
(r"(?<=%)FS", Keyword), | ||
(r"(?<=%FS)[LT][AI]", Keyword.Constant), | ||
(r"(?<=%)TA", Keyword), | ||
(r"(?<=%)TO", Keyword), | ||
(r"(?<=%)TF", Keyword), | ||
(r"(?<=%)TD", Keyword), | ||
(r"(?<=%)MO(?=MM|IN)", Keyword), | ||
(r"(?<=%MO)(MM|IN)", Keyword.Constant), | ||
(r"(?<=%ADD[0-9])[._a-zA-Z$][._a-zA-Z0-9]*", Name.Class), | ||
(r"(?<=%ADD[0-9][0-9])[._a-zA-Z$][._a-zA-Z0-9]*", Name.Class), | ||
(r"(?<=%ADD[0-9][0-9][0-9])[._a-zA-Z$][._a-zA-Z0-9]*", Name.Class), | ||
(r"(?<=%)AD", Keyword), | ||
(r"(?<=%)AM", Keyword), | ||
(r"(?<=%AM)[._a-zA-Z$][._a-zA-Z0-9]*", Name.Class), | ||
(r"(?<=%)AB", Keyword), | ||
(r"(?<=%)SR", Keyword), | ||
(r"D0*1(?=\*)", Keyword), | ||
(r"D0*2(?=\*)", Keyword), | ||
(r"D0*3(?=\*)", Keyword), | ||
(r"D[0-9]+", Name.Variable), | ||
(r"G[0-9]+", Keyword), | ||
(r"M[0-9]+", Keyword), | ||
(r"[0-9]+\.[0-9]+", Number), | ||
(r"[0-9]+", Number), | ||
] | ||
} | ||
|
||
class PyGerberDocsLexer(PythonLexer): | ||
"""The `PyGerberDocsLexer` class is a Pygments lexer for Gerber files.""" | ||
|
||
name = "PyGerber Python Docs Lexer" | ||
aliases: ClassVar[List[str]] = ["docspygerberlexer"] | ||
filenames: ClassVar[List[str]] = [] | ||
|
||
EXTRA_SYMBOLS: ClassVar[dict[str, Any]] = { | ||
**{ # noqa: PIE800 | ||
"GerberX3Builder": Name.Class, | ||
"new_pad": Name.Function, | ||
"add_pad": Name.Function, | ||
"add_cutout_pad": Name.Function, | ||
"add_trace": Name.Function, | ||
"get_code": Name.Function, | ||
"set_standard_attributes": Name.Function, | ||
}, | ||
**{ # noqa: PIE800 | ||
"GerberX3Builder": Name.Class, | ||
"dump": Name.Function, | ||
"dumps": Name.Function, | ||
"raw": Name.Property, | ||
}, | ||
**{ # noqa: PIE800 | ||
"PadCreator": Name.Class, | ||
"circle": Name.Function, | ||
"rectangle": Name.Function, | ||
"rounded_rectangle": Name.Function, | ||
"regular_polygon": Name.Function, | ||
"custom": Name.Function, | ||
}, | ||
**{ # noqa: PIE800 | ||
"Pad": Name.Class, | ||
"set_aperture_function": Name.Function, | ||
"set_drill_tolerance": Name.Function, | ||
"set_custom_attribute": Name.Function, | ||
}, | ||
**{ # noqa: PIE800 | ||
"CustomPadCreator": Name.Class, | ||
"create": Name.Function, | ||
"add_circle": Name.Function, | ||
"cut_circle": Name.Function, | ||
"add_vector_line": Name.Function, | ||
"cut_vector_line": Name.Function, | ||
"add_center_line": Name.Function, | ||
"cut_center_line": Name.Function, | ||
"add_outline": Name.Function, | ||
"cut_outline": Name.Function, | ||
"add_polygon": Name.Function, | ||
"cut_polygon": Name.Function, | ||
"add_thermal": Name.Function, | ||
}, | ||
**{ # noqa: PIE800 | ||
"Draw": Name.Class, | ||
}, | ||
**{ # noqa: PIE800 | ||
"PadDraw": Name.Class, | ||
}, | ||
**{ # noqa: PIE800 | ||
"TraceDraw": Name.Class, | ||
}, | ||
} | ||
|
||
def get_tokens_unprocessed(self, text: Any) -> Any: | ||
"""Get tokens from the text.""" | ||
if os.environ["MKDOCS_MODE"] == "1": | ||
for index, token, value in super().get_tokens_unprocessed(text): | ||
if token is Name and value in self.EXTRA_SYMBOLS: | ||
yield index, self.EXTRA_SYMBOLS[value], value | ||
else: | ||
yield index, token, value | ||
else: | ||
yield from super().get_tokens_unprocessed(text) |