# lexeme

> A lexical analyzer that turns CSS into parser friendly tokens.

Latest version **0.0.1** (published 2018-04-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install lexeme
pnpm add lexeme
yarn add lexeme
bun add lexeme
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2018-04-03 |
| First published | 2018-04-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 130.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Callum Hart |
| Maintainers | callum-hart |

## Links

- npm: https://www.npmjs.com/package/lexeme
- Repository: https://github.com/callum-hart/lexeme
- Homepage: https://github.com/callum-hart/lexeme#readme
- Issues: https://github.com/callum-hart/lexeme/issues
- npm.io page: https://npm.io/package/lexeme

## Dependencies (1)

- [prettier](https://npm.io/package/prettier.md) ^1.7.0

## Recent versions

- 0.0.1 (latest) — 2018-04-03

## README

```
  /LL
| LL
| LL         /eeeeee   /xx   /xx   /eeeeee   /mmmmmm/mmmm    /eeeeee
| LL        /ee__  ee |  xx /xx/  /ee__  ee | mm_  mm_  mm  /ee__  ee
| LL       | eeeeeeee  \  xxxx/  | eeeeeeee | mm \ mm \ mm | eeeeeeee
| LL       | ee_____/   >xx  xx  | ee_____/ | mm | mm | mm | ee_____/
| LLLLLLLL |  eeeeeee  /xx/\  xx |  eeeeeee | mm | mm | mm |  eeeeeee
|________/  \_______/ |__/  \__/  \_______/ |__/ |__/ |__/  \_______/
```

Lexeme is a [lexical analyzer](https://en.wikipedia.org/wiki/Lexical_analysis) that turns CSS into parser friendly tokens.

## Install

Install Lexeme from [npm](https://www.npmjs.com/package/lexeme):

```
npm install lexeme
```

Or with yarn:

```
yarn add lexeme
```

Then require it into any module:

```
const { tokenize } = require('lexeme');
```

## Tokens

A single token is represented as an `Array` with the following shape:

```js
[
  // The token type, for example `BRACE_OPEN`
  type{String},

  // The value of the token, for example `{`
  value{String},

  // The position of the string being tokenized, for example [1,10]
  location{Array<line<number>, column<?number>}
]
```

**Token Types**

| Type | Example |
| --- |---|
| BRACE_OPEN | `{` |
| BRACE_CLOSE | `}` |
| MEDIA_QUERY | `@media (min-width: 300px)` |
| KEYFRAME | `@keyframes slide` |
| KEYFRAME_SELECTOR | `from` \| `to` \| `0-100%` |
| FONT_FACE | `@font-face` |
| CHARSET | `@charset` |
| SUPPORTS | `@supports` |
| DECLARATION | `display: flex;` |
| SELECTOR | `nav` \| `span.icon` \| `h1, h2, h3` |

Note: Inline and multi-line comments in CSS are ignored.

## API

### tokenize

```
tokenize(CSS)
```

- **Description:**

  Transforms CSS into tokens.

- **Parameters:**
  - `CSS{String}`: The CSS source code.

- **Returns:**

  An interface to query tokens:

  ```
  {
    tokens: ƒ tokens()
    selectors: ƒ selectors()
    declarations: ƒ declarations()
    mediaQueries: ƒ mediaQueries()
    rulesets: ƒ rulesets()
    rulesetsAsObjects: ƒ rulesetsAsObjects()
  }
  ```

### .tokens()

```
tokenize(CSS).tokens();
```

- **Returns:**

  An `Array` containing all tokens.

### .selectors()

```
tokenize(CSS).selectors();
```

- **Returns:**

  An `Array` containing tokens of the type `SELECTOR`.

### .declarations()

```
tokenize(CSS).declarations();
```

- **Returns:**

  An `Array` containing tokens of the type `DECLARATION`.

### .mediaQueries()

```
tokenize(CSS).mediaQueries();
```

- **Returns:**

  An `Array` containing tokens of the type `MEDIA_QUERY`.

### .rulesets()

```
tokenize(CSS).rulesets();
```

- **Returns:**

  An `Array` containing a `Map` for each ruleset:

  ```
  [
    Map {
      selector{String},
      declarations{Array<String>}
    },
    Map {
      ...
    }
  ]
  ```

### .rulesetsAsObjects()

```
tokenize(CSS).rulesetsAsObjects();
```

- **Returns:**

  An `Array` containing an `Object` for each ruleset:

  ```
  [
    {
      selector{String},
      declarations{Array<String>}
    },
    {
      ...
    }
  ]
  ```

## Tests

Run all tests with:

```
npm test
```

Or run a specific test with:

```
npm test tokens
```

## Licence

[MIT](https://github.com/callum-hart/lexeme/blob/master/LICENSE)

Copyright (c) 2018-present, Callum Hart

---
_Source: https://npm.io/package/lexeme · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
