Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Syntax specifications

x2c’s syntax is specified here as a token grammar plus lexical transition rules that describe the current parser. The first extraction matched revision 48715bde on dev. Parser changes update these pages and etc/syntax/ by hand. tools/syntax-spec check verifies grammar references and reports generated files that differ from their data. It does not compare the data with the parser, and no gate runs it. The language reference supplies the meaning and constraints of the forms.

  • Source grammar: EBNF, precedence, contextual decisions, and macro extension contracts. The EBNF file is the same grammar included in that chapter, generated from etc/syntax/grammar.x.
  • Lexical specification: byte classes, ordered recognition, numeric and escape rules, nested modes, token reclassification, layout, and failure behavior.
  • Syntax as data: ordinary x2c Lists, their algebra, and a tool for generation, reference checks, dependency graphs, and lexical mode graphs.
  • x2c syntax map: the additions beyond C11, grouped for onboarding and linked to the specification data.

The grammar describes source after tokenization and indentation rewriting. It is not a claim that a context-free recognizer can decide whether an x2c program is valid. Names, types, macro signatures, and preprocessor state participate in parsing. The grammar marks these dependencies as external recognizers.

Catalog of existing material

This catalog lists the existing sources of syntax rules. Paths below are repository-relative. The table gives function names because line numbers change.

MaterialLocationWhat it specifiesLimits
Language referencedocs/src/reference/language.mdUser-facing forms, examples, semantic constraints; a small protocol grammar under ProtocolsNo complete grammar or lexical rule inventory
Syntax guidesdocs/src/guide/{from-c,indentation,macros,match,collections,symbols,protocols}.mdExplanations and examples of individual formsExamples do not define all accepted token sequences
Declaration parsersrc/parse/parse.xRecursive-descent rules for units, imports, types, declarations, declarators, parameters, fields, enumeratorsMixes recognition with name binding, collection, and semantic work
Expression parser and precedence ledgersrc/expressions/expressions.x, src/operator-ledger.xExpression productions, ambiguity decisions, complete binary operator precedence rowsDepends on type and macro state
Statement parsersrc/statements.xStatements, blocks, match/catch arms, directive-aware governed statementsMacro-produced statements also enter binding
Literal and lambda parserssrc/literals/literals.x, src/lambdas.xQuoted data, interpolation, patterns, lambdas and capturesToken modes determine which spellings reach them
Macro parser and built-inssrc/macros/macros.x, etc/builtin-macros.xCategory table, definitions, invocations, quotations, typed holes, keyword aliases; built-in class and foreachGrammar is extended by visible definitions; libraries such as lib/system-macros.x add more
Protocol parsersrc/protocol/protocol.xProtocol definitions, associated types, members, adoptionsWitness resolution and type constraints are separate
AST source patternssrc/grammar.xExecutable source-shaped macro patterns and AST accessors used by loweringDespite the filename, not a complete source grammar
Character scannerslib/scan.xAllocation-free byte recognizers, keywords, longest operator prefix, literal escapes, failure statusesA scanner in isolation does not specify tokenizer dispatch or context
Tokenizer and layoutlib/tokenizer.xOrdered scanning, mode stack, operand context, keyword reclassification, indentation rewrite, positionsContextual and stateful, not a single token regex table
Preprocessor handlingsrc/preprocess.x, src/frontend.xDirective handling, conditional groups, configured units and host-preprocessed importsHost C preprocessing has its own language and target configuration
Compiler token preparationsrc/compiler/parsing/tokens.x (Compiler.tokenize, _retag_keywords)Phase ordering and contextual in/match token typesRuns after raw tokenization and conditional scanning
Lisp readerlib/lisp/lisp.x (LispReader)Lists, reader prefixes and atomic forms over Lisp-mode tokensEvaluation and compile-time operations are not source grammar
Generated API pagesdocs/src/internals/compiler-api/, notably parse.md and grammar.mdPublic signatures and source doc commentsDerived API documentation, not an independent grammar
Architecture and ownership mapdocs/src/internals/{architecture,implementation-map}.mdPhase order and syntax ownersDescribes organization, not acceptance rules
Scanner/tokenizer testsunittest/test-scan.x, unittest/test-tokenizer.xExecutable recognition, boundary, mode and layout casesFinite observations, not a formal definition
Compiler fixturesunittest/compiler-fixtures/Token dumps, AST/translation outputs, diagnostics, successful and rejected programsExpectations cover selected cases, not all derivations
Editor grammarsetc/vsc-extension/syntaxes/{x2c,x2c-lisp}.tmLanguage.jsonTextMate regular expressions and highlighting contextsApproximate; cannot decide acceptance or reproduce binding
Editor grammar testsetc/vsc-extension/test/current-grammar.test.xExpected highlighting scopesContains syntax-shaped samples that are not accepted programs; not an acceptance corpus
Bootstrap copiesbootstrap/lib/scan.{c,h}, bootstrap/lib/tokenizer.{c,h}Generated C implementation of lexical rulesDerived copies, not independent owners
Executable examplesexamples/manifest.txt and its programsWorking uses of the language with recorded checksDemonstrations, not exhaustive syntax rules

Status and boundaries

The extraction has three layers:

  1. Byte recognition and token transitions in the lexical specification.
  2. Token productions in etc/syntax/grammar.x, rendered as x2c.ebnf, with explicit external recognizers.
  3. Context and semantic constraints in the grammar chapter and language reference.

This specification is written and reviewed by hand. No parser is generated from it, and nothing proves it equal to the compiler. Named external recognizers cover the macro syntax, which definitions extend, and host-specific syntax. The lexical chapter identifies observed implementation edges separately from the supported spelling described by the book.

C translation constraints, preprocessing expressions, type checking, ownership, overload/protocol resolution, macro evaluation, AST lowering, and emitted C are outside the source grammar. The host compiler still checks the generated C. Resource limits and diagnostic wording are not productions.