Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add token span info, fixes #1 and #13 #15

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ concurrency:
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
conformance:
name: Conformance
conformance-rust:
name: Conformance Rust
runs-on: "ubuntu-latest"
steps:
- name: Checkout
Expand Down Expand Up @@ -51,15 +51,37 @@ jobs:
command: clippy
args: -- --deny warnings

test:
conformance-ts:
name: Conformance TypeScript
runs-on: "ubuntu-latest"
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '22'

- name: Install Dependencies
run: cd bindings/typescript && npm install

- name: Eslint
run: cd bindings/typescript && npm run lint

- name: Prettier
run: cd bindings/typescript && npm run format:check


test-rust:
strategy:
matrix:
os:
- windows-latest
- macos-latest
- ubuntu-latest

name: Test (${{ matrix.os }})
name: Test Rust (${{ matrix.os }})
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
Expand All @@ -83,14 +105,14 @@ jobs:
toolchain: stable
override: true

- name: Test
- name: Test Rust
uses: actions-rs/cargo@v1
with:
command: test

publish-crates-io:
if: startsWith(github.ref, 'refs/tags/v')
needs: [ conformance, test ]
needs: [ conformance-rust, test-rust ]

name: Publish to Crates.io
runs-on: "ubuntu-latest"
Expand All @@ -112,7 +134,7 @@ jobs:

publish-npm:
if: startsWith(github.ref, 'refs/tags/v')
needs: [ conformance, test ]
needs: [ conformance-rust, test-rust ]

name: Publish to NPM
runs-on: "ubuntu-latest"
Expand All @@ -130,7 +152,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '20'
node-version: '22'

- name: Install Dependencies
run: cd bindings/typescript && npm install
Expand Down
4 changes: 4 additions & 0 deletions bindings/typescript/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore artifacts:
dist
node_modules
src/pkg
5 changes: 5 additions & 0 deletions bindings/typescript/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"tabWidth": 2,
"useTabs": false,
"endOfLine": "lf"
}
48 changes: 26 additions & 22 deletions bindings/typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
It is focused on providing educational resources for learning about parsing algorithms and compiler construction. The library is designed to be easy to use and understand, making it ideal for students, educators, and developers interested in language processing.

### Table of Contents

1. [Installation](#installation)
2. [Basic Usage](#basic-usage)
3. [Defining a Grammar](#defining-a-grammar)
Expand All @@ -16,20 +17,23 @@ It is focused on providing educational resources for learning about parsing algo
Before using the `dotlr` library, you need to install it. The following instructions assume you have a project with `npm` already set up.

```bash
npm install dotlr
npm install dotlr
```

### Importing the Library

To use the `dotlr` library, import it into your TypeScript files:

```ts
import { Grammar, LR1Parser, LALRParser } from 'dotlr';
import { Grammar, LR1Parser, LALRParser } from "dotlr";
```

this library uses `ts-results` under the hood to handle errors and results.

```ts
import { Ok, Err } from 'ts-results';
import { Ok, Err } from "ts-results-es";
```

## Basic Usage

The core of the `dotlr` library revolves around defining a grammar and using it to create a parser. The following steps will guide you through this process.
Expand All @@ -50,12 +54,12 @@ const grammarStr = `
const grammarResult = Grammar.parse(grammarStr);

if (grammarResult.ok) {
const grammar = grammarResult.val;
console.log("Grammar successfully parsed!");
console.log(grammar.getSymbols());
console.log(grammar.getProductions());
const grammar = grammarResult.val;
console.log("Grammar successfully parsed!");
console.log(grammar.getSymbols());
console.log(grammar.getProductions());
} else {
console.error("Failed to parse grammar:", grammarResult.val);
console.error("Failed to parse grammar:", grammarResult.val);
}
```

Expand All @@ -71,24 +75,24 @@ The `LR1Parser` class allows you to create an LR(1) parser for the grammar and u
const lr1ParserResult = LR1Parser.fromGrammar(grammar);

if (lr1ParserResult.ok) {
const lr1Parser = lr1ParserResult.val;

const input = "aab";
const parseResult = lr1Parser.parse(input);

if (parseResult.ok) {
const parseTree = parseResult.val;
console.log("Parse successful!");
console.log(parseTree);
} else {
console.error("Parse error:", parseResult.val);
}
const lr1Parser = lr1ParserResult.val;

const input = "aab";
const parseResult = lr1Parser.parse(input);

if (parseResult.ok) {
const parseTree = parseResult.val;
console.log("Parse successful!");
console.log(parseTree);
} else {
console.error("Parse error:", parseResult.val);
}
} else {
console.error("Failed to create LR(1) parser:", lr1ParserResult.val);
console.error("Failed to create LR(1) parser:", lr1ParserResult.val);
}
```

- **LR1Parser.fromGrammar()**: Consumes the `Grammar` object and returns an `LR1Parser`, you cannot reuse the *Grammar* object, if you need it, you can clone it by using `grammar.clone()`.
- **LR1Parser.fromGrammar()**: Consumes the `Grammar` object and returns an `LR1Parser`, you cannot reuse the _Grammar_ object, if you need it, you can clone it by using `grammar.clone()`.
- **parser.parse()**: method attempts to parse the given input string according to the LR(1) grammar. Returns a parse tree if successful.
- **parser.trace()** method can be used to trace the parsing process. It returns a trace and the resulting parse tree at each step, if parsing is successful.
- **parser.tokenize()** method can be used to tokenize the input string. It returns a list of tokens.
Expand Down
22 changes: 10 additions & 12 deletions bindings/typescript/build.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import {execSync} from 'child_process';
import fs from "fs/promises"

import { execSync } from "child_process";
import fs from "fs/promises";

async function init() {
console.log("Starting build...")
execSync('tsc', {stdio: 'inherit'});
await fs.cp("./src/pkg", "./dist/pkg", {recursive: true});
await fs.unlink("./dist/pkg/package.json");
await fs.unlink("./dist/pkg/README.md");
await fs.unlink("./dist/pkg/.gitignore");
console.log("Build complete")

console.log("Starting build...");
execSync("tsc", { stdio: "inherit" });
await fs.cp("./src/pkg", "./dist/pkg", { recursive: true });
await fs.unlink("./dist/pkg/package.json");
await fs.unlink("./dist/pkg/README.md");
await fs.unlink("./dist/pkg/.gitignore");
console.log("Build complete");
}

init()
init();
13 changes: 13 additions & 0 deletions bindings/typescript/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";

export default [
{
ignores: ["src/pkg/", "dist/", "node_modules/", "build.js"],
},
{
files: ["src/**/*.{js,mjs,cjs,ts}"],
},
pluginJs.configs.recommended,
...tseslint.configs.recommended,
];
Loading
Loading