Commit Graph

117 Commits

Author SHA1 Message Date
co63oc 9c125a26db Fix typos in comments 2025-03-08 08:09:58 -08:00
Paolo Bonzini ff5b5e66c9 optimize variable assignments
Except for set_variable(), the variable name is certainly an identifier because it
comes from the parser; thus, the check is unnecessary.  Move the regular expression
match to func_set_variable().

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-01-08 13:45:26 -05:00
Charles Brunet 37c5bffbff mformat: better handling of continuation lines
Fixes #13566. Fixes #13567.
2024-09-06 11:17:59 -07:00
Eli Schwartz 19847ba24a
update various deprecation notices to call out meson 2.0 2024-08-30 15:44:13 -04:00
Charles Brunet d9ba42217f format: fix indentation of comments
Fixes #13508

- Fix indentation of comments in arrays
- Fix indentation of comments in dicts
- Fix indentation of comments in if clauses
- Fix indentation of comments in foreach clauses
2024-08-20 09:38:27 -07:00
Charles Brunet dbfd3e8c41 format: fix edge case with empty functions
format was adding a new empty line each time when trying to split a long line containing a function with no arguments
2024-06-26 15:44:49 -04:00
Charles Brunet 2b37101998 meson format command 2024-04-08 10:43:57 -07:00
Charles Brunet bd4fd90730 parser: revert to single StringNode type
this will allow transforming string types in the formater
2024-04-08 10:43:57 -07:00
Charles Brunet 154b9e78d5 fix colon wrongly named column in parser
In #02ff955, I used the word `columns` instead of `colons`, but the
meaning really was about the ':' symbol.
2024-04-08 10:43:57 -07:00
Charles Brunet 715dc27b2b Detect utf8 bom from meson build files
Some text editors on Windows may use utf8bom encoding by default.
Prevent crash and properly report misencoded files.

Fixes #12766.
2024-02-23 15:00:39 -05:00
Eli Schwartz 6f7e745052
Remove implicit-optional assignment in `__init__` that cannot ever be true
IfClauseNode is only ever initialized in such a way that this attribute
is immediately set to something valid. And attempting to access its
value when the value is None would be a pretty broken error anyway. The
assignment served no purpose, but did perform a frivolous runtime op in
addition to angering mypy's checks for implicit None.
2024-02-12 18:52:43 -05:00
Dylan Baker e991c4d454 Use SPDX-License-Identifier consistently
This replaces all of the Apache blurbs at the start of each file with an
`# SPDX-License-Identifier: Apache-2.0` string. It also fixes existing
uses to be consistent in capitalization, and to be placed above any
copyright notices.

This removes nearly 3000 lines of boilerplate from the project (only
python files), which no developer cares to look at.

SPDX is in common use, particularly in the Linux kernel, and is the
recommended format for Meson's own `project(license: )` field
2023-12-13 15:19:21 -05:00
Charles Brunet 14e35b63c0 parser: allow whitespaces and comments in cont_eol
FIXME: another approach would be to consider cont_eol as comment (i.e.
add backslash and whitespaces to the comment regex). In both cases it
works until we want to parse comments separately.

TODO?: handle eol_cont inside a string (to split long string without
breaking lines). Probably a bad idea and better to simply join a
multiline string.
2023-09-11 07:51:19 -04:00
Charles Brunet 11ef2a536c parser: preserve whitespaces and comments 2023-09-11 07:51:19 -04:00
Charles Brunet 5b29eff8ad parser: simplify other node constructors 2023-09-11 07:51:18 -04:00
Charles Brunet 13ddf8bd02 parser: simplify by using Unary and Binary Operator Node 2023-09-11 07:51:18 -04:00
Charles Brunet 0f4891cdf4 parser: simplify Assignment and PlusAssignment nodes 2023-09-11 07:51:18 -04:00
Charles Brunet 02ff9553db parser: add SymbolNode to preserve operators 2023-09-11 07:51:18 -04:00
Charles Brunet f13260dd43 parser: add ElseNode 2023-09-11 07:51:18 -04:00
Charles Brunet 4b7a56caa2 parser: remember previous Token 2023-09-11 07:51:18 -04:00
Charles Brunet b94167ef50 parser: preserve value of all tokens 2023-09-11 07:51:18 -04:00
Charles Brunet 8d357ba62c parser: use IdNode for foreach varnames 2023-09-11 07:51:18 -04:00
Charles Brunet 306562b466 parser: use IdNode for function name and assignment name 2023-09-11 07:51:18 -04:00
Charles Brunet 35936283d2 parser: preserve escape chars in strings
use separate Node for multiline strings
2023-09-11 07:51:18 -04:00
Charles Brunet a730a2fe21 parser: remove useless __str__ methods on nodes 2023-09-11 07:51:17 -04:00
Charles Brunet 5707d39017 parser: preserve number base 2023-09-11 07:51:17 -04:00
Charles Brunet 3ff3b8abf6 parser: more specific error for float numbers 2023-09-11 07:51:17 -04:00
JCWasmx86 79ed2415e9 Add ParenthesizedNode 2023-09-11 07:51:17 -04:00
Eli Schwartz 90ce084144
treewide: automatic rewriting of all comment-style type annotations
Performed using https://github.com/ilevkivskyi/com2ann

