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

ched-shred

v0.3.0

Published

Lightweight C syntax parser intended for reading header files.

Downloads

6

Readme

ched-shred

Lightweight C syntax parser intended for reading .h header files.

Still in early development, not yet ready for any real use!

Interfaces

// a token is an individual unit of C syntax
// 'int' | '{' | '}' | ...
type TokenString = string;

interface Macro extends TokenString[] {
  macroArgumentNames? : string[];
  isVariadic? : boolean;
}

interface MacroSet {
  [macroName:string] : MacroSet;
}

Preprocessor

Main Class

The default export of the 'ched-shred/preprocessor' module is a Readable stream class named Preprocessor. The output of this stream a series of token strings representing the preprocessed code.

  • Output (Object Mode): TokenStrings
  • Constructor parameters:
    • initialPath : string
    • options (optional) : {...}
      • resolvePath(string, string) => string
        • Default: (p1, p2) => path.resolve(path.dirname(p1), p2)
      • createReadStream() : string => stream.Readable
        • Default: (p) => fs.createReadStream(p, 'utf8')
      • trigraphMode: 'replace' | 'ignore' | 'error'
        • Default: 'replace'
      • initialMacros: MacroSet
  • Properties:
    • .macros : MacroSet

Helper Functions

createMacroSet()

Creates and returns a new MacroSet. If a plain object is passed as a parameter, the properties of this object will be added to the new macro set.

Low-Level Transforms

The 'ched-shred/preprocessor' module also contains a number of Transform stream classes that replicate each phase of the C preprocessor.

TrigraphReplaceTransform

Replace the nine trigraphs (??=, ??/, ??', ??(, ??), ??!, ??<, ??>, ??-) with the symbols they represent.

  • Input (Chunked): Text
  • Output (Chunked): Text

TrigraphErrorTransform

Pass through input to output unchanged, but throw an error if a trigraph is detected in the input.

  • Input (Chunked): Text
  • Output (Chunked): Text

ContinuedLineTransform

Join together one line with the next when the first ends in a \ backslash, with optional whitespace after it.

  • Input (Chunked): Text
  • Output (Chunked): Text

CommentToWhitespaceTransform

Replace comments (of the style /*...*/ and //...) with a single space character, ignoring those inside string literals.

  • Input (Chunked): Text
  • Output (Chunked): Text

Note that /*...*/-style comments do not nest.

DirectiveSplitTransform

Split text content into preprocessor directives and sections of code. Each code section only includes complete lines of code, so they can be tokenized independently. Directives and code sections are both guaranteed to end with a whitespace character (an extra space will be appended to the final line if there is no whitespace before the end), so that the text will always produce a set of complete tokens if processed by TokenizeTransform.

  • Input (Chunked): Text
  • Output (Object Mode):
    • ["", "...code section\n"]
    • ["#directive", "...directive parameters\n"]

Note that any whitespace between # and the name of a directive is removed in the output.

Comments and line continuators need to be handled first before this transform is applied, or the results are likely to become mangled.

Depending on the input stream there may sometimes be a run of two or more code sections without a directive.

TokenizeTransform

Split the incoming text stream into a series of atomic token strings.

  • Input (Chunked): Text
  • Output (Object Mode): TokenStrings

MacroExpansionTransform

Pass through a stream of token strings, expanding macros as they are encountered. Will throw an error if .end() is called while a function-like macro is left unfinished.

  • Input (Object Mode): TokenStrings
  • Output (Object Mode): TokenStrings
  • Constructor parameters:
    • macros : MacroSet
  • Properties:
    • macros : MacroSet

Syntax Parser

The 'ched-shred/c-syntax' module contains one main Transform class:

TokenParseTransform

This transform stream takes in a series of preprocessed tokens and emits one object for each complete top-level declaration that it has read.

  • Input (Object Mode): TokenStrings
  • Output (Object Mode): Declaration objects, one of the following:
    • InitDeclaration
    • FunctionDefinition
    • LegacyFunctionDefinition