Node.js · API
SyntaxKind
Enum numérico que clasifica caracteres y tokens. Los primeros valores describen caracteres sueltos del texto; el resto, los tokens que arma el lexer. Es la base de las utilidades de bajo nivel del paquete.
import { SyntaxKind } from "donly";
SyntaxKind.keyword; // 11
SyntaxKind[11]; // 'keyword' Valores
| Valor | Nombre |
|---|---|
| 0 | unknown |
| 1 | alphabet |
| 2 | integer |
| 3 | whitespace |
| 4 | newline |
| 5 | dot |
| 6 | underscore |
| 7 | singleQuote |
| 8 | doubleQuote |
| 9 | openCurlyBrace |
| 10 | closeCurlyBrace |
| 11 | keyword |
| 12 | string |
| 13 | numeric |
| 14 | boolean |
| 15 | null |
| 16 | comment |
| 17 | indent |
| 18 | heredoc |
donToParts
Divide un texto en partes con su tipo. Es lo que usa este sitio para resaltar ejemplos DON.
import { donToParts } from "donly";
donToParts("port 8080");
// [
// { type: 'keyword', value: 'port', _syntax_type: 11 },
// { type: 'literal', value: ' ', _syntax_type: 3 },
// { type: 'numeric', value: '8080', _syntax_type: 13 },
// ] LexerParser
Convierte texto en un Lexema con la lista
tokens. Cada Token tiene
type (un SyntaxKind),
span y text(). La opción debug incluye también los tokens
invisibles.
import { LexerParser, SyntaxKind } from "donly";
const lexema = new LexerParser().parse("port 8080");
lexema.tokens.map((token) => [SyntaxKind[token.type], token.text()]);
// [ [ 'keyword', 'port' ], [ 'numeric', '8080' ] ] SyntaxEncode
Es el parser sintáctico (SyntaxParser). Recibe texto o un
Lexema y devuelve el árbol del documento (children y
span), sin convertirlo en
Directive.
import { SyntaxEncode } from "donly";
const document = new SyntaxEncode().parse("a 1 { b }");
document.children.length; // 1