Skip to content

openpeeps/sweetsyntax

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

19 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

SweetSyntax 🍭 A generic parser and AST explorer
for analyzing programming languages

nimble install sweetsyntax

API reference
Github Actions Github Actions

😍 Key Features

  • Fast, compiled and efficient (check benchmarks section)
  • Generic parser & AST explorer
  • Embeddable in other languages via FFI πŸ‘‰ Lua, JavaScript (N-API), Ruby, Python, PHP
  • Easy-to-use API for integration into various applications
  • Built-in syntax support for: C, Crystal, D lang, Go, JavaScript, Nim, PHP, Python, Ruby, and Rust
  • Zero-copy parsing using MemFiles
  • Context-aware error reporting while parsing
  • Written in Nim

What's this for?

SweetSyntax is a powerful and flexible generic parser and AST explorer for analyzing programming languages! It is designed to be integrated into other applications, such as code editors, documentation generators, linting tools and other sweet things!

Parse any language by defining its grammar in a YAML specification file: tokens, operators (prefix, infix, postfix, assignment), statement keywords, block delimiters, and feature flags (arrow functions, generators, async/await, template literals). The parser uses a Pratt parsing approach with a language-agnostic core and per-language statement handlers.

Key capabilities:

  • Lexer: Generic tokenizer handling identifiers, literals (int, float, hex, octal, binary, bigint, string, regex), comments, and operators
  • Parser: Pratt (precedence-climbing) parser with configurable operator precedence and associativity
  • AST: Typed node tree with support for statements, expressions, infix/prefix/postfix operations, function declarations, and more
  • YAML-driven: Language syntaxes are pure YAML, allowing for custom statement handlers

Embeddable SweetSyntax

SweetSyntax is written in Nim, and thanks to Nim's versatile compilation model, can be embedded natively into a wide range of host languages. This is WIP via https://github.com/openpeeps/clue toolkit

Language Integration
Lua Load the compiled .so/.dll via LuaJIT FFI or a lightweight C binding
JavaScript Use as a Node.js native addon via N-API
Ruby Bundle as a Ruby C extension
Python Call through Python's CFFI or ctypes
PHP Expose as a PHP extension written in C

The Nim library compiles to a small, self-contained shared object that any FFI-capable language can load, making SweetSyntax a portable parsing engine for your polyglot projects.

Examples

Parsing JavaScript

import sweetsyntax

# Parse a file β€” the library detects the language from the extension
let program = parseJavaScript("app.js")
echo toJson(program)

Parsing with custom features

let program = parseScript("module.ts", jsHandlers,
  features = {featAsync, featArrowFn, featGenerators,
              featLabeledStmt, featTemplateLit})

Parsing with custom language handlers

proc myHandlers(p: var GenericParser) =
  stmtHandler p, "myKeyword":
    walk p
    result = Node(kind: nkStatement,
      children: @[Node(kind: nkIdent, name: "myKeyword"), parseExpression(p)])

let program = parseScript("file.ext", myHandlers,
  features = {featAsync, featArrowFn})

Error Reporting

SweetSyntax has built-in support for context-aware reporting. For example:

  return a == null || b == null ## NaN : a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
                                ^
Error (2:33) Unexpected prefix token: '#'

Benchmarks

SweetSyntax is built for speed. Below is a hyperfine benchmark parsing and validating a full copy of d3.js (v7.9.0, ~20k lines, unminified, from cdnjs.com). The entire pipeline (lexing, parsing, and AST generation) completes in under 120ms on my πŸ”₯ rastafarian Ryzen 5 with 6 cores/12 threads:

hyperfine --runs 4 './sweetsyntax_benc_d3'
Benchmark 1: ./sweetsyntax_benc_d3
  Time (mean Β± Οƒ):     116.5 ms Β±   1.1 ms    [User: 107.5 ms, System: 7.1 ms]
  Range (min … max):   115.9 ms … 118.2 ms    4 runs

❀ Contributions & Support

OpenCode Switch to Open-Source LLMs via OpenCode GO, choosing from a variety of powerful models such as DeepSeek, Qwen, Kimi, GLM-5, MiniMax, MiMo. πŸ• Use our referral link to get started!

🎩 License

LGPLv3 license. Made by Humans from OpenPeeps.
Copyright OpenPeeps & Contributors β€” All rights reserved.

About

SweetSyntax 🍭 A generic parser and AST explorer for analyzing programming languages

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

Generated from openpeeps/pistachio