{"id":26407297,"url":"https://github.com/yuqingc/lxa","last_synced_at":"2025-03-17T17:29:45.093Z","repository":{"id":34888885,"uuid":"187305310","full_name":"yuqingc/lxa","owner":"yuqingc","description":"Lexical analysis/Regular expression engine written in TypeScript","archived":false,"fork":false,"pushed_at":"2023-01-04T21:46:06.000Z","size":899,"stargazers_count":2,"open_issues_count":12,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2023-12-26T20:02:05.818Z","etag":null,"topics":["compiler","lexical-analysis","regular-expression","regular-expression-engine"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yuqingc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-18T02:34:11.000Z","updated_at":"2022-01-09T08:52:59.000Z","dependencies_parsed_at":"2023-01-15T10:01:10.421Z","dependency_job_id":null,"html_url":"https://github.com/yuqingc/lxa","commit_stats":null,"previous_names":[],"tags_count":1,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuqingc%2Flxa","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuqingc%2Flxa/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuqingc%2Flxa/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuqingc%2Flxa/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yuqingc","download_url":"https://codeload.github.com/yuqingc/lxa/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244077631,"owners_count":20394332,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["compiler","lexical-analysis","regular-expression","regular-expression-engine"],"created_at":"2025-03-17T17:29:44.374Z","updated_at":"2025-03-17T17:29:45.086Z","avatar_url":"https://github.com/yuqingc.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# lxa\n\nA lexical analysis / regular expression engine written in TypeScript\n\n[![npm](https://img.shields.io/npm/v/lxa.svg)](https://www.npmjs.com/package/lxa)\n[![Codecov](https://img.shields.io/codecov/c/github/yuqingc/lxa.svg)](https://codecov.io/gh/yuqingc/lxa)\n[![GitHub issues](https://img.shields.io/github/issues/yuqingc/lxa.svg)](https://github.com/yuqingc/lxa/issues)\n[![Travis (.org)](https://img.shields.io/travis/yuqingc/lxa.svg)](https://travis-ci.com/yuqingc/lxa)\n[![GitHub](https://img.shields.io/github/license/yuqingc/lxa.svg)](https://github.com/yuqingc/lxa/blob/master/LICENSE)\n\n[![996.icu](https://img.shields.io/badge/link-996.icu-red.svg)](https://996.icu)\n\n## Get started\n\n### Install with NPM or Yarn\n\n- With NPM\n  ```\n  $ npm install lxa --save\n  ```\n\n- With Yarn\n\n  ```\n  $ yarn add lxa\n  ```\n\n### Quick starting example\n\nLet's get started by generating a regular expression checker, testing whether a string is of the language of `/(a|b)*cd?/` using *lxa*.\n\n\u003e Tips: You will see there are concepts of *NFAs* and *DFAs* in the example code. Don't be worried about that since using *lxa* does not require the prerequisite knowledge of [NFAs (Non-deterministic Finite Automata)](https://en.wikipedia.org/wiki/Nondeterministic_finite_automaton) and [DFAs (Deterministic Finite Automata)](https://en.wikipedia.org/wiki/Deterministic_finite_automaton). It's not hard for you to build your own lexical analyzer or regular expression tools following this guide. Understanding those concepts helps you acquire a deeper understanding of the *lxa*'s principle though.\n\nThe expression of `(a|b)*cd?` consists of three parts, which also consist of smaller units, and so on. The following describes all the parts of the entire expression.\n\nThe entire expression is the concatenation of the following three expressions\n\n- `(a|b)*`\n\n  - which is the closure of `(a|b)`\n\n    - which is the union of `a` and `b`\n\n- A single character of `c`\n\n- `d?`\n\n  - The concatenation of single character `d` and empty string (We mark empty string as `ε` (epsilon)\n\n1. First, we need to create *states* for each part of the expression and combine them together.\n\n    ```ts\n    import { stateOps, epsilon } from 'lxa';\n    const {  SingleInputState,  UnionState, ClosureState } = stateOps;\n\n    // state for single character 'a' and 'b'\n    const state_for_a = new SingleInputState('a');\n    const state_for_b = new SingleInputState('b');\n\n    // and generate the union of 'a' and 'b', (a|b)\n    const union_of_a_and_b = new UnionState(state_for_a, state_for_b);\n\n    // and then the closure `(a|b)*`\n    const union_of_a_and_b_closure = new ClosureState(union_of_a_and_b);\n\n    // and concatenate `(a|b)*` with c\n    const concat_with_c = new ConcatState(union_of_a_and_b_closure, new SingleInputState('c'));\n\n    // Before we generate the final expression,\n    // we generate the union of 'd' and empty string,\n    // representing `d?` or `d|ε`\n    const d_or_empty = new UnionState(\n      new SingleInputState('d'),\n      new SingleInputState(epsilon),\n      // `true` means this is the final accepted state.\n      // Refer to API doc for more detail.\n      true,\n    );\n\n    // Finally, we concatenate them all\n    const final = new ConcatState(concat_with_c, d_or_empty);\n    ```\n2. Generate a DFA for testing.\n\n    ```ts\n    import { NFA } from 'lxa';\n    const dfa = new NFA(final).toDFA();\n\n    dfa.test('aaac'); // true\n    dfa.test('abcd') // true\n    dfa.test('bbbcd') // true\n    dfa.test('ad') // false\n    ```\nIt is verbose to union or concatenate multiple states because we need to nest those states in a very deep hierarchy, especially when the expression is complicated. We have provided you with two util functions [`concatMultipleStates()`](#concatmultiplestates), [`unionMultipleStates()`](#unionmultiplestates) to union or concatenate multiple states such that we don't have to nest them all.\n\n```ts\nimport { concatMultipleStates } from 'lxa';\n\n// This is much concise\nconst final = concatMultipleStates(\n  union_of_a_and_b_closure, \n  new SingleInputState('c'),\n  d_or_empty\n);\n```\n\n## APIs\n\n### `epsilon`\n\n`epsilon` is a singleton object representing an empty string. It can be used as the argument for `input` of the `StateOp`'s constructor.\n\n### `stateOps`\n\n#### `stateOps.StateOp`\n\nThis is the base class. Please do not instantiate it explicitly. You can use it as a type notation for TypeScript. The following classes are subclasses of `StateOp`.\n\n#### `stateOps.SingleInputState `\n\n`constructor SingleInputState(input: InputType, accepted?: boolean): SingleInputState`\n\n- `inputType` is either a `string` type or the [`epsilon`](#epsilon) object\n- `accepted` indicates whether the current state is accepted or not. If the current state is accepted and there is no more input string, the whole regular expression is accepted. Refer to the the explanation for *NFAs* and *DFAs* for more details about the *accepted* state. Default to `false`.\n\n#### `stateOps.ConcatState`\n\n`constructor ConcatState(a: StateOp, b: StateOp): ConcatState`\n\nConcatenates two states. Use [`concatMultipleStates()`](#concatmultiplestates) for a shorthand of concatenating more states.\n\n#### `stateOps.UnionState`\n\n`constructor UnionState(a: StateOp, b: StateOp, accepted?: boolean): UnionState`\n\nUnions two states. Use Use [`unionMultipleStates()`](#unionmultiplestates) for a shorthand of uniting more states.\n\n- `accepted`, ditto\n\n#### `stateOps.ClosureState`\n\n`constructor ClosureState(a: StateOp, accepted?: boolean): ClosureState`\n\nGenerates the closure of a state.\n\n- `a` is the input state to use to generate the closure\n- `accepted`, ditto\n\n### `concatMultipleStates`\n\n`function concatMultipleStates(...states: StateOp[]): StateOp`\n\nConcatenates multiple states together. Shorthand for nesting constructors of `stateOps.ConcatState`\n\n### `unionMultipleStates`\n\n`function unionMultipleStates({states, accepted}): StateOp`\n\nUnites multiple states together. Shorthand for nesting constructors of `stateOps.UnionState`\n\n- `states` is an array of `StateOp` instances\n- `accepted`, ditto\n\n### `NFA`\n\n#### `NFA constructor`\n\n`constructor NFA(state: StateOp): NFA`\n\n#### `NFA.prototype.toDFA`\n\n`NFA.prototype.toDFA(): DFA`\n\nReturns a `DFA` instance generating from the `NFA` instance caller\n\n### `DFA`\n\n#### `DFA.prototype.test`\n\n`DFA.prototype.test(input: string): boolean`\n\nChecks if the input string is of the expression language\n\n## License\n\nUnder the [MIT License](https://github.com/yuqingc/lxa/blob/master/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuqingc%2Flxa","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyuqingc%2Flxa","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuqingc%2Flxa/lists"}