Comparing version 0.1.1 to 0.1.2
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./lexer"), exports); | ||
__exportStar(require("./parser"), exports); | ||
exports.Tokens = require("./tokens"); | ||
exports.Unordered = require("./unordered"); | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const token_1 = require("./token"); | ||
const tokens_1 = require("./tokens"); | ||
/** | ||
* The lexer turns input into a list of tokens. | ||
*/ | ||
class Lexer { | ||
constructor(input, opts = {}) { | ||
var _a; | ||
/** | ||
* @param input - Input string. | ||
*/ | ||
constructor(input) { | ||
this.quotes = []; | ||
this.position = 0; | ||
this.finished = false; | ||
this.input = input; | ||
this.quotes = (_a = opts.quotes) !== null && _a !== void 0 ? _a : []; | ||
} | ||
/** | ||
* Sets the quotes to use. | ||
* This can be done in the middle of lexing. | ||
* @param quotes - List of pairs of open and close quotes. | ||
*/ | ||
setQuotes(quotes) { | ||
this.quotes = quotes; | ||
return this; | ||
} | ||
/** | ||
* Whether the lexer is finished. | ||
*/ | ||
get finished() { | ||
return this.position >= this.input.length; | ||
} | ||
match(s) { | ||
@@ -25,30 +44,39 @@ const sub = this.input.slice(this.position, this.position + s.length); | ||
} | ||
/** | ||
* Gets the next token. | ||
*/ | ||
next() { | ||
if (this.finished) { | ||
return null; | ||
return { done: true, value: null }; | ||
} | ||
return this.pWS() || this.pQuoted() || this.pWord() || this.pEof(); | ||
const t = this.nextToken(); | ||
if (t == null) { | ||
throw new Error('Unexpected end of input (this should never happen).'); | ||
} | ||
return { done: false, value: t }; | ||
} | ||
pWS() { | ||
var _a; | ||
const w = (_a = this.matchR(/^\s+/)) === null || _a === void 0 ? void 0 : _a[0]; | ||
if (w == null) { | ||
return null; | ||
} | ||
nextToken() { | ||
return this.pQuoted() || this.pWord(); | ||
} | ||
pWs() { | ||
var _a, _b; | ||
const w = (_b = (_a = this.matchR(/^\s*/)) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : ''; | ||
this.shift(w.length); | ||
return new token_1.Ws(w); | ||
return w; | ||
} | ||
pQuoted() { | ||
var _a; | ||
var _a, _b, _c; | ||
for (const [openQ, closeQ] of this.quotes) { | ||
const w = this.match(openQ); | ||
if (w == null) { | ||
const open = this.match(openQ); | ||
if (open == null) { | ||
continue; | ||
} | ||
let inner = (_a = this.matchR(/^\S+/)) === null || _a === void 0 ? void 0 : _a[0]; | ||
if (inner == null) { | ||
return null; | ||
} | ||
this.shift(open.length); | ||
let inner = (_b = (_a = this.matchR(/^[^]+/)) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : ''; | ||
inner = Lexer.sliceTo(inner, [closeQ]); | ||
return new token_1.Quoted(openQ + inner + closeQ, inner); | ||
this.shift(inner.length); | ||
const close = (_c = this.match(closeQ)) !== null && _c !== void 0 ? _c : ''; | ||
this.shift(close.length); | ||
const s = this.pWs(); | ||
return new tokens_1.Quoted(open + inner + close, inner, s); | ||
} | ||
@@ -65,3 +93,4 @@ return null; | ||
this.shift(w.length); | ||
return new token_1.Word(w); | ||
const s = this.pWs(); | ||
return new tokens_1.Word(w, s); | ||
} | ||
@@ -76,18 +105,13 @@ static sliceTo(word, xs) { | ||
} | ||
pEof() { | ||
if (this.position === this.input.length) { | ||
this.finished = true; | ||
return new token_1.Eof(''); | ||
} | ||
return null; | ||
[Symbol.iterator]() { | ||
return this; | ||
} | ||
*[Symbol.iterator]() { | ||
let token = this.next(); | ||
while (token != null) { | ||
yield token; | ||
token = this.next(); | ||
} | ||
/** | ||
* Runs the lexer. | ||
*/ | ||
lex() { | ||
return [...this]; | ||
} | ||
} | ||
exports.Lexer = Lexer; | ||
exports.default = Lexer; | ||
//# sourceMappingURL=lexer.js.map |
@@ -1,21 +0,40 @@ | ||
import { Token } from './token'; | ||
export declare class Lexer { | ||
import { Token } from './tokens'; | ||
/** | ||
* The lexer turns input into a list of tokens. | ||
*/ | ||
export default class Lexer implements IterableIterator<Token> { | ||
private readonly input; | ||
private readonly quotes; | ||
private quotes; | ||
private position; | ||
private finished; | ||
constructor(input: string, opts?: LexerOptions); | ||
/** | ||
* @param input - Input string. | ||
*/ | ||
constructor(input: string); | ||
/** | ||
* Sets the quotes to use. | ||
* This can be done in the middle of lexing. | ||
* @param quotes - List of pairs of open and close quotes. | ||
*/ | ||
setQuotes(quotes: [string, string][]): this; | ||
/** | ||
* Whether the lexer is finished. | ||
*/ | ||
get finished(): boolean; | ||
private match; | ||
private matchR; | ||
private shift; | ||
next(): Token | null; | ||
private pWS; | ||
/** | ||
* Gets the next token. | ||
*/ | ||
next(): IteratorResult<Token>; | ||
private nextToken; | ||
private pWs; | ||
private pQuoted; | ||
private pWord; | ||
private static sliceTo; | ||
private pEof; | ||
[Symbol.iterator](): Generator<Token, void>; | ||
[Symbol.iterator](): this; | ||
/** | ||
* Runs the lexer. | ||
*/ | ||
lex(): Token[]; | ||
} | ||
export declare type LexerOptions = { | ||
quotes?: [string, string][] | null; | ||
}; |
{ | ||
"name": "lexure", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Lexer and parser for semi-structured user input.", | ||
@@ -32,3 +32,4 @@ "keywords": [ | ||
"build": "rimraf dist && tsc", | ||
"test": "jest --coverage" | ||
"test": "jest --coverage", | ||
"prepublishOnly": "npm run test && npm run build" | ||
}, | ||
@@ -35,0 +36,0 @@ "jest": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31067
18
586
1