# cxx-frontend

> A compiler front end for the C++ language

Latest version **1.1.27** (published 2023-10-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install cxx-frontend
pnpm add cxx-frontend
yarn add cxx-frontend
bun add cxx-frontend
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high maintenance score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.1.27 |
| Published | 2023-10-03 |
| First published | 2021-10-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 1.6 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 164 |
| Author | Roberto Raggi |
| Maintainers | robertoraggi |
| Keywords | c++, compiler, lexer, parser, preprocessor, syntax |

## Links

- npm: https://www.npmjs.com/package/cxx-frontend
- Repository: https://github.com/robertoraggi/cplusplus
- Homepage: https://github.com/robertoraggi/cplusplus#readme
- Issues: https://github.com/robertoraggi/cplusplus/issues
- npm.io page: https://npm.io/package/cxx-frontend

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 1.1.27 (latest) — 2023-10-03
- 1.1.26 — 2023-09-24
- 1.1.25 — 2023-09-21
- 1.1.24 — 2023-09-18
- 1.1.23 — 2023-09-17
- 1.1.22 — 2023-09-16
- 1.1.21 — 2023-09-04
- 1.1.20 — 2023-08-28
- 1.1.19 — 2023-08-22
- 1.1.18 — 2023-08-14
- 1.1.17 — 2023-07-12
- 1.1.16 — 2023-06-11
- 1.1.15 — 2023-06-04
- 1.1.14 — 2022-02-26
- 1.1.13 — 2021-12-02
- … 14 more at https://npm.io/package/cxx-frontend/versions

## README

# A compiler front end for the C++ language

cxx-frontend is a work-in-progress compiler frontend for C++23

The compiler frontend is designed to be a powerful tool for developers, enabling them to parse, analyze, and modify C++ source code. This project aims to provide a robust foundation for building a complete C++ frontend, staying
up-to-date with the latest language features and standards.

# Changelog and What's New

For updates, improvements, and recent features in cxx-frontend, please consult the [Changelog](CHANGELOG.md).

# Key Features

- **Syntax Analysis**: APIs to scan, preprocess, parse, and inspect the syntax of source code, making it a versatile tool for various code analysis tasks.

- **Multi-Language Support**: In addition to C++, the library provides APIs for TypeScript and JavaScript.

- **C++-23 Support**: Latest language enhancements, syntax, and features (WIP).

## Syntax Checker and AST Browser Showcase

Storybook and CodeMirror are used to demonstrate how to create a syntax checker and navigate the Abstract Syntax Tree (AST)

https://robertoraggi.github.io/cplusplus/

## Installing from npm

To integrate the latest stable version of the C++ Compiler Frontend bindings into your project, you can install them from npm:

```sh
npm install cxx-frontend
```

Once installed, you can use the bindings in your Node.js or web projects as needed.

## Getting Started Using Example Projects

These projects are pre-configured and serve as starting points for various [use cases](https://github.com/robertoraggi/cplusplus/tree/main/templates).

For Node.js

```sh
npx degit robertoraggi/cplusplus/templates/cxx-parse cxx-parse
cd cxx-parse
npm install
node .
```

For web-based applications, use these commands to clone, set up, and start a development server:

```sh
npx degit robertoraggi/cplusplus/templates/cxx-browser-esm-vite cxx-browser-esm-vite
cd cxx-browser-esm-vite
npm install
npm run dev
```

## Build the npm package (requires docker)

```sh
cd packages/cxx-frontend

# prepare the package
npm ci

# compile WASM and TypeScript code
npm run build

# build the package
npm pack
```

## Build the WASM/WASI (requires docker)

```sh
# build for WASI and install it under build.em/install/
./scripts/build-wasi.sh

# run the C++ front end CLI tool using wasmtime
wasmtime \
  --mapdir=/::build.wasi/install \
  --mapdir tests::tests \
  build.wasi/install/usr/bin/cxx.wasm -- \
  tests/manual/source.cc -ast-dump
```

## Native Build and CLI tools

On Linux, macOS and Windows:

```sh
# install the python packages required to run the unit tests (optional)
pip install -r tests/unit_tests/requirements.txt

# configure the source code
cmake . \
 -G Ninja \
 -B build \
 -DCMAKE_BUILD_TYPE=Release \
 -DCXX_INTERPROCEDURAL_OPTIMIZATION=1

# build
cmake --build build

# run the unit tests
cd build
ctest --progress
```

## Serialize the AST

Use `-emit-ast` to serialize the AST of a C++ program to a flatbuffer binary file

```sh
# serialize the AST
$ ./build/src/frontend/cxx -emit-ast source.cc -o source.ast
```

You can use any flatbuffers supported decoder to read the AST, e.g.

```sh
# Use flatc to dump the AST to JSON
$ ./build/_deps/flatbuffers-build/flatc --raw-binary -t build/src/parser/cxx/ast.bfbs  -- source.ast

$ ll source.*
source.ast source.cc source.json
```

## License

Copyright (c) 2023 Roberto Raggi roberto.raggi@gmail.com

Licensed under the [MIT](LICENSE) license.

---
_Source: https://npm.io/package/cxx-frontend · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