This has no actual effect on the codebase as type checkers (still)
support both and negligible effect on runtime performance since
__future__ annotations ameliorates that. Technically, the bytecode would
be bigger for non function-local annotations, of which we have many
either way.

So if it doesn't really matter, why do a large-scale refactor? Simple:
because people keep wanting to, but it's getting nickle-and-dimed. If
we're going to do this we might as well do it consistently in one shot,
using tooling that guarantees repeatability and correctness.

Repeat with:

```
com2ann mesonbuild/
```
2023-08-11 13:41:03 -04:00
Eli Schwartz a01418db0a
remove useless type annotations
These annotations all had a default initializer of the correct type, or
a parent class annotation.
2023-08-11 13:37:17 -04:00
Xavier Claessens 7ac6afe265 mparser: Further cleanup node definitions
- Include BaseNode position in hash methods, integer is the most
  straightforward way of differentiating nodes.
- Exclude non hashable fields from hash method.
- Avoid using default values in BaseNode that way subclasses can have
  fields wihtout default value without repeating init=False.
- Nodes that does not add fields does not need `@dataclass`.
- Make all node types hashable because they can be used for feature_key
  in FeatureCheckBase.use().
- Remove unused type annotations
2023-06-01 15:32:18 -04:00
Dylan Baker 1ed1c8e2b0 mparser: add equality operators to nodes
This makes use of dataclasses, but without a dataclass generated
initializer. This means that we get nice `__repr__` and `__eq__` methods
without having to type them by hand.

Pylance understands `dataclass(init=False)`, but mypy doesn't.
https://github.com/microsoft/pyright/issues/1753
https://github.com/python/mypy/issues/10309
2023-06-01 15:32:18 -04:00
Eli Schwartz cc23996266
mparser: Add partial AST to exceptions
Surprisingly enough we need to do this twice. In some cases
(failing-meson/72 triggers this) we can error out after parsing the
codeblock, but without getting the expected eof.

We need to catch both exceptions as either one can interrupt the built
codeblock object.

Co-authored-by: Xavier Claessens <xavier.claessens@collabora.com>
2023-03-01 23:28:43 -05:00
Eli Schwartz 9423631b76
mparser: use an inherited ParseException everywhere 2023-03-01 23:13:29 -05:00
Eli Schwartz 774212e738
Revert "Exit meson with an error if an invalid escape sequence is found in a"
This reverts commit 348248f0a1.

The rules were relaxed in commit ccc4ce28cc
to permit this, so it's never possible to raise this exception anymore.
But that commit was incomplete, and didn't remove the now-useless
infrastructure for exception handling.

The test needed to test this was always broken, and then removed in
commit 465ef856ac, and still this useless
try/except persisted.
2023-03-01 23:13:28 -05:00
Xavier Claessens f0dc61a764 interpreter: Add testcase..endtestcase clause support
This is currently only enabled when running unit tests to facilitate
writing failing unit tests.

Fixes: #11394
2023-03-01 20:13:34 -05:00
Eli Schwartz 680b5ff819
treewide: add future annotations import 2023-02-01 17:01:30 -05:00
Dylan Baker 7b59a2e3ea mparser: Use a literal for the ComparisonNode
And fix a bug where `not in` is in the wrong order.
2023-01-17 15:14:53 -08:00
Dylan Baker 8f9c6c10c4 mparser: Don't create an exception to pass to mlog.warning
Just call `mlog.code_line` directly, since the exception is never raised.
2023-01-03 14:49:02 -05:00
Dylan Baker 7460ab20cd mlog: move code for printing code with a caret to the mlog module
We need this outside the constructor for the ParseException class, so
let's pull it out. mlog seemed like a good place since it's a text
formatting function, and has no dependencies.
2023-01-03 14:49:02 -05:00
Dylan Baker 3e55891ae7 mparser: don't pass a SimpleNamespace where a BaseNode is expected
mypy spotted this as well. And it turns out that we're not setting the
column either for the warning, so improvements!
2023-01-03 14:49:02 -05:00
Eli Schwartz e5a9272034
typing: fix some broken Sequence annotations
T.Sequence is a questionable concept. The idea is to hammer out generic,
maximally forgiving APIs that operate on protocols, which is a fancy way
of saying "I don't care if you use tuples or lists". This is rarely
needed, actually, and in exchange for this fancy behavior you get free
bugs.

Specifically, `somestr` is of type `T.Sequence[str]`, and also
`somestr[0]` is another string of type you guessed it. It's ~~turtles~~
strings all the way down.

