Commit Graph
25 Commits
Author SHA1 Message Date
Jon Ross-Perkins 3cab211489 Change handling of invalid patterns to produce a valid parse tree. (#2768)
At present, `var *;` is a crash because it has errors that weren't being properly stored/handled. This changes the handling to give more to track invalid parses in patterns.
2023-04-15 03:22:31 -07:00
Jon Ross-Perkins a905cdea30 Improve parsing of invalid expressions. (#2726)
This addresses crashes for infix operator expressions, but the approach should more generally yield balanced parsed trees.
2023-03-31 20:51:59 -07:00
Jon Ross-Perkins e89fb83e7e Rename self/Self enums to SelfValueIdentifier/SelfTypeIdentifier (#2701)
Using `Identifier` because `self` and `Self` will resolve to different things. While `i32` uses `Literal`, it'd always resolve to the same type.
2023-03-22 10:07:09 -07:00
Jon Ross-Perkins d0105e119f Parse self in expressions. (#2697)
`self` wasn't being handled in expression logic, only parameter logic. This is needed for `self.x`.

Also renames SelfDeducedParameter to SelfIdentifier because it's parsed fairly consistently with Identifier; this better represents the situations where `self` will need to be handled, it's where `Identifier` is allowed (although `Identifier` could also refer to a type... but I still think `SelfDeducedParameter` is a more difficult to understand name).
2023-03-20 14:13:29 -07:00
Jon Ross-PerkinsandRichard Smith d7ab71ba7d Parsing for generic and template parameters. (#2685)
Also cleans up some comments about related parse nodes. Currently basic and not heavily validated.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2023-03-16 10:50:35 -07:00
Jon Ross-Perkins e613ad5323 Reorganize interface parsing so that it's shared with class and constraint (#2666)
We could similarly add others -- this is intended to make it easy to add more that parse essentially the same.

The functionality expected is that types will use GetDeclarationContext in order to error on certain functionality in the declaration scope loop. e.g., with how constraints and interfaces currently don't allow definitions.

I've only moved out `package` because it's only valid on the top line. It might still be good to parse it later, but with slightly different logic because it would always be an error, and the declaration context isn't quite the right framing for that.

Also unifies some errors with `fn`.
2023-03-13 17:24:01 -07:00
Jon Ross-Perkins 4083d7f5b9 Reorganize interface parsing to be more consistent with other declarations. (#2646)
The comments in parse_node_kind.def capture the change being made here.

Before:

```
//   _external_: DeclaredName
//     InterfaceBodyStart
//     _external_: statements
//   InterfaceBodyEnd
// InterfaceDefinition
```

After:

```
//     InterfaceIntroducer
//     DeclaredName
//   InterfaceDefinitionStart
//   _external_: declarations
// InterfaceDefinition
```

Really I just want to treat introduced things consistently. `var` defines my philosophy here: it doesn't always have a `DeclaredName`, so the `VarIntroducer` _must_ be the bounding node. By being consistent with that, I believe that overall the structure becomes easier to understand (that is, there are fewer inconsistencies to understand).

This also adds InterfaceDeclaration, since I think it can be predicted we'll have that, and it's helpful for making recover consistent with HandleDeclarationError.

Similarly, I'm also trying to standardize the loop processing a little with HandleDeclarationLoop. In the current approach, InterfaceDefinitionFinish isn't a necessary state, so I'm removing it.
2023-03-06 16:31:01 -08:00
Jon Ross-Perkins 6bb0a5b55e Do a cleanup of the x-macro enum comments. (#2650)
Trying to make some boilerplate-y comments more boilerplate, and also explain what's in the files a little more.
2023-03-06 15:13:41 -08:00
Jon Ross-Perkins 11deb14dc6 Handle var init-with-self situations. (#2488)
The problem I'm trying to solve is: `var x: i32 = x;`. This change makes it so that name lookup fails, by removing `x` from name lookup between the `=` and `;`.

`var x: i32` still adds to name lookup to handle future situations like `var (x: i32, x: i32);` which is still a redefinition of `x`; if we don't add `x` to name lookup, it gets harder to catch that example.

The VariableDeclaration/VariableInitializer refactor in ParseTree supports this by given a bracketing-like structure for semantics to cue that it's entering an initialization expression. With this, VariableInitializer can remove the name lookup and queue it to be restored. VariableDeclaration doesn't need to change too much since it's still bracketed by VariableIntroducer, and so we just traverse slightly differently.

Note this also incidentally changes a little about NameReference, that it's returning the storage consistently instead of the name. You can see this e.g. in global_lookup.carbon, `Assign(node8, node4): node2;` using node4 (VarStorage) instead of Node5 (BindName). Really either _could_ work, since from a BindName we can get to the VarStorage, and that may be reason to switch later if we find it preferable to have the BindName for whatever reason.

But the *actual* value in NameLookup is a BindName so that errors can associate with the _name_ instead of the "storage" parse node, which is currently the `:`. This is mainly for fail_duplicate_decl.carbon, which has a "Previous definition" note that points at the storage's parse node.
2022-12-28 12:55:40 -08:00
Kareem Ergawy c74e39dbb3 [parser] More support for interfaces: methods and self deduced param. (#2427)
Summary:

Extends the current support for parsing `interface`s. In particular, adds support for parsing functions and `me` params.
2022-12-16 11:23:02 -08:00
Jon Ross-Perkins 6bfd202f4e Finish bracketing of parse nodes. (#2430)
- Finishes remaining "todo" parse nodes.
- Improving error recovery for invalid designators and structs, so that the parse tree still looks similar to a valid parse tree.
- Call expressions now have the thing being called as a child (of the start) instead of a sibling.
- Use of Start is replacing use of End in several parse nodes, like structs and call expressions.
- Adjusting documentation of parse node structures in an attempt to make it more consistent and understandable.
- The current state for interfaces and if/else is mostly being documented, not altered.
2022-11-30 09:14:07 -08:00
Jon Ross-PerkinsandChandler Carruth 84deb62aef Work on ParseTree structure to use more bracketed structures. (#2416)
This works on multiple statements to make them better for the bracketing model. Stub nodes are added in more cases of invalid syntax, simply so that the semantics has reliably structured input. Comments in parse_node_kind.def now try to show the expected parse tree structure in postorder form.

This labels If, While, and For a little differently in parse nodes so that at the start of the postorder traversal, it'll already be available to semantics which structure is being processed. I need to do a little more with If in particular, but this felt like a reasonable stopping point.

While this makes significant parser changes, the changes to parser_state.def are minimal, mostly naming-related. The actual flow isn't substantively changed, just a couple minor names and the new As(If|While) state which allows distinguishing IfCondition and WhileCondition.

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2022-11-28 15:02:59 -08:00
Kareem Ergawyandergawy a508390423 [parser] Start re-implementing interfaces using the stack parser. (#2412)
First change towards re-implementing interfaces using the new parser. I kept it small to make sure we are on the same page regarding stack states and how the parse tree should look like.

Co-authored-by: ergawy <kareem.ergawy@guardsquare.com>
2022-11-28 12:58:41 -08:00
Jon Ross-PerkinsandChandler Carruth 5cd05efc62 Introduce verification checks on subtree_size (#2414)
This switches the Verify method to walk postorder so that we can see how much subtree_size is really used, and shift towards removing it. It also starts calling Verify.

Also, I think I'd lost the reserve/size check during Parser refactoring, so I'm putting that back in as part of Verify.

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2022-11-18 16:45:59 -08:00
Jon Ross-Perkins fbed1398fe Change ParameterList to use a Start that brackets params (#2403)
The code change is small here. I'm breaking this out because it has a lot of test churn, and semantics doesn't have significant logic around it yet.
2022-11-17 08:57:22 -08:00
Jon Ross-Perkins 5da32bd560 Rename to StructLiteralOrStructTypeLiteralStart (#2404)
As requested
2022-11-17 08:56:48 -08:00
Jon Ross-Perkins c165ab6c2b Change StructLiteral and StructTypeLiteral parsing to use an ambiguous start. (#2396)
This is necessary in order to use a bracketing approach; parsing doesn't know the contained format until it parses the first element, which we don't want to do look-ahead for. I think the bracketing is higher value than knowing the format before adding the node.
2022-11-15 22:44:57 -08:00
Jon Ross-Perkins b914f46ec5 Change keyword statements to bracket arguments. (#2394)
This changes `return`, `break`, and `continue` to treat the keyword as the "start" and semicolon as the "parent", essentially bracketing the keyword.

Pragmatically this is focusing on making `return` work with only one ParseNodeKind: because `return` and `;` now bracket the expression, we can tightly determine whether the `return` has arguments without looking at subtree size. However, it's possible that `break` and `continue` may in the future take some kind of label as an argument, so the consistency seems beneficial there too.

Note this eliminates the StatementEnd ParseNodeKind, as it's obsolete with this change.
2022-11-15 22:42:54 -08:00
Jon Ross-Perkins 7b48ac7258 Start reorienting the ParseTree towards a more efficient SemanticsIR production. (#2275)
In summary - some of the changes here are focused on producing the same test results for SemanticsIR, but I think the next step will be to change the SemanticsIR structure to reduce how much is added to the traversal stack.

Switching semantics to a postorder traversal is intended to be more efficient. The traversal stack is to eliminate risk of recursion limits within the semantic analysis that could come from layered code structures. However, we need to start considering the implications for type-checking and what the ParseTree looks like, as well as copying of data here.

As we start thinking about type-checking in SemanticsIR, it's helpful for a function to know its own signature in order to perform lookup recursive calls. The challenge in the post-order walk without this change is it doesn't know it's in a function definition (or similar) until it reaches the FunctionDeclaration; this restructures so that either:

1. For a declaration, the signature is a child of FunctionDeclaration(";")
2. For a definition, the signature is a child of FunctionDefinitionStart("{") which pairs with FunctionDefinition("}"), replacing CodeBlock.

This similarly reorients CodeBlock to be CodeBlockStart("{") as the first child of CodeBlock("}"). I'm not doing that with ParameterList here just because it affects a bit more, and felt like it could be delayed.

Overall, my goal is making the postorder traversal more intuitive along scope boundaries. I think we may also not need subtree_size, so I'm avoiding use of that now.

Currently the SemanticsIRFactory implementation is less clean than I might like (there are a couple comments to this point), but I was starting to feel like a more complete rewrite would be appropriate rather than trying to clean it up further: in particular, I think the node structures are off, but changing them is significant and also changes test output; in turn it may also warrant more substantial ParseTree changes. If you prefer from a reviewer POV, I can do a more complete rewrite.
2022-10-18 16:15:57 -07:00
8d5d48a4a7 [toolchain][parser] Parse for statement. (#2180)
Summary:

Add support for parsing `for` loops.

Co-authored-by: ergawy <kareem.ergawy@guardsquare.com>
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-10-03 08:36:19 -07:00
Kareem Ergawyandergawy d44cde3c15 [Toolchain][Parser] Add support for package directive. (#2144)
Summary:

Adds parsing support for the `package` directive as specified by the
`Code and name organization` design doc.

Co-authored-by: ergawy <kareem.ergawy@guardsquare.com>
2022-09-07 10:54:27 +03:00
Richard Smith fe28dd9a33 [toolchain] Parsing support for struct literals, following #561 / #653. (#699)
This supports both struct type literals, `{.x: i32, .y: i32}`, and struct
value literals, `{.x = 3, .y = 4}`. The degenerate case of `{}` is
treated as a struct value literal, with the expectation that an empty
struct value has the same type/value duality as an empty tuple value.
2021-08-27 18:18:33 -07:00
Richard Smith b1993a6cd0 [toolchain] Parsing support for tuple literal syntax. (#694)
For now, we permit non-empty tuples to have trailing commas, and require
a trailing comma if there's exactly one list element. The exact rule
here has not yet been decided.
2021-08-02 16:18:18 -07:00
Richard Smith 61b30b1243 [toolchain] Parse variables and parameters as 'name: Type'. (#574)
Factor out code for pattern parsing, that only recognizes this form for
now, and uniformly form a 'PatternBinding' parse node for this, for
both variables and patterns.
2021-06-14 14:28:45 -07:00
Chandler Carruth 8f8ab23a77 Move the toolchain into a top-level directory. (#567)
This should clean up our top level directory and the build patterns.

No non-mechanical edits here. Just injecting `toolchain/` and
`TOOLCHAIN_` and then running formatting tools.
2021-06-08 03:01:37 -07:00