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

Pure ESM #1953

Merged
merged 19 commits into from
Jul 8, 2023
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
73 changes: 0 additions & 73 deletions .eslintrc.js

This file was deleted.

1 change: 1 addition & 0 deletions examples/grammars/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./graphql/gen/graphql-bundled.min.mjs
7 changes: 3 additions & 4 deletions examples/grammars/calculator/calculator_embedded_actions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"use strict"
/**
* An Example of implementing a Calculator with embedded actions (semantics).
*
Expand All @@ -10,12 +9,12 @@
* for an alternative:
* https://github.com/chevrotain/chevrotain/blob/master/examples/grammars/calculator/calculator_pure_grammar.js
*/
const {
import {
createToken,
Lexer,
EmbeddedActionsParser,
tokenMatcher
} = require("chevrotain")
} from "chevrotain"

// ----------------- lexer -----------------
// using the NA pattern marks this Token class as 'irrelevant' for the Lexer.
Expand Down Expand Up @@ -194,7 +193,7 @@ class Calculator extends EmbeddedActionsParser {
const parser = new Calculator()

// wrapping it all together
module.exports = function (text) {
export function parseEmbedded(text) {
const lexResult = CalculatorLexer.tokenize(text)
// setting a new input will RESET the parser instance's state.
parser.input = lexResult.tokens
Expand Down
5 changes: 2 additions & 3 deletions examples/grammars/calculator/calculator_pure_grammar.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"use strict"
/**
* An Example of implementing a Calculator with separated grammar and semantics (actions).
* This separation makes it easier to maintain the grammar and reuse it in different use cases.
Expand All @@ -9,7 +8,7 @@
* See farther details here:
* https://chevrotain.io/docs/guide/concrete_syntax_tree.html
*/
const { createToken, tokenMatcher, Lexer, CstParser } = require("chevrotain")
import { createToken, tokenMatcher, Lexer, CstParser } from "chevrotain"

// ----------------- lexer -----------------
// using the NA pattern marks this Token class as 'irrelevant' for the Lexer.
Expand Down Expand Up @@ -242,7 +241,7 @@ class CalculatorInterpreter extends BaseCstVisitor {
// We only need a single interpreter instance because our interpreter has no state.
const interpreter = new CalculatorInterpreter()

module.exports = function (text) {
export function parsePure(text) {
// 1. Tokenize the input.
const lexResult = CalculatorLexer.tokenize(text)

Expand Down
26 changes: 13 additions & 13 deletions examples/grammars/calculator/calculator_spec.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
const assert = require("assert")
import assert from "assert"
import { parseEmbedded } from "./calculator_embedded_actions.js"
import { parsePure } from "./calculator_pure_grammar.js"

describe("The Calculator Grammar", () => {
context("Embedded Actions", () => {
const calc = require("./calculator_embedded_actions")
it("can calculate an expression", () => {
assert.equal(calc("1 + 2").value, 3)
assert.equal(parseEmbedded("1 + 2").value, 3)
})

it("can calculate an expression with operator precedence", () => {
// if it was evaluated left to right without taking into account precedence the result would have been 9
assert.equal(calc("1 + 2 * 3").value, 7)
assert.equal(parseEmbedded("1 + 2 * 3").value, 7)
})

it("can calculate an expression with operator precedence #2", () => {
assert.equal(calc("(1 + 2) * 3").value, 9)
assert.equal(parseEmbedded("(1 + 2) * 3").value, 9)
})

it("can calculate an expression with many parenthesis", () => {
assert.equal(calc("((((666))))").value, 666)
assert.equal(parseEmbedded("((((666))))").value, 666)
})

it("can calculate an expression with power function", () => {
assert.equal(calc("1 + power(2,2)").value, 5)
assert.equal(parseEmbedded("1 + power(2,2)").value, 5)
})
})

context("Pure Grammar with Separated Semantics", () => {
const calc = require("./calculator_pure_grammar")
it("can calculate an expression", () => {
assert.equal(calc("1 + 2").value, 3)
assert.equal(parsePure("1 + 2").value, 3)
})

it("can calculate an expression with operator precedence", () => {
// if it was evaluated left to right without taking into account precedence the result would have been 9
assert.equal(calc("1 + 2 * 3").value, 7)
assert.equal(parsePure("1 + 2 * 3").value, 7)
})

it("can calculate an expression with operator precedence #2", () => {
assert.equal(calc("(1 + 2) * 3").value, 9)
assert.equal(parsePure("(1 + 2) * 3").value, 9)
})

it("can calculate an expression with many parenthesis", () => {
assert.equal(calc("((((666))))").value, 666)
assert.equal(parsePure("((((666))))").value, 666)
})

it("can calculate an expression with power function", () => {
assert.equal(calc("1 + power(2,2)").value, 5)
assert.equal(parsePure("1 + power(2,2)").value, 5)
})
})
})
31 changes: 14 additions & 17 deletions examples/grammars/css/css.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const XRegExp = require("xregexp")
const chevrotain = require("chevrotain")
const { Lexer, CstParser } = chevrotain
import XRegExp from "xregexp"
import { Lexer, CstParser, createToken as orgCreateToken } from "chevrotain"

// ----------------- lexer -----------------
// Based on the specs in:
Expand All @@ -23,7 +22,7 @@ function MAKE_PATTERN(def, flags) {
// array of cssTokens
const cssTokens = []
const createToken = function () {
const newToken = chevrotain.createToken.apply(null, arguments)
const newToken = orgCreateToken.apply(null, arguments)
cssTokens.push(newToken)
return newToken
}
Expand Down Expand Up @@ -607,18 +606,16 @@ class CssParser extends CstParser {
// reuse the same parser instance.
const parser = new CssParser()

module.exports = {
parseCss: function (text) {
const lexResult = CssLexer.tokenize(text)
// setting a new input will RESET the parser instance's state.
parser.input = lexResult.tokens
// any top level rule may be used as an entry point
const cst = parser.stylesheet()

return {
cst: cst,
lexErrors: lexResult.errors,
parseErrors: parser.errors
}
export function parseCss(text) {
const lexResult = CssLexer.tokenize(text)
// setting a new input will RESET the parser instance's state.
parser.input = lexResult.tokens
// any top level rule may be used as an entry point
const cst = parser.stylesheet()

return {
cst: cst,
lexErrors: lexResult.errors,
parseErrors: parser.errors
}
}
4 changes: 2 additions & 2 deletions examples/grammars/css/css_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require("assert")
const parseCss = require("./css").parseCss
import assert from "assert"
import { parseCss } from "./css.js"

describe("The CSS Grammar", () => {
it("can parse a simple CSS without errors", () => {
Expand Down
5 changes: 2 additions & 3 deletions examples/grammars/csv/csv.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"use strict"
/**
* An Example of implementing a CSV Grammar with Chevrotain.
*
* Based on: https://github.com/antlr/grammars-v4/blob/master/csv/CSV.g4
*
* Note that this is a pure grammar without any actions (either embedded or via a CST Visitor).
*/
const { createToken, Lexer, CstParser, EMPTY_ALT } = require("chevrotain")
import { createToken, Lexer, CstParser, EMPTY_ALT } from "chevrotain"

// ----------------- lexer -----------------
const Text = createToken({ name: "Text", pattern: /[^,\n\r"]+/ })
Expand Down Expand Up @@ -67,7 +66,7 @@ class CsvParser extends CstParser {
// reuse the same parser instance.
const parser = new CsvParser([])

module.exports = function (text) {
export function parseCsv(text) {
// 1. Tokenize the input.
const lexResult = CsvLexer.tokenize(text)

Expand Down
11 changes: 6 additions & 5 deletions examples/grammars/csv/csv_spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"use strict"
import path from "path"
import { fileURLToPath } from "url"
import fs from "fs"
import assert from "assert"
import { parseCsv } from "./csv.js"

const path = require("path")
const fs = require("fs")
const assert = require("assert")
const parseCsv = require("./csv")
const __dirname = path.dirname(fileURLToPath(import.meta.url))

describe("The CSV Grammar", () => {
const samplePath = path.resolve(__dirname, "./sample.csv")
Expand Down
12 changes: 3 additions & 9 deletions examples/grammars/ecma5/ecma5_api.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"use strict"

const tokenize = require("./ecma5_lexer").tokenize
const ECMAScript5Parser = require("./ecma5_parser").ECMAScript5Parser
import { tokenize } from "./ecma5_lexer.js"
import { ECMAScript5Parser } from "./ecma5_parser.js"

const parserInstance = new ECMAScript5Parser()

function parse(str) {
export function parse(str) {
const tokens = tokenize(str)
parserInstance.input = tokens
parserInstance.orgText = str
Expand All @@ -15,7 +13,3 @@ function parse(str) {
throw Error("Sad Sad Panda")
}
}

module.exports = {
parse
}
12 changes: 3 additions & 9 deletions examples/grammars/ecma5/ecma5_lexer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"use strict"

/**
* ECMAScript cannot be easily lexed using a distinct lexing phase.
* See: https://users.soe.ucsc.edu/~cormac/papers/dls14a.pdf
Expand All @@ -11,9 +9,9 @@
* https://github.com/chevrotain/chevrotain/blob/master/test/full_flow/ecma_quirks/ecma_quirks.ts
*
*/
const acorn = require("acorn")
import * as acorn from "acorn"
const acornTokTypes = acorn.tokTypes
const tokens = require("./ecma5_tokens")
import * as tokens from "./ecma5_tokens.js"

function createChevToken(chevTokenClass, acornToken) {
return {
Expand All @@ -24,7 +22,7 @@ function createChevToken(chevTokenClass, acornToken) {
}
}

function tokenize(str) {
export function tokenize(str) {
const result = []
for (let token of acorn.tokenizer(str, { ecmaVersion: 6 })) {
let acornType = token.type
Expand Down Expand Up @@ -325,7 +323,3 @@ function tokenize(str) {

return result
}

module.exports = {
tokenize
}
18 changes: 5 additions & 13 deletions examples/grammars/ecma5/ecma5_parser.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
"use strict"

const {
import {
EmbeddedActionsParser,
EOF,
tokenMatcher,
MismatchedTokenException
} = require("chevrotain")
const tokens = require("./ecma5_tokens")
// for conciseness
const t = tokens
} from "chevrotain"
import * as t from "./ecma5_tokens.js"

const ENABLE_SEMICOLON_INSERTION = true
const DISABLE_SEMICOLON_INSERTION = false

// as defined in https://www.ecma-international.org/ecma-262/5.1/index.html
class ECMAScript5Parser extends EmbeddedActionsParser {
export class ECMAScript5Parser extends EmbeddedActionsParser {
set orgText(newText) {
this._orgText = newText
}

constructor() {
super(tokens, {
super(t, {
// Reduces Parser Initialization time and this grammar does not need
// a larger lookahead.
maxLookahead: 2
Expand Down Expand Up @@ -905,7 +901,3 @@ const insertedSemiColon = {
endOffset: NaN,
automaticallyInserted: true
}

module.exports = {
ECMAScript5Parser
}
5 changes: 2 additions & 3 deletions examples/grammars/ecma5/ecma5_spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use strict"
import { expect } from "chai"

const expect = require("chai").expect
const parse = require("./ecma5_api").parse
import { parse } from "./ecma5_api.js"

describe("The ECMAScript5 Grammar", () => {
const sampleText = `var chevrotain = require("chevrotain")
Expand Down
Loading