Code Monkey home page Code Monkey logo

peggy-tag's Introduction

peggy-tag

Tagged template strings for Peggy grammars

Installation

npm install peggy-tag

Usage

import peggy from "peggy-tag";

const parse = peggy`foo = $("f" "o"+)`;
console.log(parse("foooo")); // "foooo"

const traceGrammar = peggy.withOptions({ trace: true });
const trace = traceGrammar`num = n:$[0-9]+ { return parseInt(n, 10); }`
console.log(trace("123"));
// 8:20-8:20 rule.enter num
// 8:20-8:23 rule.match num
// 123

If your grammar imports rules from other grammars, you MUST use the async functions withImports or withImportsOptions

import {withImports, withImportsOptions} from "peggy-tag";

const parse = await withImports`
import Foo from './test/fixtures/foo.js'
bar = Foo`;
console.log(parse("foo")); // "foo"

const traceGrammar = await withImportsOptions({ trace: true });
const trace = traceGrammar`num = n:$[0-9]+ { return parseInt(n, 10); }`
console.log(trace("123"));
// 11:20-11:20 rule.enter num
// 11:20-11:23 rule.match num
// 123

Notes:

  • This currently is only tested on Node 18+, no browser version yet.
  • Node 20.8+ and --experimental-vm-modules are required for the async versions that allow importing libraries.
  • This is for NON-performance-sensitive code (e.g. prototypes), because the parser with be generated every time the template is evaluated.
  • If your parse function's variable name has exactly five letters (like "parse" or "trace"), the column numbers will be correct. See issue #14 for discussion.

Tests codecov

peggy-tag's People

Contributors

hildjj avatar kaileykaes avatar

Stargazers

Nathan Hutchision avatar

Watchers

 avatar Charles Pick avatar  avatar

Forkers

hildjj kaileykaes

peggy-tag's Issues

Code review

@sanket143 can you take a look at this please, to see if it's more-or-less what you wanted, and to see if there's anything that can be improved?

Better estimation of first-line column numbers

With the PR about to drop, line numbers will be correct from this code:

import peggy from "peggy-tag";

const parse123 = peggy`foo = $("f" "o"+)`;
console.log(parse123("foooo")); // "foooo"

but column numbers for errors thrown from the last line will be off by three. Find a way to determine where "foooo" actually starts.

Grammar-source offsets

It would be nice if peggy.GrammarError.format supported line and column offsets so that errors could be formatted with the CallSite of the place where the template originated. This will work in node, and possibly in v8-based browsers with the trick from stack-trace.

Any interest in pegjs-template features?

I just discovered peggyjs. Really nice work!

This peggy-tag library plays a similar role to my pegjs-template library for PEG.js. I'd love to contribute the features from that library to this one. In particular, I'd be interested in contributing:

  1. The ability to interpolate in functions and use them as semantic actions. This allows you to get syntax highlighting and type checking for semantic actions, to use TypeScript in them, to capture JavaScript variables, etc etc. This isn't just interpolating the functions' source as a string; that would prevent things like capturing variables. Instead, under the hood, the functions are passed in to the parser via the grammar context.
  2. The ability to define "partial" grammars that you can interpolate into other grammars, allowing you to share common definitions across multiple grammars. I find this pretty useful for lexical stuff in particular. Again, this isn't just interpolating the shared definitions as a string, because we want to take functions along for the ride.

I've found these features quite useful in my own work. What do you think; do these feel like a good fit for peggy-tag?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.