📝Grammar
This page will cover (for the most part) how to add your own grammars.
Warning: Creating new grammar for Cosmo is a process.
Check the current syntax types to make sure any tokens you want to include in your grammar are already being lexed.
If a token you want to include in your grammar is not in the list of syntax types, you will need to create your own syntax type and lex it. Lexing a syntax is straightforward, and you should be able to figure it out without a tutorial.
Figure out what your expression/statement should be represented as. Are you adding an operator? Then you can just match your syntax in a binary operator parsing method (like
parse_additive
, includingparse_var_declaration
as well asparse_assignment
) or if it's unary, theparse_unary
method. Otherwise:Create an AST node and a parser method. Naming is straightforward, your AST node should be named what it represents. The parsing method should be named
parse_(node_name)
. For example if you added anif
statement, the node's file would be calledif.cr
, the node's class would be calledIf
, and the parsing method would be calledparse_if_statement
. Don't forget to include thevisit_nodename_expr
/visit_nodename_stmt
method inExpression
/Statement
'sVisitor
abstract class. In the case of theif
statement, it would be calledvisit_if_stmt
.
Implement the
visit_nodename_expr
/visit_nodename_stmt
method into the interpreter, and perform all necessary actions for your grammar.Implement the
visit_nodename_expr
/visit_nodename_stmt
into the resolver, and resolve any nodes associated with your new node.
Last updated