Commit Graph

73 Commits

Author SHA1 Message Date
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
Eli Schwartz 6a0fabc647
mass rewrite of string formatting to use f-strings everywhere
performed by running "pyupgrade --py36-plus" and committing the results
2021-03-04 17:16:11 -05:00
Daniel Mensinger e681235e5f
typing: fix code review 2020-09-08 20:15:58 +02:00
Daniel Mensinger fb9738b8c7
typing: fully annotate mparser.py 2020-09-08 20:15:56 +02:00
Daniel Mensinger 96eeef62ea
ast: Add AST JSON printer 2020-05-23 18:13:16 +02:00
Andrei Alexeyev 2cfbb36a84 mparser: fix precedence of arithmetic operators
The arithmetic operators are now split into two groups:
    * The add/sub group: +, -
    * The mul/div group: *, /, %

All operators within the same group are left-associative and have equal
precedence. The mul/div group has a higher precedence than the add/sub
group, as one would expect.

Previously every operator had a different precedence and was
right-associative, which resulted in surprising behavior.

This is a potentially breaking change for projects that relied on the
old incorrect behavior.

Fixes #6870
2020-04-04 13:31:07 +03:00
Daniel Mensinger d67888bf9b
types: Remove redundant __init__() -> None annotation 2020-03-02 10:52:59 +01:00
Daniel Mensinger 0302a697b8
types: Use import typing as T 2020-03-02 10:51:07 +01:00
Daniel Mensinger ab988198c7
review: Initial fixup 2020-03-02 10:47:20 +01:00
Daniel Mensinger a75255bc4c
types: Annotate the AST visitors 2020-03-02 10:34:55 +01:00
Daniel Mensinger c14aea2812
types: Annotate mparser.py
This also fixes that the keys in ArgumentNode.kwargs are
all of the type BaseNode now. Before this commit, it was
possible that both strings and Nodes where used as keys.
2020-03-02 10:34:55 +01:00
Jon Turney c8f8d58273
Rename 'subdir' -> 'filename' in location objects 2020-02-28 11:54:08 +00:00
Daniel Mensinger e4a0ee205d lgtm: Fix redundant code 2019-12-05 00:22:10 +02:00
Xavier Claessens 9b1a857473 dict: Fully evaluate keys
The only restriction is keys must be string after evaluation. This fix
various inconsistencies.
2019-12-04 16:45:56 -05:00
Marc Herbert 80bfe593fc mparser.py: actually check the type of key variable, not its value
Fixes PR #6166 and more specifically commit 4e460f04f3 that tried to
make sure the type of a key variable is a string but checked the type of
the value instead. Extends test common/228's limited coverage,
its only test case had (surprise) a string value. Also avoid reserved
python keyword 'dict' and potentially confusing string 'key'.

Implements #5231 for real.
2019-12-03 22:10:07 -05:00
fchin 4e460f04f3 Fixed issue that the key's value type wasn't checked correctly.
Added two new failing tests.
2019-11-12 09:21:10 -05:00
franczc adb4e071e6 Adding dictionary entry using string variable as key. 2019-11-12 09:21:10 -05:00
Daniel Mensinger 2b5831f94f
Fix flake8-bugbear warnings 2019-04-29 12:22:50 +02:00
Jon Turney ccc4ce28cc consistent invalid escape sequence behaviour
* docs: document unrecognized escape sequence behaviour [skip ci]

Document that unrecognized escape sequence behaviour is like python, not
C.

* Don't try to decode invalid hex escape sequences

Don't try to decode escape sequences which should contain a sequence of
hex digits, but don't, throwing a python exception.  These will treated
literally instead.

* Extend test case to cover invalid escape sequences
2019-04-28 23:06:36 +03:00
Daniel Mensinger 5a22bb7901 rewriter: Use mparser to detect the end of some nodes 2019-03-03 13:57:25 +02:00
Dylan Baker 841da29d2c Fix ternary in thing (#5007)
* tests: extend ternary test to cover bugs

See issues #5003, #3690, and #2404

* mparser: store subdir in ternary node

Ternaries don't really need subdirs, but they can be passed into
functions that expect the type they're provided to have a
subdir. Provide it to fulful the interface.

Fixes #5003
Fixes #3690
Fixes #2404
2019-03-02 12:57:24 +02:00
Daniel Mensinger 4b7b5a7185
Fixed flake8 2019-02-16 14:17:02 +01:00
Daniel Mensinger 24a2cf02e2
Can now find the assignment node of a value 2019-02-16 14:10:08 +01:00
Michael Hirsch, Ph.D da34bea893 pep8 py37 2019-01-29 22:06:11 +02:00
Daniel Mensinger e089eb7665
Fixed line and column numbers for dict and array nodes 2019-01-22 16:41:25 +01:00
Daniel Mensinger ccad493e85
Basic AST visitor pattern 2019-01-22 16:09:34 +01:00
Xavier Claessens 90c9b868b2 parser: Fix line continuation outside of (), [] or {}
The documentation states: "In other cases you can get multi-line
statements by ending the line with a \." but that seems to never have
worked.

Closes: #4720
2019-01-05 21:45:37 +02:00
Xavier Claessens a816e1c1fa Interpreter: Add 'continue' and 'break' keywords
Closes: #3601
2018-10-04 20:14:37 -04:00
Xavier Claessens fa2e096aa0 Interpreter: Add "in" and "not in" operators
Closes: #3600
2018-10-04 20:14:37 -04:00
Xavier Claessens e7dcf5cf16 Warn for future keyword (#3908) 2018-07-27 14:31:54 +03:00
Filipe Brandenburger 86d2f57e86 Add support for octal and binary int literals.
Simplify support for alternate bases using int(..., base=0) which
auto-detects it using the standard Python syntax for numbers.

Octal numbers are useful to specify permission bits and umasks.

Binary numbers are not super useful... But considering we get them for
free, let's allow them here too.

v2: Tweak the regex so it doesn't accept a decimal number with a leading
zero, which is invalid for int(..., base=0) and would raise a ValueError
if passed around.
2018-05-23 14:07:38 -07:00
Mathieu Duponchelle 10e7566ed8 dict: fix CI issues 2018-05-21 00:19:31 +02:00
Mathieu Duponchelle 195c356f91 dict: address review comments 2018-05-20 22:36:18 +02:00
Mathieu Duponchelle ecb8838082 Add new built-in type, dict
For now dicts are immutable, and do not expose any methods,
they however support "native" syntax such as [] lookup,
and foreach iterating, and can be printed.
2018-05-20 21:19:44 +02:00
Niklas Claesson cb0960a91e Remove escaping for triple-quoted strings
Fixes #3429
2018-04-21 22:57:19 +03:00
Niklas Claesson 348248f0a1 Exit meson with an error if an invalid escape sequence is found in a
string
2018-04-17 09:55:34 +00:00
Tim 'mithro' Ansell 36aab0f4b2 Complete python escape sequences aware strings
Fixes #3169
2018-04-17 09:55:34 +00:00
Jussi Pakkanen d0f2f0ad3b Newlines in single line strings should only be a warning for now. 2018-02-21 19:09:35 +02:00
Caio Marcelo de Oliveira Filho 37b702e9aa Fail if singleline string has multiple lines 2018-02-20 00:20:58 +02:00
Jussi Pakkanen a288b524bc Add support for hex int literals. 2018-02-05 22:33:48 +02:00
Jon Turney 6a1a56ab57 Report warning/error locations in a format IDEs may already know how to parse
Examples:

meson.build:2:0: ERROR: Dependency is both required and not-found
meson.build:4: WARNING: Keyword argument "link_with" defined multiple times.

These are already matched by the default compilation-error-regexp-alist in
emacs.

Also:
Don't start 'red' markup until after the \n before an error
Unabsorb full-stop at end of warning with location from mlog.warning()
Update warning_location test
2018-01-30 06:48:22 +11:00
Jon Turney 2ed875e1b4 Consolidate warning location formatting in mlog.warning()
Also use .format() rather than %
Also use build.environment rather than hardcoding 'meson.build'
2018-01-01 13:20:55 +00:00
Jon Turney 3c75ae9ced Add filename and lineno to duplicate kwargs warning
Fixes #1626

Also fix 'a an error' typo
2017-12-30 19:32:54 +00:00
Joergen Ibsen b9a0589067 Fix escaping of newlines in string literals
Replace '\n' escape sequence before '\\' to allow a literal backslash
to be inserted before the character 'n'.

Fixes #2682
2017-11-27 21:23:32 +02:00
Jussi Pakkanen 506cc57cc8 Do not permit invalid and/or nodes to be declared. Closes #1886. 2017-06-25 21:43:38 +03:00