Skip to content

Commit

Permalink
v0breaking: converted classes to plain object
Browse files Browse the repository at this point in the history
- The nodes are no longer classes but simple objects. Readonly has been removed
from all the properties of their interfaces and the parser result is not longer
"sealed". They should still not be mutated, but if you really want to you can.

- Nodes no longer have a parent property to avoid circular references. Instead
you can generate a parents map from a parser result to use getParent on if needed.

- To check a node or token is of a certain type, they now all have a `type` property.

- To check if an object is a node or a token,
you can use the new `isNode` and `isToken` utility functions.

- The non-node normalized Condition and Expression classes
are now the NormalizedCondition and NormalizedExpression interfaces.
Their object will return false if passed to isNode/Token, but they
have an AST_TYPE to be able to tell them apart.

- All node creation functions have been moved to /ast/*, /ast/classes has been removed.

- Several internal helpers have been moved, there is now an internals export
(just in case), it's not really meant to be used.
  • Loading branch information
AlansCodeLog committed Jun 9, 2024
1 parent 6b33125 commit baaab41
Show file tree
Hide file tree
Showing 77 changed files with 1,084 additions and 1,303 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ coverage
dist
!flake.lock
.direnv
todo.md
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pnpm install @witchcraft/expressit
- **Other Useful Utility Functions:**
- `extractTokens`, `getCursorInfo`, `getOppositeDelimiter`, `getSurroundingErrors` - useful for adding custom syntax highlighting.
- `prettyAst` - pretty prints a compact version of the ast for debugging
- other minor helpers - `isDelimiter`, `isQuote`, etc.
- other minor utilities - `isDelimiter`, `isQuote`, etc.
- **Pre-Configured Parsers** - Includes a pre-configured boolean parser (intended for parsing shortcut contexts in a similar way to VS Code).
- **Lots of Docs and Tests**

Expand All @@ -43,7 +43,6 @@ pnpm install @witchcraft/expressit
```ts
// while you can import from "@witchcraft/expressit", if using something like vite, it's recommended you do not use barrel imports.
import { Parser } from "@witchcraft/expressit/Parser.js"
import { ErrorToken } from "@witchcraft/expressit/classes/ErrorToken.js"

const parser = new Parser({/* opts */})
const context = {
Expand All @@ -57,8 +56,11 @@ const parser = new Parser({/* opts */})

const ast = parser.parse(input)

if (ast instanceof ErrorToken || !ast.valid) {
if (!ast.valid) {
// ...show regular errors (no input, missing tokens, etc)
if (ast.isToken) {
// empty input
}
} else {
// validation can be controlled by parser options
const errors = parser.validate(ast)
Expand All @@ -67,7 +69,7 @@ const parser = new Parser({/* opts */})

// ON AUTOCOMPLETE
const suggestions = parser.autosuggest(input, ast, cursor)
const completions = parser.autocomplete(suggestions, {
const completions = parser.autocomplete(ast,suggestions, {
// known possible suggestions
variables: ["c", "d", "e"],
// can also be values, prefixes, keywords, properties, etc
Expand Down
3 changes: 2 additions & 1 deletion demo/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
/* eslint-disable no-eval */

import "./style.css"
Expand Down Expand Up @@ -141,7 +142,7 @@ function onKeyboardInput(e, force = false) {
els.suggestion.innerHTML = str
els.pos.innerHTML = ` (${index})`

const autocompletions = parser.autocomplete(suggestions, {
const autocompletions = parser.autocomplete(ast,suggestions, {
variables: ["variable", "variable requires quoting", "variable requires escaping \""],
prefixes: ["prefix", "prefix requires quoting", "prefix requires escaping \""],
properties: ["property", "property requires quoting", "property requires escaping \""],
Expand Down
15 changes: 6 additions & 9 deletions docs-src/DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ The only exception is the handlers for the parser rules. These are internal, sho

```text
src
┣ ast
┃ ┣ builders - used to quickly build ast classes for testing
┃ ┃ - instances returned are unsealed, i.e. their parents are not assigned
┃ ┣ classes - the actual ast node classes
┃ ┣ handlers.ts - used inside grammar/ParserBase.ts to handle the creation of tokens / ast node classes
┣ ast - contains the base ast node creators
┃ ┣ builders - used to quickly build ast nodes for testing
┃ ┣ handlers.ts - used inside grammar/ParserBase.ts to handle the creation of tokens / ast nodes
┣ examples - contains fully implemented parser examples
┣ helpers
┃ ┣ general - ...various internal helper functions not related to parsing + default functions for the default parser options
┃ ┣ parser - ...various internal helper functions used before/during parsing
┃ ┗ errors.ts - the main error handling class (error types are defined in types/errors)
┣ defaults - contains the default implementations of some parser options
┣ internal - various internal helper functions used before/during parsing
┃ ┗ ExpressitError.ts - the main error handling class (error types are defined in types/errors)
┣ types - all the types and enums are stored here in their respective categories
┣ utils - exported utility functions
┣ global.d.ts - additional global types
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
"types": "./dist/ast/*",
"import": "./dist/ast/*"
},
"./helpers": {
"types": "./dist/helpers/index.d.ts",
"import": "./dist/helpers/index.js"
"./internal": {
"types": "./dist/internal/index.d.ts",
"import": "./dist/internal/index.js"
},
"./helpers/*": {
"types": "./dist/helpers/*",
"import": "./dist/helpers/*"
"./internal/*": {
"types": "./dist/internal/*",
"import": "./dist/internal/*"
},
"./methods": {
"types": "./dist/methods/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions src/Lexer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { isBlank } from "@alanscodelog/utils/isBlank.js"
import { pushIfNotIn } from "@alanscodelog/utils/pushIfNotIn.js"

import { checkParserOpts } from "./helpers/parser/checkParserOpts.js"
import { parseParserOptions } from "./helpers/parser/parseParserOptions.js"
import { checkParserOpts } from "./internal/checkParserOpts.js"
import { parseParserOptions } from "./internal/parseParserOptions.js"
import type { FullParserOptions } from "./types/index.js"

const regexFlags = /^[a-zA-Z]+/
Expand Down
Loading

0 comments on commit baaab41

Please sign in to comment.