CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
README.md120 linesDownload Raw Back to leac
1# leac2 3![lint status badge](https://github.com/mxxii/leac/workflows/lint/badge.svg)4![test status badge](https://github.com/mxxii/leac/workflows/test/badge.svg)5[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/mxxii/leac/blob/main/LICENSE)6[![npm](https://img.shields.io/npm/v/leac?logo=npm)](https://www.npmjs.com/package/leac)7[![deno](https://img.shields.io/badge/deno.land%2Fx%2F-leac-informational?logo=deno)](https://deno.land/x/leac)8 9Lexer / tokenizer.10 11 12## Features13 14- **Lightweight**. Zero dependencies. Not a lot of code.15 16- **Well tested** - comes will tests for everything including examples.17 18- **Compact syntax** - less boilerplate. Rule name is enough when it is the same as the lookup string.19 20- **No failures** - it just stops when there are no matching rules and returns the information about whether it completed and where it stopped in addition to tokens array.21 22- **Composable lexers** - instead of states within a lexer.23 24- **Stateless lexers** - all inputs are passed as arguments, all outputs are returned in a result object.25 26- **No streaming** - accepts a string at a time.27 28- **Only text tokens, no arbitrary values**. It seems to be a good habit to have tokens that are *trivially* serializable back into a valid input string. Don't do the parser's job. There are a couple of convenience features such as the ability to discard matches or string replacements for regular expression rules but that has to be used mindfully (more on this below).29 30 31## Install32 33### Node34 35```shell36> npm i leac37> yarn add leac38```39 40```ts41import { createLexer, Token } from 'leac';42```43 44### Deno45 46```ts47import { createLexer, Token } from 'https://deno.land/x/leac@.../leac.ts';48```49 50 51## Examples52 53- [JSON](https://github.com/mxxii/leac/blob/main/examples/json.ts) ([output snapshot](https://github.com/mxxii/leac/blob/main/test/snapshots/examples.ts.md#json));54- [Calc](https://github.com/mxxii/leac/blob/main/examples/calc.ts) ([output snapshot](https://github.com/mxxii/leac/blob/main/test/snapshots/examples.ts.md#calc)).55 56```typescript57const lex = createLexer([58  { name: '-', str: '-' },59  { name: '+' },60  { name: 'ws', regex: /\s+/, discard: true },61  { name: 'number', regex: /[0-9]|[1-9][0-9]+/ },62]);63 64const { tokens, offset, complete } = lex('2 + 2');65```66 67 68## API69 70- [docs/index.md](https://github.com/mxxii/leac/blob/main/docs/index.md)71 72 73## A word of caution74 75It is often really tempting to rewrite token on the go. But it can be dangerous unless you are absolutely mindful of all edge cases.76 77For example, who needs to carry string quotes around, right? Parser will only need the string content...78 79We'll have to consider following things:80 81- Regular expressions. Sometimes we want to match strings that can have a length *from zero* and up.82 83- Tokens are not produced without changing the offset. If something is missing - there is no token.84 85  If we allow a token with zero length - it will cause an infinite loop, as the same rule will be matched at the same offset, again and again.86 87- Discardable tokens - a convenience feature that may seem harmless at a first glance.88 89When put together, these things plus some intuition traps can lead to a broken array of tokens.90 91Strings can be empty, which means the token can be absent. With no content and no quotes the tokens array will most likely make no sense for a parser.92 93How to avoid potential issues:94 95- Don't discard anything that you may need to insert back if you try to immediately serialize the tokens array to string. This means whitespace are usually safe to discard while string quotes are not (what can be considered safe will heavily depend on the grammar - you may have a language with significant spaces and insignificant quotes...);96 97- You can introduce a higher priority rule to capture an empty string (opening quote immediately followed by closing quote) and emit a special token for that. This way empty string between quotes can't occur down the line;98 99- Match the whole string (content and quotes) with a single regular expression, let the parser deal with it. This can actually lead to a cleaner design than trying to be clever and removing "unnecessary" parts early;100 101- Match the whole string (content and quotes) with a single regular expression, use capture groups and [replace](https://github.com/mxxii/leac/blob/main/docs/interfaces/RegexRule.md#replace) property. This can produce a non-zero length token with empty text.102 103Another note about quotes: If the grammar allows for different quotes and you're still willing to get rid of them early - think how you're going to unescape the string later. Make sure you carry the information about the exact string kind in the token name at least - you will need it later.104 105 106## What about ...?107 108- performance - The code is very simple but I won't put any unverified assumptions here. I'd be grateful to anyone who can provide a good benchmark project to compare different lexers.109 110- stable release - Current release is well thought out and tested. I leave a chance that some changes might be needed based on feedback. Before version 1.0.0 this will be done without a deprecation cycle.111 112 113## Some other lexer / tokenizer packages114 115- [moo](https://github.com/no-context/moo);116- [doken](https://github.com/yishn/doken);117- [tokenizr](https://github.com/rse/tokenizr);118- [flex-js](https://github.com/sormy/flex-js);119- *and more, with varied level of maintenance.*120 
basant307/AI_Governance_Project · CoolFace