Skip to content

Commit 3074c0a

Browse files
WillLillisamaanq
authored andcommitted
docs: add guidance about node naming conventions
1 parent 46f8d12 commit 3074c0a

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

docs/src/creating-parsers/3-writing-the-grammar.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,21 @@ between `IdentifierReference` and `Expression`. If we were to create a concrete
179179
according to the language spec, it would have twenty levels of nesting, and it would contain nodes with names like `BitwiseXORExpression`,
180180
which are unrelated to the actual code.
181181

182+
## Standard Rule Names
183+
184+
Tree-sitter places no restrictions on how to name the rules of your grammar. It can be helpful, however, to follow certain conventions
185+
used by many other established grammars in the ecosystem. Some of these well-established patterns are listed below:
186+
187+
- `source_file`: Represents an entire source file, this rule is commonly used as the root node for a grammar,
188+
- `expression`/`statement`: Used to represent statements and expressions for a given language. Commonly defined as a choice between several
189+
more specific sub-expression/sub-statement rules.
190+
- `block`: Used as the parent node for block scopes, with its children representing the block's contents.
191+
- `type`: Represents the types of a language such as `int`, `char`, and `void`.
192+
- `identifier`: Used for constructs like variable names, function arguments, and object fields; this rule is commonly used as the `word`
193+
token in grammars.
194+
- `string`: Used to represent `"string literals"`.
195+
- `comment`: Used to represent comments, this rule is commonly used as an `extra`.
196+
182197
## Using Precedence
183198

184199
To produce a readable syntax tree, we'd like to model JavaScript expressions using a much flatter structure like this:

0 commit comments

Comments
 (0)