| 1 | # Rules |
| 2 | |
| 3 | This document contains information related to the rules of the parser for Ruby source code. |
| 4 | |
| 5 | As an example, in the documentation of many of the fields of nodes, it's mentioned that a field follows the lexing rules for `identifier` or `constant`. This document describes what those rules are. |
| 6 | |
| 7 | ## Constants |
| 8 | |
| 9 | Constants in Ruby begin with an upper-case letter. This is followed by any number of underscores, alphanumeric, or non-ASCII characters. The definition of "alphanumeric" and "upper-case letter" are encoding-dependent. |
| 10 | |
| 11 | ## Non-void expression |
| 12 | |
| 13 | Most expressions in CRuby are non-void. This means the expression they represent resolves to a value. For example, `1 + 2` is a non-void expression, because it resolves to a method call. Even things like `class Foo; end` is a non-void expression, because it returns the last evaluated expression in the body of the class (or `nil`). |
| 14 | |
| 15 | Certain nodes, however, are void expressions, and cannot be combined to form larger expressions. |
| 16 | * `BEGIN {}`, `END {}`, `alias foo bar`, and `undef foo` can only be at a statement position. |
| 17 | * The "jumps": `return`, `break`, `next`, `redo`, `retry` are void expressions. |
| 18 | * `value => pattern` is also considered a void expression. |
| 19 | |
| 20 | ## Identifiers |
| 21 | |
| 22 | Identifiers in Ruby begin with an underscore or lower-case letter. This is followed by any number of underscores, alphanumeric, or non-ASCII characters. The definition of "alphanumeric" and "lower-case letter" are encoding-dependent. |