Lexical Structure
This page describes how the Braid lexer tokenizes source code. The lexer in braid-lang/src/compiler/lexer.c converts raw source text into tokens that the parser consumes.
Source Files
Braid source files use the .br extension. Script files use .bd. Compiled bytecode artifacts use .bx. Source files are UTF-8 text.
Comments
The lexer recognizes three comment styles:
// Line comment — everything until newline is ignored
/* Block comment — everything between /* and */ is ignored */Hash comments (# line comment) are also supported at the lexer level.
Identifiers
Identifiers start with a letter (a-z, A-Z) or underscore (_), followed by zero or more letters, digits, or underscores:
let myVar = 1
let _private = 2
let camelCase123 = 3Keywords
The following identifiers are reserved keywords (from braid.h: BraidTokenType):
fn let if else while return
struct enum match model import extern
impl native true false
diameter pole observe evolveOperators
Arithmetic:
+ Addition / string concatenation - Subtraction / unary negation * Multiplication / Division % ModuloComparison:
== Equal != Not equal < Less than <= Less than or equal > Greater than >= Greater than or equalLogical:
&& AND || OR ! NOT (unary prefix)Other:
= Assignment -> Return type arrow => Match case fat arrow @ Decorator prefix ... Ellipsis (tensor slicing) . Member/field access | Pipe operatorLiterals
Integer literals: Decimal digits only (lexer supports TOK_NUMBER for integers):
let a = 42
let b = 0
let c = 1000000Float literals: Digits with a decimal point:
let pi = 3.14
let e = 2.71828
let zero = 0.0String literals: Enclosed in double or single quotes:
let s1 = "hello"
let s2 = 'world'
let s3 = "string with spaces"Boolean literals:
let t = true
let f = falseNil literal: The null value:
let nothing = nilDelimiters
{ Left brace — starts blocks, struct/diameter bodies} Right brace — ends blocks ( Left paren — function params, grouping ) Right paren [ Left bracket — arrays, tensor literals, indexing ] Right bracket ; Semicolon — terminates statements , Comma — separates elements : Colon — type annotations, struct field syntaxOperator Precedence (high to low)
()parentheses-unary negation,!logical NOT*/%multiplicative+-additive<<=>>===!=comparison&&logical AND||logical OR
Example: Full Tokenization
// Source:
let x: int = 10 + 20;
// Tokens:
TOK_LET "let"
TOK_IDENTIFIER "x"
TOK_COLON ":"
TOK_IDENTIFIER "int"
TOK_ASSIGN "="
TOK_NUMBER "10"
TOK_PLUS "+"
TOK_NUMBER "20"
TOK_SEMICOLON ";"
TOK_EOF