It's worth noting that trying to code for "protocols" is a broken
concept if the contents have semantic meaning, e.g. it operates on
"the install tags of this object" rather than "an iterable that supports
efficient element access".

The other way to use T.Sequence is "I don't like that T.List is
invariant, but also I don't like that T.Tuple makes you specify exact
ordering". This sort of works. In fact it probably does work as long as
you don't allow str in your sequences, which of course everyone allows
anyway.

Use of Sequence has cute side effects, such as actually passing lists
around, knowing that you are going to get a list and knowing that you
need to pass it on as a list, and then having to re-allocate as
`list(mylist)` "because the type annotations says it could be a str or
tuple".

Except it cannot be a str, because if it is then the application is
fatally flawed and logic errors occur to disastrous end user effects,
and the type annotations:
- do not enforce their promises of annotating types
- fail to live up to "minimal runtime penalties" due to all the `list()`

Shun this broken concept, by hardening the type annotations. As it turns
out, we do not actually need any of this covariance or protocol-ism for
a list of strings! The whole attempt was a slow, buggy waste of time.
2022-12-11 18:28:39 -05:00
Dylan Baker 4da14918cd pylint: enable consider-using-in 2022-09-19 20:57:52 -04:00
Peter Lesslie d771fc7d0b Add support for multiline f-strings
+ Extend the parser to recognize the multiline f-strings, which the
documentation already implies will work.

The syntax is like:
```
x = 'hello'
y = 'world'

msg = f'''This is a multiline string.

Sending a message: '@x@ @y@'
'''
```

which produces:
```
This is a multiline string.

Sending a message: 'hello world'

```

+ Added some f-string tests cases to "62 string arithmetic" to exercise
the new behavior.
2022-05-01 12:47:37 -04:00
Eli Schwartz 2974f2bcb8
fix malformed warning to print the way it was meant to print
Given a meson.build with the contents:

```
t = '
'
```

We want to warn that this is bad. So we emitted this warning:
```
WARNING: Newline character in a string detected, use ''' (three single quotes) for multiline strings instead.
This will become a hard error in a future Meson release.                                 t = ' 4 4
```

The line contents and the offset are printed as gibberish after a big
whitespace run. These are elsewhere often passed to ParseException,
which pretty-prints this, but newlines aren't an exception, merely a
warning, and mlog.warning doesn't accept numeric strings as anything
more meaningful than something to print as text.

Fix this (by wrapping it in a ParseException) to properly print:
```
meson.build:4: WARNING: Newline character in a string detected, use ''' (three single quotes) for multiline strings instead.
This will become a hard error in a future Meson release.
t = '
    ^
```
2022-02-16 23:00:28 -05:00
Eli Schwartz aa0450adda
fix a couple misuses of textwrap.dedent
A backslash-escape of the last newline before a run of whitespace
leading to the indented string ending and function termination `)` does
not actually escape the entire line and make it do nothing. In fact what
it does is cause all that whitespace to be part of the preceding line,
and get printed.

Meanwhile the textwrap.dedent documentation states that lines with only
whitespace get normalized. When you *don't* mess with that final line,
dedent actually does the right thing and makes the output message end
with a single newline after the important text.
2022-02-16 22:59:48 -05:00
Eli Schwartz 4b351aef26
first pass at migrating to dataclasses
In some cases, init variables that accept None as a sentinel and
immediately overwrite with [], are migrated to dataclass field
factories. \o/

Note: dataclasses by default cannot provide eq methods, as they then
become unhashable. In the future we may wish to opt into declaring them
frozen, instead/additionally.
2022-01-10 18:36:57 -05:00
Dylan Baker 4d7031437c pylint: turn on superflous-parens
We have a lot of these. Some of them are harmless, if unidiomatic, such
as `if (condition)`, others are potentially dangerous `assert(...)`, as
`assert(condtion)` works as expected, but `assert(condition, message)`
will result in an assertion that never triggers, as what you're actually
asserting is `bool(tuple[2])`, which will always be true.
2021-08-31 16:28:54 -04:00
Dylan Baker 278942a447 pylint: enable consider-iterating-dictionary
This didn't actually catch what it's supposed to, which is cases of:
```python
for x in dict.keys():
    y = dict[x]
```
But it did catch one unnecessary use of keys(), and one case where we
were doing something in an inefficient way. I've rewritten:
```python
if name.value in [x.value for x in self.kwargs.keys() if isinstance(x, IdNode)]:
```
as
``python
if any((isinstance(x, IdNode) and name.value == x.value) for x in self.kwargs):
```
Which avoids doing two iterations, one to build the list, and a
second to do a search for name.value in said list, which does a single
short circuiting walk, as any returns as soon as one check returns True.
2021-08-31 16:28:54 -04:00
Laurin-Luis Lehning 130adef778 Add support for basic format strings 2021-03-10 08:55:22 -05:00