A Tree-sitter grammar for cspaced, an indentation-based variant of the C programming language.
npm install tree-sitter-cspacedgit clone https://github.com/yourusername/tree-sitter-cspaced.git
cd tree-sitter-cspaced
npm install
npm run buildconst Parser = require('tree-sitter');
const Cspaced = require('tree-sitter-cspaced');
const parser = new Parser();
parser.setLanguage(Cspaced);
const sourceCode = `
if (condition):
printf("Hello cspaced!")
x = 42
for (int i = 0; i < 10; i++):
total += i
`;
const tree = parser.parse(sourceCode);
console.log(tree.rootNode.toString());cspaced is an indentation-based dialect of C that supports:
- Significant Indentation: 2-space indentation replaces braces
- Optional Semicolons: Inferred from newlines
- Full C Compatibility: All C constructs supported
- Clean Syntax: More readable than traditional C
// cspaced syntax
#include <stdio.h>
int fibonacci(int n):
if (n <= 1):
return n
else:
return fibonacci(n - 1) + fibonacci(n - 2)
int main(void):
printf("Hello World!\n")
return 0
Generates equivalent C code with braces and semicolons.
The grammar supports all C constructs with modified block syntax:
- Functions:
type function_name(parameters): - Control Flow:
if (condition):,while (condition):,for (init; cond; update): - Declarations: Standard C declarations with optional semicolons
- Expressions: Full C expression syntax
- Preprocessor: All standard directives
Using vim-plug:
Plug 'tree-sitter/tree-sitter'
Plug 'yourusername/tree-sitter-cspaced'Using the tree-sitter-syntax-highlighting extension or custom language server.
npm install
npm run buildnpm test# Generate parse trees for test files
npm run parse examples/test.csp├── grammar.js # Main grammar definition
├── src/ # Generated parser source
├── binding.gyp # Native compilation
├── package.json # NPM package config
└── test/ # Test files
- Grammar Rules: Defined in
grammar.jsusing Tree-sitter DSL - External Scanners: Handle significant indentation via
externals - Precedences: Define operator precedence conflicts
- Conflicts: Resolve ambiguities with optional semicolons
- cspaced - The main cspaced compiler
- language-cspaced - VS Code extension
- vim-cspaced - Vim plugin
MIT License