Skip to content

gengjiawen/c-parser

Repository files navigation

c-parser

npm version

A C11 parser written in TypeScript with GCC extensions support. Zero runtime dependencies.

Try the online playground.

Built for use with AST Explorer.

Install

npm install c11-parser

Usage

import { parse } from 'c11-parser';

const ast = parse(`
int main(void) {
    return 0;
}
`);

console.log(JSON.stringify(ast, null, 2));

Options

parse(source, {
  gnuExtensions: true, // Enable GCC extensions (default: true)
  loc: false, // Compute line/column locations on demand (default: false)
});

AST Explorer Adapter

import adapter from 'c11-parser/adapter';

API

Export Description
parse Parse C source into an AST
AST All AST node type definitions
Scanner Lexer class for tokenization
Parser Parser class with token helpers
TokenKind Token kind enumeration (180+ tokens)

Features

  • Full C11 syntax support
  • GCC __attribute__ extensions
  • Inline assembly (asm / __asm__)
  • Byte-range tracking on every AST node (start, end)
  • Optional line/column location tracking (loc) when enabled
  • Dual package: ESM + CommonJS

Development

pnpm build      # Build with tsup
pnpm test       # Run tests
pnpm lint       # Lint with oxlint
pnpm fmt        # Format with oxfmt

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages