summaryrefslogtreecommitdiff
path: root/mesonbuild/mparser.py
AgeCommit message (Collapse)Author
2023-02-01treewide: add future annotations importEli Schwartz
2023-01-17mparser: Use a literal for the ComparisonNodeDylan Baker
And fix a bug where `not in` is in the wrong order.
2023-01-03mparser: Don't create an exception to pass to mlog.warningDylan Baker
Just call `mlog.code_line` directly, since the exception is never raised.
2023-01-03mlog: move code for printing code with a caret to the mlog moduleDylan Baker
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-03mparser: don't pass a SimpleNamespace where a BaseNode is expectedDylan Baker
mypy spotted this as well. And it turns out that we're not setting the column either for the warning, so improvements!
2022-12-11typing: fix some broken Sequence annotationsEli Schwartz
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-09-19pylint: enable consider-using-inDylan Baker
2022-05-01Add support for multiline f-stringsPeter Lesslie
+ 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-02-16fix malformed warning to print the way it was meant to printEli Schwartz
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-16fix a couple misuses of textwrap.dedentEli Schwartz
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-01-10first pass at migrating to dataclassesEli Schwartz
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.
2021-08-31pylint: turn on superflous-parensDylan Baker
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-31pylint: enable consider-iterating-dictionaryDylan Baker
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-03-10Add support for basic format stringsLaurin-Luis Lehning
2021-03-04mass rewrite of string formatting to use f-strings everywhereEli Schwartz
performed by running "pyupgrade --py36-plus" and committing the results
2020-09-08typing: fix code reviewDaniel Mensinger
2020-09-08typing: fully annotate mparser.pyDaniel Mensinger
2020-05-23ast: Add AST JSON printerDaniel Mensinger
2020-04-04mparser: fix precedence of arithmetic operatorsAndrei Alexeyev
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-03-02types: Remove redundant __init__() -> None annotationDaniel Mensinger
2020-03-02types: Use import typing as TDaniel Mensinger
2020-03-02review: Initial fixupDaniel Mensinger
2020-03-02types: Annotate the AST visitorsDaniel Mensinger
2020-03-02types: Annotate mparser.pyDaniel Mensinger
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-02-28Rename 'subdir' -> 'filename' in location objectsJon Turney
2019-12-05lgtm: Fix redundant codeDaniel Mensinger
2019-12-04dict: Fully evaluate keysXavier Claessens
The only restriction is keys must be string after evaluation. This fix various inconsistencies.
2019-12-03mparser.py: actually check the type of key variable, not its valueMarc Herbert
Fixes PR #6166 and more specifically commit 4e460f04f3b2 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-11-12Fixed issue that the key's value type wasn't checked correctly.fchin
Added two new failing tests.
2019-11-12Adding dictionary entry using string variable as key.franczc
2019-04-29Fix flake8-bugbear warningsDaniel Mensinger
2019-04-28consistent invalid escape sequence behaviourJon Turney
* 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-03-03rewriter: Use mparser to detect the end of some nodesDaniel Mensinger
2019-03-02Fix ternary in thing (#5007)Dylan Baker
* 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-02-16Fixed flake8Daniel Mensinger
2019-02-16Can now find the assignment node of a valueDaniel Mensinger
2019-01-29pep8 py37Michael Hirsch, Ph.D
2019-01-22Fixed line and column numbers for dict and array nodesDaniel Mensinger
2019-01-22Basic AST visitor patternDaniel Mensinger
2019-01-05parser: Fix line continuation outside of (), [] or {}Xavier Claessens
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
2018-10-04Interpreter: Add 'continue' and 'break' keywordsXavier Claessens
Closes: #3601
2018-10-04Interpreter: Add "in" and "not in" operatorsXavier Claessens
Closes: #3600
2018-07-27Warn for future keyword (#3908)Xavier Claessens
2018-05-23Add support for octal and binary int literals.Filipe Brandenburger
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-21dict: fix CI issuesMathieu Duponchelle
2018-05-20dict: address review commentsMathieu Duponchelle
2018-05-20Add new built-in type, dictMathieu Duponchelle
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-04-21Remove escaping for triple-quoted stringsNiklas Claesson
Fixes #3429
2018-04-17Exit meson with an error if an invalid escape sequence is found in aNiklas Claesson
string
2018-04-17Complete python escape sequences aware stringsTim 'mithro' Ansell
Fixes #3169