Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tree-sitter-cspaced

A Tree-sitter grammar for cspaced, an indentation-based variant of the C programming language.

Installation

As a Node.js Module

npm install tree-sitter-cspaced

Manual Installation

git clone https://github.com/yourusername/tree-sitter-cspaced.git
cd tree-sitter-cspaced
npm install
npm run build

Usage

const 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());

Language Features

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

Examples

// 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.

Grammar Structure

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

Integration with Editors

Vim/Neovim

Using vim-plug:

Plug 'tree-sitter/tree-sitter'
Plug 'yourusername/tree-sitter-cspaced'

VS Code

Using the tree-sitter-syntax-highlighting extension or custom language server.

Development

Building the Grammar

npm install
npm run build

Testing

npm test

Debugging

# Generate parse trees for test files
npm run parse examples/test.csp

Architecture

├── grammar.js          # Main grammar definition
├── src/               # Generated parser source
├── binding.gyp        # Native compilation
├── package.json       # NPM package config
└── test/             # Test files

Contributor Guide

  1. Grammar Rules: Defined in grammar.js using Tree-sitter DSL
  2. External Scanners: Handle significant indentation via externals
  3. Precedences: Define operator precedence conflicts
  4. Conflicts: Resolve ambiguities with optional semicolons

Related Projects

License

MIT License

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages