# core/lexers/code.go.lexer.ts

> 82 lines of code and 21 definitions.

Tree: GovLab Docs
Language: typescript
Layer: processing
Canonical: https://banes-lab.com/anatomy/docs#file-docs-core-lexers-code-go-lexer-ts
Source text: https://banes-lab.com/source/docs/core/lexers/code.go.lexer.ts.txt

Listed in [core/lexers](https://banes-lab.com/api/source/docs/core/lexers.md), before [core/lexers/shell.lexer.ts](https://banes-lab.com/source/docs/core/lexers/shell.lexer.ts.md).

## Definitions

- `isIdentPart` (lexical_declaration, line 12, exported)
- `skipSpaces` (lexical_declaration, line 20, exported)
- `isIdentStart` (lexical_declaration, line 8, exported)
- `isSpace` (lexical_declaration, line 16, exported)
- `lastIdent` (lexical_declaration, line 52, exported)
- `skipBalanced` (lexical_declaration, line 75, exported)
- `depthDelta` (lexical_declaration, line 68)
- `lineAt` (lexical_declaration, line 87, exported)
- `skipIdent` (lexical_declaration, line 28, exported)
- `skipSpacesBack` (lexical_declaration, line 36, exported)
- `skipIdentBack` (lexical_declaration, line 44, exported)
- `FUNC` (lexical_declaration, line 3, exported)
- `UNDERSCORE` (lexical_declaration, line 5)
- `LINE_BREAK` (lexical_declaration, line 6)
- `last` (lexical_declaration, line 53, exported)
- `current` (lexical_declaration, line 54, exported)
- `open` (lexical_declaration, line 76, exported)
- `close` (lexical_declaration, line 77, exported)
- `depth` (lexical_declaration, line 78, exported)
- `line` (lexical_declaration, line 88, exported)
- `at` (lexical_declaration, line 89, exported)

## Contained in

- [core/lexers](https://banes-lab.com/anatomy/docs/folder-docs-core-lexers.md)

## Used by

- [core/analyzers/code.go.analyzer.ts](https://banes-lab.com/source/docs/core/analyzers/code.go.analyzer.ts.md)
- [core/parsers/code.go.parser.ts](https://banes-lab.com/source/docs/core/parsers/code.go.parser.ts.md)

## Linked from

- [core/analyzers](https://banes-lab.com/anatomy/docs/folder-docs-core-analyzers.md)
- [core/lexers](https://banes-lab.com/anatomy/docs/folder-docs-core-lexers.md)
- [core/parsers](https://banes-lab.com/anatomy/docs/folder-docs-core-parsers.md)

## Source

```typescript
import { isAlpha, isIdentifierChar, isWhitespace } from "@govlab/constants";

export const FUNC = "func";

const UNDERSCORE = "_";
const LINE_BREAK = "\n";

export const isIdentStart = function isIdentStart(char?: string): boolean {
    return typeof char === "string" && (isAlpha(char) || char === UNDERSCORE);
};

export const isIdentPart = function isIdentPart(char?: string): boolean {
    return typeof char === "string" && isIdentifierChar(char);
};

export const isSpace = function isSpace(char?: string): boolean {
    return typeof char === "string" && isWhitespace(char);
};

export const skipSpaces = function skipSpaces(src: string, from: number, bound: number): number {
    let at = from;
    while (at < bound && isSpace(src[at])) {
        at += 1;
    }
    return at;
};

export const skipIdent = function skipIdent(src: string, from: number, bound: number): number {
    let at = from;
    while (at < bound && isIdentPart(src[at])) {
        at += 1;
    }
    return at;
};

export const skipSpacesBack = function skipSpacesBack(src: string, from: number, start: number): number {
    let at = from;
    while (at >= start && isSpace(src[at])) {
        at -= 1;
    }
    return at;
};

export const skipIdentBack = function skipIdentBack(src: string, from: number, start: number): number {
    let at = from;
    while (at >= start && isIdentPart(src[at])) {
        at -= 1;
    }
    return at;
};

export const lastIdent = function lastIdent(text: string): string | null {
    let last: string | null = null;
    let current = "";
    for (const char of text) {
        if (isIdentPart(char)) {
            current += char;
            continue;
        }
        if (current !== "") {
            last = current;
        }
        current = "";
    }
    return current === "" ? last : current;
};

const depthDelta = function depthDelta(char: string | undefined, open: string, close: string): number {
    if (char === open) {
        return 1;
    }
    return char === close ? -1 : 0;
};

export const skipBalanced = function skipBalanced(src: string, from: number, pair: string): number {
    const open = pair.charAt(0);
    const close = pair.charAt(1);
    let depth = 1;
    let at = from + 1;
    while (at < src.length && depth > 0) {
        depth += depthDelta(src[at], open, close);
        at += 1;
    }
    return at;
};

export const lineAt = function lineAt(src: string, position: number): number {
    let line = 1;
    for (let at = 0; at < position && at < src.length; at += 1) {
        if (src[at] === LINE_BREAK) {
            line += 1;
        }
    }
    return line;
};
```
