npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

c-ast

v1.0.0

Published

Ansi C AST Generator

Downloads

7

Readme

c-ast

Generates an Abstract Syntax Tree from C source code.

The resulting AST provides in-depth structured access and meta data to all definitions inside the given input.

Features

####1. Comments Extraction and Association Lookup c-ast extracts all comments in the source code as their own nodes, along with intelligent node association. This allows easy lookup of code that belongs to a comment, or getting any comments that belong to a given code block via an abritrary line lookup.

####2. Indexed access to all structured nodes in the source input An index is provided for fast lookup of any line, returning the node that spans that line along with node meta data.

####3. Use a CLI tool or JavaScript api for generating ASTs Use the $ c-ast cli to output JSON, or use the API directly in your NodeJS application with require('c-ast')

Getting Started (CLI)

Use NPM to install the c-ast library and accompanying cli executable.

$ npm install c-ast
$ c-ast --help

c-ast <cmd> [args]

Commands:
  transform <input> [--range]               transform input into an AST json
  annotate  <input> [--range] [--colorize]  annotate input with node metadata

Options:
  --version  Show version number                                       
  --help     Show help     
  
$ c-ast transform specimen/sample.h                                            

The transform command will output a JSON representation from the given input.

The annotate command will output the original input with added metadata on the side— used mainly for AST analysis and inspection.

An optional range argument is also available for limiting the resulting output.

Getting Started (Javascript API)

The Javascript APIs return a Promise which resolves into a AST data structure.

const cast = require('c-ast');

// Parse text input directly
const ast = await cast.ast_from_text("code_text_input");

// Read a file from disk
const ast = await cast.ast_from_file(path_to_file);

// Takes a streaming buffer
const ast = await cast.ast_from_stream(buffer);

Examples

In this basic example, the JSON output outlines the various functions, structures, and association of comments belonging to the struct and functions.

$ c-ast transform specimen/examples.c
{
    "nodes": {
        "comments": {
            "0": {
                "id": 0,
                "type": "comments",
                "assocs": {},
                "data": {
                    "0": "// Display Prime Numbers Between two Intervals When Larger Number is Entered first",
                    "1": "// @filename example.c",
                    "2": "// @author Bailey C"
                }
            },
            "6": {
                "id": 6,
                "type": "comments",
                "assocs": {
                    "defs": [
                        7
                    ]
                },
                "data": {
                    "6": "// Structure for managing prime input boundaries"
                }
            },
            "12": {
                "id": 12,
                "type": "comments",
                "assocs": {
                    "code": [
                        13
                    ]
                },
                "data": {
                    "12": "// Main entry point"
                }
            },
            "24": {
                "id": 24,
                "type": "comments",
                "assocs": {
                    "code": [
                        27
                    ]
                },
                "data": {
                    "24": "/**",
                    "25": " * Determines primes within a boundary",
                    "26": " */"
                }
            }
        },
        "code": {
            "13": {
                "id": 13,
                "type": "code",
                "assocs": {
                    "comments": [
                        12
                    ]
                },
                "data": {
                    "13": "int main()",
                    "14": "{",
						...
                    "21": "    return determine_primes(input);",
                    "22": "}"
                }
            },
            "27": {
                "id": 27,
                "type": "code",
                "assocs": {
                    "comments": [
                        24
                    ]
                },
                "data": {
                    "27": "int determine_primes(prime_input input)",
                    "28": "{",
						...
                    "54": "}"
                }
            }
        },
        "defs": {
            "7": {
                "id": 7,
                "type": "defs",
                "assocs": {
                    "comments": [
                        6
                    ]
                },
                "data": {
                    "7": "typedef struct prime_input {",
                    "10": "} prime_input;"
                },
                "inner": [
                    {
                        "id": 8,
                        "type": "members",
                        "assocs": {},
                        "data": {
                            "8": "    int low;"
                        },
                        "parent": 7
                    },
                    {
                        "id": 9,
                        "type": "members",
                        "assocs": {},
                        "data": {
                            "9": "    int high;"
                        },
                        "parent": 7
                    }
                ],
                "index": {
                    "8": {
                        "ind": 0,
                        "type": "members"
                    },
                    "9": {
                        "ind": 1,
                        "type": "members"
                    }
                }
            }
        }
    },
    "index": {
        "0": {
            "node_id": 0,
            "type": "comments",
        },
        "1": {
            "node_id": 0,
            "type": "comments",
        },
        "2": {
            "node_id": 0,
            "type": "comments",
        }
        ...
        "54": {
            "node_id": 27,
            "type": "code",
        }
    }
}

Testing

c-ast has full test coverage of all internal and public methods, including various edge case tests and real world specimen tests for completeness.

Tests are executed with mocha via the npm test command, and can be found in the /tests folder.

License

Authoried by Bailey Cosier. Licensed under The MIT License (MIT)

For the full copyright and license information, please view the LICENSE file.