# RegexCodeParser

Source: /tuil/docs/reference/packages/code/api/regex-code-parser
Locale: en

class exported by @mwillbanks/tuil-code.



{/* Generated by tooling/docs/generate-reference.ts. */}

## class [#class]

Public class exported by `@mwillbanks/tuil-code`.

```ts
export class RegexCodeParser implements CodeParser {
  readonly id = "tuil-regex";
  readonly languages = ["*"];
  #version: number;

  constructor() {
    this.#version = 0;
  }

  parse(
    source: string,
    options: { readonly signal: AbortSignal },
  ): CodeParseResult {
    if (options.signal.aborted) throw options.signal.reason;
    const spans: CodeSpan[] = [];
    const patterns: readonly [string, RegExp][] = [
      ["comment", /\/\/.*$|\/\*[\s\S]*?\*\/|#.*$/gm],
      ["string", /(["'`])(?:\\.|(?!\1)[^\\])*\1/g],
      [
        "keyword",
        /\b(const|let|var|function|class|interface|type|return|if|else|for|while|import|export|from|async|await|throw|try|catch|new)\b/g,
      ],
      ["number", /\b\d+(?:\.\d+)?\b/g],
    ];
    for (const [kind, pattern] of patterns) {
      for (const match of source.matchAll(pattern)) {
        spans.push({
          start: match.index,
          end: match.index + match[0].length,
          kind,
        });
      }
    }
    const folds: {
      startLine: number;
      endLine: number;
    }[] = [];
    const stack: number[] = [];
    source.split("\n").forEach((line, index) => {
      for (const character of line) {
        if (character === "{") stack.push(index);
        if (character === "}") {
          const startLine = stack.pop();
          if (startLine !== undefined && index > startLine) {
            folds.push({ startLine, endLine: index });
          }
        }
      }
    });
    return Object.freeze({
      language: "text",
      version: ++this.#version,
      spans: Object.freeze(
        spans.sort((left, right) => left.start - right.start),
      ),
      folds: Object.freeze(folds),
      diagnostics: Object.freeze(
        stack.map((line) => ({
          start: source.split("\n").slice(0, line).join("\n").length,
          end: source.length,
          kind: "syntax",
          severity: "warning" as const,
          message: "Unclosed block",
        })),
      ),
    });
  }
}
```

## Members [#members]

| Member          | Type                                                                                        | Required | Description                                                                                                                       | Related types                                                            |
| --------------- | ------------------------------------------------------------------------------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------ |
| `id`            | `"tuil-regex"`                                                                              | Yes      | The `id` member uses the `"tuil-regex"` contract.                                                                                 | —                                                                        |
| `languages`     | `string[]`                                                                                  | Yes      | The `languages` member uses the `string[]` contract.                                                                              | —                                                                        |
| `#version`      | `number`                                                                                    | Yes      | The `#version` member uses the `number` contract.                                                                                 | —                                                                        |
| `__constructor` | `any`                                                                                       | Yes      | The `__constructor` member uses the `any` contract.                                                                               | —                                                                        |
| `parse`         | `(source: string, options: &#123; readonly signal: AbortSignal; &#125;) => CodeParseResult` | Yes      | The `parse` member uses the `(source: string, options: &#123; readonly signal: AbortSignal; &#125;) => CodeParseResult` contract. | [`CodeParseResult`](/tuil/docs/reference/packages/code/api/code-parse-result) |

## Parameters [#parameters]

This declaration has no public members.

## Returns [#returns]

This declaration does not return a value.

## Throws [#throws]

No thrown errors are documented for this declaration.

## Related types [#related-types]

* [`CodeParseResult`](/tuil/docs/reference/packages/code/api/code-parse-result)
* [`CodeParser`](/tuil/docs/reference/packages/code/api/code-parser)
* [`CodeSpan`](/tuil/docs/reference/packages/code/api/code-span)

## Source [#source]

[View the secondary source reference](https://github.com/mwillbanks/tuil/blob/main/packages/code/src/index.ts)

## Package [#package]

[@mwillbanks/tuil-code](/tuil/docs/reference/packages/code)
