-
Notifications
You must be signed in to change notification settings - Fork 0
Rules
CDTk’s grammar rules are the foundation for parsing source text into logical, tree-structured representations (ASTs). Rules dictate how tokens and sub-rules assemble into language constructs like expressions, assignments, or function calls. CDTk v8 introduces a robust, field-based identity system for rules and processes, which improves reliability and expressiveness in parsing.
A grammar rule in CDTk does two things:
- Describes a Pattern: Specifies the order and types of tokens or sub-rules it matches using a declarative syntax.
- Constructs Output: Maps matched elements to named fields—usually for AST node construction.
Example:
public Rule Assignment = new Rule("variable:@Identifier '=' value:@Number")
.Returns("variable", "value");-
variable:@Identifierbinds a token (from the lexer) to the fieldvariable. -
'='matches a literal character. -
value:@Numbermaps another token tovalue.
Rules are defined as public Rule fields inside a class deriving from RuleSet. The field name is the unique rule identity.
Typical Usage:
class MyRules : RuleSet
{
public Rule Expression = new Rule("left:@Term '+' right:@Term")
.Returns("left", "right");
public Rule Assignment = new Rule("variable:@Identifier '=' value:@Number")
.Returns("variable", "value");
}-
Identity: The field name (
Expression,Assignment) is the rule’s ID. - Declarative Patterns: Patterns use tokens and other rules.
- Returns Clause: Maps rule outputs to fields used for AST construction.
| Syntax Element | Meaning |
|---|---|
@TokenName |
Matches a token defined in your TokenSet
|
Field:RuleName |
Binds nested rule output to a field name |
'literal' |
Matches a literal string or operator |
| ` | ` |
* |
Zero or more (repetition) |
+ |
One or more (repetition) |
? |
Optional (zero or one) |
Example Patterns:
-
Binary Expression:
public Rule Binary = new Rule("left:@Number op:'+' right:@Number") .Returns("left", "op", "right");
-
Alternation:
public Rule Factor = new Rule("@Number | '(' expr:Expression ')'") .Returns("expr");
-
Repetition:
public Rule Args = new Rule("args:@Identifier+").Returns("args");
-
Optional:
public Rule SemiExpr = new Rule("expr:Expression ';'?").Returns("expr");
CDTk v8 includes validation utilities to check grammar and rule correctness before compilation.
How to Validate:
var rules = new MyRules();
var diagnostics = rules.Validate();
if (diagnostics.HasErrors)
{
foreach (var diagnostic in diagnostics.Items)
Console.WriteLine($"{diagnostic.Level}: {diagnostic.Message}");
}Validation catches:
- Undefined tokens/rules
- Ambiguous or recursive structures
- Pattern sequence errors
Rule outputs are mapped to object fields for AST node creation:
public Rule AssignmentNode = new Rule("variable:@Identifier '=' value:@Number")
.Returns("variable", "value");- Maps matched
@Identifierand@Numberto the resulting node’svariableandvaluefields.
Nested rules build hierarchical ASTs:
public Rule Expression = new Rule("left:@Term '+' right:@Term")
.Returns("left", "right");Advanced users may attach semantic models for deeper analysis or custom logic:
public MyModel Model => new MyModel(__AllRules, __Ast);- Models process parsed AST nodes and enhance grammar capabilities.
- Models require explicit construction and type-safe logic—no magic injection.
Rules are matched in field definition order. Place the most specific or higher-priority rules first:
public Rule Keyword = new Rule("keyword:'if'");
public Rule Identifier = new Rule("id:@Identifier");- Descriptive Field Names: Use meaningful rule and field names for clarity.
-
Validate Often: Use
.Validate()during development for correctness. - Correct Ordering: Place specific rules before general ones.
- Extend with Models: Use semantic models to add custom AST transforms.
- Check token definitions (make sure TokenSet matches usage).
- Watch for conflicting alternation/repetition.
- Use
.Validate()for early error detection.
Explore:
For further details, refer to your source documentation or core CDTk parser files.