Skip to content

Commit

Permalink
Merge pull request #8 from hildjj/grammar-location
Browse files Browse the repository at this point in the history
Grammar location
  • Loading branch information
hildjj committed Dec 3, 2023
2 parents 27b5253 + 41692d3 commit d698cba
Show file tree
Hide file tree
Showing 5 changed files with 349 additions and 2,153 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@ name: Tests
on:
push:
branches:
- '*'
- 'main'
pull_request:
branches: ['*']
branches: ['main']

jobs:
build:

strategy:
matrix:
node-version: [18.x]
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: [18.x, 20.x, 21.x]
os: [ubuntu-latest]

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: npm
- name: Install dependencies
run: npm install
- name: Check coding standards
if: matrix.node-version == '18.x' && matrix.os == 'ubuntu-latest'
if: matrix.node-version == '21.x' && matrix.os == 'ubuntu-latest'
run: npm run lint
- name: Static analysis - check types
if: matrix.node-version == '18.x' && matrix.os == 'ubuntu-latest'
if: matrix.node-version == '21.x' && matrix.os == 'ubuntu-latest'
run: npm run ts
- name: Test
run: npm run coverage
Expand Down
37 changes: 35 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
import peggy from "peggy";
import url from "node:url";

/**
*
* @param {number} depth How deep in the callstack to go? "2" is usually the
* first interesting one.
* @returns {peggy.GrammarLocation?} Location of the grammar in the enclosing
* file.
*/
function callLocation(depth) {
const old = Error.prepareStackTrace;
Error.prepareStackTrace = (_, s) => s;
const stack = new Error().stack;
Error.prepareStackTrace = old;

// Not v8, or short-stacked vs. expectations
if (!Array.isArray(stack) || (stack.length < depth)) {
return null;
}

const callsite = stack[depth];
const source = callsite.getFileName();
const path = source.startsWith("file:") ? url.fileURLToPath(source) : source;
return new peggy.GrammarLocation(
path,
{
offset: callsite.getPosition() + 1, // Go past backtick
line: callsite.getLineNumber(),
column: callsite.getColumnNumber() + 1, // Go past backtick
}
);
}

/**
* Turn a templated string into a Peggy parsing function.
Expand All @@ -13,9 +45,10 @@ function pegWithOptions(opts, strings, ...values) {
strings.forEach((string, i) => {
text += string + (values[i] || "");
});
const grammarSource = callLocation(3) || "peggy-tag";
try {
const parser = peggy.generate(text, {
grammarSource: "peggy-tag",
grammarSource,
...opts,
}).parse;
return (text, options = {}) => {
Expand All @@ -37,7 +70,7 @@ function pegWithOptions(opts, strings, ...values) {
// @ts-ignore
if (typeof e?.format === "function") {
// @ts-ignore
e.message = e.format([{ source: "peggy-tag", text }]);
e.message = e.format([{ source: grammarSource, text }]);
}
throw e;
}
Expand Down
Loading

0 comments on commit d698cba

Please sign in to comment.