Coverage for klayout_pex/netlist/expression/syntax.py: 100%
31 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-06 13:14 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-06 13:14 +0000
1#
2# --------------------------------------------------------------------------------
3# SPDX-FileCopyrightText: 2024-2026 Martin Jan Köhler and Harald Pretl
4# Johannes Kepler University, Institute for Integrated Circuits.
5#
6# This file is part of KPEX
7# (see https://github.com/iic-jku/klayout-pex).
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
21# SPDX-License-Identifier: GPL-3.0-or-later
22# --------------------------------------------------------------------------------
23#
25"""
26How a netlist flavour writes expressions (e.g. ngspice, see ngspice.py): the spelling and precedence
27of its operators, its functions, its numbers, and whether it ignores the case of names.
29The parser (see parser.py) reads the expressions of any flavour with its syntax.
31NOTE: imports neither KLayout nor generated protobuf code, so that format-only paths can use it
32"""
34from __future__ import annotations
35from dataclasses import dataclass
36from enum import Enum
37from typing import *
39from .nodes import BinaryOperator, ExpressionFunction, UnaryOperator
42class Associativity(Enum):
43 LEFT = 'left' # a - b - c is (a - b) - c
44 RIGHT = 'right' # a ** b ** c is a ** (b ** c)
47@dataclass(frozen=True)
48class BinaryOperatorSyntax:
49 operator: BinaryOperator
50 precedence: int # the higher, the stronger it binds
51 associativity: Associativity = Associativity.LEFT
54@dataclass(frozen=True)
55class ExpressionSyntax:
56 binary_operators: Mapping[str, BinaryOperatorSyntax]
57 """by spelling, e.g. '**'"""
59 unary_operators: Mapping[str, UnaryOperator]
60 """by spelling, e.g. '-'"""
62 unary_precedence: int
63 """compared with the binary operators, e.g. -2^2 is -(2^2), if ^ has a higher one"""
65 functions: Mapping[str, ExpressionFunction]
66 """by name, in lowercase if the flavour ignores the case of names"""
68 number_pattern: str
69 """the regular expression of a number, e.g. of 1.5k"""
71 parse_number: Callable[[str], float]
72 """the value of a number (a match of number_pattern)"""
74 ignores_case: bool
75 """whether e.g. W and w are the same parameter"""
77 def name_key(self, name: str) -> str:
78 """
79 The name of a parameter or function, as its syntax compares it
80 """
81 return name.lower() if self.ignores_case else name