Commit graph

328 commits

Author SHA1 Message Date
Veikka Tuominen
5d22204d2d parser: add helpful error for C style container declarations
```zig
// a.zig
struct Foo {
    a: u32,
};
```

before:
```
a.zig:1:1: error: expected test, comptime, var decl, or container field, found 'struct'
struct Foo {
^
```
after:
```
a.zig:1:8: error: 'struct Foo' is invalid
struct Foo {
       ^
a.zig:1:8: note: to declare a container do 'const Foo = struct'
struct Foo {
       ^
```
2022-07-12 12:50:59 +03:00
Philipp Lühmann
bb2929ba08
zig fmt: fix idempotency with newlines surrounding doc comment
Fixes: https://github.com/ziglang/zig/issues/11802
2022-06-28 21:38:28 +02:00
Damien Firmenich
5fafcc2b62
zig fmt: remove trailing whitespace on doc comments
Fixes #11353

The renderer treats comments and doc comments differently since doc
comments are parsed into the Ast. This commit adds a check after getting
the text for the doc comment and trims whitespace at the end before
rendering.

The `a = 0,` in the test is here to avoid a ParseError while parsing the
test.
2022-04-05 18:08:33 +03:00
Ryan Liptak
6d04ab6d5b Add std.testing.checkAllAllocationFailures
Adds a function that allows checking for memory leaks (and other problems) by taking advantage of the FailingAllocator and inducing failure at every allocation point within the provided `test_fn` (based on the strategy employed in the Zig parser tests, which can now use this function).
2022-04-04 15:32:43 +03:00
Daniel Hooper
911c839e97
add error when binary ops don't have matching whitespace on both sides
This change also moves the warning about "&&" from the AstGen into the parser so that the "&&" warning can supersede the whitespace warning.
2022-03-20 12:55:04 +02:00
Curtis Tate Wilkinson
3bb4c0c789
zig fmt: Resolve #11131 loss of comment on switch cases
Correct switch cases dropping comments in certain situations by
checking for the presence of the comment before collapsing to one line.
2022-03-14 23:10:59 +01:00
Veikka Tuominen
6b65590715 parser: add notes to decl_between_fields error 2022-02-17 22:16:26 +02:00
Veikka Tuominen
92f2767814 parser: add error for missing colon before continue expr
If a '(' is found where the continue expression was expected and it is
on the same line as the previous token issue an error about missing
colon before the continue expression.
2022-02-17 20:51:26 +02:00
Veikka Tuominen
9c36cf92f0 parser: make some errors point to end of previous token
For some errors if the found token is not on the same line as
the previous token, point to the end of the previous token.
This usually results in more helpful errors.
2022-02-17 14:23:35 +02:00
Veikka Tuominen
b85c0d6a47 std: fix tests that were not run due to refAllDecls regression 2022-02-14 15:23:45 +02:00
Veikka Tuominen
8937f18a6f std: force refAllDecls to actually resolve all decls
Only about half of the tests in std were actually being run (918 vs 2144).
2022-02-13 14:36:55 +02:00
ominitay
8ca9452a82 Remove deprecation warnings and enable test
Also fixes previously broken code uncovered by this
2022-01-29 15:49:00 +02:00
John Schmidt
e51a44b342 fmt: handle doc comments on struct members
Closes https://github.com/ziglang/zig/issues/10443.
2022-01-29 12:19:31 +01:00
Isaac Freund
9f9f215305
stage1, stage2: rename c_void to anyopaque (#10316)
zig fmt now replaces c_void with anyopaque to make updating
code easy.
2021-12-19 00:24:45 -05:00
Matthew Borkowski
c98b020ce2 parse.zig: make chained comparison operators a parse error 2021-12-02 11:59:29 -08:00
Lee Cannon
1093b09a98
allocgate: renamed getAllocator function to allocator 2021-11-30 23:32:47 +00:00
Lee Cannon
85de022c56
allocgate: std Allocator interface refactor 2021-11-30 23:32:47 +00:00
tjohnes
a130eac785 zig fmt: fix formatting for single-line containers with comments
* Fixes #8810.
* Prevent a single-line container declaration if it contains a comment
  or multiline string.
* If a container declaration cannot be single-line, ensure container
  fields are rendered with a trailing comma.
* If `Space.comma` is passed to `renderExpressionComma` or
  `renderTokenComma`, and there already exists a comma in the source,
  then render one comma instead of two.
2021-11-24 18:45:09 -08:00
Isaac Freund
f7b090d707 std.log: simplify to 4 distinct log levels
Over the last year of using std.log in practice, it has become clear to
me that having the current 8 distinct log levels does more harm than
good. It is too subjective which level a given message should have which
makes filtering based on log level weaker as not all messages will have
been assigned the log level one might expect.

Instead, more granular filtering should be achieved by leveraging the
logging scope feature. Filtering based on a combination of scope and log
level should be sufficiently powerful for all use-cases.

Note that the self hosted compiler has already limited itself to 4
distinct log levels for many months and implemented granular filtering
based on both log scope and level. This has worked very well in practice
while working on the self hosted compiler.
2021-10-24 15:04:29 -04:00
Matthew Borkowski
2ed9288246 parse.zig: better c pointer prefix parsing, don't index out of bounds on eof 2021-09-30 15:36:57 +02:00
Andrew Kelley
6cb7906394 add missing zig fmt test for saturating arithmetic 2021-09-28 17:04:19 -07:00
Robin Voetter
8672f2696f Address Spaces: zig fmt + tests 2021-09-20 02:29:04 +02:00
Philipp Lühmann
d1908c9f66
zig fmt: Keep callconv(.Inline) on function pointer types
Co-authored-by: Philipp Lühmann <mail@philipp.lu>
2021-09-14 11:36:26 +02:00
Andrew Kelley
3940a1be18 rename std.zig.ast to std.zig.Ast; use top-level fields 2021-09-01 17:54:07 -07:00
Andrew Kelley
ca21cad2bf move syntax tests out of behavior tests
parser_test.zig is where the syntax tests go
2021-09-01 17:54:06 -07:00
jdmichaud
49c9975484
zig fmt: respect trailing commas in inline assembly 2021-08-29 11:57:32 +02:00
Andrew Kelley
d29871977f remove redundant license headers from zig standard library
We already have a LICENSE file that covers the Zig Standard Library. We
no longer need to remind everyone that the license is MIT in every single
file.

Previously this was introduced to clarify the situation for a fork of
Zig that made Zig's LICENSE file harder to find, and replaced it with
their own license that required annual payments to their company.
However that fork now appears to be dead. So there is no need to
reinforce the copyright notice in every single file.
2021-08-24 12:25:09 -07:00
g-w1
ee173d5127
parser: require block in suspend expression 2021-07-15 10:55:22 +03:00
Andrew Kelley
3f680abbe2 stage2: tokenizer: require null terminated source
By requiring the source file to be null-terminated, we avoid extra
branching while simplifying the logic at the same time.

Running ast-check on a large zig source file (udivmodti4_test.zig),
master branch compared to this commit:
 * 4% faster wall clock
 * 7% fewer cache misses
 * 1% fewer branches
2021-07-02 13:27:35 -07:00
Jacob G-W
641ecc260f std, src, doc, test: remove unused variables 2021-06-21 17:03:03 -07:00
Dmitry Matveyev
00982f75e9
stage2: Remove special double ampersand parsing case (#9114)
* Remove parser error on double ampersand

* Add failing test for double ampersand case

* Add error when encountering double ampersand in AstGen

"Bit and" operator should not make sense when one of its operands
is an address.

* Check that 2 ampersands are adjacent to each other in source string

* Remove cases of unused variables in tests
2021-06-20 21:04:14 +03:00
Matthew Borkowski
483933d88d parse.zig: make parseForTypeExpr accept only a TypeExpr as body 2021-06-13 16:37:53 +03:00
Veikka Tuominen
0d022eb1d6 zig fmt: rewrite byteOffsetOf to offsetOf 2021-06-12 19:16:01 +03:00
Matthew Borkowski
ff0a15bb7a parse.zig: make parseParamDeclList check for nonfinal varargs 2021-06-08 20:50:55 +03:00
Jacob G-W
5d94e754f4 fmt: fix #8974
also add a regression test
2021-06-04 10:49:05 +03:00
Isaac Freund
608bc1cbd5
stage2: disallow 1.e9 and 0x1.p9 as float literals
Instead require `1e9` and `0x1p9`, disallowing the trailing dot.

This change to the grammar is consistent with forbidding `1.` and `0x1.`
as float literals and ensures there is only one way to do things here.
2021-05-31 19:51:11 +00:00
Isaac Freund
ec10595b65 stage2: disallow trailing dot on float literals
This disallows e.g. `1.` or `0x1.` as a float literal, which is
consistent with the grammar.
2021-05-27 21:00:44 -04:00
Isaac Freund
4a582734fd
zig fmt: replace callconv(.Inline) with the inline keyword 2021-05-20 17:13:47 +02:00
aiz
5414bd48ed
std.math.Complex: Change new() to init() 2021-05-17 21:57:51 +02:00
Andrew Kelley
173142cc36
Merge pull request #8611 from shachaf/precedence
parser: Use an operator precedence table
2021-05-13 17:40:03 -04:00
Veikka Tuominen
fd77f2cfed std: update usage of std.testing 2021-05-08 15:15:30 +03:00
LemonBoy
e863204c47 zig fmt: Fix edge case in inline asm parsing
The presence of a trailing comma in the single and only input/output
declaration confused the parser and made zig fmt discard any element
placed after the comma.
2021-05-05 03:16:07 -04:00
Shachaf Ben-Kiki
01aa74a93f parser: Use an operator precedence table
Instead of having one function per precedence level, mirroring the BNF grammar,
this uses the "precedence climbing" algorithm with a table of operator
precedences (and a few special cases that don't fit into the table as it is).

This is a first pass -- it's probably possible to put more of the parser
into this form, e.g. to support prefix/suffix operators with precedence, if
necessary, or just to simplify the code more.

It may also be possible to speed this up by putting more useful information
into the tokens during tokenization, to avoid the extra branch on the token in
operInfo.
2021-04-24 22:40:41 -07:00
LemonBoy
eabf378a56 zig fmt: Automagically fix block-less suspend exprs 2021-04-24 15:37:55 +02:00
LemonBoy
0aede1a8fc stage1: Require a block after suspend
Closes #8603
2021-04-24 10:25:43 +02:00
Isaac Freund
4081e0a475 zig fmt: bypass auto indentation for // zig fmt: on
Currently an indented `// zig fmt: on` will be indented an additional
time on every run of zig fmt.
2021-04-17 20:10:20 -04:00
Isaac Freund
5b9ea5dd1e zig fmt: fix line comment detection
Previously hasComment() would consider a string literal "//" to be a
line comment. Fix this by only searching the bytes between tokens.
2021-04-10 23:10:24 +02:00
Lewis Gaul
ecf555c693
zig fmt: render array init on one line if no trailing comma
Continue to insert a trailing comma if there is a comment or multiline
string literal present.
2021-04-10 16:39:26 +02:00
LemonBoy
5ebcd8ccaf zig fmt: Fix rendering of arrays with single row
rowSize used to return null if all the elements were placed on the same
line as the right brace, making the rendering logic skip the whole set
of elements.

Given the usage of rowSize let's just drop the null and always return
the number of elements.

Fixes #8423
2021-04-09 10:03:45 -07:00
Lewis Gaul
6787f163eb
zig fmt: don't add trailing whitespace on switch case 2021-03-16 09:26:28 +01:00